小编Wen*_*ang的帖子

创建一个按钮,在pygame中打开和关闭背景音乐

我想在我的游戏中创建一个可以控制背景音乐的按钮.第一次点击将停止背景音乐,第二次点击可以恢复音乐.现在我的按钮可以控制音乐的开启和关闭,但是我需要多次点击才能使它工作,似乎每次都没有捕获点击事件,这里是我的代码:

 for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if 20 + 50 > mouse_position[0] > 20 and 20 + 20 > mouse_position[1] > 20:
                play_music = not play_music
                if play_music:
                    pygame.mixer.music.unpause()
                else:
                    pygame.mixer.music.pause()

  pygame.display.flip()
  clock = pygame.time.Clock()
  clock.tick(15)
Run Code Online (Sandbox Code Playgroud)

python pygame button game-development

3
推荐指数
2
解决办法
159
查看次数

标签 统计

button ×1

game-development ×1

pygame ×1

python ×1