'How can I hide and show actors in python
So there's this game I'm making and I have the play button as an actor, how do I make it so that when I click it, it disappears? Using pgzero on windows.
PS: I have everything down but the disappearing part.
playbox = Actor("playbox")
createbox = Actor("createbox")
customizebox = Actor("customizebox")
choose_quiz = Actor("choosequiz")
mainboxes = [playbox, createbox, customizebox]
playbox.move_ip(200, 250)
createbox.move_ip(200,360)
customizebox.move_ip(200,470)
choose_quiz.move_ip(0, 0)
def draw():
playbox.draw()
createbox.draw()
customizebox.draw()
def on_mouse_down(pos):
#i need 'playbox' to dissapear when I click it
if playbox.collidepoint(pos):
print("working")
screen.clear()
screen.blit("choosequiz", (0, 0))
Solution 1:[1]
You should use list to draw Actors
def draw():
for item in mainboxes:
item.draw()
And now you can remove playbox from mainboxes to remove it from screen
def on_mouse_down(pos):
global mainboxes
#i need 'playbox' to dissapear when I click it
if playbox.collidepoint(pos):
print("working")
screen.clear()
screen.blit("choosequiz", (0, 0))
# keep boxes except playbox
mainboxes = [box for box in mainboxes if box != playbox]
# or
if playbox in mainboxes:
mainboxes.remove(playbox)
And if you add actor again to mainboxes then it will be displayed again.
BTW:
With normal PyGame you could keep objects in pygame.sprites.Group and it has function to simply remove object from this group - object.kill()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
