背景音乐在另一个声音后停止播放

2024-09-27 07:35:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个pygame游戏,我正在尝试添加背景音乐。这一切工作,直到我按空格键,这会导致其他声音效果(拍摄声音),但它停止了背景音乐,我不知道为什么和如何解决这个问题。
pygame.mixer.music.load(background_music) pygame.mixer.music.play(-1,0.0)
这就是按空格键时的声音效果(在主循环中):
if keys[pygame.K_SPACE]: if (len(bullets) < 4 and shootLoop == 0 and not (ship.isBolt)) or (len(bullets) < 8 and shootLoop == 0 and ship.isBolt): if not (ship.isBolt): bullets.add(Projectile(round(ship.rect.x + ship.width / 2 - 5), ship.rect.y - ship.height - 8)) else: bullets.add(Projectile(ship.rect.x + ship.width - 11, ship.rect.y - ship.height + 8)) bullets.add(Projectile(ship.rect.x - 4, ship.rect.y - ship.height + 8)) pygame.mixer.music.load(SOUND_SHOT) pygame.mixer.music.play()
这是我用来播放背景音乐的代码
如何修复?你知道吗


Tags: andrectadd声音ifmusicpygameheight
2条回答
pygame.mixer.music.load(os.path.join(os.getcwd(), 'sound', 'back-ground-music'))
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(loops=-1)  

pygame.mixer.Channel(0).play(pygame.mixer.Sound('sound\effects.wav'), maxtime=600)

使用频道如果你想另一个效果的声音使用pygame.mixer.Channel频道(1) 例如你能试试这个吗

你可以这样使用它:

pygame.mixer.music.load("background_music")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)

相关问题 更多 >

    热门问题