× Main Menu

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

Adding Events

Canvas Key is down event

This is a simple event; we are checking if the Space is pressed AND we want to make sure game active Boolean is set to false. If both of these conditions are met then we can run the reset function. This function is required when the game ends and user wants to play it again.

Pop balloons event

This is the mouse click event. First, we want to make sure that the game is active Boolean is true then we can go inside the first if statement and move to the second one.

In the second if statement we check if the user has clicked on a rectangle if so, we create another local rectangle inside called activeRec. With this new rectangle we can check if we have clicked on an object and if that object is a rectangle. We are using this method to identify the balloon objects we have clicked on.

Load the mp3 file to the player and play it. Lastly remove the rectangle clicked on from the canvas. Add 1 to the score integer.

Start Game Function –

Type the code down as is below into your own function in visual studio

Above is the start game function. this functions purpose is load everything back to its default values. First, we start the game timer, set missed balloons and score to 0. Set interval to 90, speed to 3 and change game is active Boolean to true.

Reset Game Function

Above is the game reset function. this function will remove all of the elements from the screen for the player to have a fresh start in this game. There are two for each loop in this function. the first one is identifying the rectangles in this canvas and then adding to the item remover list. The second for each loop is finding all of the rectangles saved inside the

Go to Page 4 to start on the game engine event

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: