/
dino_game.py
373 lines (295 loc) · 11.4 KB
/
dino_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
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
import pygame
import neat
import os
from pygame.locals import *
import random
pygame.init()
#initialising and setting up the font
pygame.font.init()
stat_font = pygame.font.SysFont("couriernew", 30)
#getting all the pictures we need
#Dinosaur
dino_pic = pygame.image.load(os.path.join("data", "dino_.png"))
dino_run1 = pygame.image.load(os.path.join("data", "dino_1.png"))
dino_run2 = pygame.image.load(os.path.join("data", "dino_2.png"))
dino_duck1 = pygame.image.load(os.path.join("data", "dino_ducking1.png"))
dino_duck2 = pygame.image.load(os.path.join("data", "dino_ducking2.png"))
#Bird
bird1 = pygame.image.load(os.path.join("data", "ptera1.png"))
bird2 = pygame.image.load(os.path.join("data", "ptera2.png"))
#Cactus
cactus_pic = pygame.image.load(os.path.join("data", "cacti-small.png"))
cactus_many = pygame.image.load(os.path.join("data", "cacti-big.png"))
#Ground
ground_pic = pygame.image.load(os.path.join("data", "ground.png"))
#Game Features
game_over = pygame.image.load(os.path.join("data", "game_over.png"))
replay = pygame.image.load(os.path.join("data", "replay_button.png"))
speed = 8
class Dino:
def __init__(self):
self.Img = dino_pic
self.WIDTH,self.HEIGHT = 44,48
self.Img = pygame.transform.scale(self.Img,(self.WIDTH,self.HEIGHT))
self.img = self.Img
#starting position of dino
self.x = 60
self.y = 170
self.g = -0.25 #gravity effect
self.vel = 7
self.animation_time = 3
self.img_count = 0
self.t = 0
self.hitbox = pygame.Rect(self.x + 5, self.y, self.WIDTH - 15, self.HEIGHT - 5)
self.runimg1 = pygame.transform.scale(dino_run1, (self.WIDTH,self.HEIGHT))
self.runimg2 = pygame.transform.scale(dino_run2,
(self.WIDTH, self.HEIGHT))
self.duckimg1 = pygame.transform.scale(dino_duck1,
(self.WIDTH + 15, self.HEIGHT))
self.duckimg2 = pygame.transform.scale(dino_duck2,
(self.WIDTH + 15, self.HEIGHT))
self.run_imgs = [self.runimg1,self.runimg2]
self.imgs = [self.img, self.runimg1, self.runimg2]
self.duck_imgs = [self.duckimg1,self.duckimg2]
self.count = 0
self.isJump = False
self.isDuck = False
def jump(self):
self.y -= self.vel
self.isJump = True
def update(self):
if self.y < 170:
self.vel = self.vel + self.g*self.t #v = u+at
self.y -= self.vel
self.t += 0.15
#if we're done jumping, we refresh all variables
if self.y > 170:
self.y = 170 #so that it doesn't go too high up
self.t = 0
self.vel = 7
self.isJump = False
if self.isDuck:
self.hitbox = pygame.Rect(self.x + 5, self.y + 20, self.WIDTH + 12, self.HEIGHT - 20)
self.img = self.duck_imgs[int(self.count) % 2]
self.count += 0.2
elif self.isJump:
self.hitbox = pygame.Rect(self.x + 5, self.y, self.WIDTH - 15, self.HEIGHT - 5)
self.img = self.Img
else:
self.hitbox = pygame.Rect(self.x + 5, self.y, self.WIDTH - 15, self.HEIGHT - 5)
self.img = self.run_imgs[int(self.count) % 2]
self.count += 0.2
def draw(self,win):
win.blit(self.img, (self.x,self.y))
#hitbox check, red rectangle
#pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
class Bird:
def __init__(self,x):
self.x = x
self.WIDTH,self.HEIGHT = 50,40
self.wing_flap = [pygame.transform.scale(bird1,
(self.WIDTH, self.HEIGHT)),pygame.transform.scale(bird2,
(self.WIDTH, self.HEIGHT))]
self.img = None
self.allowed = False
self.passed = False
self.heights = [175,150,107,10,20]
self.y = random.choice(self.heights)
self.speed = 8
self.count = 0
self.indicator = 1
self.hitbox0 = pygame.Rect(self.x, self.y + 10, self.WIDTH, self.HEIGHT - 12)
def set_height(self):
self.y = random.choice(self.heights)
#generate a bird if its allowed then reset
def update(self):
if self.allowed:
self.set_height()
self.allowed = False
self.img = self.wing_flap[int(self.count)%2] #smooth animation
self.count += 0.1
self.x -= self.speed
self.hitbox0 = pygame.Rect(self.x, self.y + 10, self.WIDTH,
self.HEIGHT - 12)
def detect_collide(self, dino):
return self.hitbox0.colliderect(dino.hitbox)
def get_width(self):
return self.WIDTH
def get_height(self):
return (self.HEIGHT-12)
def draw(self,win):
self.img = self.wing_flap[int(self.count) % 2] #keep this
win.blit(self.img, (self.x, self.y))
class Cactus:
gap = 450
def __init__(self,x):
self.x = x
self.width = 45
self.height = 44
self.speed = 8
self.img1 = pygame.transform.scale(cactus_pic,(self.width,self.height))
self.img2 = pygame.transform.scale(cactus_many,(self.width+5,self.height))
self.y = 175
self.count = 0
self.img = None
self.indicator = 0
self.passed = False
self.cactus_choice = random.choice((1,2))
if self.cactus_choice == 1:
self.hitbox0 = pygame.Rect(self.x, self.y, self.width, self.height)
elif self.cactus_choice == 2:
self.hitbox0 = pygame.Rect(self.x, self.y, self.width+5, self.height)
def get_width(self):
if self.cactus_choice == 1:
return self.width
elif self.cactus_choice == 2:
return int(self.width+5)
def get_height(self):
if self.cactus_choice == 1 or self.cactus_choice == 2:
return self.height
def update(self):
if self.cactus_choice == 1 or self.cactus_choice == 2:
self.x -= self.speed
if self.cactus_choice == 1:
self.hitbox0 = pygame.Rect(self.x, self.y, self.width, self.height)
else:
self.hitbox0 = pygame.Rect(self.x, self.y, self.width+5, self.height)
def detect_collide(self, dino):
return self.hitbox0.colliderect(dino.hitbox)
def draw(self, win):
if self.cactus_choice == 1:
win.blit(self.img1, (self.x, self.y))
elif self.cactus_choice == 2:
win.blit(self.img2,(self.x,self.y))
class Ground:
def __init__(self):
self.ground_length = 1202
self.img1 = ground_pic
self.img2 = ground_pic
self.img1_x = 0
self.img1_y = 200
self.img2_x = self.img1_x + self.ground_length
self.img2_y = self.img1_y
self.speed = 8
def draw(self, win):
win.blit(self.img1, (self.img1_x, self.img1_y))
win.blit(self.img2, (self.img2_x, self.img2_y))
def update(self):
self.img1_x -= self.speed
self.img2_x -= self.speed
if self.img1_x + self.ground_length < 0:
self.img1_x = self.img2_x + self.ground_length
elif self.img2_x + self.ground_length < 0:
self.img2_x = self.img1_x + self.ground_length
def draw_window(win, dinos, cacti, ground, score_value):
win.fill((240,240,240))
for cactus in cacti:
cactus.draw(win)
score = stat_font.render("Score: " + str(int(score_value)), True,
(200, 200, 200))
win.blit(score, (680,30))
ground.draw(win)
for dino in dinos:
dino.draw(win)
pygame.display.update()
def main(genomes, config):
nets = []
ge = []
dinos = []
#every genome has its own network
for a, g in genomes:
net = neat.nn.FeedForwardNetwork.create(g,config)
nets.append(net)
dinos.append(Dino())
g.fitness = 0
ge.append(g)
ground = Ground()
cacti = [Cactus(1350)]
win = pygame.display.set_mode((800,230))
clock = pygame.time.Clock()
score_value = 0
gameover = game_over
replay_button = replay
run = True
play = True
dead = False
frame_rate = 60
while play:
if not dead:
draw_window(win,dinos,cacti,ground,score_value)
while run:
clock.tick(frame_rate)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if len(dinos) <= 0:
play = False
break
for x,dino in enumerate(dinos):
dino.update()
ge[x].fitness += 0.1
output = nets[x].activate((8*100, dino.y, cacti[0].y, dino.y-cacti[0].y,
cacti[0].x - dino.x, cacti[0].get_height(),
cacti[0].get_width()))
action = (output.index(max(output)))
if action == 0:
dino.isJump = True
dino.isDuck = False
if dino.isJump == True:
dino.jump()
else:
dino.isDuck = True
dino.isJump = False
if dino.isJump == True:
dino.jump()
add_cactus = False
rem = []
for cactus in cacti:
for x,dino in enumerate(dinos):
if cactus.detect_collide(dino):
ge[x].fitness -= 1
dinos.pop(x)
nets.pop(x)
ge.pop(x)
if not cactus.passed and cactus.x < dino.x + 5:
cactus.passed = True
if len(cacti) <= 1:
add_cactus = True
if cactus.x + cactus.get_width() + 5 < 0:
rem.append(cactus)
cactus.update()
if add_cactus:
score_value += 1
for g in ge:
g.fitness += 5
if score_value > 10:
choice = random.choice((1,2))
else:
choice = 1
if choice == 1:
cacti.append(Cactus(random.randint(500,600)))
else:
cacti.append(Bird(random.randint(500,600)))
for r in rem:
cacti.remove(r)
ground.update()
draw_window(win,dinos,cacti,ground, score_value)
pygame.display.update()
if dead:
run = False
#main()
"""Here, we get information out of the config file
and collect the relevant statistics about the generations to show"""
def run(config_path):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path) # defining all the subheadings we used in our config textfile
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats) # this is for displaying our statistics on the screen
winner = p.run(main, 50) # run main 50 times
if __name__ == "__main__":
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "config.txt")
run(config_path)