Version 1

In this first version you have to get the object.
Catch the Falling Objects Game
Overview
In this game, the player controls a basket that can move left and right at the bottom of the screen. Objects fall from the top of the screen, and the player’s goal is to catch as many of these objects as possible using the basket. Each object caught increases the player’s score. If an object hits the ground, the player loses a life. The game continues until the player runs out of lives.
Key Features
- Player Control: The player moves the basket left and right using the arrow keys.
- Falling Objects: Multiple objects fall from the top of the screen at a constant speed.
- Collision Detection: The game detects when the basket catches an object.
- Score Tracking: The score increases each time the basket catches an object.
- Life System: The player starts with a set number of lives, and each object that hits the ground reduces the player’s lives.
- Game Over Condition: The game ends when the player loses all their lives.
Code Description
Importing Libraries and Initialization
The game starts by importing the necessary libraries and initializing Pygame.
import pygame import random # Initialize Pygame pygame.init()
Constants and Colors
We define the screen dimensions, frame rate, and some colors for drawing objects.
# Constants SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 FPS = 60 # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0)
Setting Up the Display
We set up the game window and the clock to control the frame rate.
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Catch the Falling Objects")
# Clock for controlling the frame rate
clock = pygame.time.Clock()
Basket Properties
We define the properties of the basket, including its size, position, and speed.
# Basket properties basket_width = 100 basket_height = 20 basket_x = (SCREEN_WIDTH - basket_width) // 2 basket_y = SCREEN_HEIGHT - basket_height - 10 basket_speed = 10
Object Properties
We define the properties of the falling objects and create a list to hold multiple objects.
# Object properties
object_width = 30
object_height = 30
object_speed = 5
# Create multiple objects
num_objects = 5
objects = [{'x': random.randint(0, SCREEN_WIDTH - object_width), 'y': -object_height} for _ in range(num_objects)]
Basket Movement Function
A function to handle the basket’s movement based on keyboard input.
# Basket movement
def move_basket(keys, basket_x):
if keys[pygame.K_LEFT] and basket_x > 0:
basket_x -= basket_speed
if keys[pygame.K_RIGHT] and basket_x < SCREEN_WIDTH - basket_width:
basket_x += basket_speed
return basket_x
Collision Detection Function
A function to check if the basket catches an object.
# Check for collision
def check_collision(basket_x, basket_y, object_x, object_y):
if (object_x < basket_x + basket_width and
object_x + object_width > basket_x and
object_y < basket_y + basket_height and
object_y + object_height > basket_y):
return True
return False
Main Game Loop
The main loop of the game handles the movement of the basket and objects, checks for collisions, updates the score and lives, and renders the game elements.
# Main game loop
running = True
score = 0
lives = 3
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
basket_x = move_basket(keys, basket_x)
# Move the objects
for obj in objects:
obj['y'] += object_speed
if obj['y'] > SCREEN_HEIGHT:
obj['y'] = -object_height
obj['x'] = random.randint(0, SCREEN_WIDTH - object_width)
lives -= 1
# Check for collision
if check_collision(basket_x, basket_y, obj['x'], obj['y']):
score += 1
obj['y'] = -object_height
obj['x'] = random.randint(0, SCREEN_WIDTH - object_width)
if lives <= 0:
running = False
# Fill the screen with a color
screen.fill(WHITE)
# Draw the basket
pygame.draw.rect(screen, BLACK, (basket_x, basket_y, basket_width, basket_height))
# Draw the objects
for obj in objects:
pygame.draw.rect(screen, RED, (obj['x'], obj['y'], object_width, object_height))
# Display the score and lives
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, BLACK)
lives_text = font.render(f"Lives: {lives}", True, BLACK)
screen.blit(score_text, (10, 10))
screen.blit(lives_text, (10, 50))
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(FPS)
pygame.quit()
Summary
This version of the game challenges the player to catch falling objects with a basket. It includes:
- A movable basket controlled by the arrow keys.
- Multiple falling objects.
- Collision detection to check if the basket catches an object.
- Score tracking for each object caught.
- A life system that decreases when an object reaches the bottom of the screen.
- The game ends when the player runs out of lives.
import pygame
import random
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Catch the Falling Objects")
# Clock for controlling the frame rate
clock = pygame.time.Clock()
# Basket properties
basket_width = 100
basket_height = 20
basket_x = (SCREEN_WIDTH - basket_width) // 2
basket_y = SCREEN_HEIGHT - basket_height - 10
basket_speed = 20
# Object properties
object_width = 30
object_height = 30
object_speed = 2
# Create multiple objects
num_objects = 3
objects = [{'x': random.randint(0, SCREEN_WIDTH - object_width), 'y': -object_height} for _ in range(num_objects)]
# Basket movement
def move_basket(keys, basket_x):
if keys[pygame.K_LEFT] and basket_x > 0:
basket_x -= basket_speed
if keys[pygame.K_RIGHT] and basket_x < SCREEN_WIDTH - basket_width:
basket_x += basket_speed
return basket_x
# Check for collision
def check_collision(basket_x, basket_y, object_x, object_y):
if (object_x < basket_x + basket_width and
object_x + object_width > basket_x and
object_y < basket_y + basket_height and
object_y + object_height > basket_y):
return True
return False
# Main game loop
running = True
score = 0
lives = 3
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
basket_x = move_basket(keys, basket_x)
# Move the objects
for obj in objects:
obj['y'] += object_speed
if obj['y'] > SCREEN_HEIGHT:
obj['y'] = -object_height
obj['x'] = random.randint(0, SCREEN_WIDTH - object_width)
lives -= 1
# Check for collision
if check_collision(basket_x, basket_y, obj['x'], obj['y']):
score += 1
obj['y'] = -object_height
obj['x'] = random.randint(0, SCREEN_WIDTH - object_width)
if lives <= 0:
running = False
# Fill the screen with a color
screen.fill(WHITE)
# Draw the basket
pygame.draw.rect(screen, BLACK, (basket_x, basket_y, basket_width, basket_height))
# Draw the objects
for obj in objects:
pygame.draw.rect(screen, RED, (obj['x'], obj['y'], object_width, object_height))
# Display the score and lives
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, BLACK)
lives_text = font.render(f"Lives: {lives}", True, BLACK)
screen.blit(score_text, (10, 10))
screen.blit(lives_text, (10, 50))
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(FPS)
pygame.quit()
Second version
This is the second version of the game where the player must avoid the falling objects instead of catching them.
Avoid the Falling Objects Game
Overview
In this game, the player controls a basket that can move left and right at the bottom of the screen. Objects fall from the top of the screen, and the player’s goal is to avoid these objects. If the basket collides with a falling object, the player loses a life. The game continues until the player runs out of lives. The objective is to survive as long as possible without colliding with any falling objects.
Key Features
- Player Control: The player moves the basket left and right using the arrow keys.
- Falling Objects: Multiple objects fall from the top of the screen at a constant speed.
- Collision Detection: The game detects when the basket collides with a falling object.
- Score Tracking: The score increases each time an object safely passes the bottom of the screen without a collision.
- Life System: The player starts with a set number of lives, and each collision with a falling object reduces the player’s lives.
- Game Over Condition: The game ends when the player loses all their lives.
Code Description
Importing Libraries and Initialization
The game starts by importing the necessary libraries and initializing Pygame.
import pygame import random # Initialize Pygame pygame.init()
Constants and Colors
We define the screen dimensions, frame rate, and some colors for drawing objects.
# Constants SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 FPS = 60 # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0)
Setting Up the Display
We set up the game window and the clock to control the frame rate.
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Avoid the Falling Objects")
# Clock for controlling the frame rate
clock = pygame.time.Clock()
Basket Properties
We define the properties of the basket, including its size, position, and speed.
# Basket properties basket_width = 100 basket_height = 20 basket_x = (SCREEN_WIDTH - basket_width) // 2 basket_y = SCREEN_HEIGHT - basket_height - 10 basket_speed = 10
Object Properties
We define the properties of the falling objects and create a list to hold multiple objects.
# Object properties
object_width = 30
object_height = 30
object_speed = 5
# Create multiple objects
num_objects = 5
objects = [{'x': random.randint(0, SCREEN_WIDTH - object_width), 'y': -object_height} for _ in range(num_objects)]
Basket Movement Function
A function to handle the basket’s movement based on keyboard input.
# Basket movement
def move_basket(keys, basket_x):
if keys[pygame.K_LEFT] and basket_x > 0:
basket_x -= basket_speed
if keys[pygame.K_RIGHT] and basket_x < SCREEN_WIDTH - basket_width:
basket_x += basket_speed
return basket_x
Collision Detection Function
A function to check if the basket collides with a falling object.
# Check for collision
def check_collision(basket_x, basket_y, object_x, object_y):
if (object_x < basket_x + basket_width and
object_x + object_width > basket_x and
object_y < basket_y + basket_height and
object_y + object_height > basket_y):
return True
return False
Main Game Loop
The main loop of the game handles the movement of the basket and objects, checks for collisions, updates the score and lives, and renders the game elements.
# Main game loop
running = True
score = 0
lives = 3
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
basket_x = move_basket(keys, basket_x)
# Move the objects
for obj in objects:
obj['y'] += object_speed
if obj['y'] > SCREEN_HEIGHT:
obj['y'] = -object_height
obj['x'] = random.randint(0, SCREEN_WIDTH - object_width)
score += 1 # Increase score when the object safely passes
# Check for collision
if check_collision(basket_x, basket_y, obj['x'], obj['y']):
lives -= 1
obj['y'] = -object_height
obj['x'] = random.randint(0, SCREEN_WIDTH - object_width)
if lives <= 0:
running = False
# Fill the screen with a color
screen.fill(WHITE)
# Draw the basket
pygame.draw.rect(screen, BLACK, (basket_x, basket_y, basket_width, basket_height))
# Draw the objects
for obj in objects:
pygame.draw.rect(screen, RED, (obj['x'], obj['y'], object_width, object_height))
# Display the score and lives
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, BLACK)
lives_text = font.render(f"Lives: {lives}", True, BLACK)
screen.blit(score_text, (10, 10))
screen.blit(lives_text, (10, 50))
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(FPS)
pygame.quit()
Summary
This version of the game challenges the player to avoid falling objects with a basket. It includes:
- A movable basket controlled by the arrow keys.
- Multiple falling objects that the player must avoid.
- Collision detection to check if the basket collides with an object.
- Score tracking that increases each time an object safely passes the bottom of the screen without collision.
- A life system that decreases when the basket collides with a falling object.
- The game ends when the player loses all their lives.
import pygame
import random
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Avoid the Falling Objects")
# Clock for controlling the frame rate
clock = pygame.time.Clock()
# Basket properties
basket_width = 100
basket_height = 20
basket_x = (SCREEN_WIDTH - basket_width) // 2
basket_y = SCREEN_HEIGHT - basket_height - 10
basket_speed = 10
# Object properties
object_width = 30
object_height = 30
object_speed = 5
# Create multiple objects
num_objects = 5
objects = [{'x': random.randint(0, SCREEN_WIDTH - object_width), 'y': -object_height} for _ in range(num_objects)]
# Basket movement
def move_basket(keys, basket_x):
if keys[pygame.K_LEFT] and basket_x > 0:
basket_x -= basket_speed
if keys[pygame.K_RIGHT] and basket_x < SCREEN_WIDTH - basket_width:
basket_x += basket_speed
return basket_x
# Check for collision
def check_collision(basket_x, basket_y, object_x, object_y):
if (object_x < basket_x + basket_width and
object_x + object_width > basket_x and
object_y < basket_y + basket_height and
object_y + object_height > basket_y):
return True
return False
# Main game loop
running = True
score = 0
lives = 3
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
basket_x = move_basket(keys, basket_x)
# Move the objects
for obj in objects:
obj['y'] += object_speed
if obj['y'] > SCREEN_HEIGHT:
obj['y'] = -object_height
obj['x'] = random.randint(0, SCREEN_WIDTH - object_width)
score += 1 # Increase score when the object safely passes
# Check for collision
if check_collision(basket_x, basket_y, obj['x'], obj['y']):
lives -= 1
obj['y'] = -object_height
obj['x'] = random.randint(0, SCREEN_WIDTH - object_width)
if lives <= 0:
running = False
# Fill the screen with a color
screen.fill(WHITE)
# Draw the basket
pygame.draw.rect(screen, BLACK, (basket_x, basket_y, basket_width, basket_height))
# Draw the objects
for obj in objects:
pygame.draw.rect(screen, RED, (obj['x'], obj['y'], object_width, object_height))
# Display the score and lives
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, BLACK)
lives_text = font.render(f"Lives: {lives}", True, BLACK)
screen.blit(score_text, (10, 10))
screen.blit(lives_text, (10, 50))
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(FPS)
pygame.quit()
Version 3 – With Aliens
Let’s add some graphic

The code
import pygame
import random
import time
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Avoid the Falling Aliens")
# Clock for controlling the frame rate
clock = pygame.time.Clock()
# Basket properties
basket_width = 100
basket_height = 20
basket_x = (SCREEN_WIDTH - basket_width) // 2
basket_y = SCREEN_HEIGHT - basket_height - 10
basket_speed = 10
# Load alien images
alien_images = [pygame.image.load(f'alien{i}.png') for i in range(1, 4)]
alien_width = alien_images[0].get_width()
alien_height = alien_images[0].get_height()
# Object properties
object_speed = 5
# Create multiple objects
num_objects = 5
objects = [{'x': random.randint(0, SCREEN_WIDTH - alien_width),
'y': -alien_height,
'image': random.choice(alien_images),
'speed': random.randint(3, 6)} for _ in range(num_objects)]
# Basket movement
def move_basket(keys, basket_x):
if keys[pygame.K_LEFT] and basket_x > 0:
basket_x -= basket_speed
if keys[pygame.K_RIGHT] and basket_x < SCREEN_WIDTH - basket_width:
basket_x += basket_speed
return basket_x
# Check for collision
def check_collision(basket_x, basket_y, object_x, object_y):
if (object_x < basket_x + basket_width and
object_x + alien_width > basket_x and
object_y < basket_y + basket_height and
object_y + alien_height > basket_y):
return True
return False
# Main game loop
running = True
score = 0
lives = 3
start_time = time.time()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
basket_x = move_basket(keys, basket_x)
# Move the objects
current_time = time.time()
for obj in objects:
obj['y'] += obj['speed']
if obj['y'] > SCREEN_HEIGHT:
obj['y'] = -alien_height
obj['x'] = random.randint(0, SCREEN_WIDTH - alien_width)
obj['image'] = random.choice(alien_images)
obj['speed'] = random.randint(3, 6) + int((current_time - start_time) / 10)
score += 1 # Increase score when the object safely passes
# Check for collision
if check_collision(basket_x, basket_y, obj['x'], obj['y']):
lives -= 1
obj['y'] = -alien_height
obj['x'] = random.randint(0, SCREEN_WIDTH - alien_width)
obj['image'] = random.choice(alien_images)
obj['speed'] = random.randint(3, 6) + int((current_time - start_time) / 10)
if lives <= 0:
running = False
# Fill the screen with a color
screen.fill(WHITE)
# Draw the basket
pygame.draw.rect(screen, BLACK, (basket_x, basket_y, basket_width, basket_height))
# Draw the objects
for obj in objects:
screen.blit(obj['image'], (obj['x'], obj['y']))
# Display the score and lives
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, BLACK)
lives_text = font.render(f"Lives: {lives}", True, BLACK)
screen.blit(score_text, (10, 10))
screen.blit(lives_text, (10, 50))
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(FPS)
pygame.quit()
Loading from a spreadsheet
import pygame
import random
import time
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Avoid the Falling Aliens")
# Clock for controlling the frame rate
clock = pygame.time.Clock()
# Basket properties
basket_width = 100
basket_height = 20
basket_x = (SCREEN_WIDTH - basket_width) // 2
basket_y = SCREEN_HEIGHT - basket_height - 10
basket_speed = 10
# Load sprite sheet and split into individual images
def load_spritesheet(filename, sprite_width, sprite_height, num_sprites):
sheet = pygame.image.load(filename).convert_alpha()
sprites = []
for i in range(num_sprites):
rect = pygame.Rect(i * sprite_width, 0, sprite_width, sprite_height)
image = sheet.subsurface(rect)
sprites.append(image)
return sprites
# Load alien images from sprite sheet
alien_images = load_spritesheet('aliens_spritesheet2.png', 30, 30, 3)
alien_width = alien_images[0].get_width()
alien_height = alien_images[0].get_height()
# Object properties
object_speed = 5
# Create multiple objects
num_objects = 5
objects = [{'x': random.randint(0, SCREEN_WIDTH - alien_width),
'y': -alien_height,
'image': random.choice(alien_images),
'speed': random.randint(3, 6)} for _ in range(num_objects)]
# Basket movement
def move_basket(keys, basket_x):
if keys[pygame.K_LEFT] and basket_x > 0:
basket_x -= basket_speed
if keys[pygame.K_RIGHT] and basket_x < SCREEN_WIDTH - basket_width:
basket_x += basket_speed
return basket_x
# Check for collision
def check_collision(basket_x, basket_y, object_x, object_y):
if (object_x < basket_x + basket_width and
object_x + alien_width > basket_x and
object_y < basket_y + basket_height and
object_y + alien_height > basket_y):
return True
return False
# Main game loop
running = True
score = 0
lives = 3
start_time = time.time()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
basket_x = move_basket(keys, basket_x)
# Move the objects
current_time = time.time()
for obj in objects:
obj['y'] += obj['speed']
if obj['y'] > SCREEN_HEIGHT:
obj['y'] = -alien_height
obj['x'] = random.randint(0, SCREEN_WIDTH - alien_width)
obj['image'] = random.choice(alien_images)
obj['speed'] = random.randint(3, 6) + int((current_time - start_time) / 10)
score += 1 # Increase score when the object safely passes
# Check for collision
if check_collision(basket_x, basket_y, obj['x'], obj['y']):
lives -= 1
obj['y'] = -alien_height
obj['x'] = random.randint(0, SCREEN_WIDTH - alien_width)
obj['image'] = random.choice(alien_images)
obj['speed'] = random.randint(3, 6) + int((current_time - start_time) / 10)
if lives <= 0:
running = False
# Fill the screen with a color
screen.fill(WHITE)
# Draw the basket
pygame.draw.rect(screen, BLACK, (basket_x, basket_y, basket_width, basket_height))
# Draw the objects
for obj in objects:
screen.blit(obj['image'], (obj['x'], obj['y']))
# Display the score and lives
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, BLACK)
lives_text = font.render(f"Lives: {lives}", True, BLACK)
screen.blit(score_text, (10, 10))
screen.blit(lives_text, (10, 50))
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(FPS)
pygame.quit()
Subscribe to the newsletter for updates
Tkinter templatesTwitter: @pythonprogrammi - python_pygame
Claude's Games
1. Memory gameVideos
Speech recognition gamePygame's Platform Game