...

Rocket Launch

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.

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 Open Template
  • 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

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

  • Before the main program loop add background image
  • In your main program loop add blit command to display bg image
  • Add display.update() command
  • 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.

    Prev Next
    Step 5: Add Rocket to the screen

    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:

    Prev Next
    Step 6: Make Rocket fly

    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:

    Prev Next
    Complete Code
    Submit and Share

    Submit your final project



    Copy your Trinket Url

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