-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.py
More file actions
351 lines (309 loc) · 13.6 KB
/
Game.py
File metadata and controls
351 lines (309 loc) · 13.6 KB
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
# This is the main game file for Flappy Bird where the game loop runs, do not edit this file, except for the select imports below.
# Imports
import os
import pygame
from pygame.locals import *
import random
import sys
import torch
import torch.nn as nn
import copy
from Template import Model # Change to Answers if you want to see the solution
from Template import mutate_bird # Change to Answers if you want to see the solution
from Template import select_next_round # Change to Answers if you want to see the solution
PIPE_SPACE = 120
GROUND = 430
GRAVITY = 40
JUMP = 10
SPEED = 7
SPAWN_RATE = 300
DESIRED_FPS = 99999
VSYNC = 0
INITIAL_POP = 40
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
# Class for a bird which has its own position, death time, NN, and hitbox
class Bird:
# Initializes values
def __init__(self):
self.x = 50
self.y = 200 + random.randint(-50, 50)
self.velocity = 0
self.deathTime = -1
self.died = False
self.player_box = Rect(50, 200, 20, 20)
self.model = Model(4, 1)
# Normalizes input values and returns model output
def forward(self, closest_pipe_x, closest_pipe_y):
y = self.y / 500
v = self.velocity / 40
input = torch.tensor([closest_pipe_x, closest_pipe_y, y, v]).float()
return self.model(input)
# Resets values except for model
def reset(self):
self.x = 50
self.y = 200 + random.randint(-50, 50)
self.velocity = 0
self.deathTime = -1
self.died = False
self.player_box = Rect(50, 200, 20, 20)
# Returns a clone
def clone(self):
return copy.deepcopy(self)
def make_good(self, model=1):
if model == 1:
weights = [0.0530, 0.6592, -1.2402, -0.0786]
biases = [0.2874]
elif model == 2:
weights = [0.1125, 0.5206, -0.9979, -0.2995]
biases = [0.2953]
with torch.no_grad():
# Set weights
self.model.fc1.weight.data = torch.tensor([weights])
# Set biases/bias
self.model.fc1.bias.data = torch.tensor(biases)
# Class for a pipe object
class Pipe:
def __init__(self):
self.x = 500
# self.y is in the range [25, 285] (assuming constants aren't changed)
self.y = random.randint(25, GROUND - PIPE_SPACE - 25)
self.scored = False
# Mutates the weights and bias of a bird's model using a mutation rate and strength
# specified by the parameters
def mutate(bird):
return mutate_bird(bird)
# Runs the game
# show_good determines whether to show a pre-trained model or train a new one
# Also, some sound-effects are turned off when show_good is False (cause it gets annoying)
def run_game(evolve=False, next_round=[], show_good=False, gen=0):
# pygame setup
pygame.init()
screen = pygame.display.set_mode((500, 500), vsync=VSYNC)
clock = pygame.time.Clock()
pygame.display.set_caption("Crappy Bird")
pygame.display.set_icon(pygame.image.load(resource_path("game_assets\\favicon.ico")))
running = True
birds = []
# Initializes the bird population
if not show_good:
if evolve:
for i in range(len(next_round)):
birds.append(next_round[i])
for _ in range(INITIAL_POP - len(next_round)):
birds.append(mutate(next_round[random.randint(0, len(next_round) - 1)]))
else:
for i in range(INITIAL_POP):
birds.append(Bird())
for i in range(INITIAL_POP):
birds[i].reset()
else:
birds = [Bird()]
birds[0].make_good(model=1)
pipes = []
last_pipe_step = 0
game_started = False
# space_pressed = False
step = 0
score = 0
jump_sound = pygame.mixer.Sound(resource_path("game_assets\\audio\\wing.wav"))
point_sound = pygame.mixer.Sound(resource_path("game_assets\\audio\\point.wav"))
hit_sound = pygame.mixer.Sound(resource_path("game_assets\\audio\\hit.wav"))
die_sound = pygame.mixer.Sound(resource_path("game_assets\\audio\\die.wav"))
mb0 = pygame.image.load(resource_path("game_assets\\mb0.png"))
spc1 = pygame.image.load(resource_path("game_assets\\spc1.png"))
spc2 = pygame.image.load(resource_path("game_assets\\spc2.png"))
spc3 = pygame.image.load(resource_path("game_assets\\spc3.png"))
background = pygame.image.load(resource_path("game_assets\\background-day.png"))
game_over_sprite = pygame.image.load(resource_path("game_assets\\gameover.png"))
all_done = False
cur_generation = gen
while running:
# Uses a step variable to keep track of time
if not all_done:
step += 1
all_done = True
for bird in birds:
if bird.deathTime == -1:
all_done = False
# uncomment to display fps
# print(clock.get_fps())
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_n:
for bird in birds:
if bird.deathTime != -1:
for name, param in bird.model.named_parameters():
print(f"Param {name}:")
print(param.detach().cpu().numpy())
break
# Gets the x and y distance from the nearest pipe
closest_pipe_x = 800
closest_pipe_y = 1000
for pipe in pipes:
if -2 < pipe.x < closest_pipe_x:
closest_pipe_x = pipe.x
closest_pipe_y = pipe.y
# Normalizes the distance
closest_pipe_y = (closest_pipe_y - 25) / 260
closest_pipe_x = (closest_pipe_x + 2) / 502
# Runs the model for each bird
if game_started:
for bird in birds:
if bird.forward(closest_pipe_x, closest_pipe_y) < 0.5:
if show_good:
jump_sound.play()
bird.velocity = -JUMP
# Makes sure to automatically start the next round
if evolve:
game_started = True
if all_done and not show_good:
cur_generation += 1
print(f"Generation: {cur_generation}")
run_game(True, select_next_round(birds), gen=cur_generation)
# poll for events
# on space or mouse, jump
keys = pygame.key.get_pressed()
mouse = pygame.mouse.get_pressed()
if keys[pygame.K_SPACE] or mouse[0]:
game_started = True
if all_done:
run_game(show_good=True)
if all_done:
game_started = False
# If "n" is pressed, print out the weights and biases of the surviving birds' NNs
# fill the screen with a color to wipe away anything from last frame
screen.fill("#03c6fc")
# draw background
screen.blit(background, (0, 0))
screen.blit(background, (288, 0))
# draw pipes
for pipe in pipes:
pipe_sprite = pygame.image.load(resource_path("game_assets\\pipe-green.png"))
pipe_sprite = pygame.transform.scale(pipe_sprite, (52, 320))
pipe_sprite = pygame.transform.flip(pipe_sprite, False, True)
screen.blit(pipe_sprite, (pipe.x, pipe.y - 320))
pipe_sprite = pygame.image.load(resource_path("game_assets\\pipe-green.png"))
pipe_sprite = pygame.transform.scale(pipe_sprite, (50, 320))
screen.blit(pipe_sprite, (pipe.x, pipe.y + PIPE_SPACE))
# draw base
base_sprite = pygame.image.load(resource_path("game_assets\\base.png"))
base_sprite = pygame.transform.scale(base_sprite, (336, 112))
screen.blit(base_sprite, (0 - (5 * step / SPEED % 24), GROUND))
screen.blit(base_sprite, (336 - (5 * step / SPEED) % 24, GROUND))
# draw player
for bird in birds:
if bird.deathTime == -1:
if bird.velocity < 0:
player_sprite = pygame.image.load(resource_path("game_assets\\yellowbird-downflap.png"))
player_sprite = pygame.transform.scale(player_sprite, (34, 24))
elif 0 <= bird.velocity < 5:
player_sprite = pygame.image.load(resource_path("game_assets\\yellowbird-midflap.png"))
player_sprite = pygame.transform.scale(player_sprite, (34, 24))
else:
player_sprite = pygame.image.load(resource_path("game_assets\\yellowbird-upflap.png"))
player_sprite = pygame.transform.scale(player_sprite, (34, 24))
rotation = -bird.velocity * 4
if bird.velocity < 3:
rotation = 12
if rotation > 90:
rotation = 90
player_sprite = pygame.transform.rotate(player_sprite, rotation)
else:
player_sprite = pygame.image.load(resource_path("game_assets\\yellowbird-upflap.png"))
player_sprite = pygame.transform.rotate(player_sprite, -90)
if not all_done:
bird.x -= 5 / SPEED
if bird.y < GROUND - 24:
if not bird.died and show_good:
die_sound.play()
bird.died = True
bird.y += 3
else:
bird.y = GROUND - 24
if bird.x < 0:
continue
screen.blit(player_sprite, (bird.x - 7, bird.y - 5))
if bird.deathTime == -1 and game_started:
bird.y += bird.velocity / 1000 * 60 * 5
bird.player_box = Rect(bird.x, bird.y, 20, 20)
# temporarily render hit-box
# pygame.draw.rect(screen, "#FF0000", bird.player_box)
if bird.y < 0 and bird.deathTime == -1:
bird.deathTime = step
bird.y = 0
bird.velocity = 0
if bird.y < GROUND:
bird.velocity += GRAVITY / 1000 * 5
else:
bird.velocity = 0
bird.y = screen.get_height() - 50
if not all_done and game_started:
if step > last_pipe_step:
pipes.append(Pipe())
last_pipe_step = step + SPAWN_RATE
# purge pipes that are off-screen
pipes = [pipe for pipe in pipes if pipe.x > -50]
pipe_boxes = []
for pipe in pipes:
if pipe.x < -2 and not pipe.scored:
pipe.scored = True
score += 1
point_sound.play()
pipe.x -= 5 / SPEED
pipe_boxes.append(Rect(pipe.x, 0, 50, pipe.y))
pipe_boxes.append(
Rect(pipe.x, pipe.y + PIPE_SPACE, 50, screen.get_height() - pipe.y - PIPE_SPACE))
for bird in birds:
for pipe_box in pipe_boxes:
if bird.player_box.colliderect(pipe_box) and bird.deathTime == -1:
bird.deathTime = step
if bird.player_box.y > GROUND and bird.deathTime == -1:
bird.deathTime = step
score_string = str(score)
score_sprites = [pygame.image.load(resource_path(f"game_assets\\{num}.png")) for num in score_string]
score_x = (500 - len(score_string) * 20) / 2
for i, sprite in enumerate(score_sprites):
screen.blit(sprite, (score_x + i * 20, 50))
all_done = True
for bird in birds:
if bird.deathTime == -1:
all_done = False
if all_done:
hit_sound.play()
elif all_done:
# Game over screen
dark_fill = pygame.Surface((500, 500))
dark_fill.set_alpha(32)
dark_fill.fill((0, 0, 0))
screen.blit(dark_fill, (0, 0))
screen.blit(game_over_sprite, (154, 125))
score_string = str(score)
score_sprites = [pygame.image.load(resource_path(f"game_assets\\{num}.png")) for num in score_string]
score_x = (500 - len(score_string) * 20) / 2
for i, sprite in enumerate(score_sprites):
screen.blit(sprite, (score_x + i * 20, 200))
screen.blit(mb0, (186, 350))
screen.blit(spc1, (218, 350))
screen.blit(spc2, (250, 350))
screen.blit(spc3, (282, 350))
else:
# Start screen
start_message = pygame.image.load(resource_path("game_assets\\message.png"))
screen.blit(start_message, (158, 70))
screen.blit(mb0, (186, 350))
screen.blit(spc1, (218, 350))
screen.blit(spc2, (250, 350))
screen.blit(spc3, (282, 350))
pygame.display.flip()
clock.tick()
pygame.quit()
# CHANGE "show_good" TO TRUE TO RUN WITH A PRE-TRAINED MODEL
# AND FALSE TO TRAIN A NEW MODEL
if __name__ == '__main__':
run_game(show_good=False)