-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_opencv_snake.py
455 lines (422 loc) · 17.7 KB
/
test_opencv_snake.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import numpy as np
import cv2
import random
import time
def collision_with_apple(apple_position, score):
apple_position = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score += 1
return apple_position, score
def collision_with_boundaries(snake_head):
if snake_head[0]>=500 or snake_head[0]<0 or snake_head[1]>=500 or snake_head[1]<0 :
return 1
else:
return 0
def collision_with_self(snake_position):
snake_head = snake_position[0]
if snake_head in snake_position[1:]:
return 1
else:
return 0
# def sight_vector():
img = np.zeros((500,500,3),dtype='uint8')
# Initial Snake and Apple position
snake_position = [[250,250],[240,250],[230,250]]
snake_vision = [[260,250]]
apple_position = [random.randrange(1,50)*10,random.randrange(1,50)*10]
obstacle_pink = [[150, 200],[160, 200],[170, 200],[180, 200],[190, 200],[200, 200]]
apple = cv2.imread('apple_image.jpg')
score = 0
prev_button_direction = 1
button_direction = 1
snake_head = [250,250]
# Energy & Cost
initial_energy = 300
frame_cost = 0
key_pressed = []
while True:
initial_energy -=5
if key_pressed !=[]:
key_pressed.clear()
cv2.imshow('a', img)
cv2.waitKey(1)
img = np.zeros((500, 500, 3), dtype='uint8')
# Display Apple
cv2.rectangle(img, (apple_position[0], apple_position[1]), (apple_position[0] + 10, apple_position[1] + 10),
(0, 0, 255), 3)
# Display Snake
for position in snake_position:
cv2.rectangle(img, (position[0], position[1]), (position[0] + 10, position[1] + 10), (0, 255, 0), 3)
# Takes step after fixed time
t_end = time.time() + 0.2
k = -1
while time.time() < t_end:
if k == -1:
k = cv2.waitKey(125)
else:
continue
# 0-Left, 1-Right, 3-Up, 2-Down, 4-Up_Right, 5-Up_Left, 6-Down_Right, 7-Down-Left, q-Break
# a-Left, d-Right, w-Up, s-Down, e-Up_Right, q-Up_Left, x-Down_Right, z-Down_Left
if k == ord('a') and prev_button_direction != 1:
button_direction = 0
key_pressed.clear()
key_pressed.append('Yes')
elif k == ord('d') and prev_button_direction != 0:
button_direction = 1
key_pressed.clear()
key_pressed.append('Yes')
elif k == ord('w') and prev_button_direction != 2:
button_direction = 3
key_pressed.clear()
key_pressed.append('Yes')
elif k == ord('s') and prev_button_direction != 3:
button_direction = 2
key_pressed.clear()
key_pressed.append('Yes')
elif k == ord('e') and prev_button_direction != 7:
button_direction = 4
key_pressed.clear()
key_pressed.append('Yes')
elif k == ord('q') and prev_button_direction != 6:
button_direction = 5
key_pressed.clear()
key_pressed.append('Yes')
elif k == ord('x') and prev_button_direction != 5:
button_direction = 6
key_pressed.clear()
key_pressed.append('Yes')
elif k == ord('z') and prev_button_direction != 4:
button_direction = 7
key_pressed.clear()
key_pressed.append('Yes')
elif k == ord('c'):
break
else:
button_direction = button_direction
prev_button_direction = button_direction
# For autonomous snake. Change from button to snake move
if button_direction == 0:
head_direction = 'left'
if button_direction == 1:
head_direction = 'right'
if button_direction == 2:
head_direction = 'down'
if button_direction == 3:
head_direction = 'up'
if button_direction == 4:
head_direction = 'up_right'
if button_direction == 5:
head_direction = 'up_left'
if button_direction == 6:
head_direction = 'down_right'
if button_direction == 7:
head_direction = 'down_left'
# Change the head position based on the button direction
if key_pressed !=[]:
if button_direction == 1:
snake_head[0] += 10
elif button_direction == 0:
snake_head[0] -= 10
elif button_direction == 2:
snake_head[1] += 10
elif button_direction == 3:
snake_head[1] -= 10
elif button_direction == 4:
snake_head[0] += 10
snake_head[1] -= 10
elif button_direction == 5:
snake_head[0] -= 10
snake_head[1] -= 10
elif button_direction == 6:
snake_head[0] += 10
snake_head[1] += 10
elif button_direction == 7:
snake_head[0] -= 10
snake_head[1] += 10
# Snake Vision
vision_vector_list = []
longest_distance = 21
#Remove "key_pressed" when the agent moves on its own
if key_pressed != []:
### SEE Right
if head_direction == 'right':
#central line
for i in range(1,longest_distance):
update_x = i*10
new_position = [snake_head[0]+update_x, snake_head[1]]
vision_vector_list.append(new_position)
#under central line
for i in range(1,longest_distance):
#adding 10 more rows
update_y = i*10
update_x = i * 10
for x in range(1,longest_distance):
if x>i:
update_xx = x*10
new_position = [snake_head[0]+update_xx, snake_head[1]+update_y]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
# above central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
new_position = [snake_head[0] + update_xx, snake_head[1] - update_y]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
### SEE Left
if head_direction == 'left':
#central line
for i in range(1,longest_distance):
update_x = i*10
new_position = [snake_head[0]-update_x, snake_head[1]]
vision_vector_list.append(new_position)
#under central line
for i in range(1,longest_distance):
#adding 10 more rows
update_y = i*10
update_x = i * 10
for x in range(1,longest_distance):
if x>i:
update_xx = x*10
new_position = [snake_head[0]-update_xx, snake_head[1]+update_y]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
# above central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
new_position = [snake_head[0] - update_xx, snake_head[1] - update_y]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
### SEE Up
if head_direction == 'up':
#central line
for i in range(1,longest_distance):
update_y = i*10
new_position = [snake_head[0], snake_head[1]-update_y]
vision_vector_list.append(new_position)
#on the left of central line
for i in range(1,longest_distance):
#adding 10 more rows
update_y = i*10
update_x = i * 10
for x in range(1,longest_distance):
if x>i:
update_xx = x*10
update_yy = x*10
new_position = [snake_head[0]-update_x, snake_head[1]-update_yy]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
#on the right of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] + update_x, snake_head[1] - update_yy]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
### SEE Down
if head_direction == 'down':
# central line
for i in range(1, longest_distance):
update_y = i * 10
new_position = [snake_head[0], snake_head[1] + update_y]
vision_vector_list.append(new_position)
# on the left of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] + update_x, snake_head[1] + update_yy]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
# on the right of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] - update_x, snake_head[1] + update_yy]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
### SEE Up Right
if head_direction == 'up_right':
# central line
for i in range(1, longest_distance):
update_y = i * 10
update_x = i*10
new_position = [snake_head[0]+update_x, snake_head[1] - update_y]
vision_vector_list.append(new_position)
# on the left of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] + update_x, snake_head[1] - update_yy]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
# # on the right of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] + update_xx, snake_head[1] - update_y]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
### SEE Up Left
if head_direction == 'up_left':
# central line
for i in range(1, longest_distance):
update_y = i * 10
update_x = i*10
new_position = [snake_head[0]-update_x, snake_head[1] - update_y]
vision_vector_list.append(new_position)
# on the left of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] - update_xx, snake_head[1] - update_y]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
# # # on the right of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] - update_x, snake_head[1] - update_yy]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
### SEE Down Right
if head_direction == 'down_right':
# central line
for i in range(1, longest_distance):
update_y = i * 10
update_x = i*10
new_position = [snake_head[0]+update_x, snake_head[1] + update_y]
vision_vector_list.append(new_position)
# on the left of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] + update_x, snake_head[1] + update_yy]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
# # # on the right of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] + update_xx, snake_head[1] + update_y]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
### SEE Down Left
if head_direction == 'down_left':
# central line
for i in range(1, longest_distance):
update_y = i * 10
update_x = i*10
new_position = [snake_head[0]-update_x, snake_head[1] + update_y]
vision_vector_list.append(new_position)
# on the left of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] - update_xx, snake_head[1] + update_y]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
# # # # on the right of central line
for i in range(1, longest_distance):
# adding 10 more rows
update_y = i * 10
update_x = i * 10
for x in range(1, longest_distance):
if x > i:
update_xx = x * 10
update_yy = x * 10
new_position = [snake_head[0] - update_x, snake_head[1] + update_yy]
if new_position not in vision_vector_list:
vision_vector_list.append(new_position)
# Display Vision
for x in vision_vector_list:
for position in snake_vision:
cv2.rectangle(img, (x[0], x[1]), (x[0], x[1]), (80, 80, 80), 3)
print(vision_vector_list)
# Increase Snake length on eating apple
if snake_head == apple_position:
initial_energy+=300
apple_position, score = collision_with_apple(apple_position, score)
snake_position.insert(0, list(snake_head))
else:
snake_position.insert(0, list(snake_head))
snake_position.pop()
print('Snake -->',head_direction)
print('Snake ',snake_head)
# Check for a collision with the border
if snake_head[0] > 490:
snake_head = [490, snake_head[1]]
if snake_head[0] < 10:
snake_head = [10, snake_head[1]]
if snake_head[1] > 490:
snake_head = [snake_head[0], 490]
if snake_head[1] < 10:
snake_head = [snake_head[0], 10]
print('Energy --> ',initial_energy)
# # On collision kill the snake and print the score
# if collision_with_boundaries(snake_head) == 1 or collision_with_self(snake_position) == 1:
# font = cv2.FONT_HERSHEY_SIMPLEX
# img = np.zeros((500, 500, 3), dtype='uint8')
# cv2.putText(img, 'Your Score is {}'.format(score), (140, 250), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
# cv2.imshow('a', img)
# cv2.waitKey(0)
# cv2.imwrite('D:/downloads/ii.jpg', img)
# break
cv2.destroyAllWindows()