问题描述
我在谷歌上搜索了很多关于如何解决这个问题的信息,但我是一个初学者,没有一篇文章与 python 街机相关,所以我无法弄清楚。
我正在尝试在我的游戏中播放背景音乐,我尝试了 python arcade Background Music example,但是在尝试获取位置时,示例向我抛出了一个错误,说: TypeError: get_stream_position() takes 1 positional argument but 2 were given
。是否有一些更新搞砸了一些代码或什么?
以下是我认为与问题相关的代码:
def __init__(self,width,height,title):
super().__init__(width,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:Example 我想用作参考不起作用 - TypeError: get_stream_position() takes 1 positional argument but 2 were given
。
解决方法
看来,您使用的是旧版 Arcade。您有两个选择:
- 使用
pip install arcade --upgrade
更新 Arcade - 通过从
self.music.get_stream_position()
中删除所有参数来稍微修改您的代码
这是旧版 Arcade 的工作示例:
import arcade
import time
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 300
SCREEN_TITLE = "Starting Template Simple"
MUSIC_VOLUME = 0.5
class MyGame(arcade.Window):
""" Main application class. """
def __init__(self,width,height,title):
super().__init__(width,title)
arcade.set_background_color(arcade.color.WHITE)
# Variables used to manage our music. See setup() for giving them
# values.
self.music_list = []
self.current_song_index = 0
self.current_player = None
self.music = None
def advance_song(self):
""" Advance our pointer to the next song. This does NOT start the song. """
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):
""" Play the song. """
# Stop what is currently playing.
if self.music:
self.music.stop()
# Play the next song
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)
# This is a quick delay. If we don't do this,our elapsed time is 0.0
# and on_update will think the music is over and advance us to the next
# song before starting this one.
time.sleep(0.03)
def setup(self):
""" Set up the game here. Call this function to restart the game. """
# List of music
self.music_list = [":resources:music/funkyrobot.mp3",":resources:music/1918.mp3"]
# Array index of what to play
self.current_song_index = 0
# Play the song
self.play_song()
def on_draw(self):
""" Render the screen. """
arcade.start_render()
position = self.music.get_stream_position()
length = self.music.get_length()
size = 20
margin = size * .5
# Print time elapsed and total
y = SCREEN_HEIGHT - (size + margin)
text = f"{int(position) // 60}:{int(position) % 60:02} of {int(length) // 60}:{int(length) % 60:02}"
arcade.draw_text(text,y,arcade.csscolor.BLACK,size)
# Print current song
y -= size + margin
text = f"Currently playing: {self.music_list[self.current_song_index]}"
arcade.draw_text(text,size)
def on_update(self,dt):
position = self.music.get_stream_position()
# The position pointer is reset to 0 right after we finish the song.
# This makes it very difficult to figure out if we just started playing
# or if we are doing playing.
if position == 0.0:
self.advance_song()
self.play_song()
def main():
""" Main method """
window = MyGame(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_TITLE)
window.setup()
arcade.run()
if __name__ == "__main__":
main()