Mini Project
Mini Project
Submitted To
Faculty Name Dr. Rashmi Agrwal
Teacher’s sign
Topic
GAME
Flappy Bird
Flappy Bird Game using PyGame in
Python
Project Overview
Flappy Bird is a simple yet challenging game where the
player controls a bird that must navigate through a
series of pipes by tapping the screen or pressing a key to
make the bird flap its wings. The objective is to pass
through as many pipes as possible without colliding with
them. Each successful pass earns the player a point, and
the game becomes increasingly difficult as more pipes
appear.
Key Features
Simple Controls:
Control the bird with a single tap or key press to make it
flap and ascend.
Scoring System:
Earn points for successfully navigating through pipes
and challenge yourself to achieve a high score.
Collision Detection:
Experience the tension as the game detects collisions,
ending the game if the bird hits a pipe.
Dynamic Difficulty:
The game progressively becomes more challenging as you
accumulate points, keeping you on the edge of your seat.
Technologies Used:
This Flappy Bird game is built using the Python
programming language and leverages popular libraries
such as Pygame for graphics and input handling. Pygame
provides a platform for creating 2D games and
multimedia applications, making it an ideal choice for
this project.
Acknowledgement
I would like to express my sincere gratitude to the following
individuals and entities who have played a pivotal role in the
successful completion of this Python project, as part of my
Master of Computer Applications (MCA) program:
This project would not have been possible without the collective
efforts and support of these individuals and communities. Thank
you for being an integral part of this journey.
Ranjeet
MCA Candidate
School of Computer Applications
Manav Rachna International Institute of Research and Studies
Certificate
This is to certify that the project report entitled “FLAPPY
BIRD” submitted in partial fulfillment of the degree of
MASTER OF COMPUTER APPLICATIONS (MCA) to Manav
Rachna International Institute of Research & Studies,
Faridabad, Haryana is carried out under my guidance.
By Mr. Ranjeet
Roll No. 23/SCA/MCA/098
1. Game Loop:
A game loop is crucial for continuously updating the
game state and rendering frames. In Python, this
often involves the use of a loop that repeatedly
updates the game logic, handles user input, and
refreshes the display.
2. Pygame Library:
Pygame is a set of Python modules designed for
writing video games. It provides functions for
handling graphics, user input, and other gaming-
related tasks. Pygame simplifies the process of
building a game in Python.
4. Collision Detection:
Detecting collisions between the bird and pipes is a
critical aspect of the game. This involves comparing
the positions and dimensions of game elements and
responding accordingly when a collision is detected.
5. Physics Simulation:
The game may incorporate a basic physics
simulation for the bird's movement, including
gravity and velocity. This simulation is updated in
the game loop to create realistic motion.
6. Score Tracking:
The game keeps track of the player's score, which
increments each time the bird successfully passes
through a set of pipes. This involves counting and
displaying the score on the screen.
7. Sound Effects:
Python's ability to handle sound can be utilized to
add audio effects to the game. For instance, playing
a sound when the bird flaps its wings or when a
collision occurs.
8. Randomization:
Randomization is used to determine the position and
gap size of the pipes, creating variability in each
playthrough. Python's random module is often
employed for this purpose.
9. Physics Simulation:
The game may incorporate a basic physics
simulation for the bird's movement, including
gravity and velocity. This simulation is updated in
the game loop to create realistic motion.
Source code:
import pygame, sys, random
def draw_floor():
screen.blit(floor_surface,(floor_x_pos,900))
screen.blit(floor_surface,(floor_x_pos + 576,900))
def create_pipe():
random_pipe_pos = random.choice(pipe_height)
bottom_pipe = pipe_surface.get_rect(midtop =
(700,random_pipe_pos))
top_pipe = pipe_surface.get_rect(midbottom =
(700,random_pipe_pos - 300))
return bottom_pipe,top_pipe
def move_pipes(pipes):
for pipe in pipes:
pipe.centerx -= 5
visible_pipes = [pipe for pipe in pipes if pipe.right > -50]
return visible_pipes
def draw_pipes(pipes):
for pipe in pipes:
if pipe.bottom >= 1024:
screen.blit(pipe_surface,pipe)
else:
flip_pipe = pygame.transform.flip(pipe_surface,False,True)
screen.blit(flip_pipe,pipe)
def check_collision(pipes):
global can_score
for pipe in pipes:
if bird_rect.colliderect(pipe):
death_sound.play()
can_score = True
return False
def rotate_bird(bird):
new_bird = pygame.transform.rotozoom(bird,-bird_movement *
3,1)
return new_bird
def bird_animation():
new_bird = bird_frames[bird_index]
new_bird_rect = new_bird.get_rect(center =
(100,bird_rect.centery))
return new_bird,new_bird_rect
def score_display(game_state):
if game_state == 'main_game':
score_surface =
game_font.render(str(int(score)),True,(255,255,255))
score_rect = score_surface.get_rect(center = (288,100))
screen.blit(score_surface,score_rect)
if game_state == 'game_over':
score_surface = game_font.render(f'Score: {int(score)}'
,True,(255,255,255))
score_rect = score_surface.get_rect(center = (288,100))
screen.blit(score_surface,score_rect)
def pipe_score_check():
global score, can_score
if pipe_list:
for pipe in pipe_list:
if 95 < pipe.centerx < 105 and can_score:
score += 1
score_sound.play()
can_score = False
if pipe.centerx < 0:
can_score = True
# Game Variables
gravity = 0.32
bird_movement = 0
game_active = True
score = 0
high_score = 0
can_score = True
bg_surface = pygame.image.load('assets/background-
day.png').convert()
bg_surface = pygame.transform.scale2x(bg_surface)
floor_surface = pygame.image.load('assets/base.png').convert()
floor_surface = pygame.transform.scale2x(floor_surface)
floor_x_pos = 0
bird_downflap =
pygame.transform.scale2x(pygame.image.load('assets/bluebird-
downflap.png').convert_alpha())
bird_midflap =
pygame.transform.scale2x(pygame.image.load('assets/bluebird-
midflap.png').convert_alpha())
bird_upflap =
pygame.transform.scale2x(pygame.image.load('assets/bluebird-
upflap.png').convert_alpha())
bird_frames = [bird_downflap,bird_midflap,bird_upflap]
bird_index = 0
bird_surface = bird_frames[bird_index]
bird_rect = bird_surface.get_rect(center = (100,512))
BIRDFLAP = pygame.USEREVENT + 1
pygame.time.set_timer(BIRDFLAP,200)
# bird_surface = pygame.image.load('assets/bluebird-
midflap.png').convert_alpha()
# bird_surface = pygame.transform.scale2x(bird_surface)
# bird_rect = bird_surface.get_rect(center = (100,512))
pipe_surface = pygame.image.load('assets/pipe-green.png')
pipe_surface = pygame.transform.scale2x(pipe_surface)
pipe_list = []
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE,1200)
pipe_height = [400,600,800]
game_over_surface =
pygame.transform.scale2x(pygame.image.load('assets/message.p
ng').convert_alpha())
game_over_rect = game_over_surface.get_rect(center = (288,512))
flap_sound = pygame.mixer.Sound('sound/sfx_wing.wav')
death_sound = pygame.mixer.Sound('sound/sfx_hit.wav')
score_sound = pygame.mixer.Sound('sound/sfx_point.wav')
score_sound_countdown = 100
SCOREEVENT = pygame.USEREVENT + 2
pygame.time.set_timer(SCOREEVENT,100)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
bird_movement = 0
bird_movement -= 12
flap_sound.play()
if event.key == pygame.K_SPACE and game_active ==
False:
game_active = True
pipe_list.clear()
bird_rect.center = (100,512)
bird_movement = 0
score = 0
if event.type == SPAWNPIPE:
pipe_list.extend(create_pipe())
if event.type == BIRDFLAP:
if bird_index < 2:
bird_index += 1
else:
bird_index = 0
bird_surface,bird_rect = bird_animation()
screen.blit(bg_surface,(0,0))
if game_active:
# Bird
bird_movement += gravity
rotated_bird = rotate_bird(bird_surface)
bird_rect.centery += bird_movement
screen.blit(rotated_bird,bird_rect)
game_active = check_collision(pipe_list)
# Pipes
pipe_list = move_pipes(pipe_list)
draw_pipes(pipe_list)
# Score
pipe_score_check()
score_display('main_game')
else:
screen.blit(game_over_surface,game_over_rect)
high_score = update_score(score,high_score)
score_display('game_over')
# Floor
floor_x_pos -= 1
draw_floor()
if floor_x_pos <= -576:
floor_x_pos = 0
pygame.display.update()
clock.tick(120)
OUTPUT: