如何解决我如何停止 pygame 图片重叠
#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,(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
当我打开暂停菜单时,它会显示正确的图像,但是当我打开或关闭声音时,它会显示两个图像。
我该如何解决这个问题?
解决方法
不要不要递归调用pause_menu
。您必须在每一帧中清除绘制场景。在 `
copy
current_screen = screen.copy()
blit
暂停循环中的副本并在其上绘制菜单:
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,(display_height - sound_image_y_size)))
pygame.display.update()
完整的pause_menu
函数:
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))
if updated_sound_state:
screen.blit(sound_on_image,(display_height - sound_image_y_size)))
if not updated_sound_state:
screen.blit(sound_off_image,(display_height - sound_image_y_size)))
pygame.display.update()
return sound_state
典型的 PyGame 应用程序循环必须:
- 通过
pygame.event.pump()
或pygame.event.get()
处理事件。 - 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过
pygame.display.update()
或pygame.display.flip()
更新显示 - 限制每秒帧数以限制 CPU 使用率
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。