Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.py
More file actions
Latest commit
124 lines (103 loc) · 4.57 KB
/
Copy pathSnake.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
importrandom
importthreading
importtime
importAnimations
importDirection
importFrameCollection2DasFrames
importGame
importapi
classSnake(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
defget_menu_frame(self):
returnself._menu_frame
defhas_menu_animation(self):
returnTrue
defstart_game(self):
pass
defplay_animation(self):
self.an=Animations.TickerAnimation("snake")
self.an.start()
defclose_animation(self):
self.an.stop()
defstopped_animation(self):
returnself.an.stopped()
defis_animation_alive(self):
returnself.an.is_alive()
defdone(self):
pass
defrun(self):
Direction.direction_p_1.value=int(Direction.Direction.UP)
whilenotself.failed:
ifDirection.direction_p_1.value==0:
Direction.direction_p_1.value=int(Direction.Direction.UP)
ifself.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])
forxinrange(1, self.snake_length):
self.snake_loc[self.snake_length-x] =self.snake_loc[self.snake_length-x-1]
ifDirection.direction_p_1.value==int(Direction.Direction.FORTH):
y=self.snake_loc[0]
self.snake_loc[0] = [y[0], y[1] +1, y[2]]
elifDirection.direction_p_1.value==int(Direction.Direction.BACK):
y=self.snake_loc[0]
self.snake_loc[0] = [y[0], y[1] -1, y[2]]
elifDirection.direction_p_1.value==int(Direction.Direction.RIGHT):
y=self.snake_loc[0]
self.snake_loc[0] = [y[0], y[1], y[2] +1]
elifDirection.direction_p_1.value==int(Direction.Direction.LEFT):
y=self.snake_loc[0]
self.snake_loc[0] = [y[0], y[1], y[2] -1]
elifDirection.direction_p_1.value==int(Direction.Direction.UP):
y=self.snake_loc[0]
self.snake_loc[0] = [y[0] +1, y[1], y[2]]
elifDirection.direction_p_1.value==int(Direction.Direction.DOWN):
y=self.snake_loc[0]
self.snake_loc[0] = [y[0] -1, y[1], y[2]]
forsinself.snake_loc:
api.led_on(s)
api.led_on(self.pickup_loc)
forlocinself.snake_loc:
loc[0] =loc[0] %8
loc[1] =loc[1] %8
loc[2] =loc[2] %8
ifself.snake_loc[0] inself.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
forxinrange(0, 8):
api.led_on([0, x, 0], [7, x, 0], [0, x, 7], [7, x, 7])
api.display(api.leds)
time.sleep(1)
ifself.snake_loc[0][0] %8==self.pickup_loc[0] andself.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
whilenotfound_spawn:
self.pickup_loc= [random.randint(0, 7), random.randint(0, 7), random.randint(0, 7)]
ifself.pickup_locnotinself.snake_loc:
found_spawn=True
api.display(api.leds)
time.sleep(0.2)