-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
330 lines (252 loc) · 11 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
325
326
327
328
329
330
import pygame
import sys
import random
import networkx as nx
from edges import edges
class snake_game:
def __init__(self):
self.w = 600
self.h = 600
self.snake_size = 20
self.fps = 40
self.screen = pygame.display.set_mode([self.w,self.h])
self.clock = pygame.time.Clock()
self.framIter = 0
self.snake = Player([290,290])
self.snakeGroup = pygame.sprite.Group()
self.snakeGroup.add(self.snake)
self.foods = pygame.sprite.Group()
self.graph = nx.Graph()
self.graph.add_edges_from(edges)
def reset(self):
self.snake.rect.center = (290,290)
self.snake.direction = pygame.K_UP
self.snake.length = 0
self.snake.tails = []
self.snake.tailsObject = pygame.sprite.Group()
self.foods = pygame.sprite.Group()
self.screen = pygame.display.set_mode([self.w,self.h])
self.framIter = 0
self.update_screen()
self.generate_food()
def crash(self):
if self.snake.rect.center[0] not in range(0,self.w) or self.snake.rect.center[1] not in range(0,self.w):
return True
elif pygame.sprite.spritecollide(self.snake, self.snake.tailsObject, True):
return True
else: return False
def eat(self):
if pygame.sprite.spritecollide(self.snake, self.foods, True):
self.snake.create_tail()
self.generate_food()
self.snake.length += 1
return True
else: return False
def generate_food(self):
while True:
check = 1
x = random.randint(0,29)*20 + 10
y = random.randint(0,29)*20 + 10
if (x,y) == self.snake.rect.center:
check = 0
for tail in self.snake.tails:
if tail.rect.center == (x,y):
check = 0
if check == 1:
break
food = Food([x,y])
food.image.fill((110, 215, 45))
self.foods.add(food)
self.currentfood = food
return [x,y]
def lock_move(self,old_direction,lock_list):
relativD = ['forward','left','right']
for i in range(3):
if lock_list[i] == 0:
if self.snake.direction == relativ_to_absolute(relativD[i],old_direction):
return True
return False
def one_step(self,direction,lock_list):
done = False
reward = 0
self.framIter += 1
old_direction = self.snake.direction
self.snake.direction = direction
self.snake.run()
if self.crash(): #check crash to waall or tails
reward = -10
done = True
elif self.lock_move(old_direction,lock_list):
reward = -10
# done = True
# print('thats wrong')
# print (self.currentfood.rect.center)
# print('----')
# if 1 not in lock_list:
# print(lock_list)
# print(self.snake.rect.center)
# for tail in self.snake.tails:
# print(tail.rect.center)
else:
done = False
if self.eat(): reward = +10 #check eat foods
return reward, done, self.snake.length
def update_screen(self):
score_font = pygame.font.SysFont("comicsansms", 25)
value = score_font.render("Score: " + str(self.snake.length), True, (255, 255, 102))
self.screen.fill((50, 153, 213))
self.screen.blit(value, [0, 0])
self.snakeGroup.draw(self.screen)
self.foods.draw(self.screen)
self.snake.tailsObject.draw(self.screen)
pygame.display.update()
pygame.display.flip()
def get_next_move_pos(self,direction):
abs_direction = relativ_to_absolute(direction,self.snake.direction)
current_point = self.snake.rect.center
for i in range(2):
if abs_direction == self.snake.move[i]:
current_point = (current_point[0] + self.snake.vx * [-1, 1][i] ,current_point[1])
elif abs_direction == self.snake.move[2:4][i]:
current_point = (current_point[0] ,current_point[1] + self.snake.vy * [-1, 1][i])
return current_point
def check_into_ground(self,point):
if point[0] < 600 and point[0] > 0 and point[1] > 0 and point[1] < 600:
return True
else: return False
def check_path(self):
graph = self.graph.copy()
graph = self.remove_tails_graph(graph)
forward_point = self.get_next_move_pos('forward')
try:
if self.check_into_ground(forward_point):
forward_path = nx.has_path(graph, self.get_graph_node(forward_point), self.get_graph_node(self.currentfood.rect.center))
else: forward_path = False
except : forward_path = False
try:
left_point = self.get_next_move_pos('left')
if self.check_into_ground(left_point):
left_path = nx.has_path(graph, self.get_graph_node(left_point), self.get_graph_node(self.currentfood.rect.center))
else: left_path = False
except : left_path = False
try:
right_point = self.get_next_move_pos('right')
if self.check_into_ground(right_point):
right_path = nx.has_path(graph, self.get_graph_node(right_point), self.get_graph_node(self.currentfood.rect.center))
else: right_path = False
except: right_path = False
del graph
return (forward_path, left_path, right_path )
def get_graph_node(self,point):
node = int(str(point[0]) + str(point[1]))
return node
def remove_tails_graph(self,graph):
nodes = []
for tail in range(self.snake.length-1):
nodes.append(self.get_graph_node(self.snake.tails[tail].rect.center))
if self.check_into_ground(self.snake.rect.center):
nodes.append(self.get_graph_node(self.snake.rect.center))
graph.remove_nodes_from(nodes)
return graph
def relativ_to_absolute(relativeDirect,curent_ABSdirection):
relativD = {'forward':0,'left':1,'right':-1}
pygame_format_ABSdirection = [pygame.K_UP,pygame.K_RIGHT, pygame.K_DOWN, pygame.K_LEFT]
curent_ABSdirection_Index = pygame_format_ABSdirection.index(curent_ABSdirection)
ABS_direction = pygame_format_ABSdirection[(curent_ABSdirection_Index - relativD[relativeDirect])%4]
return ABS_direction
def absolute_to_relative(curentDirect,Directions):
direction_Index = {pygame.K_UP : 0 ,pygame.K_RIGHT : 1 ,pygame.K_DOWN : 2 ,pygame.K_LEFT : 3 }
return (Directions[(direction_Index[curentDirect]%4)], Directions[(direction_Index[curentDirect]-1)%4], Directions[(direction_Index[curentDirect]+1)%4])
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([20, 20])
self.rect = self.image.get_rect()
self.image.fill((155, 0, 155))
self.move = [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN]
self.vx = 20
self.vy = 20
self.rect.center = pos
self.direction = pygame.K_UP
self.length = 0
self.tails = []
self.tailsObject = pygame.sprite.Group()
def run(self):
if self.direction != None:
for i in range(2):
if self.direction == self.move[i]:
self.rect.x += self.vx * [-1, 1][i]
elif self.direction == self.move[2:4][i]:
self.rect.y += self.vy * [-1, 1][i]
self.tail_run()
def tail_run(self):
temp_direction1 = None
temp_direction2 = None
temp_pos1 = None
temp_pos2 = None
firstCheck = 1
for tail in self.tails:
if firstCheck == 1:
temp_pos1 = tail.rect.center
for i in range(2):
if self.direction == self.move[i]:
tail.rect.center = (self.rect.center[0] - 20 * [-1, 1][i],self.rect.center[1])
elif self.direction == self.move[2:4][i]:
tail.rect.center = (self.rect.center[0],self.rect.center[1] - 20 * [-1, 1][i])
temp_direction1 = tail.direction
tail.direction = self.direction
firstCheck = 0
else:
temp_pos2 = tail.rect.center
tail.rect.center = temp_pos1
temp_pos1 = temp_pos2
temp_direction2 = tail.direction
tail.direction = temp_direction1
temp_direction1 = temp_direction2
def create_tail(self):
for i in range(2):
if self.direction == self.move[i]:
pos = (self.rect.center[0] - 20 * [-1, 1][i],self.rect.center[1])
elif self.direction == self.move[2:4][i]:
pos = (self.rect.center[0],self.rect.center[1] - 20 * [-1, 1][i])
tail = Tail(pos)
tail.direction = self.direction
self.tailsObject.add(tail)
self.tails.append(tail)
def get_danger(self):
up_danger = [abs(0 - self.rect.center[1])]
right_danger = [abs(600 - self.rect.center[0])]
down_danger = [abs(600 - self.rect.center[1])]
left_danger = [abs(0 - self.rect.center[0])]
for tail in self.tails:
if tail.rect.center[1] == self.rect.center[1]:
if tail.rect.center[0] > self.rect.center[0]:
right_danger.append(tail.rect.center[0] - self.rect.center[0])
else:
left_danger.append(self.rect.center[0] - tail.rect.center[0])
if tail.rect.center[0] == self.rect.center[0]:
if tail.rect.center[1] > self.rect.center[1]:
down_danger.append(tail.rect.center[1] - self.rect.center[1])
else:
up_danger.append(self.rect.center[1] - tail.rect.center[1])
return min(up_danger), min(right_danger), min(left_danger), min(down_danger)
class Tail(pygame.sprite.Sprite):
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([20, 20])
self.rect = self.image.get_rect()
self.rect.center = pos
self.image.fill((255, 100, 255))
self.direction = None
self.move = [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN]
self.vx = 20
self.vy = 20
class Food(pygame.sprite.Sprite):
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([20, 20])
self.rect = self.image.get_rect()
self.rect.center = pos
self.image.fill((110, 215, 45))
if __name__ == '__main__':
pass