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))
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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 = <div class="su-box su-box-style-default" id="" style="border-color:#000000;border-radius:3px"><div class="su-box-title" style="background-color:#333333;color:#FFFFFF;border-top-left-radius:1px;border-top-right-radius:1px">This is box title</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px"></div></div>
# 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()
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0