【问题标题】:Play music and get user input simultaneously?播放音乐并同时获取用户输入?
【发布时间】:2014-11-13 13:04:47
【问题描述】:

我可以同时从 pygame 获取输入和播放音乐吗?

我现在的代码:

from pygame import mixer

mixer.init()
mixer.music.load('song.mp3')
mixer.music.play()

var = raw_input("Input: ")
print "you said ", var


while pygame.mixer.music.get_busy():
    pygame.time.Clock().tick(10)

在我输入一些东西之前它工作正常,然后程序停止:

Input: test
you said  test
Traceback (most recent call last):
  File "play.py", line 10, in <module>
    while pygame.mixer.music.get_busy():
NameError: name 'pygame' is not defined

【问题讨论】:

    标签: python input pygame


    【解决方案1】:
    from pygame import mixer
    

    mixer 放入您的命名空间,但不放入pygame

    您可以:

    while mixer.music.get_busy():
    

    import pygame
    from pygame import mixer
    

    导入pygame 以及您需要的子包通常是一个好方法

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多