-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappybird.py
236 lines (190 loc) · 7.96 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
227
228
229
230
231
232
233
234
235
236
import pygame, sys, random
def set_bg():
b = ["bluebird", "redbird", "yellowbird"]
birdtype = str(random.choice(b))
birdtype = 'assets/' + birdtype
if birdtype != 'assets/redbird' or birdtype == 'assets/yellowbird':
bg_image = pygame.image.load('assets/background-day.png').convert()
#scales the bg_image loaded in the above line
bg_image = pygame.transform.scale(bg_image, (432, 768))
pipe = pygame.image.load('assets/pipe-green.png').convert()
pipe = pygame.transform.scale(pipe, (int(pipe.get_width() * 1.5), int(pipe.get_height() * 1.5)))
else:
bg_image = pygame.image.load('assets/background-night.png').convert()
#scales the bg_image loaded in the above line
bg_image = pygame.transform.scale(bg_image, (432, 768))
pipe = pygame.image.load('assets/pipe-red.png').convert()
pipe = pygame.transform.scale(pipe, (int(pipe.get_width() * 1.5), int(pipe.get_height() * 1.5)))
return bg_image,pipe
def set_birds():
b = ["bluebird", "redbird", "yellowbird"]
birdtype = str(random.choice(b))
birdtype = 'assets/' + birdtype
bird_midflap = pygame.transform.scale(pygame.image.load(
birdtype+'-midflap.png').convert_alpha(), (int(34 * 1.2), int(24 * 1.2)))
bird_upflap = pygame.transform.scale(pygame.image.load(
birdtype + '-upflap.png').convert_alpha(), (int(34 * 1.2), int(24 * 1.2)))
bird_downflap = pygame.transform.scale(pygame.image.load(
birdtype + '-downflap.png').convert_alpha(), (int(34 * 1.2), int(24 * 1.2)))
birds = [bird_upflap, bird_midflap, bird_downflap]
return birds
def move_floor():
screen.blit(floor, (floor_x_position, 660))
screen.blit(floor, (floor_x_position + 432, 660))
def put_pipes(pipe_rects):
for pipe_rect in pipe_rects:
if pipe_rect.bottom >= 768:
screen.blit(pipe, pipe_rect)
else:
flippipe = pygame.transform.flip(pipe, False, True)
screen.blit(flippipe,pipe_rect)
def move_pipes(pipe_rects):
for pipe_rect in pipe_rects:
pipe_rect.centerx -= 5
visible_pipe_rects = [pipe_rect for pipe_rect in pipe_rects if pipe_rect.right > -40]
return visible_pipe_rects
def create_pipe_rects():
height = random.choice(pipe_heights)
bottom_pipe = pipe.get_rect(midtop=(450, height))
top_pipe = pipe.get_rect(midbottom = (450, height - 200))
pipe_rects.extend((bottom_pipe,top_pipe))
return pipe_rects
def collision_check(pipe_rects):
for pipe_rect in pipe_rects:
if bird_rect.colliderect(pipe_rect):
death_sound.play()
birds = set_birds()
score_update_factor = True
return False
if bird_rect.bottom >= 660 or bird_rect.top <= -50:
death_sound.play()
birds = set_birds()
score_update_factor = True
return False
return True
def rotate_bird(bird):
rbird = pygame.transform.rotozoom(bird, -(bird_movement*2), 1)
return rbird
def animate_bird():
new_bird = birds[bird_anim_index]
new_bird_rect = new_bird.get_rect(center=(60, bird_rect.centery))
return new_bird, new_bird_rect
def display_score():
if game_status:
score_surface = game_font.render(str(int(score)), True, (255, 255, 255))
score_rect = score_surface.get_rect(center=(216, 80))
screen.blit(score_surface, score_rect)
else:
score_surface = game_font.render(f'Score : {int(score)}', True, (255, 255, 255))
score_rect = score_surface.get_rect(center=(216, 80))
screen.blit(score_surface, score_rect)
high_score_surface = game_font.render(f'High Score : {int(high_score)}', True, (255, 255, 255))
high_score_rect = high_score_surface.get_rect(center=(216, 610))
screen.blit(high_score_surface, high_score_rect)
gameover_rect = gameover.get_rect(center=(216, 300))
screen.blit(gameover, gameover_rect)
restart_surface = game_font.render(
f'Press Space to START', True, (220, 40, 40))
restart_rect = restart_surface.get_rect(center=(216, 730))
screen.blit(restart_surface, restart_rect)
bg_image = set_bg()
birds = set_birds()
def score_updater():
global score, pipe_rects, score_update_factor, high_score
if pipe_rects:
for pipe in pipe_rects:
if 55 < pipe.centerx < 65 and score_update_factor:
score += 1
point_sound.play()
score_update_factor = False
if pipe.centerx < 0:
score_update_factor = True
if score > high_score:
high_score = score
#initialize the pygame
pygame.init()
#pygame.mixer.pre_init(channels=1, buffer=512)
#sets the default canvas screen
screen = pygame.display.set_mode((432, 768))
game_font = pygame.font.Font('04B_19.ttf', 30)
name_font = pygame.font.Font('04B_19.ttf', 14)
#game variables
gravity_factor = 0.18
game_status = True #used to check the game's status
score = 0
high_score = 0
floor = pygame.image.load('assets/base.png').convert()
floor = pygame.transform.scale(floor, (504, 168))
floor_x_position = 0
bird_anim_index = 0
bird_movement = 0
birds = set_birds()
bird = birds[bird_anim_index]
bird_rect = bird.get_rect(center=(60, 384))
BIRDFLAP = pygame.USEREVENT + 1
pygame.time.set_timer(BIRDFLAP, 200)
score_update_factor = True
bg_image, pipe = set_bg()
pipe_rects = []
PLACEPIPETIMER = pygame.USEREVENT #timer variable activates on user event timer
pygame.time.set_timer(PLACEPIPETIMER, 1200) #sets timer
pipe_heights = [300, 380, 500]
gameover = pygame.transform.scale(pygame.image.load('assets/message.png').convert_alpha(), (int(184* 1.2), int(267* 1.2)))
fly_sound = pygame.mixer.Sound('sound/sfx_wing.wav')
death_sound = pygame.mixer.Sound('sound/sfx_hit.wav')
point_sound = pygame.mixer.Sound('sound/sfx_point.wav')
#pygame's clock
clock = pygame.time.Clock()
while True:
#loop gets all the event from the user (eg.mouse input,keyboard input,etc.)
for event in pygame.event.get():
if event.type == pygame.QUIT: #this event is the exit button input
pygame.quit() #quits pygame
sys.exit() #ends the screen in a proper way
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_status:
bird_movement = 0
bird_movement -= 8
fly_sound.play()
if event.key == pygame.K_SPACE and game_status == False:
bg_image, pipe = set_bg()
birds = set_birds()
game_status = True
bird_rect.center = (60, 384)
pipe_rects.clear()
bird_movement = 0
score = 0
if event.type == PLACEPIPETIMER:
pipe_rects = create_pipe_rects()
if event.type == BIRDFLAP:
if bird_anim_index < 2:
bird_anim_index += 1
else:
bird_anim_index = 0
bird,bird_rect = animate_bird()
#bg
screen.blit(bg_image, (0, 0)) #sets the bg_image to the surface
if game_status:
#bird
bird_movement += gravity_factor
bird_rect.centery += bird_movement
rotated_bird = rotate_bird(bird)
screen.blit(rotated_bird, bird_rect)
game_status = collision_check(pipe_rects)
#pipes
pipe_rects = move_pipes(pipe_rects)
put_pipes(pipe_rects)
#score
score_updater()
display_score()
#floor
floor_x_position -= 1
move_floor()
if floor_x_position <= -432:
floor_x_position = 0
score_surface = name_font.render("By Inbakrish", True, (255, 255, 255))
score_rect = score_surface.get_rect(center=(50, 25))
screen.blit(score_surface, score_rect)
display_score()
pygame.display.update() #updates the screen with the things in the loop
clock.tick(90) #sets the frame rate limit to 90 , so that it doesnt exeeds 90fps