如何停止街机库中的背景音乐?
How do I stop the background music in the arcade library?
我需要音乐一直播放到游戏结束,此时我希望它停止,但我尝试的任何方法似乎都不起作用。
下面是我正在使用的代码的简化版本。如果有的话,声音应该立即停止播放,因为 self.game
在更新中立即设置为 False,但无论我尝试什么,音乐都会继续播放。
import arcade
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Game"
class MyGame(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
self.game = True
self.music = arcade.load_sound("soundtrack.wav")
def setup(self):
self.music.play()
def on_draw(self):
arcade.start_render()
arcade.set_background_color(arcade.color.WHITE)
def update(self, delta_time):
self.game = False
if not self.game:
# stop the music
???
window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
window.setup()
arcade.run()```
简单的声音控制示例:
import arcade
class Game(arcade.Window):
def __init__(self):
super().__init__(400, 300, 'Sound demo')
self.text = 'stop'
self.music = arcade.load_sound(':resources:music/funkyrobot.mp3')
self.media_player = self.music.play()
def on_draw(self):
arcade.start_render()
arcade.draw_text(text=f'Click to {self.text} music', start_x=200, start_y=150, anchor_x='center', font_size=16)
def on_mouse_press(self, x, y, button, key_modifiers):
if self.text == 'stop':
self.text = 'start'
self.media_player.pause()
else:
self.text = 'stop'
self.media_player.play()
Game()
arcade.run()
单击时声音变为 on/off:
我需要音乐一直播放到游戏结束,此时我希望它停止,但我尝试的任何方法似乎都不起作用。
下面是我正在使用的代码的简化版本。如果有的话,声音应该立即停止播放,因为 self.game
在更新中立即设置为 False,但无论我尝试什么,音乐都会继续播放。
import arcade
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Game"
class MyGame(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
self.game = True
self.music = arcade.load_sound("soundtrack.wav")
def setup(self):
self.music.play()
def on_draw(self):
arcade.start_render()
arcade.set_background_color(arcade.color.WHITE)
def update(self, delta_time):
self.game = False
if not self.game:
# stop the music
???
window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
window.setup()
arcade.run()```
简单的声音控制示例:
import arcade
class Game(arcade.Window):
def __init__(self):
super().__init__(400, 300, 'Sound demo')
self.text = 'stop'
self.music = arcade.load_sound(':resources:music/funkyrobot.mp3')
self.media_player = self.music.play()
def on_draw(self):
arcade.start_render()
arcade.draw_text(text=f'Click to {self.text} music', start_x=200, start_y=150, anchor_x='center', font_size=16)
def on_mouse_press(self, x, y, button, key_modifiers):
if self.text == 'stop':
self.text = 'start'
self.media_player.pause()
else:
self.text = 'stop'
self.media_player.play()
Game()
arcade.run()
单击时声音变为 on/off: