Share via


Step 6: Add a SoundPlayer

Next, add some sound to your maze game. One sound should be played when the user touches a wall and gets sent back to the starting point, and a different sound should be played when the user wins. In this step, you add a sound that's played when the mouse pointer touches a wall. Although it may seem complicated, just a few lines of code are needed.

link to videoFor a video version of this topic, see Tutorial 2: Create a Maze in Visual Basic - Video 4 or Tutorial 2: Create a Maze in C# - Video 4.

To add a SoundPlayer for noise

  1. Start by adding a SoundPlayer to your form's code, just above the constructor.

    Public Class Form1
    
        ' This SoundPlayer plays a sound whenever the player hits a wall. 
        Private startSoundPlayer = New System.Media.SoundPlayer("C:\Windows\Media\chord.wav")
    
        Public Sub New()
            ' This call is required by Windows Forms Designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            MoveToStart()
        End Sub
    
    public partial class Form1 : Form
    {
        // This SoundPlayer plays a sound whenever the player hits a wall.
        System.Media.SoundPlayer startSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\chord.wav");
    
        public Form1()
        {
            InitializeComponent();
            MoveToStart();
        }
    

    Note

    The first line (public partial class Form1 : Form) has appeared several times before. It's important, because it includes the class keyword. The class keyword appears many times, because a class is a basic building block of any program.

  2. Earlier, you put your mouse pointer over the word MessageBox in the statement MessageBox.Show("Congratulations!");, to make the IDE open a tooltip. Do this again now, but take a closer look at the first line, which appears as follows.

    Tooltip

    Tooltip

    Note

    The class keyword appears in the first line. It appears frequently because your code is organized into classes as follows: Your program has classes, each class has methods, and each method has statements. There are numerous built-in classes, such as MessageBox. The MessageBox class has a method called Show(), and when called, it executes statements that open a message box. You have also worked with Button, Label, and Panel classes. When you set their properties, you worked with another aspect of classes: A class can have properties as well as methods, and setting those properties can cause the class to execute statements that change behavior.

    As you may realize, SoundPlayer is a class that plays a sound. When you create a SoundPlayer with the new keyword, it loads a sound from a file, which you can play using its Play() method. You will use this SoundPlayer to play the Windows Chord sound when the player starts a new game, or when the pointer touches a wall and the player has to start over. (That's why it's called startSoundPlayer.)

  3. If you want to use different sounds, replace the path between the quotation marks in the new statement (C:\Windows\Media\chord.wav) with the path of the sound file that you want to use.

    When you build your form in Windows Forms Designer, you use the IDE to help you create your own class, in this case, a class called Form1. When you added that line of code above your constructor, you added a new SoundPlayer to your form, just like you previously added a button or a label. The statement is located outside of the methods so that the SoundPlayer can be accessed by more than one method. That's why you had to put the new statement inside your form's code but outside of its methods. You named it startSoundPlayer, the same way you named one of your Label controls finishLabel.

    After you add the statement to create a new SoundPlayer and call it startSoundPlayer, it appears in the IntelliSense window, just like labels, buttons, and other controls.

    This may seem complicated, but it's similar to what you did previously in the IDE. For example, when you use the IDE's Toolbox to add a button or label to the form, the IDE adds lines of code automatically that are used to create a new button or a new label. You do the same now, except this time, you create a SoundPlayer. (A second SoundPlayer is created in the next tutorial step.)

To continue or review