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.
The main program loop will contain 3 main sections:
Type the code as follows:
To begin with, we're going to make a simple pygame program that does nothing but open a window and run a game 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.
This would work for background picture or any other pictures that you want to display on the screen.
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.
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:
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:
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:
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:
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:
In order to receive your completion certificate all projects should be submited before 6th April 2023
You have completed this project.