Pygame Course ยท Learn Coding by Making Games

Mingming Li โ€” Welcome! Click any heading below to expand the content.

๐Ÿ“– Table of Contents

1. Student introduction activities

2. Learning goals

3. Install Python & Thonny

There are many ways to write and run Python code. In this course, we will use Thonny (great for beginners).

๐Ÿ’ก Pro tip: After installing Thonny, you need to install Pygame, NumPy, and Matplotlib. Open Thonny, go to "Tools" โ†’ "Manage Packages", search for "pygame", click "Install". Repeat for "numpy" and "matplotlib".

4. A simple video game (Code-1)

We will write, understand, and modify this code. It produces a simple jumping game.

โฌ‡๏ธ Download game-simple.py

Let's play the game and have some fun!


  1 import pygame
  2 pygame.init()
  3 screen = pygame.display.set_mode((800, 400))
  4 clock = pygame.time.Clock()
  5 
  6 player = pygame.Rect(80, 300, 40, 40)
  7 cactus = pygame.Rect(800, 300, 30, 40)
  8 gravity = 0
  9 
 10 while True:
 11     for event in pygame.event.get():
 12         if event.type == pygame.QUIT:
 13             pygame.quit()
 14             exit()
 15 
 16     keys = pygame.key.get_pressed()
 17     if keys[pygame.K_SPACE] and player.bottom >= 400:
 18         gravity = -20
 19 
 20     gravity += 1
 21     player.y += gravity
 22     if player.bottom > 400:
 23         player.bottom = 400
 24 
 25     cactus.x -= 5
 26     if cactus.x < -50:
 27         cactus.x = 900
 28 
 29     screen.fill((255,255,255))
 30     pygame.draw.rect(screen, (0,255,0), player)
 31     pygame.draw.rect(screen, (255,0,0), cactus)
 32     pygame.display.flip()
 33     clock.tick(60)
        

5. Challenge: Modify the Game

Try changing these values and see what happens!
  • Change cactus.x -= 5 to cactus.x -= 10 โ†’ What happens to the speed?
  • Change gravity = -20 to gravity = -30 โ†’ How does the jump change?
  • Change player = pygame.Rect(80, 300, 40, 40) width to 60 โ†’ Does the game become easier or harder?
  • Change the window size from (800, 400) to (1000, 500) โ†’ What happens?

Write down your observations! Experimenting is the best way to learn.

6. Python Essentials

Can you find the above concepts in Code-1?

7. Pygame Essentials

Pygame is a Python package for making games.

Key concepts: Game Loop, Events, Rectangles, Coordinate System, Collision Detection

8. What is a video game?

9. Essential components of a video game

A video game is all about where, when, and how we put images (and play sounds).

10. Adding images to our game (Code-2)

โฌ‡๏ธ Download game-1-cactus.py

You also need to download these two images:

โฌ‡๏ธ Download player.png โฌ‡๏ธ Download cactus.png

Make sure to put the code and images in the same folder.

๐ŸŽจ Color legend: ๐ŸŸข Green = Image-related code

  1 import pygame
  2 from sys import exit
  3 from random import randint
  4 
  5 pygame.init()
  6 screen = pygame.display.set_mode((800, 400))
  7 pygame.display.set_caption('Game')
  8 clock = pygame.time.Clock()
  9 
 10 game_active = False
 11 gravity = 0
 12 
 13 # Player image
 14 player_img = pygame.image.load('player.png').convert_alpha()
 15 player_img = pygame.transform.rotozoom(player_img, 0, 0.5)
 16 player_rect = player_img.get_rect(midbottom=(80, 400))
 17 
 18 # Cactus image
 19 cactus_img = pygame.image.load('cactus.png').convert_alpha()
 20 cactus_rect = cactus_img.get_rect(midbottom=(randint(900, 1100), 400))
 21 
 22 while True:
 23     # ... event handling ...
 24     if game_active:
 25         screen.blit(player_img, player_rect)
 26         screen.blit(cactus_img, cactus_rect)
 27     # ... rest of game loop ...
        

Note: Code-2 adds a game over screen, start screen, and collision detection!

11. How can we add more cactus to the game? (Code-3)

Instead of one cactus, we use a LIST to store multiple cacti!

โฌ‡๏ธ Download game-more-cactus.py

 22 # List to store active cacti
 23 active_cacti = []
 24 last_spawn_time = pygame.time.get_ticks()
 25 
 54 # Update all cacti
 55 for cactus_rect in active_cacti[:]:
 56     cactus_rect.x -= 6
 57     if cactus_rect.x <= -100:
 58         active_cacti.remove(cactus_rect)
 59 
 60 # Spawn new cactus every 1 second
 61 if current_time - last_spawn_time > 1000:
 62     last_spawn_time = current_time
 63     new_cactus = cactus_img.get_rect(midbottom=(900, 400))
 64     active_cacti.append(new_cactus)
        

12. Let's have more fun!

Now that you understand the basic game, here are some ways to make it more exciting!

๐Ÿ’ก Start simple! Pick ONE idea and implement it. Test it. Then add another.

Visual Improvements

Audio

Gameplay Mechanics

13. Quick Reference Cheatsheet

Common Pygame Commands

WhatCode
Import Pygameimport pygame
Initializepygame.init()
Create windowscreen = pygame.display.set_mode((800, 400))
Load imageimg = pygame.image.load('file.png').convert_alpha()
Draw imagescreen.blit(img, rect)
Get rectanglerect = img.get_rect(center=(x,y))
Check key pressedpygame.key.get_pressed()
Rectangle collisionrect1.colliderect(rect2)
Draw rectanglepygame.draw.rect(screen, (R,G,B), rect)
Fill screenscreen.fill((R,G,B))
Update displaypygame.display.flip()
Control FPSclock.tick(60)
Quit properlypygame.quit(); exit()

14. Common Errors & Solutions

15. Check Your Understanding

  1. What does pygame.Rect(80, 300, 40, 40) mean?
  2. What does gravity += 1 do?
  3. Why do we need clock.tick(60)?
  4. What happens if we remove screen.fill((255,255,255))?
  5. How does collision detection work?
  6. What is the difference between pygame.image.load() and .convert_alpha()?
  7. Why do we use a list (active_cacti = []) for multiple cacti?
๐Ÿ“– Click for Answers
  1. Creates a rectangle at x=80, y=300, width=40, height=40
  2. Increases gravity by 1 each frame (makes player fall faster)
  3. Limits the game to 60 frames per second (controls game speed)
  4. Previous frames leave "trails" on screen instead of clearing
  5. colliderect() checks if two rectangles overlap
  6. .convert_alpha() preserves transparency for smoother rendering
  7. Lists can hold multiple objects; we can loop through them to update/draw each cactus

16. Your Progress Tracker

Check off each item as you complete it:
I installed Python and Thonny
I ran game-simple.py (Code-1)
I understand Code-1 line by line
I completed Challenge (modified values)
I downloaded player.png and cactus.png
I ran Code-2 with images
I understand how images are loaded and drawn
I ran Code-3 with multiple cacti
I understand how lists store multiple cacti
I tried at least ONE "More Fun" idea
I shared my game with a classmate