IP2 – Introduction to Programming 2 (Python)
Astana IT University
School of Software Engineering
Introduction to Programming 2 (Python) – IP2
Assignment 1: Introduction to PyGame
Game Development with Python
1st Attestation – 25 Points
Course IP2 – Introduction to Programming 2 (Python)
Assignment Assignment 1 (Individual)
Topic Introduction to PyGame – Building a Simple Game
Weight 25 points (1st Attestation)
Submission Upload to LMS ([Link]) + Oral Defense
Deadline Refer to LMS for exact due date
1. Assignment Overview
In this assignment, you will use the PyGame library to create a simple interactive game
application. This assignment integrates the core Python concepts you have learned in
Weeks 1–3 (variables, data types, control flow, loops, and collections) with an introduction to
game development using PyGame.
You will build a “Catch the Falling Objects” game where a player controls a basket (or a
character) at the bottom of the screen to catch objects falling from the top. The game keeps
track of the score, lives, and difficulty level.
2. Learning Objectives
By completing this assignment, you will:
• Install and configure a third-party Python library (PyGame).
• Apply variables, data types, and arithmetic operators in a real-world context.
• Use conditional statements and loops to control game logic.
• Work with Python lists and dictionaries to manage game objects and state.
• Understand the basics of an event-driven game loop.
• Handle user input (keyboard events) in a graphical application.
3. Prerequisites
Before starting, make sure you have:
• Python 3.10+ installed on your computer.
• A code editor (VS Code or PyCharm recommended).
• PyGame installed. Run the following command in your terminal:
pip install pygame
Astana IT University – Academic Year 2025–2026 | Page 1
IP2 – Introduction to Programming 2 (Python)
4. Game Description: “Catch the Falling Objects”
4.1 Game Concept
The player controls a basket (a rectangle) at the bottom of the screen using the left and right
arrow keys. Objects (colored circles or squares) fall from the top of the screen at random
positions. The player must catch the objects to earn points. If an object reaches the bottom
without being caught, the player loses a life. The game ends when all lives are lost.
4.2 Game Features
Feature Description
Player basket A rectangle at the bottom of the screen, moved with arrow keys
(LEFT/RIGHT).
Falling objects Colored shapes that spawn at random x-positions at the top and fall
downward.
Score system Each caught object adds points to the score. Display the current score
on screen.
Lives The player starts with 3 lives. Missing an object costs 1 life.
Difficulty The falling speed increases every 5 caught objects (stored in a
list/dict).
Game Over When lives reach 0, display “Game Over” with the final score.
The player can press R to restart or Q to quit.
5. Technical Requirements
Your code must demonstrate the following Python concepts from Weeks 1–3:
5.1 Variables and Data Types (Week 1)
1. Use meaningful variable names (e.g., player_x, score, lives, fall_speed).
2. Use integers, floats, strings, and booleans appropriately.
3. Use arithmetic operators for score calculation, position updates, and collision
detection.
5.2 Control Flow and Loops (Week 2)
4. Use if/elif/else statements for collision detection, game state checks, and input
handling.
5. Use a while loop for the main game loop.
6. Use a for loop to iterate through the list of falling objects.
5.3 Collections (Week 3)
7. Use a list to store falling objects (each object can be a dictionary with keys like "x",
"y", "color", "speed").
8. Use a dictionary to store the game state (score, lives, level, game_over status).
9. Use a tuple to define constant values such as colors, e.g., RED = (255, 0, 0).
Astana IT University – Academic Year 2025–2026 | Page 2
IP2 – Introduction to Programming 2 (Python)
6. Starter Code Template
Use the following template as your starting point. You must complete the sections marked
with TODO comments.
import pygame
import random
# Initialize PyGame
[Link]()
# --- Constants (use tuples for colors) ---
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (220, 50, 50)
GREEN = (50, 180, 50)
BLUE = (50, 100, 220)
YELLOW = (240, 200, 50)
COLORS = [RED, GREEN, BLUE, YELLOW]
# --- Game State (use a dictionary) ---
game_state = {
"score": 0,
"lives": 3,
"level": 1,
"game_over": False,
"fall_speed": 3
}
# --- Player (basket) settings ---
basket_width = 120
basket_height = 20
basket_x = SCREEN_WIDTH // 2 - basket_width // 2
basket_y = SCREEN_HEIGHT - 50
basket_speed = 8
# --- Falling objects (use a list of dictionaries) ---
falling_objects = []
# --- Screen setup ---
screen = [Link].set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
[Link].set_caption("Catch the Falling Objects")
clock = [Link]()
font = [Link]("Arial", 28)
# TODO 1: Write a function to create a new falling object
# It should return a dictionary with keys: "x", "y",
# "color", "size"
# x = random position, y = 0 (top), color = random from COLORS
Astana IT University – Academic Year 2025–2026 | Page 3
IP2 – Introduction to Programming 2 (Python)
# TODO 2: Write the main game loop (while not game_over)
# Inside the loop:
# a) Handle events (QUIT, key presses for LEFT/RIGHT)
# b) Spawn new objects (e.g., every 30 frames)
# c) Move each object down using a for loop
# d) Check collision with basket (if/elif/else)
# e) Remove caught or missed objects from the list
# f) Update game_state dictionary (score, lives, level)
# g) Increase difficulty every 5 points
# h) Draw everything on the screen
# i) Display score and lives
# TODO 3: Write the Game Over screen
# Display "Game Over" and final score
# Allow R to restart, Q to quit
[Link]()
7. Task Breakdown and Grading Rubric
The assignment is graded out of 25 points as follows:
# Task Points Criteria
1 PyGame setup and window creation 3 Window opens, title set, proper
screen dimensions, game loop runs
smoothly.
2 Player basket movement (keyboard 3 Basket moves left/right with arrow
input) keys, stays within screen bounds.
3 Falling objects: spawning and 4 Objects spawn at random x-positions,
movement fall smoothly. Uses list of dictionaries
correctly.
4 Collision detection 3 Correct detection of catch (basket)
and miss (bottom of screen). Uses
if/elif/else.
5 Score and lives system 3 Score increments on catch, lives
decrement on miss. Values stored in
game_state dictionary.
6 Difficulty progression 2 Fall speed increases every 5 points.
Level tracked in game_state.
7 Game Over screen and restart 2 Game Over message with final score
displayed. R to restart, Q to quit.
8 Code quality and comments 3 Meaningful variable names, clear
comments, proper code structure, no
unused code.
Astana IT University – Academic Year 2025–2026 | Page 4
IP2 – Introduction to Programming 2 (Python)
9 Oral defense 2 Student explains their code clearly
and can answer questions about their
implementation.
Total 25
8. Submission Requirements
1. Submit a single Python file named: YourName_Assignment1.py
2. Upload the file to LMS ([Link]) before the deadline.
3. Be prepared to defend your assignment during the oral defense session.
4. Your code must run without errors when the instructor executes it.
5. Do NOT submit any AI-generated code unless explicitly permitted by your instructor.
9. Oral Defense: Sample Questions
During the defense, the instructor may ask questions such as:
• Explain how the game loop works in your code.
• Why did you use a dictionary for game_state instead of separate variables?
• How does your collision detection work? Walk me through the logic.
• How do you add a new falling object to the list? What data does each object contain?
• What happens if you modify the list while iterating over it? How did you handle this?
• Change the number of starting lives to 5. Now make the basket wider. (Live coding
test.)
10. Hints and Tips
Collision Detection Logic
An object is “caught” if all of the following are true:
• The object’s y-coordinate is at or below the basket’s y-coordinate.
• The object’s x-coordinate is between basket_x and basket_x + basket_width.
Modifying a List While Iterating
Do not remove items from a list while looping through it with a for loop. Instead, create a new
list of objects to keep:
# Safe way to filter a list:
falling_objects = [obj for obj in falling_objects
if not is_caught(obj) and not is_missed(obj)]
Spawning Objects Periodically
Use a frame counter to spawn new objects at regular intervals:
frame_count = 0
Astana IT University – Academic Year 2025–2026 | Page 5
IP2 – Introduction to Programming 2 (Python)
spawn_rate = 30 # Every 30 frames
# Inside your game loop:
frame_count += 1
if frame_count % spawn_rate == 0:
falling_objects.append(create_object())
11. Bonus Challenges (Optional, up to +3 extra points)
For students who finish early and want to push further:
• +1 point: Add different object types (some worth more points, some “bad” objects that
cost a life if caught).
• +1 point: Add sound effects using [Link] (catch sound, miss sound, game
over sound).
• +1 point: Add a high-score tracking system that saves/loads from a text file.
12. Academic Integrity Reminder
This is an individual assignment. You must write your own code.
Copying code from classmates, the internet, or AI tools without explicit instructor
permission is a violation of the academic integrity policy and will result in a grade of
zero.
You are encouraged to discuss concepts with peers, but the code you submit must be
entirely your own work. During the oral defense, you must be able to explain every line
of your code.
13. Useful Resources
• PyGame Official Documentation: [Link]
• PyGame Getting Started Tutorial: [Link]
• Course Lecture Notes: [Link] (Weeks 1–3)
• Eric Matthes, Python Crash Course (3rd Edition) – Chapter on PyGame projects.
Good luck! Have fun building your first game!
Astana IT University – Academic Year 2025–2026 | Page 6