-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
176 lines (128 loc) · 6.34 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
import pygame
import random
import time
import math
from tqdm import tqdm
import numpy as np
def display_snake(snake_position, display):
for position in snake_position:
pygame.draw.rect(display, (255, 0, 0), pygame.Rect(position[0], position[1], 10, 10))
def display_apple(apple_position, display):
pygame.draw.rect(display, (0, 255, 0), pygame.Rect(apple_position[0], apple_position[1], 10, 10))
def starting_positions():
snake_start = [100, 100]
snake_position = [[100, 100], [90, 100], [80, 100]]
apple_position = [random.randrange(1, 50) * 10, random.randrange(1, 50) * 10]
score = 3
return snake_start, snake_position, apple_position, score
def apple_distance_from_snake(apple_position, snake_position):
return np.linalg.norm(np.array(apple_position) - np.array(snake_position[0]))
def generate_snake(snake_start, snake_position, apple_position, button_direction, score):
if button_direction == 1:
snake_start[0] += 10
elif button_direction == 0:
snake_start[0] -= 10
elif button_direction == 2:
snake_start[1] += 10
else:
snake_start[1] -= 10
if snake_start == apple_position:
apple_position, score = collision_with_apple(apple_position, score)
snake_position.insert(0, list(snake_start))
else:
snake_position.insert(0, list(snake_start))
snake_position.pop()
return snake_position, apple_position, score
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_start):
if snake_start[0] >= 500 or snake_start[0] < 0 or snake_start[1] >= 500 or snake_start[1] < 0:
return 1
else:
return 0
def collision_with_self(snake_start, snake_position):
# snake_start = snake_position[0]
if snake_start in snake_position[1:]:
return 1
else:
return 0
def blocked_directions(snake_position):
current_direction_vector = np.array(snake_position[0]) - np.array(snake_position[1])
left_direction_vector = np.array([current_direction_vector[1], -current_direction_vector[0]])
right_direction_vector = np.array([-current_direction_vector[1], current_direction_vector[0]])
is_front_blocked = is_direction_blocked(snake_position, current_direction_vector)
is_left_blocked = is_direction_blocked(snake_position, left_direction_vector)
is_right_blocked = is_direction_blocked(snake_position, right_direction_vector)
return current_direction_vector, is_front_blocked, is_left_blocked, is_right_blocked
def is_direction_blocked(snake_position, current_direction_vector):
next_step = snake_position[0] + current_direction_vector
snake_start = snake_position[0]
if collision_with_boundaries(next_step) == 1 or collision_with_self(next_step.tolist(), snake_position) == 1:
return 1
else:
return 0
def generate_random_direction(snake_position, angle_with_apple):
direction = 0
if angle_with_apple > 0:
direction = 1
elif angle_with_apple < 0:
direction = -1
else:
direction = 0
return direction_vector(snake_position, angle_with_apple, direction)
def direction_vector(snake_position, angle_with_apple, direction):
current_direction_vector = np.array(snake_position[0]) - np.array(snake_position[1])
left_direction_vector = np.array([current_direction_vector[1], -current_direction_vector[0]])
right_direction_vector = np.array([-current_direction_vector[1], current_direction_vector[0]])
new_direction = current_direction_vector
if direction == -1:
new_direction = left_direction_vector
if direction == 1:
new_direction = right_direction_vector
button_direction = generate_button_direction(new_direction)
return direction, button_direction
def generate_button_direction(new_direction):
button_direction = 0
if new_direction.tolist() == [10, 0]:
button_direction = 1
elif new_direction.tolist() == [-10, 0]:
button_direction = 0
elif new_direction.tolist() == [0, 10]:
button_direction = 2
else:
button_direction = 3
return button_direction
def angle_with_apple(snake_position, apple_position):
apple_direction_vector = np.array(apple_position) - np.array(snake_position[0])
snake_direction_vector = np.array(snake_position[0]) - np.array(snake_position[1])
norm_of_apple_direction_vector = np.linalg.norm(apple_direction_vector)
norm_of_snake_direction_vector = np.linalg.norm(snake_direction_vector)
if norm_of_apple_direction_vector == 0:
norm_of_apple_direction_vector = 10
if norm_of_snake_direction_vector == 0:
norm_of_snake_direction_vector = 10
apple_direction_vector_normalized = apple_direction_vector / norm_of_apple_direction_vector
snake_direction_vector_normalized = snake_direction_vector / norm_of_snake_direction_vector
angle = math.atan2(
apple_direction_vector_normalized[1] * snake_direction_vector_normalized[0] - apple_direction_vector_normalized[
0] * snake_direction_vector_normalized[1],
apple_direction_vector_normalized[1] * snake_direction_vector_normalized[1] + apple_direction_vector_normalized[
0] * snake_direction_vector_normalized[0]) / math.pi
return angle, snake_direction_vector, apple_direction_vector_normalized, snake_direction_vector_normalized
def play_game(snake_start, snake_position, apple_position, button_direction, score, display, clock):
crashed = False
while crashed is not True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
display.fill((255, 255, 255))
display_apple(apple_position, display)
display_snake(snake_position, display)
snake_position, apple_position, score = generate_snake(snake_start, snake_position, apple_position,
button_direction, score)
pygame.display.set_caption("SCORE: " + str(score))
pygame.display.update()
clock.tick(50000)
return snake_position, apple_position, score