# Pseudo-code for a game loop (conceptual)
game_running = True
clock_speed = 60 # Frames Per Second (FPS)
# while game_running:
# # 1. Handle Events
# for event in get_events():
# if event.type == QUIT:
# game_running = False
# if event.type == KEYPRESS:
# handle_keypress(event.key)
#
# # 2. Update Game State
# player.move()
# check_collisions()
# update_score()
#
# # 3. Draw Everything
# screen.fill(BLACK)
# draw_player()
# draw_enemies()
# draw_score()
#
# # 4. Control Frame Rate
# wait(1/clock_speed)
print("๐ฎ This is the pattern for EVERY game!")Chapter 2.1: Welcome to Pygame - Make Games with Python!
Turn Your Code into Interactive Adventures
๐ฎ PROJECT 2.1 | Difficulty: Intermediate | Time: 10 minutes
๐ Complexity Level: Intermediate โญโญ
Learn game development with Pygame! Perfect for students who know Python basics and want to build something interactive and fun.
๐ป Interactive Options:
- ๐ Open in JupyterLite - Full Jupyter environment in your browser
- ๐ฅ Download Notebook (Challenge) - For use in local Jupyter or Google Colab
- ๐ก Note: Pygame runs best locally with a display, but weโll show examples!
๐ Introduction: Why Game Development?
Ever wanted to create your own games? With Pygame, you can build:
- ๐ Classic games like Snake and Pong
- ๐พ Space shooters like Space Invaders
- ๐ฎ Platformers, puzzle games, and more!
Game development teaches:
- Event handling - responding to keyboard and mouse input
- Graphics - drawing shapes and managing sprites
- Game loops - the heartbeat of every game
- Collision detection - making objects interact
- Math & physics - movement, gravity, and trajectories
๐ฏ Real-World Connection:
Game development isnโt just for fun! The skills you learn apply to:
- Mobile apps (touch events, animations)
- Data visualizations (interactive graphics)
- Simulations (physics, modeling)
- UI/UX design (user interaction patterns)
Companies like NASA use game engines for training simulators, and medical professionals use them for surgical practice!
๐ฎ What is Pygame?
Pygame is a Python library that makes game development accessible. It provides:
| Feature | What It Does |
|---|---|
| Display | Create windows and draw graphics |
| Events | Handle keyboard, mouse, and controller input |
| Sprites | Manage game objects (characters, enemies, items) |
| Collision | Detect when objects touch or overlap |
| Sound | Play music and sound effects |
| Time | Control game speed and frame rate |
Why Pygame?
- โ Python-based: Use a language you already know!
- โ Simple: Create a game in under 100 lines of code
- โ Powerful: Build professional-looking games
- โ Cross-platform: Works on Windows, Mac, and Linux
- โ Active community: Tons of tutorials and examples
๐ฏ Game Development Fundamentals
The Game Loop
Every game has a โgame loopโ that runs continuously:
while game_running:
1. Handle events (keyboard, mouse, etc.)
2. Update game state (move objects, check collisions)
3. Draw everything to the screen
4. Control frame rate (60 FPS)
This loop runs 60 times per second in most games!
๐น๏ธ FPS (Frames Per Second)
- 30 FPS: Minimum for smooth motion
- 60 FPS: Standard for most games
- 120+ FPS: High-performance gaming
A higher FPS means smoother gameplay!
Coordinates and Movement
In Pygame, the screen is a grid of pixels:
(0,0) โโโโโโโโโโโโโโโโโโโโ> X
โ
โ Your Screen
โ (800 x 600 pixels)
โ
โ (800, 600)
โ
Y
Important: Y increases downward (opposite of math class!)
# Example: Moving objects
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
def move_right(self, speed):
self.x += speed # Move right = increase X
def move_left(self, speed):
self.x -= speed # Move left = decrease X
def move_down(self, speed):
self.y += speed # Move down = increase Y
def move_up(self, speed):
self.y -= speed # Move up = decrease Y
def __str__(self):
return f"Position({self.x}, {self.y})"
# Example usage
player = Position(100, 100)
print(f"Start: {player}")
player.move_right(50)
print(f"After moving right: {player}")
player.move_down(30)
print(f"After moving down: {player}")Colors in Pygame
Colors are defined using RGB (Red, Green, Blue) values:
# Colors are tuples of (Red, Green, Blue)
# Each value ranges from 0-255
# Common colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
# Custom colors
DARK_GRAY = (64, 64, 64)
LIGHT_BLUE = (135, 206, 250)
ORANGE = (255, 165, 0)
# Color mixing example
def mix_colors(color1, color2):
"""Mix two colors by averaging their RGB values"""
r = (color1[0] + color2[0]) // 2
g = (color1[1] + color2[1]) // 2
b = (color1[2] + color2[2]) // 2
return (r, g, b)
purple = mix_colors(RED, BLUE)
print(f"Red + Blue = {purple}")
olive = mix_colors(YELLOW, GREEN)
print(f"Yellow + Green = {olive}")๐ฎ Types of Games Youโll Build
Snake Game ๐
Classic arcade game where you:
- Control a snake that grows when eating food
- Avoid hitting walls or yourself
- Score points for each food item
Key Concepts: Grid-based movement, collision detection, dynamic lists
Space Invaders ๐พ
Retro shooter where you:
- Control a spaceship
- Shoot enemies moving across the screen
- Dodge enemy bullets
- Clear waves of invaders
Key Concepts: Sprite management, projectile physics, game states
What Makes Games Fun?
# Game design principles
game_design = {
"Clear Goal": "Score 100 points, defeat all enemies",
"Challenge": "Difficulty increases over time",
"Feedback": "Visual/audio responses to actions",
"Reward": "Points, levels, achievements",
"Control": "Responsive, intuitive controls",
}
print("๐ฏ Elements of Fun Games:")
for element, description in game_design.items():
print(f" โข {element}: {description}")๐ What Youโll Learn
By the end of this chapter, youโll be able to:
โ
Set up Pygame and create game windows
โ
Handle keyboard and mouse events
โ
Draw shapes and images on screen
โ
Implement game physics and collision detection
โ
Build a complete Snake game
โ
Create a Space Invaders clone
โ
Understand sprite management with matrices
๐จ Game Development Workflow
# Typical game development workflow
workflow_steps = [
"1. ๐ก Design: Plan your game mechanics",
"2. ๐จ Assets: Create or find graphics/sounds",
"3. โ๏ธ Core Loop: Build the game loop",
"4. ๐ฎ Controls: Implement input handling",
"5. ๐ฏ Gameplay: Add game mechanics",
"6. ๐ Debug: Test and fix issues",
"7. โจ Polish: Add visual effects and audio",
"8. ๐ Release: Share with the world!"
]
print("๐ฎ Game Development Workflow:")
for step in workflow_steps:
print(f" {step}")๐ Fun Fact: The first video game, โPong,โ was created in 1972 and consisted of just two rectangles and a circle. Today, games are multi-billion dollar industries, but the core concepts remain the same!
Many successful indie games were created by solo developers or small teams using tools like Pygame!
๐ค Game Design Challenge
Before we code, letโs think like game designers:
๐ญ Design Exercise:
Think about your favorite game. Consider:
- Whatโs the main goal? (e.g., reach the exit, defeat enemies, solve puzzles)
- What makes it challenging? (time limits, obstacles, complex puzzles)
- What keeps you playing? (progression, unlockables, story)
- How do you control it? (keyboard, mouse, touch)
Now, sketch out a simple game idea: - Whatโs the playerโs goal? - What obstacles will they face? - How will the difficulty increase?
Write down your ideaโwe might build it later!
๐ Pygame vs Other Game Engines
How does Pygame compare to other tools?
| Tool | Best For | Difficulty | Type |
|---|---|---|---|
| Pygame | Learning, 2D games | Easy | Python library |
| Unity | Professional 3D/2D | Medium | Full engine |
| Godot | Indie 2D/3D games | Medium | Full engine |
| Scratch | Absolute beginners | Very Easy | Visual programming |
| PyGame Zero | Young programmers | Very Easy | Python library |
Pygame is perfect for learning because:
- You write actual Python code (not visual blocks)
- Itโs not overwhelming like full engines
- You understand whatโs happening โunder the hoodโ
- Skills transfer to other frameworks
๐ Whatโs Next?
In the next slide, weโll:
- Install Pygame using UV
- Create your first game window
- Draw basic shapes
- Handle keyboard input
- Make things move!
Get ready to see your code come to life! ๐ฎ