...

Back to Leadership Board

Challenge #7: Pygame Animation

Start Date: November 24, 2023
Submit By: 8:30 am on December 01, 2023

In this project, we will delve into the fundamentals of game development using the Pygame library. Gain insights into game structure and design as you create your own animation.
We will cover the following:

  •     Pygame library
  •     Graphics and animation
  •     Control (keyboard, mouse, etc.)
  •     Build your animation
Watch a Video Tutorial

Video Tutorial

Challenge #7: Pygame Animation

Create an engaging Pygame animation by setting up a screen, incorporating background images, implementing horizontal and vertical movements, and adding character control. Follow video tutorials and customize your themed animation.

Challenge yourself by experimenting with additional features, and document your code for clarity. Showcase your coding and creative skills in this visually appealing project.

Challenge Details:
  • Create a new Trinket, make sure you use Pygame, or simply use this template Pygame Template
  • Set Up the Screen: Initialise a Pygame window and configure the screen with the desired dimensions.
  • Add Images: Add at least 3 objects to your animation. Visit https://www.pngwing.com/ to find and download images for your objects. Resize and modify these images as needed to fit them into your animation.
  • Add Movement:
    • Horizontal Movement: Implement logic for an object (e.g., a car, spaceship) to move smoothly from one side of the screen to the other.
    • Vertical Movement:Introduce an object (e.g., a balloon, falling leaves) that gracefully moves vertically.
    • Character Movement with Key Press: Create a character object (e.g., a player, animal) that responds to keyboard inputs. Enable left/right or up/down movement based on arrow key presses.
  • Bring Your Animation to Life: Combine all elements to create a harmonious and dynamic animation.
  • Document Your Code: Add comments and documentation throughout your code to explain the purpose of different sections. This will serve as a reference for future modifications.
  • Challenge Yourself: Experiment with additional features such as sound effects, particle effects, or object interactions. Push your creativity to enhance the overall immersive quality of the animation.
Too Hard? If this challenge feels too challenging, don't worry! Try your best to follow a video tutorial for step-by-step guidance.

Next
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.
    Prev Next
    Importing and Initialising the Pygame library
  • The first step is to import the Pygame library. Place import statements at the top of your code.
  • We also need to import pre-defined libraty file and settings
  • Next, initialise it with init()
  • Type the code as follows:

    Prev Next
    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.

    Prev Next
    Add background and load images to the game

    Visit https://www.pngwing.com/ to find and download images for your objects. Resize and modify these images as needed to fit them into your animation. Make sure you rename your downladed images and give them a meiningfull name.

    In trinket clikc to Image icon and add your images

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

  • Before the main program loop we load the pictures
  • In your main program loop we display them with blit command
  • We also added the pygame.display.update() command to our code to consistently refresh the screen and make the changes visible to the user.
  • Prev Next
    Add Game Logic

    In the next section of the game logic, we will add the following:

  • Logic to move the cloud horizontally.
  • Logic to move the balloon upward.
  • Logic to move the player character right and left with key presses.
  • Full code so far should be following:

    Prev Next
    Submit your Challenge

    Submit your final project





    Copy your Trinket Url

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

  • Do you have a question?



    Prev