2014-03-24 48 views
0

我有一个pygame的问题。我想知道我怎么能像我的游戏中的歌曲的“开/关”按钮。如何制作“开关”音乐按钮?

if event.type == MOUSEBUTTONDOWN: 
    if event.pos[0] > 35 and event.pos[0] < 105 and event.pos[1] > 460 and event.pos[1] < 565: 
    if pygame.mixer.music.play(): 
     pygame.mixer.music.pause() 
    elif pygame.mixer.music.pause(): 
     pygame.mixer.music.unpause() 

在此先感谢,对不起我的英语不好。

+0

你应该解释一下什么是你的代码的问题。它不工作?它会引发错误? – pmoleri

回答

4

您不应该在if条件中要求pygame.mixer.music.play()条件,因为那是play函数不是状态。

而是保持状态的变量:

music_playing = True 
pygame.mixer.music.play() 

... 
while ...: 

    for events...: 

     if event.type == MOUSEBUTTONDOWN: 
      if event.pos[0] > 35 and event.pos[0] < 105 and event.pos[1] > 460 and event.pos[1] < 565: 
       if music_playing: 
        pygame.mixer.music.pause() 
        music_playing = False 
       else: 
        pygame.mixer.music.unpause() 
        music_playing = True 
+0

好的,非常感谢你的回答。我只是把一个“其他”而不是“elif”,现在它的工作。 – user3456154

+0

你说得对,现在我纠正了它。 – pmoleri