-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhub_lights_snake.py
More file actions
151 lines (132 loc) · 4.51 KB
/
Copy pathhub_lights_snake.py
File metadata and controls
151 lines (132 loc) · 4.51 KB
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
from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App
from mindstorms.control import wait_for_seconds, wait_until, Timer
from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to
import math
import random
hub = MSHub()
snake_head = (3, 0)
snake_front_mid = (2, 0)
snake_back_mid = (1, 0)
snake_tail = (0, 0)
last_direction = 'right'
def main():
hub.speaker.beep()
while True:
reset_snake()
begin_snake_movement()
def reset_snake():
global snake_head
global snake_front_mid
global snake_back_mid
global snake_tail
global last_direction
snake_head = (3, 0)
snake_front_mid = (2, 0)
snake_back_mid = (1, 0)
snake_tail = (0, 0)
last_direction = 'right'
def begin_snake_movement():
draw_snake(True)
wait_for_seconds(0.8)
failed_move_attempts = 0
while True:
success = snake_travel(get_random_direction())
if not success:
failed_move_attempts += 1
else:
failed_move_attempts = 0
if failed_move_attempts >= 20:
hub.speaker.beep()
wait_for_seconds(0.2)
hub.speaker.beep()
wait_for_seconds(0.2)
hub.light_matrix.off()
return
def get_random_direction():
random_number = random.randint(0, 3)
direction = 'none'
if (random_number == 0):
direction = 'right'
if (random_number == 1):
direction = 'left'
if (random_number == 2):
direction = 'up'
if (random_number == 3):
direction = 'down'
return direction
def draw_snake(hub_sideways):
hub.light_matrix.off()
draw_snake_section(snake_head, 100, hub_sideways)
draw_snake_section(snake_front_mid, 90, hub_sideways)
draw_snake_section(snake_back_mid, 80, hub_sideways)
draw_snake_section(snake_tail, 70, hub_sideways)
def draw_snake_section(coordinates, brightness, hub_sideways):
start_x = 0
start_y = 0
if hub_sideways:
temp_x = coordinates[1]
start_y = 4 - coordinates[0]
start_x = temp_x
hub.light_matrix.set_pixel(start_x, start_y, brightness)
def snake_travel(direction):
success = try_move_snake(direction)
if not success:
return False
draw_snake(True)
wait_for_seconds(0.8)
return True
def try_move_snake(direction):
global snake_tail
global snake_front_mid
global snake_back_mid
global snake_head
global last_direction
potential_head = snake_head
potential_front_mid = snake_front_mid
potential_back_mid = snake_back_mid
potential_tail = snake_tail
potential_last_direction = last_direction
# don't allow complete reversal of direction
if is_reverse(direction, last_direction):
return False
potential_tail = potential_back_mid
potential_back_mid = potential_front_mid
potential_front_mid = potential_head
if direction == 'right':
potential_head = (potential_head[0] + 1, potential_head[1])
potential_last_direction = 'right'
if direction == 'down':
potential_head = (potential_head[0], potential_head[1] + 1)
potential_last_direction = 'down'
if direction == 'left':
potential_head = (potential_head[0] - 1, potential_head[1])
potential_last_direction = 'left'
if direction == 'up':
potential_head = (potential_head[0], potential_head[1] - 1)
potential_last_direction = 'up'
# don't allow head to move where the tail currently exists
if potential_head == snake_tail:
return False
# avoid exceeding boundaries of LED grid
if is_section_valid(potential_head) and is_section_valid(potential_front_mid) and is_section_valid(potential_back_mid) and is_section_valid(potential_tail):
last_direction = potential_last_direction
snake_head = potential_head
snake_front_mid = potential_front_mid
snake_back_mid = potential_back_mid
snake_tail = potential_tail
return True
return False
def is_section_valid(section):
return section[0] >= 0 and section[0] < 5 and section[1] >= 0 and section[1] < 5
def is_reverse(direction, last_direction):
if direction == 'right' and last_direction == 'left':
return True
if direction == 'left' and last_direction == 'right':
return True
if direction == 'up' and last_direction == 'down':
return True
if direction == 'down' and last_direction == 'up':
return True
return False
# Execute Program
main()