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

Adding Events

Canvas Key is down event

        private void canvasKeyIsDown(object sender, KeyEventArgs e)
        {
            // if the space key is pressed and the game is not active
            if (e.Key == Key.Space && gameisactive == false)
            {
                // run the reset function
                ResetGame();
            }
        }

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

        private void popBalloons(object sender, MouseButtonEventArgs e)
        {
            // check i the game is active
            // this method will stop the player from the clicking the buttons when the game has stopped
            if (gameisactive)
            {
                if (e.OriginalSource is Rectangle)
                {
                    // if the click source is a rectangle then we will create a new rectangle
                    // and link it to the rectangle that sent the click event
                    Rectangle activeRec = (Rectangle)e.OriginalSource; // create the link between the sender rectangle

                    //load the mp3 file to the player URI
                    player.Open(new Uri("../../files/pop_sound.mp3", UriKind.RelativeOrAbsolute));
                    player.Play(); // play the mp3 file
                    MyCanvas.Children.Remove(activeRec); // find the rectangle and remove it from the canvas
                    score++; // add 1 to the score
                }
            }
        }

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

        private void startGame()
        {
            // this function will start the game
            // first start the timer
            gameTimer.Start();
            // set missed balloon to 0
            missedBalloons = 0;
            // set score to 0
            score = 0;
            // set intervals to 90
            intervals = 90;
            // change game is active boolean to true
            gameisactive = true;

            speed = 3; // set speed to 3
        }

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

        private void ResetGame()
        {
            // this function will reset the game and remove any unused item from the canvas
            // run loop to find any rectangles in this canvas
            foreach (var x in MyCanvas.Children.OfType<Rectangle>())
            {
                // if found add it to the item remover list
                itemRemover.Add(x);
            }

            // check if there is any rectangles in the item remover list
            foreach (Rectangle y in itemRemover)
            {
                // if found remove it from the canvas
                MyCanvas.Children.Remove(y);
            }
            // clear everything form the list
            itemRemover.Clear();
            // start the game function
            startGame();
        }

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.