Reputation: 103
#in the game loop
if event.key == pygame.K_p:
paused = True
pause_menu(paused, sound_state)
if updated_sound_state:
sound_state = True
mixer.music.unpause()
elif not updated_sound_state:
sound_state = False
mixer.music.pause()
#the pause menu
def pause_menu(paused, sound_state):
global updated_sound_state
updated_sound_state = sound_state
mixer.music.pause()
if updated_sound_state:
screen.blit(sound_on_image, (5, (display_height - sound_image_y_size)))
if not updated_sound_state:
screen.blit(sound_off_image, (5, (display_height - sound_image_y_size)))
while paused:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYUP:
if event.key == pygame.K_p:
paused = False
if event.key == pygame.K_s:
if sound_state:
updated_sound_state = False
paused = False
pause_menu(paused=True, sound_state=updated_sound_state)
elif not sound_state:
updated_sound_state = True
paused = False
pause_menu(paused=True, sound_state=updated_sound_state)
return sound_state
When I open the pause menu it shows the correct image but when i change sound on or off then it shows both images.
When i back out of pause and go back in to pause it does show the correct image.
How do i fix this?
Upvotes: 0
Views: 256
Reputation: 210878
Do not call pause_menu
recursively. You have to clear an draw the scene in every frame. Get a copy
of the screen before the `
current_screen = screen.copy()
blit
the copy in the pause loop and draw the menu over it:
screen.blit(current_screen, (0, 0))
if updated_sound_state:
screen.blit(sound_on_image, (5, (display_height - sound_image_y_size)))
if not updated_sound_state:
screen.blit(sound_off_image, (5, (display_height - sound_image_y_size)))
pygame.display.update()
Complete pause_menu
function:
def pause_menu(paused, sound_state):
global updated_sound_state
updated_sound_state = sound_state
mixer.music.pause()
current_screen = screen.copy()
while paused:
for event in pygame.event.get():
if event.type == pygame.KEYUP:
if event.key == pygame.K_p:
paused = False
if event.key == pygame.K_s:
if sound_state:
updated_sound_state = False
paused = False
elif not sound_state:
updated_sound_state = True
paused = False
screen.blit(current_screen, (0, 0))
if updated_sound_state:
screen.blit(sound_on_image, (5, (display_height - sound_image_y_size)))
if not updated_sound_state:
screen.blit(sound_off_image, (5, (display_height - sound_image_y_size)))
pygame.display.update()
return sound_state
The typical PyGame application loop has to:
pygame.event.pump()
or pygame.event.get()
.blit
all the objects)pygame.display.update()
or pygame.display.flip()
Upvotes: 1