-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.py
268 lines (234 loc) · 8.51 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
import math
import pygame
import random
WIDTH = 500
LENGTH = 500
FRAME = 20
SPEED = 3
RADUIS = 4
CHENGEANGEL = SPEED * 2 * (math.pi / 180)
BACKGROUND = (0, 0, 0)
WIATUNTILHOP = (10 * FRAME, 15 * FRAME)
HOPINGTIME = int(3.5 * FRAME / SPEED)
# history_game = [[0] * WIDTH for i in range(LENGTH)]
# id != 0 or 1 !!!!
from_color_to_id = {(255, 0, 0): 2, (0, 255, 0): 3, None: 4, (0, 0, 255): 5, (255, 255, 255): 6}
from_id_to_color = {2: (255, 0, 0), 3: (0, 255, 0), 4: (0, 0, 0), 5: (0, 0, 255), 6: (255, 255, 255)}
def draw_all(win, lst_of_snakes, history_game):
for s in lst_of_snakes:
pygame.draw.circle(win, s.color, s.int_pos, RADUIS)
for x, row in enumerate(history_game):
for y, p in enumerate(row):
if p != 0 and p != 1:
pygame.draw.circle(win, from_id_to_color[p], (x, y), RADUIS)
def outside(int_pos: tuple) -> bool:
if int_pos[0] < RADUIS or int_pos[0] + RADUIS > 500:
# self.is_dead = True
return True
if int_pos[1] < RADUIS or int_pos[1] + RADUIS > 500:
# self.is_dead = True
return True
return False
def collision_between_two_dots(pos1, pos2):
x1 = pos1[0]
x2 = pos2[0]
y1 = pos1[1]
y2 = pos2[1]
if x1 == x2 or x1 < x2 <= x1 + 1 + RADUIS or x1 - 1 - RADUIS <= x2 < x1:
if y1 == y2 or y1 < y2 <= y1 + 1 + RADUIS or y1 - 1 - RADUIS <= y2 < y1:
return True
return False
class Snake:
def __init__(self, head_x, head_y, direct, color, is_dead=False):
self.position = (head_x, head_y)
self.int_pos = (int(head_x), int(head_y))
self.last = self.int_pos
self.color = color
self.is_dead = is_dead
self.direct = direct
self.is_hop = False
self.when_to_hop = random.choice(WIATUNTILHOP)
self.when_to_stop = HOPINGTIME
self.survival_time = 0
self.id = from_color_to_id[color]
self.place = None
def move(self):
v1 = self.position[0] + SPEED * math.cos(self.direct)
v2 = self.position[1] + SPEED * math.sin(self.direct)
tu = (v1, v2)
self.position = tu
self.int_pos = (round(self.position[0]), round(self.position[1]))
def add(self, history_game):
x = self.int_pos[0]
y = self.int_pos[1]
history_game[x][y] = self.id
for rx in range(2 * RADUIS):
if rx == RADUIS:
continue
if rx > RADUIS:
rx = RADUIS - rx
for ry in range(2 * RADUIS):
if ry == RADUIS:
continue
if ry > RADUIS:
ry = RADUIS - ry
if rx == 0 and ry == 0:
continue
if 0 <= self.int_pos[0] + rx < 500 and 0 <= self.int_pos[1] + ry < 500:
if history_game[x + rx][y + ry] == 0:
history_game[x + rx][y + ry] = 1
return history_game
def collision(self, history_game):
for rx in range(2 * RADUIS):
if rx > RADUIS:
rx = RADUIS - rx
for ry in range(2 * RADUIS):
if ry > RADUIS:
ry = RADUIS - ry
if 0 <= self.int_pos[0] + rx < 500 and 0 <= self.int_pos[1] + ry < 500:
if history_game[self.int_pos[0] + rx][self.int_pos[1] + ry]:
if not collision_between_two_dots((self.int_pos[0] + rx, self.int_pos[1] + ry), self.last):
self.is_dead = True
return True
return False
def outside(self):
if self.int_pos[0] < RADUIS or self.int_pos[0] + RADUIS > 500:
self.is_dead = True
return True
if self.int_pos[1] < RADUIS or self.int_pos[1] + RADUIS > 500:
self.is_dead = True
return True
return False
def crete_input_pixel_around(self, history_game, win=None) -> list:
"""
:param history_game:
:param win: a debugging tool
:return:
"""
around = 1
snake = self
x = snake.int_pos[0]
y = snake.int_pos[1]
lst = []
num = 16
for _ in range(around):
for ry in range(2 * RADUIS + 1):
if ry > RADUIS:
ry = RADUIS - ry
# add from left
if outside((x + RADUIS + num, y + ry)):
lst.append(1)
else:
lst.append(history_game[x + RADUIS + num][y + ry])
# add from right
if outside((x - RADUIS - num, y + ry)):
lst.append(1)
else:
lst.append(history_game[x - RADUIS - num][y + ry])
if win is not None:
pygame.draw.circle(win, (0, 0, 255), (x + RADUIS + num, y + ry), 1)
pygame.draw.circle(win, (0, 0, 255), (x - RADUIS - num, y + ry), 1)
for rx in range(2 * RADUIS):
if rx == RADUIS:
continue
if rx > RADUIS:
rx = RADUIS - rx
# add from top
if outside((x + rx, y + RADUIS + num)):
lst.append(1)
else:
lst.append(history_game[x + rx][y + RADUIS + num])
# add from bottom
if outside((x + rx, y - RADUIS - num)):
lst.append(1)
else:
lst.append(history_game[x + rx][y - RADUIS - num])
if win is not None:
pygame.draw.circle(win, (0, 0, 255), (x + rx, y + RADUIS + num), 1)
pygame.draw.circle(win, (0, 0, 255), (x + rx, y - RADUIS - num), 1)
lst.append(snake.direct)
return lst
def crete_input_nearest_well(self, history_game, win=None) -> list:
"""
find first well from 8 directs
:param history_game:
:param win: a debugging tool
:return:
"""
nums_of_angels = 8
lst = []
snake = self
for i in range(nums_of_angels):
angle = (360 / nums_of_angels) * i
x = snake.int_pos[0] + (RADUIS * lst_of_moves[i][0])
y = snake.int_pos[1] + (RADUIS * lst_of_moves[i][1])
times = 0
run = True
while run and times < 10:
times += 1
for rx in range(2 * RADUIS):
if rx > RADUIS:
rx = RADUIS - rx
for ry in range(RADUIS):
if outside((x + rx, y + (ry * lst_of_moves[i][1]))):
run = False
break
if run and history_game[x + rx][y + (ry * lst_of_moves[i][1])]:
run = False
break
if run:
x += lst_of_moves[i][0]
y += lst_of_moves[i][1]
if win is not None:
pygame.draw.circle(win, (255, 255, 255), (x - lst_of_moves[i][0], y - lst_of_moves[i][1]), 2)
if run:
lst.append(500)
else:
temp_dis = math.sqrt(((snake.int_pos[0] + lst_of_moves[i][0] - x) ** 2) +
(snake.int_pos[1] + lst_of_moves[i][1] - y) ** 2)
lst.append(temp_dis)
lst.append(snake.direct)
return lst
def change_angle(self, symbol: int) -> None:
# symbol = 0/1/2
if symbol == 2:
pass
elif symbol == 1:
self.direct += CHENGEANGEL
else:
self.direct -= CHENGEANGEL
def crete_list_angle() -> list:
""" value = (move_x, move_y) """
nums_of_angels = 8
lst = []
for i in range(nums_of_angels):
angle = (360 / nums_of_angels) * i
# move 4????
num = RADUIS * 1
if angle == 0:
move_x = num
move_y = 0
elif angle == 90:
move_x = 0
move_y = num
elif angle == 180:
move_x = -num
move_y = 0
elif angle == 270:
move_x = 0
move_y = -num
elif angle < 90:
move_x = num
move_y = num
elif angle < 180:
move_x = -num
move_y = num
elif angle < 270:
move_x = -num
move_y = -num
else:
move_x = num
move_y = -num
lst.append((move_x, move_y))
return lst
lst_of_moves = crete_list_angle()