-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path2snake_moves.py
64 lines (57 loc) · 1.92 KB
/
2snake_moves.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 microbit import *
class Snake:
""" This class contains the functions that operate
on our game as well as the state of the game.
It's a handy way to link the two.
"""
def __init__(self):
""" Special function that runs when you create
a "Snake", ie. when you run
game = Snake()
init stands for "Initialisation"
"""
self.current_direction = "up"
# snake is a list of the pixels that the snake is at
self.snake = [[2, 2]]
# food is the co-ords of the current food
self.food = [0, 2]
# whether or not to end the game, used after update
self.end = False
def handle_input(self):
""" We'll use this function to take input from the
user to control which direction the snake is going
in.
"""
pass
def update(self):
""" This function will update the game state
based on the direction the snake is going.
"""
# copy the old head
new_head = list(self.snake[-1])
if self.current_direction == "up":
new_head[1] -= 1
elif self.current_direction == "down":
new_head[1] += 1
elif self.current_direction == "left":
new_head[0] -= 1
elif self.current_direction == "right":
new_head[0] += 1
self.snake.append(new_head)
self.snake = self.snake[1:]
def draw(self):
""" This makes the game appear on the LEDs. """
display.clear()
display.set_pixel(self.food[0], self.food[1], 5)
for part in self.snake:
display.set_pixel(part[0], part[1], 9)
# game is an "instance" of Snake
game = Snake()
# this is called our "game loop" and is where everything
# happens
while True:
game.handle_input()
game.update()
game.draw()
# this makes our micro:bit do nothing for 500ms
sleep(500)