for HCC classes taught by Rylan Schubkegel
Due 12/11 at 11:59
Flappy Bird is a classic game where the player controls a bird and tries to avoid obstacles. In this assignment, you will create a simple version of Flappy Bird using Python and Pygame.
The project will be split into three weeks but you must submit the program each week. This week, you will get familiar with drawing graphics using basic shapes.
Pygame is a Python library for creating games. It provides a set of functions and classes that make it easy to create graphics and sound for your game.
Open project.toml
and update the [tool.poetry.dependencies]
section to match the following:
[tool.poetry.dependencies]
python = ">=3.10.0,<3.12"
pygame = "^2.1.2"
dearpygui = "^1.6.2"
Next time you run a project, Replit will automatically install the required packages.
Open Command Prompt (View > Terminal or Ctrl + `) and run the following:
pip install pygame
Now you can import the pygame
module in your Python code!
This project will be larger than previous projects which makes it important to organize the code. Here is the basic structure your program should follow:
Here is a basic template for you to start with:
# Import modules
import pygame
# Define constants (using CAPS_CASE convention)
WINDOW_WIDTH = 320
WINDOW_HEIGHT = 180
# Initialize Pygame
pygame.init()
game_window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Flappy Bird")
# TODO: Define functions here
# Game loop
running = True
while running:
# Handle events (allows the user to quit the game)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Draw background color
game_window.fill(WHITE)
# TODO: Call functions here
# Render graphics to window
pygame.display.flip()
Pygame provides a set of functions that you can use to draw shapes on the screen. Here are some examples:
# Draw a rectangle
color = (255, 0, 0)
x = 100
y = 100
width = 50
height = 50
pygame.draw.rect(game_window, color, (x, y, width, height))
# Draw a circle
color = (0, 255, 0)
x = 100
y = 100
radius = 50
pygame.draw.circle(game_window, color, (x, y), radius)
# Draw a line
color = (0, 0, 255)
start_x = 0
start_y = 0
end_x = 100
end_y = 100
pygame.draw.line(game_window, color, (start_x, start_y), (end_x, end_y))
Use these functions to draw a bird your new function!
NOTE: Feel free to customize the bird to your liking, it doesn’t have to look like mine!
In Pygame, colors are represented by tuples of three integers (RGB) where each integer ranges from 0 to 255. For example, (0, 0, 0)
is black, (255, 255, 255)
is white, and (255, 0, 0)
is red.
To define a color that you like, you can use a color picker (this one from W3Schools is good) and copy the RGB values.
When your program meets the requirements, submit your .py
file here: