-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlappyBird.py
226 lines (162 loc) · 5.26 KB
/
FlappyBird.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
FlappyBird OOP game, made as a Course Project for Software Engineering Design course at VilniusTech.
Copyright(C) Paulius Leveris 2021
(part 1 of 2)
"""
import random
import sys
import pygame
from pygame.locals import *
import Bird
import Pipe
pygame.init()
WIDTH = 288
HEIGHT = 512
FPS = 30
CLOCK = pygame.time.Clock()
output = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird OOP game")
pygame.display.set_icon(pygame.image.load('game.ico'))
game_back = [pygame.image.load("content/bg.png")]
digits = [pygame.image.load('content/0.png'),
pygame.image.load('content/1.png'),
pygame.image.load('content/2.png'),
pygame.image.load('content/3.png'),
pygame.image.load('content/4.png'),
pygame.image.load('content/5.png'),
pygame.image.load('content/6.png'),
pygame.image.load('content/7.png'),
pygame.image.load('content/8.png'),
pygame.image.load('content/9.png')]
SCORE = 0
SOUNDS = {
'die': pygame.mixer.Sound('content/die.wav'),
'hit': pygame.mixer.Sound('content/hit.wav'),
'point': pygame.mixer.Sound('content/point.wav'),
'wing': pygame.mixer.Sound('content/wing.wav')
}
def getPipesReady():
lower_new_pipe_1 = Pipe.LowerPipe()
upper_new_pipe_1 = Pipe.UpperPipe()
Pipe.PIPE_LOWER = random.randrange(Pipe.PIPE_GAP + 30, Pipe.MIN_Y - 30)
Pipe.PIPE_UPPER = Pipe.PIPE_LOWER - Pipe.PIPE_GAP - Pipe.PIPE_HEIGHT
lower_new_pipe_2 = Pipe.LowerPipe()
lower_new_pipe_2.x += (WIDTH * 0.5)
global lower_pipe_list
lower_pipe_list = [lower_new_pipe_1, lower_new_pipe_2]
upper_new_pipe_2 = Pipe.UpperPipe()
upper_new_pipe_2.x += (WIDTH * 0.5)
global upper_pipe_list
upper_pipe_list = [upper_new_pipe_1, upper_new_pipe_2]
def generateBackground():
global background
background = game_back
def setupBirds():
global bird_ind
bird_ind = random.randrange(0, 3)
global bird
bird = Bird.Bird(bird_ind)
def remapObjsOnScreen():
output.blit(random.choice(background), (0, 0))
for pipe in lower_pipe_list:
pipe.draw(output)
for pipe in upper_pipe_list:
pipe.draw(output)
bird.draw(output)
show_score(SCORE)
pygame.display.update()
def show_score(score):
score_digits = [int(x) for x in list(str(score))]
total_width = 0
for digit in score_digits:
total_width += digits[digit].get_width()
x_offset = (WIDTH - total_width) / 2
for digit in score_digits:
output.blit(digits[digit], (x_offset, HEIGHT * 0.1))
x_offset += digits[digit].get_width()
# --------- UPDATER FUNCTIONS ---------
def update_pipe_list():
Pipe.PIPE_LOWER = random.randrange(Pipe.PIPE_GAP + 30, Pipe.MIN_Y - 30)
Pipe.PIPE_UPPER = Pipe.PIPE_LOWER - Pipe.PIPE_GAP - Pipe.PIPE_HEIGHT
if 0 < lower_pipe_list[0].x < 3:
new_pipe = Pipe.LowerPipe(pipe_col)
lower_pipe_list.append(new_pipe)
if lower_pipe_list[0].x < 0 - Pipe.PIPE_WIDTH:
lower_pipe_list.pop(0)
if 0 < upper_pipe_list[0].x < 3:
new_pipe = Pipe.UpperPipe(pipe_col)
upper_pipe_list.append(new_pipe)
if upper_pipe_list[0].x < 0 - Pipe.PIPE_WIDTH:
upper_pipe_list.pop(0)
# What if collision is found?
def detect_collision():
if (bird.y + Bird.bird_pics[0][1].get_height()) >= 400 - 5:
if not bird.endOfLife:
SOUNDS['hit'].play()
bird.endOfLife = True
return True
else:
for upper_pipe, lower_pipe in zip(upper_pipe_list, lower_pipe_list):
if (pygame.sprite.collide_mask(bird, upper_pipe) is not None) or \
(pygame.sprite.collide_mask(bird, lower_pipe) is not None):
if not bird.endOfLife:
SOUNDS['hit'].play()
SOUNDS['die'].play()
bird.endOfLife = True
return True
return False
def check_for_score():
player_mid_pos = bird.x + Bird.bird_pics[0][1].get_width() / 2
for pipe in upper_pipe_list:
pipe_mid_pos = pipe.x + Pipe.PIPE_IMAGES[0][0].get_width() / 2
if pipe_mid_pos <= player_mid_pos < pipe_mid_pos + 4 and not bird.endOfLife:
global SCORE
SCORE += 1
SOUNDS['point'].play()
# --------- MAIN GAME FUNCTIONS ---------
def start():
generateBackground()
setupBirds()
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == KEYDOWN and (event.key == K_UP or event.key == K_SPACE): return
output.blit(random.choice(background), (0, 0))
bird.draw(output)
bird.update()
pygame.display.update()
CLOCK.tick(FPS)
def main_loop():
global SCORE
SCORE = 0
start()
getPipesReady()
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
elif (event.type == MOUSEBUTTONDOWN or (event.type == KEYDOWN and
(event.key == K_UP or event.key == K_SPACE))) and \
(not bird.y <= 0) and not bird.endOfLife:
bird.jump()
SOUNDS['wing'].play()
elif bird.endOfLife and (event.type == KEYDOWN and (event.key == K_UP or event.key == K_SPACE) or
event.type == MOUSEBUTTONDOWN):
main_loop()
update_pipe_list()
check_for_score()
for pipe in lower_pipe_list:
pipe.update()
for pipe in upper_pipe_list:
pipe.update()
bird.update()
if detect_collision():
for upper_pipe, lower_pipe in zip(upper_pipe_list, lower_pipe_list):
upper_pipe.x_vel = 0
lower_pipe.x_vel = 0
remapObjsOnScreen()
CLOCK.tick(FPS)
main_loop() # GAME INITIALIZER