-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
324 lines (242 loc) · 10.1 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
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
import pygame
import random
from enum import Enum
from collections import namedtuple
import numpy as np
# initializing pygame
pygame.init()
# setting the font
font = pygame.font.Font('arial.ttf', 25)
#font = pygame.font.SysFont('arial', 25)
class Direction(Enum):
"""Enum direction"""
RIGHT = 1
LEFT = 2
UP = 3
DOWN = 4
class train_stage(Enum):
"""Enum train mode"""
Zeros_1 = 1
Noise_2 = 2
Involving_3 = 3
Both_heads_4 = 4
Frozen_conv_5 = 5
class conv_loading_mode(Enum):
"""Enum convolution loading mode"""
Dense_small_network = 1
Conv_network = 2
# named tuple for points
Point = namedtuple('Point', 'x, y')
# colors
WHITE = (255, 255, 255)
RED = (200,0,0)
BLUE1 = (0, 0, 255)
BLUE2 = (0, 100, 255)
BLACK = (0,0,0)
ORANGE_RED = (255, 69, 0)
Chartreuse_color = (127, 255, 0)
Gold_color = (255, 215, 0)
BLOCK_SIZE = 20
SPEED = 100000000000000
display_distances_to_walls = False
display_direction_to_myself = False
class SnakeGameAI:
"""Snake game AI class"""
def __init__(self, w=640, h=640):
"""Initializer"""
self.w = w
self.h = h
self.myself_to_the_right_of_me = False
self.myself_to_the_left_of_me = False
self.myself_to_the_front_of_me = False
self.front_wall = 0
self.right_wall = 0
self.left_wall = 0
self.last_turn_right = False
self.last_turn_left = False
# initialization of a display
self.display = pygame.display.set_mode((self.w, self.h))
pygame.display.set_caption('Snake deep RL')
# setting the clock
self.clock = pygame.time.Clock()
# reseting the game - start from the beginning
self.reset()
#-------------------------------------------------------------------------------
def reset(self):
"""Reseting the game"""
# initializing game state
# moving into the right direction at the beginning
self.direction = Direction.RIGHT
# the head will be at he center of a screen
self.head = Point(self.w/2, self.h/2)
# !!! a snake is a list of points where the first one is a head !!!
self.snake = [self.head,
Point(self.head.x-BLOCK_SIZE, self.head.y),
Point(self.head.x-(2*BLOCK_SIZE), self.head.y)]
# zeroing score
# score - how long a snake is
# it's not reward
self.score = 0
# we have no food
self.food = None
# placing food initially
self._place_food()
# discarding the frame iteration number
self.frame_iteration_number = 0
# reseting new parameters
self.myself_to_the_right_of_me = False
self.myself_to_the_left_of_me = False
self.myself_to_the_front_of_me = False
self.front_wall = 0
self.right_wall = 0
self.left_wall = 0
self.last_turn_right = False
self.last_turn_left = False
#-------------------------------------------------------------------------------
def _place_food(self):
"""Placing the food"""
# getting random coordinates
x = random.randint(0, (self.w-BLOCK_SIZE )//BLOCK_SIZE )*BLOCK_SIZE
y = random.randint(0, (self.h-BLOCK_SIZE )//BLOCK_SIZE )*BLOCK_SIZE
# !!! food is just a point
self.food = Point(x, y)
# preventing food being inside a snake
if self.food in self.snake:
self._place_food()
#-------------------------------------------------------------------------------
def play_iteration(self, action):
"""Play a single iteration given an action - returns reward"""
# incrementing the iteration number
self.frame_iteration_number += 1
# 1. if we press escape - quit the game
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# 2. move
# calling an internal method for moving
self._move(action)
# inserting the head to the first position
self.snake.insert(0, self.head)
# 3. check if game over
reward = 0
game_over = False
# if we've got a collision or iterate 100 times bigger than the len of snake
if self.is_collision():
game_over = True
reward = -10
return reward, game_over, self.score
elif self.frame_iteration_number > 100*len(self.snake):
game_over = True
reward = -10
return reward, game_over, self.score
# 4. place new food or just move
# if the head of a snake is with the food
if self.head == self.food:
self.score += 1
reward = 10
self._place_food()
else:
# ??? removing last element ???
# moving in such a way ???
self.snake.pop()
# 5. update ui and clock
# updating ui
self._update_ui()
# next tick of clock
self.clock.tick(SPEED)
# 6. return game over and score
return reward, game_over, self.score
#-------------------------------------------------------------------------------
def is_collision(self, pt=None):
"""Collision detection, default poit - head"""
if pt is None:
pt = self.head
# hits boundary
if pt.x > self.w - BLOCK_SIZE or pt.x < 0 or pt.y > self.h - BLOCK_SIZE or pt.y < 0:
return True
# hits itself - it's the commonplace
# it's necessary to differenciate between hitting itself and a wall
if pt in self.snake[1:]:
return True
return False
#-------------------------------------------------------------------------------
def _update_ui(self):
"""Updating ui"""
# return
# filling display black
self.display.fill(BLACK)
# for each element in a snake
for pt in self.snake:
# draw the output rectangle
pygame.draw.rect(self.display, BLUE1, pygame.Rect(pt.x, pt.y, BLOCK_SIZE, BLOCK_SIZE))
# draw the internal rectangle
pygame.draw.rect(self.display, BLUE2, pygame.Rect(pt.x+4, pt.y+4, 12, 12))
# draw the head of a snake
pygame.draw.rect(self.display, RED, pygame.Rect(self.snake[0].x+4, self.snake[0].y+4, 5, 5))
# draw food
pygame.draw.rect(self.display, RED, pygame.Rect(self.food.x, self.food.y, BLOCK_SIZE, BLOCK_SIZE))
# displaying the score
text = font.render("Score: " + str(self.score), True, WHITE)
self.display.blit(text, [0, 0])
if(display_direction_to_myself):
# displaying the presence to the right
if(self.myself_to_the_right_of_me):
text = font.render("Right", True, Chartreuse_color)
self.display.blit(text, [450, 350])
# displaying the presence to the right
if(self.myself_to_the_left_of_me):
text = font.render("Left", True, Chartreuse_color)
self.display.blit(text, [450, 400])
# displaying the presence in front
if(self.myself_to_the_front_of_me):
text = font.render("Front", True, Chartreuse_color)
self.display.blit(text, [450, 450])
# if we should display distnances to the walls
if(display_distances_to_walls):
# displaying the distance to the front wall
text = font.render("forward_wall: " + str(self.front_wall), True, Gold_color)
self.display.blit(text, [10, 350])
# displaying the distance to the right wall
text = font.render("right_wall: " + str(self.right_wall), True, Gold_color)
self.display.blit(text, [10, 400])
# displaying the distance to the left wall
text = font.render("left_wall: " + str(self.left_wall), True, Gold_color)
self.display.blit(text, [10, 450])
if(self.last_turn_right):
text = font.render("Last turn right", True, Gold_color)
self.display.blit(text, [10, 250])
if(self.last_turn_left):
text = font.render("Last turn left", True, Gold_color)
self.display.blit(text, [10, 300])
# udates the contents of the entire display
pygame.display.flip()
#-------------------------------------------------------------------------------
def _move(self, action):
"""Move according to action enum"""
# [straight, right, left]
clock_wise_list = [Direction.RIGHT, Direction.DOWN, Direction.LEFT, Direction.UP]
# getting an index of the direction in a clockwise list
idx = clock_wise_list.index(self.direction)
if np.array_equal(action, [1, 0, 0]):
new_dir = clock_wise_list[idx] # no change
elif np.array_equal(action, [0, 1, 0]):
next_idx = (idx + 1) % 4
new_dir = clock_wise_list[next_idx] # right turn r -> d -> l -> u
else: # [0, 0, 1]
next_idx = (idx - 1) % 4
new_dir = clock_wise_list[next_idx] # left turn r -> u -> l -> d
self.direction = new_dir
x = self.head.x
y = self.head.y
# depending on the direction - increment the position of a head
if self.direction == Direction.RIGHT:
x += BLOCK_SIZE
elif self.direction == Direction.LEFT:
x -= BLOCK_SIZE
elif self.direction == Direction.DOWN:
y += BLOCK_SIZE
elif self.direction == Direction.UP:
y -= BLOCK_SIZE
# setting the position of a head
self.head = Point(x, y)