Reputation: 127
Ok it might be ridiculous but I am creating a pygame game and currently I am working on the opening window. Now I have added only 2 buttons to the window; Play and Help. The Play button works completely fine but the Help button function doesn't. I am not telling that the Help button is not working. The code inside the function is not working. here is my code:
class Button:
# I did not include this because it works completely fine. The problem is in the help window function.
def help_window():
win.fill((0,0,0)) # Doesn't fill the screen completely. The buttons are still shown.
def main():
# The main function
def main_menu():
title_font = pygame.font.SysFont("comicsans", 70)
run = True
win.fill((90, 255, 180))
while run:
play_button = Button((0,255,0), WIDTH/2 - 100, HEIGHT/2 - 30, 200, 70, "Play")
play_button.draw(win, (0,0,0))
help_button = Button((0,255,0), WIDTH/2 - 100, HEIGHT/2 - play_button.height - 120, 200, 70, "Help")
help_button.draw(win, (0,0,0))
pygame.display.update()
pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if play_button.isOver(pos):
main()
elif help_button.isOver(pos):
help_window() # The help window function
pygame.quit()
main_menu()
What do I do?
Upvotes: 0
Views: 42
Reputation: 3900
If I understand your problem correctly, the buttons should not appear anymore as soon as the player clicks on the help button.
The problem is that when the player clicks the button this calls win.fill((0,0,0))
but then it the main loop you still draw the buttons (play_button.draw(win, (0,0,0))
and help_button.draw(win, (0,0,0))
).
You might need to use a boolean variable to know if your buttons must be drawned or not:
draw_button = True # enable buttons drawing
while run:
if draw_button:
play_button = Button((0,255,0), WIDTH/2 - 100, HEIGHT/2 - 30, 200, 70, "Play")
play_button.draw(win, (0,0,0))
help_button = Button((0,255,0), WIDTH/2 - 100, HEIGHT/2 - play_button.height - 120, 200, 70, "Help")
help_button.draw(win, (0,0,0))
pygame.display.update()
pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if play_button.isOver(pos):
main()
elif help_button.isOver(pos):
help_window()
draw_button = False # stop drawing buttons
Upvotes: 2