python - 玩python游戏背景音乐不耽误实际游戏
问题描述
我正在制作一个刽子手 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() 播放了音乐但仍然延迟了游戏。(目标输出是与我制作的游戏同时播放的音乐)
Ps:我更喜欢适用于所有操作系统或至少 Linux 和 Windows 的解决方案