For school I have to make a text adventure and I have just created the menu. Now I want to have background music whilst in the menu and I have kind of figured out how. The problem is however, when I, for example, go to the options menu, the music stops playing. What I want it for the music to continuously play until the 'start' button is pressed.
Here is my menu code:
class Program
{
static string Playername = "";
static string CorrectIntro = "";
static ConsoleColor TextColor = ConsoleColor.White;
static System.Threading.Thread MusicPlayer;
private static SoundPlayer IntroMusic;
private static SoundPlayer MenuSelector;
public static void Main(string[] args)
{
MusicPlayer = new System.Threading.Thread(new System.Threading.ThreadStart(BackgroundMusic));
//Menu Selector Sound
MenuSelector = new System.Media.SoundPlayer();
MenuSelector.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\MenuSelector.wav";
Menu();
}
public static void BackgroundMusic()
{
IntroMusic.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\IntroMusic.wav";
IntroMusic.Play();
}
private static void Menu()
{
IntroMusic = new System.Media.SoundPlayer();
IntroMusic.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\IntroMusic.wav";
IntroMusic.Play();
Console.ForegroundColor = TextColor;
Zin("The story of Benedict Henderson");
Enter('\n');
Enter('\n');
Zin("[1] Start");
Enter('\n');
Zin("[2] Options");
Enter('\n');
Zin("[3] Credits");
Enter('\n');
Zin("[4] Quit");
Enter('\n');
ConsoleKeyInfo MenuSelector = Console.ReadKey(true);
switch (MenuSelector.Key)
{
case ConsoleKey.D1:
Intro();
break;
case ConsoleKey.D2:
Options();
break;
case ConsoleKey.D3:
Credits();
break;
case ConsoleKey.D4:
Quit();
break;
}
Console.ReadKey();
}
//Quit function
private static void Quit()
{
MenuSelector.Play();
Environment.Exit(0);
}
//Credits function
private static void Credits()
{
MenuSelector.Play();
Console.Clear();
Zin("This game is made and designed by Stijn van der Neut");
Enter('\n');
Enter('\n');
Zin("Press enter to return to the menu");
ConsoleKeyInfo ReturnToMenu = Console.ReadKey(true);
if (ReturnToMenu.Key == ConsoleKey.Enter)
{
Console.Clear();
Menu();
}
}
//Option function
private static void Options()
{
MenuSelector.Play();
Console.Clear();
Zin("Please select the desired text color");
Enter('\n');
Enter('\n');
Zin("[1] White");
Enter('\n');
Zin("[2] Red");
Enter('\n');
Zin("[3] Blue");
Enter('\n');
Zin("[4] Green");
Enter('\n');
Zin("[5] Magenta");
Enter('\n');
Zin("[6] Cyan");
Enter('\n');
Enter('\n');
Zin("Press enter to return to the menu");
ConsoleKeyInfo ColorSelector = Console.ReadKey(true);
//Get player input for ColorSelector
switch (ColorSelector.Key)
{
case ConsoleKey.D1:
MenuSelector.Play();
TextColor = ConsoleColor.White;
break;
case ConsoleKey.D2:
MenuSelector.Play();
TextColor = ConsoleColor.Red;
break;
case ConsoleKey.D3:
MenuSelector.Play();
TextColor = ConsoleColor.Blue;
break;
case ConsoleKey.D4:
MenuSelector.Play();
TextColor = ConsoleColor.Green;
break;
case ConsoleKey.D5:
MenuSelector.Play();
TextColor = ConsoleColor.Magenta;
break;
case ConsoleKey.D6:
MenuSelector.Play();
TextColor = ConsoleColor.Cyan;
break;
case ConsoleKey.Enter:
MenuSelector.Play();
Console.Clear();
Menu();
break;
default:
TextColor = ConsoleColor.Black;
break;
}
Console.ForegroundColor = TextColor;
Console.Clear();
Menu();
}
Sorry for the long block of code, and by the way, take it easy on me, I just started my coding education for 3 months now :).
So I to clarify things. There are 2 SoundPlayer objects, one that plays the background music and the other (MenuSelector) that plays a short little sound when you go to a submenu.
I changed my code to this because apparently the play function automatically creates a new thread for you instead of having to do it yourself. The music still doesn't continuously play throughout the entire menu though.
class Program
{
static string Playername = "";
static string CorrectIntro = "";
static ConsoleColor TextColor = ConsoleColor.White;
private static SoundPlayer IntroMusic;
private static SoundPlayer MenuSelector;
public static void Main(string[] args)
{
//Menu Selector Sound
MenuSelector = new System.Media.SoundPlayer();
MenuSelector.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\MenuSelector.wav";
Menu();
}
private static void Menu()
{
IntroMusic = new System.Media.SoundPlayer();
IntroMusic.SoundLocation = @"C:\Users\stijn\Desktop\School\Programmeren\Periode 1\Text Adventure\Sounds\IntroMusic.wav";
IntroMusic.Play();
Console.ForegroundColor = TextColor;
Zin("The story of Benedict Henderson");
Enter('\n');
Enter('\n');
Zin("[1] Start");
Enter('\n');
Zin("[2] Options");
Enter('\n');
Zin("[3] Credits");
Enter('\n');
Zin("[4] Quit");
Enter('\n');
ConsoleKeyInfo MenuSelector = Console.ReadKey(true);
switch (MenuSelector.Key)
{
case ConsoleKey.D1:
Intro();
break;
case ConsoleKey.D2:
Options();
break;
case ConsoleKey.D3:
Credits();
break;
case ConsoleKey.D4:
Quit();
break;
}
Console.ReadKey();
}
//Quit function
private static void Quit()
{
MenuSelector.Play();
Environment.Exit(0);
}
//Credits function
private static void Credits()
{
MenuSelector.Play();
Console.Clear();
Zin("This game is made and designed by Stijn van der Neut");
Enter('\n');
Enter('\n');
Zin("Press enter to return to the menu");
ConsoleKeyInfo ReturnToMenu = Console.ReadKey(true);
if (ReturnToMenu.Key == ConsoleKey.Enter)
{
Console.Clear();
Menu();
}
}
//Option function
private static void Options()
{
MenuSelector.Play();
Console.Clear();
Zin("Please select the desired text color");
Enter('\n');
Enter('\n');
Zin("[1] White");
Enter('\n');
Zin("[2] Red");
Enter('\n');
Zin("[3] Blue");
Enter('\n');
Zin("[4] Green");
Enter('\n');
Zin("[5] Magenta");
Enter('\n');
Zin("[6] Cyan");
Enter('\n');
Enter('\n');
Zin("Press enter to return to the menu");
ConsoleKeyInfo ColorSelector = Console.ReadKey(true);
//Get player input for ColorSelector
switch (ColorSelector.Key)
{
case ConsoleKey.D1:
MenuSelector.Play();
TextColor = ConsoleColor.White;
break;
case ConsoleKey.D2:
MenuSelector.Play();
TextColor = ConsoleColor.Red;
break;
case ConsoleKey.D3:
MenuSelector.Play();
TextColor = ConsoleColor.Blue;
break;
case ConsoleKey.D4:
MenuSelector.Play();
TextColor = ConsoleColor.Green;
break;
case ConsoleKey.D5:
MenuSelector.Play();
TextColor = ConsoleColor.Magenta;
break;
case ConsoleKey.D6:
MenuSelector.Play();
TextColor = ConsoleColor.Cyan;
break;
case ConsoleKey.Enter:
MenuSelector.Play();
Console.Clear();
Menu();
break;
default:
TextColor = ConsoleColor.Black;
break;
}
Console.ForegroundColor = TextColor;
Console.Clear();
Menu();
}