Skip to content

Commit bd1616f

Browse files
authored
Flappy bird game clone (#208)
* Added flapy bird game * added flappy bird game * updated code according to PEP18 * updated code according to PEP8 * updated code according to PEP18 * updated code according to PEP8
1 parent d257ea5 commit bd1616f

35 files changed

+352
-0
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
339 Bytes
Loading
336 Bytes
Loading
345 Bytes
Loading
339 Bytes
Loading
346 Bytes
Loading
345 Bytes
Loading
344 Bytes
Loading
345 Bytes
Loading
338 Bytes
Loading
343 Bytes
Loading
Loading
Loading
470 Bytes
Loading
Loading
758 Bytes
Loading
Loading
Loading
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Flappy Bird
2+
3+
[![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com)
4+
[![forthebadge](https://forthebadge.com/images/badges/built-with-swag.svg)](https://forthebadge.com)
5+
[![forthebadge](https://forthebadge.com/images/badges/made-with-python.svg)](https://forthebadge.com)
6+
7+
Flappy Bird is an implementation of the famous flappy bird game in pygame. The game is specifically meant to run on pydroid3 on android, It may not work properly in desktop.
8+
9+
Install pydroid3 on Android from here : [pydroid3 playstore](https://play.google.com/store/apps/details?id=ru.iiec.pydroid3&hl=en_IN&gl=US)
10+
11+
![Alt text](app.webp?raw=true "Flappy Bird")
12+
13+
## How to Download
14+
15+
Download this project from here [Download Flappy Bird](https://downgit.github.io/#/home?url=https://github.com/pyGuru123/Python-Games/tree/master/Flappy%20Bird)
16+
17+
## Requirements
18+
19+
Use the package manager [pip](https://pip.pypa.io/en/stable/) to install following packages :-
20+
* Pygame
21+
22+
```bash
23+
pip install pygame
24+
```
25+
26+
pygame is already installed in pydroid3, no installation required.
27+
28+
## Usage
29+
30+
Navigate and click main.py to open the game in pydroid3, tap anywhere to start the game. The objective of the game is jump and pass through the poles without touching them.
31+
32+
Controls:
33+
* Tap on the screen to make the bird jump.
34+
35+
## Contributing
36+
37+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
38+
39+
Please make sure to update tests as appropriate.
190 KB
Binary file not shown.
94.3 KB
Binary file not shown.
173 KB
Binary file not shown.
Binary file not shown.
29.2 KB
Binary file not shown.
1.2 MB
Binary file not shown.

Code-Sleep-Python/Flappy Bird/main.py

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import pygame
2+
import random
3+
4+
from objects import Grumpy, Pipe, Base, Score
5+
6+
# Setup *******************************************
7+
8+
pygame.init()
9+
SCREEN = WIDTH, HEIGHT = 288, 512
10+
display_height = 0.80 * HEIGHT
11+
info = pygame.display.Info()
12+
width = info.current_w
13+
height = info.current_h
14+
15+
if width >= height:
16+
win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
17+
else:
18+
win = pygame.display.set_mode(
19+
SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)
20+
21+
clock = pygame.time.Clock()
22+
FPS = 60
23+
24+
# COLORS
25+
26+
RED = (255, 0, 0)
27+
WHITE = (255, 255, 255)
28+
BLACK = (0, 0, 0)
29+
30+
# Backgrounds
31+
32+
bg1 = pygame.image.load('Assets/background-day.png')
33+
bg2 = pygame.image.load('Assets/background-night.png')
34+
35+
bg = random.choice([bg1, bg2])
36+
im_list = [pygame.image.load('Assets/pipe-green.png'),
37+
pygame.image.load('Assets/pipe-red.png')]
38+
pipe_img = random.choice(im_list)
39+
40+
gameover_img = pygame.image.load('Assets/gameover.png')
41+
flappybird_img = pygame.image.load('Assets/flappybird.png')
42+
flappybird_img = pygame.transform.scale(flappybird_img, (200, 80))
43+
44+
# Sounds & fx
45+
46+
47+
die_fx = pygame.mixer.Sound('Sounds/die.wav')
48+
hit_fx = pygame.mixer.Sound('Sounds/hit.wav')
49+
point_fx = pygame.mixer.Sound('Sounds/point.wav')
50+
swoosh_fx = pygame.mixer.Sound('Sounds/swoosh.wav')
51+
wing_fx = pygame.mixer.Sound('Sounds/wing.wav')
52+
53+
# Objects
54+
55+
pipe_group = pygame.sprite.Group()
56+
base = Base(win)
57+
score_img = Score(WIDTH // 2, 50, win)
58+
grumpy = Grumpy(win)
59+
60+
# Variables
61+
62+
base_height = 0.80 * HEIGHT
63+
speed = 0
64+
game_started = False
65+
game_over = False
66+
restart = False
67+
score = 0
68+
start_screen = True
69+
pipe_pass = False
70+
pipe_frequency = 1600
71+
72+
running = True
73+
while running:
74+
win.blit(bg, (0, 0))
75+
76+
if start_screen:
77+
speed = 0
78+
grumpy.draw_flap()
79+
base.update(speed)
80+
win.blit(flappybird_img, (40, 50))
81+
else:
82+
83+
if game_started and not game_over:
84+
85+
next_pipe = pygame.time.get_ticks()
86+
if next_pipe - last_pipe >= pipe_frequency:
87+
y = display_height // 2
88+
pipe_pos = random.choice(range(-100, 100, 4))
89+
height = y + pipe_pos
90+
91+
top = Pipe(win, pipe_img, height, 1)
92+
bottom = Pipe(win, pipe_img, height, -1)
93+
pipe_group.add(top)
94+
pipe_group.add(bottom)
95+
last_pipe = next_pipe
96+
97+
pipe_group.update(speed)
98+
base.update(speed)
99+
grumpy.update()
100+
score_img.update(score)
101+
102+
if (pygame.sprite.spritecollide(grumpy, pipe_group, False) or
103+
grumpy.rect.top <= 0):
104+
game_started = False
105+
if grumpy.alive:
106+
hit_fx.play()
107+
die_fx.play()
108+
grumpy.alive = False
109+
grumpy.theta = grumpy.vel * -2
110+
111+
if grumpy.rect.bottom >= display_height:
112+
speed = 0
113+
game_over = True
114+
115+
if len(pipe_group) > 0:
116+
p = pipe_group.sprites()[0]
117+
if (grumpy.rect.left > p.rect.left and
118+
grumpy.rect.right < p.rect.right and not pipe_pass
119+
and grumpy.alive):
120+
pipe_pass = True
121+
122+
if pipe_pass:
123+
if grumpy.rect.left > p.rect.right:
124+
pipe_pass = False
125+
score += 1
126+
point_fx.play()
127+
128+
if not grumpy.alive:
129+
win.blit(gameover_img, (50, 200))
130+
131+
for event in pygame.event.get():
132+
if event.type == pygame.QUIT:
133+
running = False
134+
if event.type == pygame.KEYDOWN:
135+
if event.key == pygame.K_ESCAPE or \
136+
event.key == pygame.K_q:
137+
running = False
138+
if event.type == pygame.MOUSEBUTTONDOWN:
139+
if start_screen:
140+
game_started = True
141+
speed = 2
142+
start_screen = False
143+
144+
game_over = False
145+
last_pipe = pygame.time.get_ticks() - pipe_frequency
146+
next_pipe = 0
147+
pipe_group.empty()
148+
149+
speed = 2
150+
score = 0
151+
152+
if game_over:
153+
start_screen = True
154+
grumpy = Grumpy(win)
155+
pipe_img = random.choice(im_list)
156+
bg = random.choice([bg1, bg2])
157+
158+
clock.tick(FPS)
159+
pygame.display.update()
160+
pygame.quit()
+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import pygame
2+
import random
3+
4+
SCREEN = WIDTH, HEIGHT = 288, 512
5+
display_height = 0.80 * HEIGHT
6+
7+
pygame.mixer.init()
8+
wing_fx = pygame.mixer.Sound('Sounds/wing.wav')
9+
10+
11+
class Grumpy:
12+
def __init__(self, win):
13+
self.win = win
14+
15+
self.im_list = []
16+
bird_color = random.choice(['red', 'blue', 'yellow'])
17+
for i in range(1, 4):
18+
img = pygame.image.load(f'Assets/Grumpy/{bird_color}{i}.png')
19+
self.im_list.append(img)
20+
21+
self.reset()
22+
23+
def update(self):
24+
# gravity
25+
self.vel += 0.3
26+
if self.vel >= 8:
27+
self.vel = 8
28+
if self.rect.bottom <= display_height:
29+
self.rect.y += int(self.vel)
30+
31+
if self.alive:
32+
33+
# jump
34+
if pygame.mouse.get_pressed()[0] == 1 and not self.jumped:
35+
wing_fx.play()
36+
self.jumped = True
37+
self.vel = -6
38+
if pygame.mouse.get_pressed()[0] == 0:
39+
self.jumped = False
40+
41+
self.flap_counter()
42+
43+
self.image = pygame.transform.rotate(
44+
self.im_list[self.index], self.vel * -2)
45+
else:
46+
if self.rect.bottom <= display_height:
47+
self.theta -= 2
48+
self.image = pygame.transform.rotate(
49+
self.im_list[self.index], self.theta)
50+
51+
self.win.blit(self.image, self.rect)
52+
53+
def flap_counter(self):
54+
self.counter += 1
55+
if self.counter > 5:
56+
self.counter = 0
57+
self.index += 1
58+
if self.index >= 3:
59+
self.index = 0
60+
61+
def draw_flap(self):
62+
self.flap_counter()
63+
if self.flap_pos <= -10 or self.flap_pos > 10:
64+
self.flap_inc *= -1
65+
self.flap_pos += self.flap_inc
66+
self.rect.y += self.flap_inc
67+
self.rect.x = WIDTH // 2 - 20
68+
self.image = self.im_list[self.index]
69+
self.win.blit(self.image, self.rect)
70+
71+
def reset(self):
72+
self.index = 0
73+
self.image = self.im_list[self.index]
74+
self.rect = self.image.get_rect()
75+
self.rect.x = 60
76+
self.rect.y = int(display_height) // 2
77+
self.counter = 0
78+
self.vel = 0
79+
self.jumped = False
80+
self.alive = True
81+
self.theta = 0
82+
self.mid_pos = display_height // 2
83+
self.flap_pos = 0
84+
self.flap_inc = 1
85+
86+
87+
class Base:
88+
def __init__(self, win):
89+
self.win = win
90+
91+
self.image1 = pygame.image.load('Assets/base.png')
92+
self.image2 = self.image1
93+
self.rect1 = self.image1.get_rect()
94+
self.rect1.x = 0
95+
self.rect1.y = int(display_height)
96+
self.rect2 = self.image2.get_rect()
97+
self.rect2.x = WIDTH
98+
self.rect2.y = int(display_height)
99+
100+
def update(self, speed):
101+
self.rect1.x -= speed
102+
self.rect2.x -= speed
103+
104+
if self.rect1.right <= 0:
105+
self.rect1.x = WIDTH - 5
106+
if self.rect2.right <= 0:
107+
self.rect2.x = WIDTH - 5
108+
109+
self.win.blit(self.image1, self.rect1)
110+
self.win.blit(self.image2, self.rect2)
111+
112+
113+
class Pipe(pygame.sprite.Sprite):
114+
def __init__(self, win, image, y, position):
115+
super(Pipe, self).__init__()
116+
117+
self.win = win
118+
self.image = image
119+
self.rect = self.image.get_rect()
120+
pipe_gap = 100 // 2
121+
x = WIDTH
122+
123+
if position == 1:
124+
self.image = pygame.transform.flip(self.image, False, True)
125+
self.rect.bottomleft = (x, y - pipe_gap)
126+
elif position == -1:
127+
self.rect.topleft = (x, y + pipe_gap)
128+
129+
def update(self, speed):
130+
self.rect.x -= speed
131+
if self.rect.right < 0:
132+
self.kill()
133+
self.win.blit(self.image, self.rect)
134+
135+
136+
class Score:
137+
def __init__(self, x, y, win):
138+
self.score_list = []
139+
for score in range(10):
140+
img = pygame.image.load(f'Assets/Score/{score}.png')
141+
self.score_list.append(img)
142+
self.x = x
143+
self.y = y
144+
145+
self.win = win
146+
147+
def update(self, score):
148+
score = str(score)
149+
for index, num in enumerate(score):
150+
self.image = self.score_list[int(num)]
151+
self.rect = self.image.get_rect()
152+
self.rect.topleft = self.x - 15 * len(score) + 30 * index, self.y
153+
self.win.blit(self.image, self.rect)

0 commit comments

Comments
 (0)