× Main Menu

WPF C# Tutorial – Create a fun Balloon popping game in visual studio

Start Programming –

Let’s add the Threading name space to this project so we can use the dispatcher timer. Each line of code has been commenting in the program. Every line that starts with the // double slash is a comment, inside of visual studio it will show as a green line. In these lines you can write notes, explain your code for some one or eve just add what the line is doing for future reference.

mooict wpf tutorial - add the threading name space to the project, we need this for the timer

This line using System.Windows.Threading; allows us to use the timer class from inside of it. Timer is the main game object that we need to make the balloons move, track their positions, spawn new balloons and more.

Add the variables –

mooict wpf tutorial - set up all of the variables for the balloon pop game

Here are the variables for this game. Add the above the public MainWindow() line. All of these are global variables and can be accessed from any of the functions inside of this program.

The main window function –

This function will come default with every project, this function runs when the main window loads so we can set instructions here to load when the window does.

Under the initialize component line add them all. In this function we are setting up the game timer first the tick this will link with the gameEngine event we created earlier. Set the game timer interval to run every 20 milliseconds. So this gameEngine event will run every 20 milliseconds in this game.

Set the canvas background to the background image we created and run the reset game function.

Go to Page 3 to start adding events for this game

2 responses to “WPF C# Tutorial – Create a fun Balloon popping game in visual studio”

  1. coco says:

    When I start the game it works fine but after a few pops the game stops and in the .cs says that there is an exception unhandled “the enumerator is not valid because the collection changed.” in:
    foreach(var x in MyCanvas.Children.OfType())

  2. mari says:

    @coco: I had the same problem. My brackets were closing in the wrong place.

Comment on this tutorial and let us know how you got on -

%d bloggers like this: