-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial.py
440 lines (335 loc) · 13.1 KB
/
tutorial.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import simple_greedy_agent
from settings import *
from generator import *
from collisions import *
import cnn_agent as agent_interace
"""
GLOBALS
"""
pygame.init()
settings = get_settings()
agent = None
display = pygame.display.set_mode((settings['display_width'], settings['display_height']))
pygame.display.set_caption('Snake')
action_space = [
pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_UP}) ,
pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_RIGHT}) ,
pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_DOWN}) ,
pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_LEFT}) ,
]
default_font = settings['default_font']
title_font = settings['title_font']
score_font = settings['score_font']
SEG_DIM = settings['block_dim']
snake_head_img = None
snake_body_img = None
hor = None
ver = None
apple_img = None
star_img = None
wall_img = None
hole_img = None
head = None
tail = []
tail_rotations = []
facing = None
opposite = None
direction = None
apple = None
walls = []
tunnels = []
stars = []
score = 0
apples_ate = 0
tunnels_burrowed = 0
stars_collected = 0
"""
GAME SETUP
"""
def init_gfx():
global snake_head_img, snake_body_img, hor, ver, apple_img, star_img, wall_img, hole_img
snake_head_img = pygame.image.load("snake head.png")
snake_head_img = pygame.transform.scale(snake_head_img, (SEG_DIM, SEG_DIM))
snake_body_img = pygame.image.load("snake body.png")
snake_body_img = pygame.transform.scale(snake_body_img, (SEG_DIM, SEG_DIM))
hor = snake_body_img
ver = pygame.transform.rotate(snake_body_img, 90)
apple_img = pygame.image.load("apple.png")
apple_img = pygame.transform.scale(apple_img, (SEG_DIM, SEG_DIM))
star_img = pygame.image.load("star.png")
star_img = pygame.transform.scale(star_img, (SEG_DIM, SEG_DIM))
wall_img = pygame.image.load("wall.png")
wall_img = pygame.transform.scale(wall_img, (SEG_DIM, SEG_DIM))
hole_img = pygame.image.load("hole.png")
hole_img = pygame.transform.scale(hole_img, (SEG_DIM, SEG_DIM))
def init_env():
global facing, direction, opposite, apple
facing = settings['starting_direction']
direction = settings['starting_direction']
opposite = Directions.get_opposite(facing)
if settings['autostart']:
direction = facing
if settings['walls_on']:
generate_walls(walls, tunnels, settings['display_width'], settings['display_width'], settings['display_height'], settings['wall_max_w'], settings['wall_max_h'])
if settings['tunnels_on']:
generate_tunnels(tunnels, walls, settings['tunnel_count'], settings['display_width'], settings['display_height'], SEG_DIM)
collided = True
while collided:
apple = spawn_apple(settings['display_width'], settings['display_height'], SEG_DIM)
collided = False
if any_collisions(apple, walls, tunnels, tail, head, [i['star_rect'] for i in stars]):
collided = True
def init_snake():
global head, orient_points, tail, tail_rotations
head = spawn_snake_seg(settings['starting_x'], settings['starting_y'], SEG_DIM, SEG_DIM)
tail = []
tail_rotations = []
for _ in range(settings['starting_tail']):
move_head(facing)
tail.append(spawn_snake_seg())
def init_agents():
global agent
model = agent_interace.create_model(pygame.surfarray.array3d(display), action_space)
agent = agent_interace.Agent(model, action_space)
def reset_scores():
global score, apples_ate, tunnels_burrowed, stars_collected
score = 0
apples_ate = 0
tunnels_burrowed = 0
stars_collected = 0
def create_game():
init_gfx()
init_env()
init_snake()
init_agents()
"""
GAME MECHANICS
"""
def move_head(direction):
global head, tail, head_x, head_y, facing, opposite, snake_head_img
if direction == opposite:
direction = facing
if facing != direction:
orientate_head(direction)
if direction == Directions.UP:
head.top -= SEG_DIM
facing = Directions.UP
opposite = Directions.get_opposite(direction)
elif direction == Directions.RIGHT:
head.left += SEG_DIM
facing = Directions.RIGHT
opposite = Directions.get_opposite(direction)
elif direction == Directions.DOWN:
head.top += SEG_DIM
facing = Directions.DOWN
opposite = Directions.get_opposite(direction)
elif direction == Directions.LEFT:
head.left -= SEG_DIM
facing = Directions.LEFT
opposite = Directions.get_opposite(direction)
def orientate_head(direction):
global snake_head_img
rotation = facing.value - direction.value
snake_head_img = pygame.transform.rotate(snake_head_img, rotation)
def move_tail(prev_head, prev_direction):
global tail, snake_body_img, tail_rotations
if tail:
new_tail = tail[1:]
new_tail.append(prev_head)
tail = new_tail
new_rotations = tail_rotations[1:]
if prev_direction == Directions.UP or prev_direction == Directions.DOWN:
new_rot = ver
else:
new_rot = hor
new_rotations.append(new_rot)
tail_rotations = new_rotations
def grow(prev_head, last_direction):
global head, tail, snake_body_img, tail_rotations
new_segment = prev_head.copy()
tail.append(new_segment)
if last_direction == Directions.RIGHT or last_direction == Directions.LEFT:
tail_rotations.append(hor)
else:
tail_rotations.append(ver)
def burrow_tunnel(tunnel_ind):
global head, tunnels_burrowed
emerging_tunnel = tunnel_ind
while emerging_tunnel == tunnel_ind:
emerging_tunnel = random.randrange(len(tunnels))
tunnels_burrowed += 1
head = tunnels[emerging_tunnel].copy()
def fade_stars():
for star in stars:
star['countdown'] -= 1
if star['countdown'] == 0:
stars.remove(star)
def get_text_object(text, color, large_font=False):
if large_font:
text_surf = title_font.render(text, True, color)
else:
text_surf = default_font.render(text, True, color)
return text_surf, text_surf.get_rect()
def display_message(message, color, large_font=False, y_displace=0):
text_surf, text_rect = get_text_object(message, color, large_font)
text_rect.center = (settings['display_width'] / 2, settings['display_height'] / 2 + y_displace)
display.blit(text_surf, text_rect)
def render_score():
text = score_font.render("Score: " + str(score) +
" Stars collected: " + str(stars_collected) +
" Apples ate: " + str(apples_ate) +
" Tunnels burrowed: " + str(tunnels_burrowed), True, pygame.Color('green'))
display.blit(text, [0, 0])
def render():
global head, tail, display, wall_img, walls, snake_head_img
display.fill(settings['background_color'])
display.blit(snake_head_img, head)
display.blit(apple_img, apple)
for rotation, seg in enumerate(tail):
display.blit(tail_rotations[rotation], seg)
for wall in walls:
start_x = wall.left
start_y = wall.top
end_x = wall.left + wall.width
end_y = wall.top + wall.height
for i in range(start_x, end_x, SEG_DIM):
for j in range(start_y, end_y, SEG_DIM):
block = pygame.Rect(i, j, SEG_DIM, SEG_DIM)
display.blit(wall_img, block)
for tunnel in tunnels:
display.blit(hole_img, tunnel)
for star in stars:
display.blit(star_img, star['star_rect'])
render_score()
pygame.display.update()
update_agent()
def update_agent():
if settings['agent_hook']:
agent.learn()
def intro_screen():
global display
intro = True
while intro:
display.fill(settings['background_color'])
display_message("Welcome to Snake", pygame.Color('green'), large_font=True, y_displace=-150)
display_message("- Use the arrow buttons to move", pygame.Color('blue'), y_displace=-60)
display_message("- Eat the apples to grow!", pygame.Color('blue'), y_displace=-20)
display_message("- Avoid walls, edges and yourself!", pygame.Color('blue'), y_displace=20)
display_message("- Collect stars for points", pygame.Color('blue'), y_displace=60)
display_message("- Burrow tunnels at your own risk!!!", pygame.Color('blue'), y_displace=100)
display_message(" optional features: walls || tunnels || stars", pygame.Color('purple'), y_displace=170)
display_message("- press ENTER to continue or SPACE to exit game", pygame.Color(random_color()), y_displace=200)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
return done
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
done = False
return done
elif event.key == pygame.K_SPACE:
done = True
return done
"""
GAME LOOP
"""
def game_loop(done=False):
global apple
game_over = False
clock = pygame.time.Clock()
state = pygame.surfarray.array3d(display)
action = None
reward = None
next_state = None
while not done:
state = pygame.surfarray.array3d(display)
if game_over:
if settings['auto_reset']:
create_game()
while game_over:
display.fill(settings['background_color'])
display_message("GAME OVER", pygame.Color('red'), True, -60)
display_message("Press ENTER to play again or SPACE to quit", pygame.Color('black'), False, 50)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
game_over = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
create_game()
game_over = False
elif event.key == pygame.K_SPACE:
game_over = False
done = True
direction = facing
if settings['agent_hook']:
pygame.event.post(agent.act(pygame.surfarray.array3d(display)))
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
direction = Directions.UP
elif event.key == pygame.K_RIGHT:
direction = Directions.RIGHT
elif event.key == pygame.K_DOWN:
direction = Directions.DOWN
elif event.key == pygame.K_LEFT:
direction = Directions.LEFT
action = action_space[event.type]
temp_head = head.copy()
temp_direction = direction
move_head(direction)
if settings['walls_on']:
if has_wall_collided(head, walls):
game_over = True
if has_self_collided(head, tail):
game_over = True
if has_apple_collided(head, apple, score, settings['apple_reward'], apples_ate):
reward = settings['apple_reward']
grow(temp_head, temp_direction)
collided = True
while collided:
apple = spawn_apple(settings['display_width'], settings['display_height'], SEG_DIM)
collided = False
if any_collisions(apple, walls, tunnels, tail, head, [i['star_rect'] for i in stars]):
collided = True
else:
move_tail(temp_head, temp_direction)
if settings['stars_on']:
if has_star_collided(head, stars, score, settings['star_reward'], stars_collected):
reward = settings['star_reward']
if settings['tunnels_on']:
tunnel_ind = has_tunnel_collided(head, tunnels)
if tunnel_ind >= 0:
burrow_tunnel(tunnel_ind)
if head.left < 0 or head.left >= settings['display_width'] or head.top < 0 or head.top >= settings[
'display_height']:
if settings['auto_reset']:
display_message("YOU LOSE... Resetting game", pygame.Color('red'))
create_game()
else:
game_over = True
collided = True
while collided:
star = spawn_star(settings['star_prob'], settings['display_width'], settings['display_height'], SEG_DIM)
if not star:
break
collided = False
if any_collisions(star, head, tail, apple, walls, tunnels):
collided = True
stars.append({'star_rect': star, 'countdown': settings['star_countdown']})
fade_stars()
clock.tick(10)
render()
next_state = pygame.surfarray.array3d(display)
if settings['agent_hook']:
reward = reward if not reward == None else 0
agent.store_memory(state, action, reward, next_state, game_over)
create_game()
done = intro_screen()
game_loop(done)
pygame.quit()
quit()