-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.py
278 lines (205 loc) · 6.21 KB
/
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
import cv2
import numpy as np
import time
import sys
import threading
from pynput import keyboard
from pynput.keyboard import Key
def snake_update():
global arena
global arena_dimensions
global snake
global position
global snake_color
global snake_size
global snake_width
global snake_head_size
global snake_head_color
global score
global snake_has_eaten_food
#itr =1
color_selector=0
gap_length=10
draw_color=(0,0,0)
snake_has_eaten_food=False
game_over=False
while not game_over:
#itr=itr+1
# print(snake)
arena= np.zeros(arena_dimensions,np.uint8) # refreshing arena
#draw a boundary
tail_end=(snake[-1][0],snake[-1][1])
snake_itr=0
for index in range(0,snake_size): # updating the arena
points=snake[index]
i=points[0]
j=points[1]
direct=points[2]
if position[i][j] != -1: # get the break points
direct=position[i][j]
#direction logic
if direct==0:
i=i-1 #left
elif direct==1:
i=i+1 #right
elif direct==2:
j=j-1 #up
elif direct==3:
j=j+1 #down
snake_itr=snake_itr+1
if snake_has_gone_out_of_boundary(i,j):
game_over=True
if snake_itr<snake_head_size:
draw_color=snake_head_color
#print('painting head')
else:
if snake_itr%gap_length==0:
color_selector=1-color_selector
draw_color=snake_color[color_selector]
cv2.rectangle(arena,(i-snake_width,j-snake_width),(i+snake_width,j+snake_width),draw_color,-1)
snake[index]=(i,j,direct)
position[tail_end]=-1 # removing direction constraint from snake's tail end
food_spawner()
obj=snake_has_encountered()
if obj=='food':
#print('food eaten')
snake_has_eaten_food=True
score+=1
sys.stdout.write('Score : {0}\r'.format(score))
sys.stdout.flush()
increase_snake_length()
elif obj=='a_snake':
print('snake bitten')
game_over=True
# snake has eaten a snake. Game over
cv2.imshow("Snake",arena)
if cv2.waitKey(1)==27:
cv2.destroyAllWindows()
break
print('Game Over')
print('Press esc to quit')
def snake_has_gone_out_of_boundary(i,j):
global arena_dimensions
if (i<0 or i>=arena_dimensions[0]) or (j<0 or j>=arena_dimensions[1]):
return True
else:
return False
def increase_snake_length():
global snake
global snake_size
delta_length=10
direct=snake[-1][2]
x_itr=0
y_itr=0
if direct==0:
#snake was moving left
x_itr=1
elif direct==1:
#snake was moving right
x_itr=-1
elif direct==2:
#snake was moving up
y_itr=1
elif direct==3:
#snake was moving down
y_itr=-1
else:
print('Damn! Where did that come from!!')
for i in range(0,delta_length):
snake.append((snake[-1][0]+x_itr,snake[-1][1]+y_itr,direct))
snake_size+=1
def snake_has_encountered():
global snake
global snake_size
global snake_width
global arena
global food_position
global food_radius
#checking for self intersection
snake_head_coor=np.array([snake[0][0],snake[0][1]])
for index in range(snake_width+10,snake_size):
coor=np.array([snake[index][0],snake[index][1]])
distance=np.linalg.norm(snake_head_coor-coor)
if distance<snake_width:
return 'a_snake'
#checking if the snake has eaten food
distance=np.linalg.norm(food_position-snake_head_coor)
if distance<food_radius:
return 'food'
return 'nothing'
def on_press(key):
global snake
global arena
# print(snake[-1])
try:
if key==Key.up and not(snake[0][2] == 3): #if the snake is moving down, it cannot directly move up
position[snake[0][0]][snake[0][1]]=2
#print('move up')
elif key==Key.down and not(snake[0][2] == 2):
position[snake[0][0]][snake[0][1]]=3
#print('move down')
elif key==Key.left and not(snake[0][2] == 1):
position[snake[0][0]][snake[0][1]]=0
#print('move left')
elif key==Key.right and not(snake[0][2] == 0):
position[snake[0][0]][snake[0][1]]=1
#print('move right')
except AttributeError:
print('Hmmm there is some problem.')
def on_release(key):
if key == keyboard.Key.esc:
#stop listner
return False
def snake_intersecting_food(food_position):
global arena
if arena[food_position][0]==0 and arena[food_position][1]==0 and arena[food_position][2]==0 : #food spawned at an empty location
return False #would be better if we checked not just a point but an area around it which is clear
else:
return True
def food_spawner():
import random
global food_position
global snake_has_eaten_food
global arena_dimensions
global arena
global food_color
global food_radius
if snake_has_eaten_food:
food_position=(random.randint(10,arena_dimensions[0]-50),random.randint(10,arena_dimensions[1]-50))
while snake_intersecting_food(food_position):
food_position=(random.randint(10,arena_dimensions[0]-50),random.randint(10,arena_dimensions[1]-50))
snake_has_eaten_food=False
#draw the food
cv2.circle(arena,(food_position[0],food_position[1]), food_radius, food_color, -1)
#print('food spawned at '+str(food_position))
if __name__ =="__main__":
arena_dimensions=(600,600,3)
arena= np.zeros(arena_dimensions,np.uint8) #play area
position= np.zeros((arena_dimensions[0],arena_dimensions[1]),np.int8) #direction pointers
for a in range(0,arena_dimensions[0]-1):
for b in range(0,arena_dimensions[0]-1):
position[a][b]=-1
food_color=(0,0,255)
snake_color=[(255,255,255),(255,0,0)]
snake_size=50
snake_width=3
starting_offset=10
snake_starting=snake_size+starting_offset
snake_head_size=10
snake_head_color=(0,255,0)
score=0
food_position=np.array([200,10])
food_radius=5
snake_has_eaten_food=False
snake=[(snake_starting,starting_offset,1)] # (x,y,direction)
for j in range(snake_starting-1,starting_offset,-1):
snake.append((j,10,1))
# thread to update snake
snake_update_thread=threading.Thread(target=snake_update)
snake_update_thread.start()
with keyboard.Listener(
on_press=on_press,
on_release=on_release
) as listener:
listener.join()
snake_update_thread.join()