我在谷歌上搜索了很多关于如何解决这个问题的方法,但我是一个初学者,没有一篇文章与 python 街机相关,所以我想不通。
我正在尝试在我的游戏中播放背景音乐,我尝试了python 街机 背景音乐示例,但是在尝试获取位置时,示例向我抛出了一个错误,说:TypeError: get_stream_position() takes 1 positional argument but 2 were given
. 是否有一些更新搞砸了一些代码或什么?
这是我认为与问题相关的代码:
def __init__(self, width, height, title):
super().__init__(width, height, title)
arcade.set_background_color(arcade.color.WHITE)
self.music_list = []
self.current_song_index = 0
self.current_player = None
self.music = None
def advance_song(self):
self.current_song_index += 1
if self.current_song_index >= len(self.music_list):
self.current_song_index = 0
print(f"Advancing song to {self.current_song_index}.")
def play_song(self):
if self.music:
self.music.stop()
print(f"Playing {self.music_list[self.current_song_index]}")
self.music = arcade.Sound(self.music_list[self.current_song_index], streaming=True)
self.current_player = self.music.play(MUSIC_VOLUME)
time.sleep(0.03)
def setup(self):
self.music_list = [":resources:music/funkyrobot.mp3", ":resources:music/1918.mp3"]
self.current_song_index = 0
self.play_song()
def on_update(self, dt):
position = self.music.get_stream_position(self.current_player)
if position == 0.0:
self.advance_song()
self.play_song()
这是完整的错误:
Playing :resources:music/funkyrobot.mp3
Traceback (most recent call last):
File "c:/Users/[name]/Documents/Projects/background_music.py", line 77, in <module>
main()
File "c:/Users/[name]/Documents/Projects/background_music.py", line 74, in main
arcade.run()
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\arcade\window_commands.py", line 236, in run
pyglet.app.run()
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\app\__init__.py", line 107, in run
event_loop.run()
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\app\base.py", line 167, in run
timeout = self.idle()
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\app\base.py", line 237, in idle
redraw_all = self.clock.call_scheduled_functions(dt)
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\clock.py", line 292, in call_scheduled_functions
item.func(now - item.last_ts, *item.args, **item.kwargs)
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\arcade\application.py", line 213, in _dispatch_updates
self.dispatch_event('on_update', delta_time)
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\window\__init__.py", line 1333, in dispatch_event
if EventDispatcher.dispatch_event(self, *args) != False:
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\event.py", line 422, in dispatch_event
self._raise_dispatch_exception(event_type, args, getattr(self, event_type), exception)
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\event.py", line 476, in _raise_dispatch_exception
raise exception
File "C:\Users\[name]\AppData\Local\Programs\Python\Python38\lib\site-packages\pyglet\event.py", line 415, in dispatch_event
if getattr(self, event_type)(*args):
File "c:/Users/[name]/Documents/Projects/background_music.py", line 65, in on_update
position = self.music.get_stream_position(self.current_player)
TypeError: get_stream_position() takes 1 positional argument but 2 were given
TL; DR:我想用作参考的示例不起作用 - TypeError: get_stream_position() takes 1 positional argument but 2 were given
。