In this lesson, we will walk through the Pygame library and setup the foundation for the Pygame project and eventually build a Rocket Launch Animation.
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 below 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 rocket 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 rocket 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 can fly up and disappeared. We need to make sure rocket 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 rocket appear again after it reaches the top of the screen. We also used random.randint() function to make rocket appear on the different location. Don't forget to import random function at the top of the code like this:
You have completed this project.