-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
416 lines (329 loc) · 14.7 KB
/
main.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import random
import sys
import pygame
from pygame.locals import *
from pygame import mixer
width = 289
height = 511
framepersecond = 32
pygame.init()
# Screen and buttons
screen = pygame.display.set_mode((width, height))
background = pygame.transform.scale(pygame.image.load("gallery/sprites/background.jpeg"), (width, height))
play_button = pygame.transform.scale(pygame.image.load("buttons/play.png"), (150, 60))
option_button = pygame.transform.scale(pygame.image.load("buttons/options.png"), (150, 60))
quit_button = pygame.transform.scale(pygame.image.load("buttons/exit.png"), (150, 60))
home_button = pygame.transform.scale(pygame.image.load("buttons/icon_home.png"), (45, 45))
info_buttom = pygame.transform.scale(pygame.image.load("buttons/icon_info.png"), (45, 45))
exit_button = pygame.transform.scale(pygame.image.load("buttons/icon_x.png"), (45, 45))
title = pygame.transform.scale(pygame.image.load("gallery/sprites/flappy-bird.png"), (270, 80))
# Options
play_bgm = True
play_sfx = True
# BGM
mixer.music.load('Sound/bgm.wav')
mixer.music.play(-1)
player = pygame.transform.scale(pygame.image.load("gallery/sprites/player.png"), (32, 32)) # <-- placeholder
groundY = height * 0.9
sprites = {}
audio = {}
def homescreen():
while True:
screen.blit(background, (0,0))
mouse_pos = pygame.mouse.get_pos()
screen.blit(title, (8,100))
# start button
screen.blit(play_button, (70, 240))
# option button
screen.blit(option_button, (70, 315))
# quit button
screen.blit(quit_button, (70, 390))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if mouse_pos[0] in range(70, 220) and mouse_pos[1] in range(240, 300):
# game start
return
if mouse_pos[0] in range(70, 220) and mouse_pos[1] in range(315, 375):
# options
optionscreen()
if mouse_pos[0] in range(70, 220) and mouse_pos[1] in range(390, 450):
# exit
pygame.quit()
sys.exit()
pygame.display.update()
def optionscreen():
while True:
global play_bgm
global play_sfx
screen.blit(background, (0, 0))
mouse_pos = pygame.mouse.get_pos()
# Title
title = pygame.font.Font('freesansbold.ttf', 45).render('Options', True, (255, 255, 255))
screen.blit(title, (55, 75))
# BGM
bgm_text = pygame.font.Font('freesansbold.ttf', 30).render('BGM', True, (255, 255, 255))
screen.blit(bgm_text, (30, 210))
# SFX
sfx_text = pygame.font.Font('freesansbold.ttf', 30).render('SFX', True, (255, 255, 255))
screen.blit(sfx_text, (30, 270))
on_text_on = pygame.font.Font('freesansbold.ttf', 30).render('ON', True, (0, 255, 0))
off_text_on = pygame.font.Font('freesansbold.ttf', 30).render('OFF', True, (255, 0, 0))
on_text_off = pygame.font.Font('freesansbold.ttf', 30).render('ON', True, (0, 75, 0))
off_text_off = pygame.font.Font('freesansbold.ttf', 30).render('OFF', True, (75, 0, 0))
if play_bgm:
screen.blit(on_text_on, (130, 210))
screen.blit(off_text_off, (200, 210))
elif not play_bgm:
screen.blit(on_text_off, (130, 210))
screen.blit(off_text_on, (200, 210))
if play_sfx:
screen.blit(on_text_on, (130, 270))
screen.blit(off_text_off, (200, 270))
elif not play_sfx:
screen.blit(on_text_off, (130, 270))
screen.blit(off_text_on, (200, 270))
# Back Button
screen.blit(home_button, (5, 5))
# Credits Button
credits_text = pygame.font.Font('freesansbold.ttf', 30).render('Credits', True, (255, 255, 255))
screen.blit(credits_text, (30, 460))
screen.blit(info_buttom, (170, 453))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if mouse_pos[0] in range(5, 50) and mouse_pos[1] in range(5, 50):
return
if mouse_pos[0] in range(130, 180) and mouse_pos[1] in range(210, 240):
play_bgm = True
mixer.music.play(-1)
if mouse_pos[0] in range(200, 260) and mouse_pos[1] in range(210, 240):
play_bgm = False
mixer.music.stop()
if mouse_pos[0] in range(130, 180) and mouse_pos[1] in range(270, 300):
play_sfx = True
if mouse_pos[0] in range(200, 260) and mouse_pos[1] in range(270, 300):
play_sfx = False
if mouse_pos[0] in range(170, 215) and mouse_pos[1] in range(453, 498):
creditscreen()
pygame.display.update()
def creditscreen():
y = 0
credit_font = pygame.font.Font('freesansbold.ttf', 25)
clock = pygame.time.Clock()
name1 = credit_font.render("Minjae Rhee", True, (255, 255, 0))
name2 = credit_font.render("Michael Bhang", True, (255, 255, 0))
name3 = credit_font.render("Jehyeok Yeon", True, (255, 255, 0))
name4 = credit_font.render("Sanghyuk Seo", True, (255, 255, 0))
name5 = credit_font.render("Emily Shin", True, (255, 255, 0))
name6 = credit_font.render("Hyerim Oh", True, (255, 255, 0))
name7 = credit_font.render("Donghyeon Jeong", True, (255, 255, 0))
while True:
mouse_pos = pygame.mouse.get_pos()
screen.blit(background, (0, 0))
screen.blit(exit_button, (5,5))
credit_rect1 = name1.get_rect()
credit_rect1.center = (144, (-60+y))
credit_rect2 = name2.get_rect()
credit_rect2.center = (144, (-120+y))
credit_rect3 = name3.get_rect()
credit_rect3.center = (144, (-180+y))
credit_rect4 = name4.get_rect()
credit_rect4.center = (144, (-240+y))
credit_rect5 = name5.get_rect()
credit_rect5.center = (144, (-300+y))
credit_rect6 = name6.get_rect()
credit_rect6.center = (144, (-360+y))
credit_rect7 = name7.get_rect()
credit_rect7.center = (144, -420+y)
screen.blit(name1, credit_rect1)
screen.blit(name2, credit_rect2)
screen.blit(name3, credit_rect3)
screen.blit(name4, credit_rect4)
screen.blit(name5, credit_rect5)
screen.blit(name6, credit_rect6)
screen.blit(name7, credit_rect7)
# credits_text = pygame.font.Font('freesansbold.ttf', 15).render("Donghyeon Jeong, Jehyeok Yeon, ", True, (255, 255, 255))
# screen.blit(credits_text, (20, 55+y))
# credits_text_2 = pygame.font.Font('freesansbold.ttf', 15).render("Minjae Rhee, Michael Bhang", True, (255, 255, 255))
# screen.blit(credits_text_2, (20, 85+y))
# credits_text_3 = pygame.font.Font('freesansbold.ttf', 15).render("Hyerim Oh, Sanghyuk Seo", True, (255, 255, 255))
# screen.blit(credits_text_3, (20, 115+y))
# credits_text_4 = pygame.font.Font('freesansbold.ttf', 15).render("Emily Shin", True, (255, 255, 255))
# screen.blit(credits_text_4, (20, 145+y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if mouse_pos[0] in range(5, 50) and mouse_pos[1] in range(5, 50):
return
pygame.display.update()
y += 1
pygame.time.wait(10)
if y == 1000: y = 0
def gamescreen():
# load bgm file and play
score = 0
playerx = int(width/5)
playery = int (height/2)
basex = 0
pipeVelX = -4
playerVelY = -9
playerMaxVel = 10
playerMinVelY = -8
playerAccY = 1
font_gameover = pygame.font.Font('freesansbold.ttf', 30)
# generate pipes
newPipe1 = getRandomPipe()
newPipe2 = getRandomPipe()
# upper pipes
upperPipes = [
{'x': width+200, 'y':newPipe1[0]['y']},
{'x': width+200+(width/2), 'y':newPipe2[0]['y']},
]
#lower pipes
lowerPipes = [
{'x': width+200, 'y':newPipe1[1]['y']},
{'x': width+200+(width/2), 'y':newPipe2[1]['y']},
]
myFont = pygame.font.SysFont("arial", 20, True, True)
score_title = myFont.render("Score:", True, (255, 255, 255))
score_rect = score_title.get_rect()
score_rect.x, score_rect.y = 100, 10
playerFlapVel = -8
playerFlapped = False
score_up = True
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()
if event.type == pygame.MOUSEBUTTONUP or (event.type == KEYDOWN and event.key == K_SPACE):
if playery > 0:
playerVelY = playerFlapVel
playerFlapped = True
# play flap audio
audio['wing'].play()
# collision logic
crashTest = isCollide(playerx, playery, upperPipes, lowerPipes)
# game over
while crashTest:
gameover_text1 = font_gameover.render("GAME OVER :(", False, (255, 255, 255))
gameover_rect1 = gameover_text1.get_rect()
gameover_rect1.center = (144, 120)
gameover_text2 = font_gameover.render("Your Score: "+str((score))+"", False, (255, 255, 255))
gameover_rect2 = gameover_text2.get_rect()
gameover_rect2.center = (144, 170)
gameover_text3 = font_gameover.render("Press ESC to Quit", False, (255, 255, 255))
gameover_rect3 = gameover_text3.get_rect()
gameover_rect3.center = (144, 220)
screen.blit(gameover_text1, gameover_rect1)
screen.blit(gameover_text2, gameover_rect2)
screen.blit(gameover_text3, gameover_rect3)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return
# score logic
player_x = playerx + sprites['player'].get_width()
pipe_x = lowerPipes[0]['x'] + sprites['pipe'][0].get_width()
if pipe_x > player_x:
score_up = True
elif pipe_x < player_x and score_up:
score += 1
audio['point'].play()
score_up = False
# score rendering
score_title2 = myFont.render(str((score)), True, (255, 255, 255))
score_rect2 = score_title2.get_rect()
score_rect2.x, score_rect2.y = 170, 10
if playerVelY < playerMaxVel and not playerFlapped:
playerVelY += playerAccY
if playerFlapped:
playerFlapped = False
playerHeight = 32
playery += min(playerVelY, groundY - playery - playerHeight)
# move pipes
for upperPipe , lowerPipe in zip(upperPipes, lowerPipes):
upperPipe['x'] += pipeVelX
lowerPipe['x'] += pipeVelX
# add new pipe when first goes out of screen
if 0<upperPipes[0]['x']<5:
newpipe = getRandomPipe()
upperPipes.append(newpipe[0])
lowerPipes.append(newpipe[1])
# remove out of screen pipe
if upperPipes[0]['x'] < - sprites['pipe'][0].get_width(): # here there negative sign is also there , so be carefull
upperPipes.pop(0)
lowerPipes.pop(0)
# Lets blit our sprites now
screen.blit(sprites['background'], (0, 0))
for upperPipe, lowerPipe in zip(upperPipes, lowerPipes):
screen.blit(sprites['pipe'][0], (upperPipe['x'], upperPipe['y']))
screen.blit(sprites['pipe'][1], (lowerPipe['x'], lowerPipe['y']))
screen.blit(sprites['base'], (basex, groundY))
screen.blit(sprites['player'], (playerx, playery))
screen.blit(score_title, score_rect)
screen.blit(score_title2, score_rect2)
pygame.display.update()
framepersecond_clock.tick(framepersecond)
def isCollide(playerx, playery, upperPipes, lowerPipes):
# collision logic
if playery> groundY - 25 or playery<0:
audio['hit'].play()
return True
for pipe in upperPipes:
pipeHeight = sprites['pipe'][0].get_height()
if (playery < pipeHeight + pipe['y'] and abs(playerx - pipe['x'])+20 < sprites['pipe'][0].get_width()):
audio['hit'].play()
return True
for pipe in lowerPipes:
if (playery + sprites['player'].get_height() > pipe['y']) and abs(playerx - pipe['x'])+20 < sprites['pipe'][0].get_width():
audio['hit'].play()
return True
return False
def getRandomPipe():
pipeHeight = sprites['pipe'][0].get_height()
offset = height/3
y2 = offset + random.randrange(0, int(height - sprites['base'].get_height() - 1.2 *offset))
pipeX = width + 10
y1 = pipeHeight - y2 + offset
pipe = [
{'x': pipeX, 'y': -y1}, #upper Pipe
{'x': pipeX, 'y': y2} #lower Pipe
]
return pipe
def gameOver():
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Flappy Bird')
# load in ui sprites and add to list of sprites
# load in game over ui sprites
pygame.display.update()
# game over logic (press button to retry / go back to home / etc)
if __name__ == "__main__":
pygame.init()
framepersecond_clock = pygame.time.Clock()
pygame.display.set_caption('Flappy Bird')
sprites['base'] =pygame.image.load('gallery/sprites/base.png').convert_alpha()
sprites['pipe'] =(pygame.transform.rotate(pygame.image.load('gallery/sprites/pipe.png').convert_alpha(), 180),
pygame.image.load('gallery/sprites/pipe.png').convert_alpha()
)
# Game sounds
audio['die'] = pygame.mixer.Sound('gallery/audio/die.wav')
audio['hit'] = pygame.mixer.Sound('gallery/audio/hit.wav')
audio['point'] = pygame.mixer.Sound('gallery/audio/point.wav')
audio['swoosh'] = pygame.mixer.Sound('gallery/audio/swoosh.wav')
audio['wing'] = pygame.mixer.Sound('gallery/audio/wing.wav')
sprites['background'] = pygame.image.load('gallery/sprites/background.jpeg').convert()
bird_img = pygame.image.load('gallery/sprites/birdy.png')
sprites['player'] = pygame.transform.scale(bird_img, (30, 21)).convert_alpha()
# sprites['main'] = pygame.image.load('gallery/sprites/flappy-bird.png')
while True:
homescreen()
gamescreen()