-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
294 lines (243 loc) · 9.18 KB
/
game.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
import os
import pygame
import random
# 定义常量
W, H = 288, 512
FPS = 30
pygame.init()
SCREEN = pygame.display.set_mode((W, H))
pygame.display.set_caption('YuFan编程FlappyBrid')
CLOCK = pygame.time.Clock()
# bird = pygame.image.load('resources/images/redbird-midflap.png')
#
# bgpic = pygame.image.load('resources/images/background-day.png')
# guide = pygame.image.load('resources/images/message.png')
# floor = pygame.image.load('resources/images/base.png')
# pipe = pygame.image.load('resources/images/pipe-green.png')
# gameover = pygame.image.load('resources/images/gameover.png')
IMAGES = {}
for image in os.listdir('resources/images'):
name, extension = os.path.splitext(image)
path = os.path.join('resources/images', image)
IMAGES[name] = pygame.image.load(path)
FLOOR_Y = H - IMAGES['base'].get_height()
start = pygame.mixer.Sound('resources/audios/swoosh.wav')
die = pygame.mixer.Sound('resources/audios/die.wav')
hit = pygame.mixer.Sound('resources/audios/hit.wav')
point = pygame.mixer.Sound('resources/audios/point.wav')
fly = pygame.mixer.Sound('resources/audios/wing.wav')
def main():
while True:
start.play()
IMAGES['background'] = IMAGES[random.choice(['background-day', 'background-night'])]
color = random.choice(['redbird', 'yellowbird', 'bluebird'])
IMAGES['bird'] = [IMAGES[color + '-upflap'], IMAGES[color + '-midflap'], IMAGES[color + '-downflap']]
pipe = IMAGES[random.choice(['pipe-green', 'pipe-red'])]
IMAGES['pipe'] = [pipe, pygame.transform.flip(pipe, False, True)]
menu_window()
result = game_window()
end_window(result)
def menu_window():
# 地板移动出现视觉移动效果
floor_gap = IMAGES['base'].get_width() - W
floor_x = 0
guide_x = (W - IMAGES['message'].get_width()) / 2
guide_y = (FLOOR_Y - IMAGES['message'].get_height()) / 2
bird_x = W * 0.2
bird_y = (H - IMAGES['bird'][0].get_height()) / 2
# 菜单界面小鸟浮动
bird_y_vel = 1
bird_y_range = [bird_y - 8, bird_y + 8]
# 小鸟拍动翅膀
idx = 0
repeat = 5
frames = [0] * repeat + [1] * repeat + [2] * repeat + [1] * repeat
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
return
floor_x -= 4
if floor_x <= -floor_gap:
floor_x = 0
bird_y += bird_y_vel
if bird_y < bird_y_range[0] or bird_y > bird_y_range[1]:
# 上线浮动关键代码
bird_y_vel *= -1
# 翅膀浮动关键代码
idx += 1
idx %= len(frames)
SCREEN.blit(IMAGES['background'], (0, 0))
SCREEN.blit(IMAGES['base'], (floor_x, FLOOR_Y))
SCREEN.blit(IMAGES['message'], (guide_x, guide_y))
SCREEN.blit(IMAGES['bird'][frames[idx]], (bird_x, bird_y))
SCREEN.blit(IMAGES['tip'], (20, 20))
pygame.display.update()
CLOCK.tick(FPS)
def game_window():
fly.play()
score = 0
floor_gap = IMAGES['base'].get_width() - W
floor_x = 0
bird = Bird(W * 0.2, H * 0.4)
distance = 150
n_pairs = 4
pipe_gap = 100
pipe_group = pygame.sprite.Group()
for i in range(n_pairs):
# 随机水管长度0.3-0.7
pipe_y = random.randint(int(H * 0.3), int(H * 0.7))
pipe_up = Pipe(W + i * distance, pipe_y, True)
pipe_down = Pipe(W + i * distance, pipe_y - pipe_gap, False)
pipe_group.add(pipe_up)
pipe_group.add(pipe_down)
# pipe = Pipe(W, H * 0.5)
while True:
flap = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
fly.play()
return
elif event.key == pygame.K_UP:
flap = True
fly.play()
elif event.key == pygame.K_w:
point.play()
floor_x -= 4
if floor_x <= -floor_gap:
floor_x = 0
bird.update(flap)
first_pipe_up = pipe_group.sprites()[0]
first_pipe_down = pipe_group.sprites()[1]
if first_pipe_up.rect.right < 0:
pipe_y = random.randint(int(H * 0.3), int(H * 0.7))
new_pipe_up = Pipe(first_pipe_up.rect.x + n_pairs * distance, pipe_y, True)
new_pipe_down = Pipe(first_pipe_down.rect.x + n_pairs * distance, pipe_y - pipe_gap, False)
pipe_group.add(new_pipe_up)
pipe_group.add(new_pipe_down)
first_pipe_up.kill()
first_pipe_down.kill()
pipe_group.update()
# 判断如何输掉比赛+碰撞测试
if bird.rect.y > FLOOR_Y or bird.rect.y < 0 or pygame.sprite.spritecollideany(bird, pipe_group):
bird.dying = True
hit.play()
die.play()
result = {'bird': bird, 'pipe_group': pipe_group, 'score': score}
return result
if bird.rect.left + first_pipe_up.x_vel < first_pipe_up.rect.centerx < bird.rect.left:
point.play()
score += 1
# 碰撞代码
# for pipe in pipe_group.sprites():
# right_to_left = max(bird.rect.right, pipe.rect.right) - min(bird.rect.left, pipe.rect.left)
# bottom_to_top = max(bird.rect.bottom, pipe.rect.bottom) - min(bird.rect.top, pipe.rect.top)
# if right_to_left < bird.rect.width + pipe.rect.width and bottom_to_top < bird.rect.height + pipe.rect.height:
# hit.play()
# die.play()
# result = {'bird': bird, 'pipe_group': pipe_group}
# return result
SCREEN.blit(IMAGES['background'], (0, 0))
pipe_group.draw(SCREEN)
SCREEN.blit(IMAGES['base'], (floor_x, FLOOR_Y))
show_score(score)
SCREEN.blit(bird.image, bird.rect)
pygame.display.update()
CLOCK.tick(FPS)
def end_window(result):
# floor_gap = IMAGES['base'].get_width() - W
# floor_x = 0
gameover_x = (W - IMAGES['gameover'].get_width()) / 2
gameover_y = (FLOOR_Y - IMAGES['gameover'].get_height()) / 2
bird = result['bird']
pipe_group = result['pipe_group']
# bird = Bird(W * 0.5, H * 0.3)
while True:
if bird.dying:
bird.go_die()
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
return
# floor_x -= 4
# if floor_x <= -floor_gap:
# floor_x = 0
bird.go_die()
SCREEN.blit(IMAGES['background'], (0, 0))
pipe_group.draw(SCREEN)
SCREEN.blit(IMAGES['base'], (0, FLOOR_Y))
SCREEN.blit(IMAGES['gameover'], (gameover_x, gameover_y))
show_score(result['score'])
SCREEN.blit(bird.image, bird.rect)
pygame.display.update()
CLOCK.tick(FPS)
def show_score(score):
score_str = str(score)
n = len(score_str)
w = IMAGES['0'].get_width() * 1.1
x = (W - n * w) / 2
y = H * 0.1
for number in score_str:
SCREEN.blit(IMAGES[number], (x, y))
x += w
class Bird:
def __init__(self, x, y):
self.frames = [0] * 5 + [1] * 5 + [2] * 5 + [1] * 5
self.idx = 0
self.images = IMAGES['bird']
self.image = self.images[self.frames[self.idx]]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.y_vel = -10
self.max_y_vel = 10
self.gravity = 1
self.rotate = 45
self.max_rotate = -20
self.rotate_vel = -3
self.y_vel_after_flap = -10
self.rotate_after_flap = 45
self.dying = False
def update(self, flap=False):
if flap:
self.y_vel = self.y_vel_after_flap
self.rotate = self.rotate_after_flap
self.y_vel = min(self.y_vel + self.gravity, self.max_y_vel)
self.rect.y += self.y_vel
self.rotate = max(self.rotate + self.rotate_vel, self.max_rotate)
self.idx += 1
self.idx %= len(self.frames)
self.image = self.images[self.frames[self.idx]]
self.image = pygame.transform.rotate(self.image, self.rotate)
def go_die(self):
if self.rect.y < FLOOR_Y:
self.rect.y += self.max_y_vel
self.rotate = -90
self.image = self.images[self.frames[self.idx]]
self.image = pygame.transform.rotate(self.image, self.rotate)
else:
self.dying = False
class Pipe(pygame.sprite.Sprite):
def __init__(self, x, y, upwards=True):
pygame.sprite.Sprite.__init__(self)
if upwards:
self.image = IMAGES['pipe'][0]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.top = y
else:
self.image = IMAGES['pipe'][1]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.bottom = y
self.x_vel = -4
def update(self):
self.rect.x += self.x_vel
if __name__ == '__main__':
main()