Creating a Maze Game Using C# in .NET 4.5

Introduction

In this article you will learn how to create a maze using C# using .NET 4.5. In this article I used Labels to create maze.

Step 1

Create a new project by selecting Windows Forms Application. You must make some changes in the properties of the form like:

  1. Set it's size to 654,654
  2. Set it's Form Border Style Property to Fixed 3D so that the user can't resize the form.
  3. Set the Maximize Box to False.

Maze1.jpg

Step 2

Drag a panel from the Tool Box to your form. Using the handle button Maze2.jpg move the panel to the upper left corner of the page until the blue Spacer Line shown on the left and the top of the panel. Now drag the lower handle button so that a similar Spacer Line is shown on the right and lower side of the panel.

Maze3.jpg

Set the Border Style Property of the panel to "Fixed 3D" so that the user can see the border of the maze.

Step 3

Take a Label from the Tool Box and make the following changes in it's properties:

  1. Change it's Auto Size Property to False.
  2. Set the Background Color as you choose, I used Royal Blue.
  3. Empty it's Text Property by deleting the "Label1" from it.

Copy this Label and paste it several times and create a Maze by using your creativity.

Step 4

Take a new Label from the Tool Box and rename it as Final, this will be the end point of your game.

Now save and run your program, your output will look something like this:

Maze4.jpg

Step 5

Select the "Finish" Label and go to it's events through the Property Window, scroll down to the "Mouse Enter" event and double-click it. It will take you to the Source Code section of the application.

Maze5.jpg

Write the following code in the Source Code Page:

  private void Finish_MouseEnter(object sender, EventArgs e)

  {

      MessageBox.Show("You Won");

      Close();

  }

This code will execute when the player reaches the end point or touches the Finish Label.

Step 6

Now add a new method named "movetostart". Add this method just below the "Finish_MouseEnter" method. This method will take the user to the starting point i.e. 10 pixels down and 10 pixels to the right. Write the following code in this method:

private void movetostart()

{

    Point startingpoint = panel1.Location;

    startingpoint.Offset(10, 10);

    Cursor.Position = PointToScreen(startingpoint);

}

Step 7

After adding the method you need to call it. Since your program should call this as soon as the game starts, you should call the method from the Page Load. For that write the following code:

public Form1()

{

    InitializeComponent();

    movetostart();

}

Step 8

Now select any of the labels except the Final label and go to it's Property Window. In the Property Window go to the Events, scroll down to the "Mouse Enter" and write wall_MouseEnter.

Maze6.jpg

After doing that, double-click on it and it will take you to the coding part. Add the movetostart method to this Event.

private void wall_MouseEnter(object sender, EventArgs e)

{

    movetostart();

}

Step 9

Now go to the Edit Menu and click "Select All", then press the "CTRL" button and hold it until you click the "Final" Label. This will select all the Labels except the Final Label. Now go to the Event Table in the Property Window; scroll down to the "Mouse Enter" Event and click on the Drop Down Button present in front of it. You should see two event handlers, one should be "Final_MouseEnter" and the other should be "wall_MouseEnter". Select the "wall_MouseEnter".

Maze7.jpg

Step 10

Now we will add some sound to our Game. For that write the following code just above the Constructor. This code will play the sound on starting the game.

public partial class Form1 : Form

{

System.Media.SoundPlayer startSound = new System.Media.SoundPlayer(@"C:\Windows\Media\chord.wav");

public Form1()

{

    InitializeComponent();

    movetostart();

}

Add this code to change the sound on game completion:

public partial class Form1 : Form

{

System.Media.SoundPlayer startSound = new System.Media.SoundPlayer(@"C:\Windows\Media\chord.wav");

System.Media.SoundPlayer endSound = new System.Media.SoundPlayer(@"C:\Windows\Media\tada.wav");

public Form1()

{

    InitializeComponent();

    movetostart();

}

Step 11

Add a Play() method to call the Sound Player at the appropriate time. For the first sound add this code to the movetostart() method:

private void movetostart()

{

    startSound.Play();

    Point startingpoint = panel1.Location;

    startingpoint.Offset(10, 10);

    Cursor.Position = PointToScreen(startingpoint);

}

For the second sound add this code to the "Finish_MouseEnter()":

private void Finish_MouseEnter(object sender, EventArgs e)

{

    endSound.Play();

    MessageBox.Show("You Won");

    Close();

}

Now simply run your program and your game will be started.

Maze8.jpg