首先,抱歉我的英语不好。
我正在使用Python 3.4
和Pygame
撰写游戏。游戏包括提供屏幕视图的场景,以及从XML
脚本解析的音乐等。
我希望音乐会立即播放,但不会在下一个场景播放。
并且无法在网络上找到类似的问题。
如何立即重写代码以播放音乐场景?
我写的代码是这样的: (图形部分已删除)
xml文件位于:https://pastebin.com/bSBiv1jX
import pygame
import xml.etree.ElementTree as ET
class Script():
def __init__(self, name, filename):
self.name = name
self.parsed = ET.parse(filename)
self.root = self.parsed.getroot()
self.scene_list = self.root.findall("scene")
self.flag_list = str(self.root.findtext("flags")).split(",")
for flag in self.flag_list:
setattr(self, flag, False)
def music_play(self, number):
scene = self.scene_list[number]
if scene.findtext("music") == 'stop':
pygame.mixer.music.stop()
elif scene.findtext("music") != '':
pygame.mixer.music.load(scene.findtext("music"))
pygame.mixer.music.play(-1, 0.0)
def goto(self, number):
string = self.scene_list[number].findtext("goto")
if string == '':
return number
elif isNumber(string) == True:
return int(string)
elif isNumber(string) == False:
return string
def if_goto(self, string, number):
iflist = string.split(', ')
if getattr(self, iflist[0]) == True
return int(iflist[1])
else:
return number + 1
def isNumber(s):
try:
float(s)
return True
except ValueError:
return False
def runGame():
pygame.init()
windowSurface = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
run = True
scene_number = 0
script = Script('dummy', 'scenario/dummy.xml')
while run:
# 입력 처리
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if scene_number == script.goto(scene_number):
scene_number += 1
elif scene_number != script.goto(scene_number):
if isNumber(script.goto(scene_number)):
scene_number = script.goto(scene_number)
else:
scene_number = script.if_goto(script.goto(scene_number), scene_number)
script.music_play(scene_number)
pygame.display.update()
clock.tick(30)
pygame.quit()
def main():
runGame()
if __name__ == "__main__":
main()
答案 0 :(得分:0)
https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load
如果音乐流已播放,则会停止播放
https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.play
如果音乐已播放,则会重新启动。
在游戏循环的每次迭代中,您都会调用examine: function (request, cb) {
var options = {
host: 'graph.microsoft.com',
path: '/v1.0/me',
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': request.get('Authorization')
}
};
cb('hehe', 'hoho');
},
并重新启动音乐。如果音乐尚未播放,您应该只播放音乐。