【发布时间】:2015-12-06 04:40:10
【问题描述】:
我正在开发一款C# 控制台游戏,我真的很想在其中加入一些背景音乐。
我有一个调用 SoundPlayer 的函数,我让它按预期播放音乐。我的函数将在我的游戏的某些情况下被调用,但是当它被调用时,我希望游戏继续。目前,我的游戏按预期循环播放,但一旦到达playMusic() 功能,游戏就会停止,直到音乐停止播放。
如何在不中断主游戏循环的情况下让音乐在后台播放?对此事的任何想法将不胜感激。
音乐功能:
public void playMusic()
{
SoundPlayer sndPlayer = new SoundPlayer();
sndPlayer.SoundLocation = Environment.CurrentDirectory + "\\Music.Wav";
sndPlayer.PlaySync();
}
我的游戏循环:
int gameoverCount =0;
//Main game loop
while (gameoverCount != 1000)
{
//Select a random NPC to move
int randomNPC = StaticRandom.Instance.Next(0, npcs.characters.Count);
//Checks if the NPC has reached it's destination, if not move.
if (npcs.characters[randomNPC].destination)
{
Movement.npcMove(board, npcs.characters[randomNPC]);
}
else
{
//Gives NPC new location
npcs.characters[randomNPC].destinationX = StaticRandom.Instance.Next(3, 14);
npcs.characters[randomNPC].destinationY = StaticRandom.Instance.Next(3, 14);
npcs.characters[randomNPC].destination = true;
}
gameoverCount++;
Movement.playerMove(p1.coordX, p1.coordY, board, p1, p1.bac, p1.stepsTaken, npcs.characters[randomNPC]);
//If player is on a certain space, play music
if (board.board[p1.coordX, p1.coordY].playMusic)
{
playMusic();
}
Console.Clear();
board.showBoard();
}
【问题讨论】:
标签: c# console background-music