-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
261 lines (207 loc) · 8.45 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
# LIBRARIES #
from optparse import check_choice
from tabnanny import check
import pygame, sys, random
# FUNCTIONS & SUBPROCEDURES #
def draw_floor():
screen.blit(floor_surface, (floor_x_pos, floor_y_pos))
screen.blit(floor_surface, (floor_x_pos+screen_w, floor_y_pos))
def create_pipe():
random_pipe_pos = random.choice(pipe_height)
bottom_pipe = pipe_surface.get_rect(midtop = (pipe_x_pos, random_pipe_pos))
top_pipe = pipe_surface.get_rect(midbottom = (pipe_x_pos, random_pipe_pos - 210))
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 >= screen_h:
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):
collision_sound.play()
can_score = True
return False
if bird_rect.top <= -100 or bird_rect.bottom >= floor_y_pos:
can_score = True
return False
return True
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 = (bird_startx, bird_rect.centery))
return new_bird, new_bird_rect
def score_display(game_state):
# GAME STATES
if game_state == 'startup':
score_surface = game_font.render(f'Score: {int(score)}', True, (255,255,255))
score_rect = score_surface.get_rect(center = (screen_w/2, 100))
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 = score_surface.get_rect(center = (screen_w/2.4, 595))
screen.blit(high_score_surface, high_score_rect)
if game_state == 'playing':
score_surface = game_font.render(f'Score: {int(score)}', True, (255,255,255))
score_rect = score_surface.get_rect(center = (screen_w/2, 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 = (screen_w/2, 100))
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 = score_surface.get_rect(center = (screen_w/2.4, 595))
screen.blit(high_score_surface, high_score_rect)
def update_score(score, high_score):
if score > high_score:
high_score = score
return high_score
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
# INITIALIZATION #
#pygame.mixer.pre_init(frequency = 44100, size = 16, channels = 1, buffer = 512)
pygame.init()
screen_w = 403.2
screen_h = 716.8
screen = pygame.display.set_mode((screen_w, screen_h)) # Canvas
scale_factor = 1.428571429
clock = pygame.time.Clock()
game_font = pygame.font.Font('font/04B_19.TTF', 28)
# GAME VARIABLES #
gravity = 0.25 # Not the real value but an abstract value
bird_movement = 0
game_active = True
gameover = True
startup_menu = True
score = 0
high_score = 0
can_score = True
bg_day = pygame.image.load('sprites/background-day.png').convert()
bg_day = pygame.transform.scale(bg_day, (screen_w, screen_h))
bg_night = pygame.image.load('sprites/background-night.png').convert()
bg_night = pygame.transform.scale(bg_night, (screen_w, screen_h))
msg_w = 184*scale_factor
msg_h = 267*scale_factor
menu_surface = pygame.transform.scale((pygame.image.load('sprites/message.png').convert_alpha()), (msg_w, msg_h))
menu_rect = menu_surface.get_rect(center = (screen_w/2, screen_h/2))
gameover_surface = pygame.transform.scale((pygame.image.load('sprites/gameover-msg.png').convert_alpha()), (msg_w, msg_h))
gameover_rect = gameover_surface.get_rect(center = (screen_w/2, screen_h/2))
floor_w = screen_w
floor_h = 112
floor_surface = pygame.image.load('sprites/base.png').convert()
floor_surface = pygame.transform.scale(floor_surface, (screen_w, floor_h))
floor_x_pos = 0
floor_y_pos = 630
bird_w = 34*scale_factor
bird_h = 24*scale_factor
bird_startx = 70
bird_starty = 358.4
# bird_surface = pygame.image.load('sprites/bluebird-midflap.png').convert_alpha()
# bird_surface = pygame.transform.scale(bird_surface, (bird_w, bird_h))
# # Next line takes bird_surface and creates a rectangle around it
# bird_rect = bird_surface.get_rect(center = (bird_startx, bird_starty))
bird_downflap = pygame.transform.scale(pygame.image.load('sprites/bluebird-downflap.png').convert_alpha(), (bird_w, bird_h))
bird_midflap = pygame.transform.scale(pygame.image.load('sprites/bluebird-midflap.png').convert_alpha(), (bird_w, bird_h))
bird_upflap = pygame.transform.scale(pygame.image.load('sprites/bluebird-upflap.png').convert_alpha(), (bird_w, bird_h))
bird_frames = [bird_downflap, bird_midflap, bird_upflap]
bird_index = 0
bird_surface = bird_frames[bird_index]
bird_rect = bird_surface.get_rect(center = (bird_startx, bird_starty))
BIRDFLAP = pygame.USEREVENT + 1
pygame.time.set_timer(BIRDFLAP, 200)
pipe_w = 52*scale_factor
pipe_h = 320*scale_factor
pipe_surface = pygame.image.load('sprites/pipe-green.png').convert()
pipe_surface = pygame.transform.scale(pipe_surface, (pipe_w, pipe_h))
pipe_list = []
SPAWNPIPE = pygame.USEREVENT # Triggered by timer
pygame.time.set_timer(SPAWNPIPE, 1200) # Trigger event every 1.2 seconds
pipe_x_pos = screen_w + (2*pipe_w)
pipe_y_pos = screen_h/2
pipe_height = [300, 420, 560] # Possible heights the pipes can have
flap_sound = pygame.mixer.Sound('sound/sfx_wing.wav')
collision_sound = pygame.mixer.Sound('sound/sfx_hit.wav')
score_sound = pygame.mixer.Sound('sound/sfx_point.wav')
score_sound_countdown = 200
# GAME LOOP #
while True:
for event in pygame.event.get(): # event loop
if event.type == pygame.QUIT:
pygame.quit()
sys.exit() # Shuts down game completely
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
startup_menu = False
bird_movement = 0
bird_movement -= 8.399999997
flap_sound.play()
if event.key == pygame.K_SPACE and game_active == False:
game_active = True
pipe_list.clear()
bird_rect.center = (bird_startx, bird_starty)
bird_movement -= 8.399999997
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()
# Background display
screen.blit(bg_day, (0,0))
if score in range (20, 1000, 20):
screen.blit(bg_night, (0, 0))
if game_active and startup_menu == False:
# Bird
bird_movement += gravity
bird_rect.centery += bird_movement
rotated_bird = rotate_bird(bird_surface)
screen.blit(rotated_bird, bird_rect)
# Collision
game_active = check_collision(pipe_list)
# Pipes
pipe_list = move_pipes(pipe_list)
draw_pipes(pipe_list)
# Score
pipe_score_check()
score_display('playing')
if game_active and startup_menu:
gameover = False
bird_movement = 0
# Menu
screen.blit(menu_surface, menu_rect)
high_score = update_score(score, high_score)
score_display('startup')
if game_active == False:
gameover = True
bird_movement = 0
screen.blit(gameover_surface, gameover_rect)
high_score = update_score(score, high_score)
score_display('game_over')
# Floor
floor_x_pos -= 1
draw_floor()
if floor_x_pos <= -screen_w:
floor_x_pos = 0
pygame.display.update()
# Limit frame rate
clock.tick(120)