在不延迟实际gam的情况下播放python游戏背景音乐

2024-06-25 07:27:04 发布

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

我正在制作一个hangman python游戏来了解python语法,但是由于代码播放背景音乐.wav文件,代码延迟到音乐结束,而不是同时运行

我尝试使用os.system命令(来自os库)和subprocess.popen()在新的脚本窗口中使用不同的脚本文件打开音乐,但没有任何效果

import os
def music(): #this is the in-game function that is supposed to run the music
    os.system("musicforhangmaninit.py")


import winsound #this is the command that plays the .wav file music created earlier by this script

winsound.PlaySound('hangmanadditionals.wav', winsound.SND_FILENAME)

没有出现错误消息,但我认为我把os.system()函数弄错了,因为它似乎什么都不做,subprocess.popen()播放了音乐,但仍然延迟了游戏(目标输出是与我制作的游戏同时播放的音乐)

附言:我更喜欢在所有操作系统上工作的解决方案,或者至少在Linux和windows上工作


Tags: 文件the代码脚本游戏音乐isos