控制台背景音乐
Console Background Music
我正在开发一个 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();
}
另一方面,您正在寻找的是 Play or the PlayLooping method. As you can see in the remarks section, both these methods will play the music using a new thread. PlaySync,它使用当前线程,这就是您获得当前行为的原因。
我正在开发一个 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();
}
另一方面,您正在寻找的是 Play or the PlayLooping method. As you can see in the remarks section, both these methods will play the music using a new thread. PlaySync,它使用当前线程,这就是您获得当前行为的原因。