-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
64 lines (55 loc) · 2.22 KB
/
game.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
from sense_hat import SenseHat
from sensimate import SensiMate
from apple import Apple
from snake import Snake
from time import sleep
class Game:
def __init__(self):
self.sense = SenseHat()
self.sense.clear()
self.sense.low_light = True
self.sensimate = SensiMate(self.sense)
self.apple = Apple(self.sense, (200, 0, 0))
self.snake = Snake(self.sense, (0, 133, 0), self.apple, 1)
self.start()
def start(self):
self.animate_start()
self.game_start()
def animate_start(self):
self.sensimate.spiral(0.01, (148, 0, 211))
self.sensimate.spiral(0.01, (75, 0, 130))
self.sensimate.spiral(0.01, (0, 0, 255))
self.sensimate.spiral(0.01, (0, 255, 0))
self.sensimate.spiral(0.01, (255, 255, 0))
self.sensimate.spiral(0.01, (255, 127, 0))
self.sensimate.spiral(0.01, (255, 0, 0))
self.sense.show_message("Snake", scroll_speed=0.05)
self.sensimate.countdown(3)
self.sense.clear()
def game_start(self):
self.snake.boot()
direction = "right"
while True:
if self.snake.nap != -1:
sleep(self.snake.nap)
events = self.sense.stick.get_events()
if len(events) > 0:
newdirection = events[len(events) - 1].direction
if not ((direction == "up" and newdirection == "down")
or (direction == "down" and newdirection == "up")
or (direction == "left" and newdirection == "right")
or (direction == "right" and newdirection == "left")):
direction = newdirection
if direction == "up":
self.snake.up()
elif direction == "down":
self.snake.down()
elif direction == "left":
self.snake.left()
elif direction == "right":
self.snake.right()
else:
self.sense.show_message("You lose!", scroll_speed=0.05)
self.sense.show_message("Score: %d" % len(self.snake.xy), scroll_speed=0.05)
quit()
game = Game()