...

Balloon Pop Game

In this lesson, we will walk through the Pygame library and setup the foundation for the Pygame project and eventually build a Balloon Pop game.

Pygame Library

What is Pygame library?

Pygame is a game library - a set of tools to help programmers make games. Some of these things are:

  • Graphics and animation
  • Sound (including music)
  • Control (keyboard, mouse, gamepad, etc.)
  • Game Loop

    At the heart of every game is a loop, which we call the Game Loop. This loop is constantly running, over and over again, doing all the things that are needed to make the game work. Each time the game goes through this loop is called a frame.

    The main program loop will contain 3 main sections:

  • Capturing Events: Used to constantly listen to user inputs and react to these. It could be when the user uses the keyboard or the mouse.
  • Implementing the Game Logic: What happens when the game is running? Are cars moving forward, aliens falling from the sky, ghosts chasing you, balloons flying etc.
  • Refreshing the screen by redrawing the stage.
  • The main program loop will also use a frame rate to decide how often should the program complete the loop (refresh the screen) per second. To implement this we will use the clock object from the pygame library. The main program loop will use a timer to decide how many times it will be executed per second.
    Next
    Step 1: Let's start coding
  • In your logged-in trinket account click on New Trinket -> Pygame

  • Or Open the blank Python template trinket: Open New.

  • Prev Next
    Step 2: Importing and Initialising the Pygame library
  • The first step is to import the Pygame library. Place import statements at the top of your code.
  • Next, initialise it with init()
  • Then add quit() event
  • Type the code as follows:

    Prev Next
    Step 3:Building a Pygame Template

    To begin with, we're going to make a simple pygame program that does nothing but open a window and run a game loop.

  • We are going to define the screen to open a game window
  • Set a few variables for our game options
  • Define the Game main loop.
  • Here is our game loop, which is a while loop controlled by the variable running. If we ever want the game to end, we just have to set running to False and the loop will end.

    Prev Next
    Step 4: Add Background Picture to the game

    This would work for background picture or any other pictures that you want to display on the screen.

  • Before the main program loop add the following code:
  • In your main program loop add the following code:
  • In the above code (0,0) represents the (x,y) coordinates of where you would like your picture to appear. The background image has to be uploaded to “images” in your trinket.

    Prev Next
    Step 5: Add Balloon to the screen

    Next to the background image we need to add a balloon image, similar to bg image. We also need to define image box and centre location of the image as below:

    In your main program loop add the following code to display balloon on the screen

    Full code so far should be following:

    Prev Next
    Step 6: Make Balloon fly

    To make an object move in Pygame we need to change it’s x or y coordinates. For balloon to fly we will be changing y coordinate. This code line should be added to the main loop under the Game Logic

    Run the code and make sure balloon fly up and disappeared. We need to make sure balloon appears again, so we need to add more logic to our game. Add following code after the last line in the Game Logic section:

    This piece of code will make balloon appear again after it reaches the top of the screen. We also used random.randint() function to make balloon appear on the different location. Don’t forget to import random function at the top of the code like this:

    Prev Next
    Let's continue our Balloon Game from the last week Click Here to continue
    Step 7: Pop Balloon on mouse click

    The next we need to detect a mouse click on our balloon image. We need to add this piece of code under the Capturing Events. The game listener will detect the mouse click and compare the mouse click position with balloon image position. If the click happened on the balloon it will define as a collision and we will hide balloon as it supposed to be pop. Follow the code below:

    We also want to see a baloon pop on the mouse clikc, so balloon image should change the image to the pop image. First we load the pop image before the game loop as following:

    Next we add following code inside the Capturing events section to change the image, make sure you follow the code as below:

    Full Code so far should look like this:

    Prev Next
    Step 8: Add Game Score

    Every time we click on a balloon we will increase our score by 1 and every time we missed a balloon we will decrease our score by 1.

    So we need to add a new variable at the top of the screen before we load images

    Next, we increase the score on collision condition, so adding a line of code as below:

    And if the balloon disappeared from the screen and we didn't click on it we will be decreasing the score, adding the following line under the Game Logic if condition:

    Next, display the score as a text on the screen, for this we use the following lines of code First, define the font at the top of the program before the main loop:

    Next, add the following lines of code in the main game loop below blit commands:

    Prev Next
    Step 9: Complete and Share

    Save and Submit your project

    Don't forget to save your work. Click the Save button or Remix to save to your account.

    The gamess final code should look like this:

    Prev Next
    Step 10: Submit and Share

    Submit your final project

    In order to receive your completion certificate all projects should be submited before 6th April 2023



    Copy your Trinket Url

  • Click Share->Link
  • Copy the link to share your code
  • Paste link into the field
  • Prev