pimoroni-pico/micropython/examples/gfx_pack/snake.py

143 wiersze
2.9 KiB
Python
Czysty Zwykły widok Historia

2022-11-07 12:50:50 +00:00
import time
import random
from gfx_pack import GfxPack, SWITCH_A, SWITCH_B, SWITCH_C, SWITCH_D, SWITCH_E
2022-11-04 10:21:27 +00:00
"""
Basic Snake demo for GFX Pack
Feel free to add your own improvements :)
2022-11-07 12:50:50 +00:00
2022-11-04 10:21:27 +00:00
A = up
B = down
C = reset
D = left
E = right
"""
MOVE_UP = 0
MOVE_DOWN = 1
MOVE_LEFT = 2
MOVE_RIGHT = 3
2022-11-07 12:50:50 +00:00
2022-11-04 10:21:27 +00:00
next_move = MOVE_RIGHT
score = 0
2022-11-07 12:50:50 +00:00
head_position = (30, 30)
segments = [head_position]
2022-11-04 10:21:27 +00:00
ate_apple = False
2022-11-07 12:50:50 +00:00
apple_position = None
2022-11-04 10:21:27 +00:00
gp = GfxPack()
2022-11-07 12:50:50 +00:00
gp.set_backlight(0, 0, 0, 255)
display = gp.display
2022-11-04 10:21:27 +00:00
WIDTH, HEIGHT = display.get_bounds()
def set_new_apple():
2022-11-07 12:50:50 +00:00
global apple_position
apple_position = random.randint(0, WIDTH), random.randint(30, HEIGHT)
2022-11-04 10:21:27 +00:00
def game_over():
2022-11-07 12:50:50 +00:00
global score, segments, head_position, ate_apple
2022-11-04 10:21:27 +00:00
score = 0
2022-11-07 12:50:50 +00:00
head_position = (30, 30)
segments = [head_position]
2022-11-04 10:21:27 +00:00
ate_apple = False
set_new_apple()
pass
def check_button():
global next_move, ate_apple
2022-11-07 12:50:50 +00:00
if gp.switch_pressed(SWITCH_A):
if next_move != MOVE_DOWN:
2022-11-04 10:21:27 +00:00
next_move = MOVE_UP
2022-11-07 12:50:50 +00:00
elif gp.switch_pressed(SWITCH_B):
if next_move != MOVE_UP:
2022-11-04 10:21:27 +00:00
next_move = MOVE_DOWN
2022-11-07 12:50:50 +00:00
elif gp.switch_pressed(SWITCH_C):
if next_move != MOVE_RIGHT:
2022-11-04 10:21:27 +00:00
next_move = MOVE_LEFT
2022-11-07 12:50:50 +00:00
elif gp.switch_pressed(SWITCH_D):
if next_move != MOVE_LEFT:
2022-11-04 10:21:27 +00:00
next_move = MOVE_RIGHT
2022-11-07 12:50:50 +00:00
elif gp.switch_pressed(SWITCH_E):
2022-11-04 10:21:27 +00:00
game_over()
def check_eaten():
2022-11-07 12:50:50 +00:00
global ate_apple, head_position, apple_position, score
if head_position == apple_position:
2022-11-04 10:21:27 +00:00
ate_apple = True
score += 1
set_new_apple()
def check_collision():
for index in range(len(segments) - 1):
2022-11-07 12:50:50 +00:00
if head_position == segments[index]:
2022-11-04 10:21:27 +00:00
game_over()
return
2022-11-07 12:50:50 +00:00
if head_position[0] >= WIDTH:
2022-11-04 10:21:27 +00:00
game_over()
2022-11-07 12:50:50 +00:00
if head_position[0] <= 0:
2022-11-04 10:21:27 +00:00
game_over()
2022-11-07 12:50:50 +00:00
if head_position[1] >= HEIGHT:
2022-11-04 10:21:27 +00:00
game_over()
2022-11-07 12:50:50 +00:00
if head_position[1] <= 20:
2022-11-04 10:21:27 +00:00
game_over()
def move():
2022-11-07 12:50:50 +00:00
global head_position, segments, ate_apple
2022-11-04 10:21:27 +00:00
2022-11-07 12:50:50 +00:00
head_x, head_y = head_position
2022-11-04 10:21:27 +00:00
2022-11-07 12:50:50 +00:00
if next_move == MOVE_UP:
2022-11-04 10:21:27 +00:00
head_y -= 1
2022-11-07 12:50:50 +00:00
elif next_move == MOVE_DOWN:
2022-11-04 10:21:27 +00:00
head_y += 1
2022-11-07 12:50:50 +00:00
elif next_move == MOVE_LEFT:
2022-11-04 10:21:27 +00:00
head_x -= 1
2022-11-07 12:50:50 +00:00
elif next_move == MOVE_RIGHT:
2022-11-04 10:21:27 +00:00
head_x += 1
2022-11-07 12:50:50 +00:00
head_position = (head_x, head_y)
segments.append(head_position)
2022-11-04 10:21:27 +00:00
2022-11-07 12:50:50 +00:00
if ate_apple:
2022-11-04 10:21:27 +00:00
ate_apple = False
else:
segments.pop(0)
def draw():
display.set_pen(0)
display.clear()
display.set_pen(15)
display.text("score: {0}".format(score), 0, 0)
display.line(0, 20, 127, 20)
display.line(0, 63, 127, 63)
display.line(0, 63, 0, 20)
display.line(128, 63, 127, 20)
# Draw apple
2022-11-07 12:50:50 +00:00
display.pixel(apple_position[0], apple_position[1])
2022-11-04 10:21:27 +00:00
# Drawing snake
for segment in segments:
display.pixel(segment[0], segment[1])
display.update()
game_over()
2022-11-07 12:50:50 +00:00
while True:
2022-11-04 10:21:27 +00:00
check_button()
check_eaten()
move()
check_collision()
draw()
2022-11-07 12:50:50 +00:00
2022-11-04 10:21:27 +00:00
time.sleep(0.2)