-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.py
More file actions
124 lines (103 loc) · 4.57 KB
/
Snake.py
File metadata and controls
124 lines (103 loc) · 4.57 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
import random
import threading
import time
import Animations
import Direction
import FrameCollection2D as Frames
import Game
import api
class Snake(Game.CubeGame, threading.Thread):
_name = 'Snake'
_version = 'v0.1'
_menu_frame = [0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 0, 0]
def __init__(self, cube_size, frame_size):
Game.CubeGame.__init__(self, cube_size, frame_size, self._name)
threading.Thread.__init__(self)
self.an = None
self.snake_loc = [[0, 2, 0], [0, 1, 0], [0, 0, 0]]
self.snake_loc = [[0, 2, 0], [0, 1, 0], [0, 0, 0]]
self.snake_length = 3
self.pickup_loc = [0, 7, 1]
self.failed = False
self.score = 0
def get_menu_frame(self):
return self._menu_frame
def has_menu_animation(self):
return True
def start_game(self):
pass
def play_animation(self):
self.an = Animations.TickerAnimation("snake")
self.an.start()
def close_animation(self):
self.an.stop()
def stopped_animation(self):
return self.an.stopped()
def is_animation_alive(self):
return self.an.is_alive()
def done(self):
pass
def run(self):
Direction.direction_p_1.value = int(Direction.Direction.UP)
while not self.failed:
if Direction.direction_p_1.value == 0:
Direction.direction_p_1.value = int(Direction.Direction.UP)
if self.snake_length > len(self.snake_loc):
self.snake_loc.append(self.snake_loc[len(self.snake_loc) - 1])
self.snake_length = len(self.snake_loc)
api.led_off(self.snake_loc[self.snake_length - 1])
for x in range(1, self.snake_length):
self.snake_loc[self.snake_length - x] = self.snake_loc[self.snake_length - x - 1]
if Direction.direction_p_1.value == int(Direction.Direction.FORTH):
y = self.snake_loc[0]
self.snake_loc[0] = [y[0], y[1] + 1, y[2]]
elif Direction.direction_p_1.value == int(Direction.Direction.BACK):
y = self.snake_loc[0]
self.snake_loc[0] = [y[0], y[1] - 1, y[2]]
elif Direction.direction_p_1.value == int(Direction.Direction.RIGHT):
y = self.snake_loc[0]
self.snake_loc[0] = [y[0], y[1], y[2] + 1]
elif Direction.direction_p_1.value == int(Direction.Direction.LEFT):
y = self.snake_loc[0]
self.snake_loc[0] = [y[0], y[1], y[2] - 1]
elif Direction.direction_p_1.value == int(Direction.Direction.UP):
y = self.snake_loc[0]
self.snake_loc[0] = [y[0] + 1, y[1], y[2]]
elif Direction.direction_p_1.value == int(Direction.Direction.DOWN):
y = self.snake_loc[0]
self.snake_loc[0] = [y[0] - 1, y[1], y[2]]
for s in self.snake_loc:
api.led_on(s)
api.led_on(self.pickup_loc)
for loc in self.snake_loc:
loc[0] = loc[0] % 8
loc[1] = loc[1] % 8
loc[2] = loc[2] % 8
if self.snake_loc[0] in self.snake_loc[1:]:
self.failed = True
api.change_face(api.Face.LEFT, 0, Frames.number_to_frame(int(self.score / 100)))
api.change_face(api.Face.FRONT, 0, Frames.number_to_frame(int((self.score % 100) / 10)))
api.change_face(api.Face.RIGHT, 0, Frames.number_to_frame(int(self.score % 10)))
# Timer for cooldown
for x in range(0, 8):
api.led_on([0, x, 0], [7, x, 0], [0, x, 7], [7, x, 7])
api.display(api.leds)
time.sleep(1)
if self.snake_loc[0][0] % 8 == self.pickup_loc[0] and self.snake_loc[0][1] % 8 == self.pickup_loc[1] and \
self.snake_loc[0][2] % 8 == self.pickup_loc[2]:
self.snake_length += 1
self.score += 1
found_spawn = False
while not found_spawn:
self.pickup_loc = [random.randint(0, 7), random.randint(0, 7), random.randint(0, 7)]
if self.pickup_loc not in self.snake_loc:
found_spawn = True
api.display(api.leds)
time.sleep(0.2)