Objectives
Pygame fundamentals
- Create a window and set its title
- Explain how colours are represented
- Describe the purpose of the game loop and event loop
- Use a Clock to control framerate
- Explain why the display must be updated each frame
Drawing and animation
- Draw basic shapes (circles and rectangles) at given positions
- Update variables each frame to create movement
- Modify speed and direction of movement
- Use
WIDTHandHEIGHTin calculations to position shapes
Behaviour
- Describe what happens when shapes move beyond the window boundary
- Use simple selection to keep a moving shape within the window boundary
Code quality
- Identify and replace magic numbers
File
Make local copies of the following file before you start.
main.py
import pygame
import sys
pygame.init()
WIDTH = 600
HEIGHT = 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My First Pygame Window")
clock = pygame.time.Clock()
x = 300 # shape's starting x-position
running = True
while running:
# --- EVENT HANDLING ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# --- UPDATE WORLD ---
x = x + 1
# --- DRAW WORLD ---
screen.fill((255, 255, 255))
pygame.draw.circle(screen, (0, 0, 255), (x, 200), 40)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()Predict
Predict answers to the following questions without running the program.
- What colour will the background be?
- What shape will be drawn? Where?
- What will happen to the shape over time?
- What does
clock.tick(60)control? - What will happen when you close the window?
Run
Run the program. Compare your predictions to the actual behaviour.
Investigate
Answer these questions by examining the code.
- On what line is the window size set?
- How would you double the window size?
- What does changing the line
pygame.draw.circle(screen, (0, 0, 255), (x, 200), 40)topygame.draw.circle(screen, (0, 255, 0), (x, 200), 40)do?- What does this tell you about how Pygame represents colours?
- Which line number makes the shape move?
- Which line number is responsible for drawing the shape?
- What happens if you comment out the line
pygame.display.flip()?- What does this tell you about what this line does?
- What happens if you comment out the line
clock.tick(60)?- What does this tell you about what this line does?
- What happens when the shape reaches the edge of the window?
Modify
Make changes to explore how the code works.
- Identify and remove at least three magic numbers by replacing them with variables.
- Change the colour of the background to a colour of your choosing.
- Replace the circle with a rectangle (see the Pygame docs)
- Make changes to the movement of the shape:
- Make it move faster
- Make it move in the opposite direction
- Make it move diagonally
- Add a new shape that is centred based on a calculation using
WIDTHand `HEIGHT - Prevent the shape from leaving the window.
- Hint: use selection and check the x-coordinate relative to
WIDTHandHEIGHT
- Hint: use selection and check the x-coordinate relative to
Make
Animation
Create a small animated scene using only things covered so far.
Requirements
Your animation must include:
- One static shape
- One moving shape
- At least one colour change
- One use of
WIDTHorHEIGHTin a calculation
Ideas to try
- A rectangle sliding across the screen
- A circle moving diagonally
- A shape that changes colour once at startup
- A shape that changes colour every 10 clock ticks
- Something positioned using
WIDTH // 3orHEIGHT // 4
Screensaver
Requirements
Create a short Pygame ‘screensaver’ with multiple animated elements. Be as creative as you like.
- At least four animated shapes
- At least one shape whose movement changes over time (e.g., speed or direction)
- At least one shape that bounces off the edge of the window (separate to the shape above)
- A colour change that happens during the animation
- A timed event: after some amount of time, something must change (e.g., a shape appears/disappears, colour change, movement change)
Options
Choose two or more optional enhancements:
- A shape that follows a curved path
- Hint: change both x and y each frame by different amounts
- A shape that grows or shrinks in size
- A fade effect using gradually changing background colours
- A simple caption or title drawn using
pygame.font - A shape that ‘reacts’ to another (e.g., follows it, changes colour when near it)
- Encapsulate a shape and appropriate attributes in a class (e.g., position, speed, colour, update method)