KRITSADA icon

Simple Snake Games

KRITSADA | PRO | 10/28/19 08:56:33 AM UTC | 0 ⭐ | 594 👁️ | Never ⏰ | []
Python |

1.04 KB

|

None

|

0 👍

/

0 👎

from microbit import *
 
left = (-1, 0)
right = (+1, 0)
up = (0, -1)
down = (0, +1)
 
turnLeft = {
    up: left,
    down: right,
    left: down,
    right: up
}
 
turnRight = {
    up: right,
    down: left,
    left: up,
    right: down
}
 
snake_direction = down 
snake = [(1,0), (1,1), (1,2)]
print(snake)
 
frames = 0
last_a = False
last_b = False
 
def move(direction):
    global snake
    snake = snake[1:]
    head = snake[-1]
    new_head = ((head[0]+direction[0])%5, (head[1]+direction[1])%5)
    snake.append(new_head)
    print(snake)
 
while True:
    frames += 1
 
    now_a = button_a.is_pressed()
    now_b = button_b.is_pressed()
 
    if now_a and not last_a:
        snake_direction = turnLeft[snake_direction]
    if now_b and not last_b:
        snake_direction = turnRight[snake_direction]
 
    last_a = now_a
    last_b = now_b
 
    if frames % 10 == 0:
        move(snake_direction)
 
    display.clear()
    for chunk in snake:
        display.set_pixel(chunk[0], chunk[1], 5)
 
    sleep(50)

Comments