pygame help about adding layers

Viewed 69

I'm doing a project in pygame. I create a menu of food. In each box, when I move the mouse on it, its border turns to blue and the text "Click more" appears

8 boxes

When I click to the "Click more", I want a new layer appear on top of the boxes and I want all the boxes at the back cannot be access unless I close the layer.

white layer

However, what I'm doing right now is just draw a simple rectangle and thus, all the boxes at the back can be clicked as normal, like this

back boxes turn blue if I move the mouse on them

I've read a bit about adding layers in pygame but I can't get it. I'm only working on pygame for 2 weeks.

1 Answers

There are many ways to implement this.

One possible way is to set a boolean flag variable to False whenever the layer is being drawn, and check the value of the flag variable whenever the mouse is clicked: if it is false, then do not let the clicks get through to their handlers.

If you are implementing this using object-oriented programming, you could go as far as creating a "LayerManager" object that keeps track of the different layers, their positions, and the ways in which they overlap (if you plan on having very many layers on top of each other).

As far as I know, PyGame does not allow you to use layers the way you would like to as that is a pretty specific feature that you may have to implement yourself. If you post code, we would love to help you out!

Related