kopia lustrzana https://github.com/pimoroni/pimoroni-pico
couple of minor fixes
rodzic
678b8d7cf9
commit
b3893d0052
|
@ -3,7 +3,9 @@ import random
|
|||
from gfx_pack import GfxPack
|
||||
|
||||
"""
|
||||
Add something here Gee
|
||||
Bouncing Ball demo
|
||||
10 balls of different shades will be bouncing around your display.
|
||||
Funky...
|
||||
"""
|
||||
|
||||
gp = GfxPack()
|
||||
|
|
|
@ -12,12 +12,12 @@ C = reset
|
|||
D = left
|
||||
E = right
|
||||
"""
|
||||
|
||||
# Constants for next move state
|
||||
MOVE_UP = 0
|
||||
MOVE_DOWN = 1
|
||||
MOVE_LEFT = 2
|
||||
MOVE_RIGHT = 3
|
||||
|
||||
# In-game variables
|
||||
next_move = MOVE_RIGHT
|
||||
score = 0
|
||||
head_position = (30, 30)
|
||||
|
@ -32,11 +32,13 @@ display = gp.display
|
|||
WIDTH, HEIGHT = display.get_bounds()
|
||||
|
||||
|
||||
# Draw the apple in a random possition within the play field
|
||||
def set_new_apple():
|
||||
global apple_position
|
||||
apple_position = random.randint(0, WIDTH), random.randint(30, HEIGHT)
|
||||
|
||||
|
||||
# Reset game to start position and score
|
||||
def game_over():
|
||||
global score, segments, head_position, ate_apple
|
||||
score = 0
|
||||
|
@ -47,6 +49,7 @@ def game_over():
|
|||
pass
|
||||
|
||||
|
||||
# Poll all the butttons to see if anything has been pressed and change next direction accoringly
|
||||
def check_button():
|
||||
global next_move, ate_apple
|
||||
if gp.switch_pressed(SWITCH_A):
|
||||
|
@ -55,16 +58,17 @@ def check_button():
|
|||
elif gp.switch_pressed(SWITCH_B):
|
||||
if next_move != MOVE_UP:
|
||||
next_move = MOVE_DOWN
|
||||
elif gp.switch_pressed(SWITCH_C):
|
||||
elif gp.switch_pressed(SWITCH_D):
|
||||
if next_move != MOVE_RIGHT:
|
||||
next_move = MOVE_LEFT
|
||||
elif gp.switch_pressed(SWITCH_D):
|
||||
elif gp.switch_pressed(SWITCH_E):
|
||||
if next_move != MOVE_LEFT:
|
||||
next_move = MOVE_RIGHT
|
||||
elif gp.switch_pressed(SWITCH_E):
|
||||
elif gp.switch_pressed(SWITCH_C):
|
||||
game_over()
|
||||
|
||||
|
||||
# If the snake head and apple are on the same pixel the apple has been eaten
|
||||
def check_eaten():
|
||||
global ate_apple, head_position, apple_position, score
|
||||
if head_position == apple_position:
|
||||
|
@ -74,10 +78,12 @@ def check_eaten():
|
|||
|
||||
|
||||
def check_collision():
|
||||
# Check if the head or any of the tail segments are on the same pixel
|
||||
for index in range(len(segments) - 1):
|
||||
if head_position == segments[index]:
|
||||
game_over()
|
||||
return
|
||||
# Check the snake head has not gone beyond the play area
|
||||
if head_position[0] >= WIDTH:
|
||||
game_over()
|
||||
if head_position[0] <= 0:
|
||||
|
@ -103,8 +109,10 @@ def move():
|
|||
head_x += 1
|
||||
|
||||
head_position = (head_x, head_y)
|
||||
# Add head to body segments
|
||||
segments.append(head_position)
|
||||
|
||||
# I there is no apple remove end of the tail otherwise tail grows by 1
|
||||
if ate_apple:
|
||||
ate_apple = False
|
||||
else:
|
||||
|
@ -115,6 +123,7 @@ def draw():
|
|||
display.set_pen(0)
|
||||
display.clear()
|
||||
display.set_pen(15)
|
||||
# Draw play field including score
|
||||
display.text("score: {0}".format(score), 0, 0)
|
||||
display.line(0, 20, 127, 20)
|
||||
display.line(0, 63, 127, 63)
|
||||
|
@ -130,9 +139,11 @@ def draw():
|
|||
display.update()
|
||||
|
||||
|
||||
# Make sure game is reset to begin with
|
||||
game_over()
|
||||
|
||||
while True:
|
||||
# Game logic
|
||||
check_button()
|
||||
check_eaten()
|
||||
move()
|
||||
|
|
|
@ -25,9 +25,8 @@ gp.set_backlight(0, 0, 0) # turn the RGB backlight off
|
|||
display = gp.display
|
||||
display.set_backlight(0.4) # set the white to a low value
|
||||
|
||||
bmp = BreakoutBME68X(gp.i2c)
|
||||
# If this gives an error, try the alternative address
|
||||
# bmp = BreakoutBME68X(gp.i2c, 0x77)
|
||||
if use_bme68x_breakout:
|
||||
bmp = BreakoutBME68X(gp.i2c)
|
||||
|
||||
display.set_pen(0)
|
||||
display.clear()
|
||||
|
|
|
@ -13,7 +13,7 @@ SWITCH_E = 4
|
|||
class GfxPack:
|
||||
I2C_SDA_PIN = 4
|
||||
I2C_SCL_PIN = 5
|
||||
SWITCh_PINS = (12, 13, 14, 15, 22)
|
||||
SWITCH_PINS = (12, 13, 14, 15, 22)
|
||||
LED_R_PIN = 6
|
||||
LED_G_PIN = 7
|
||||
LED_B_PIN = 8
|
||||
|
@ -37,9 +37,9 @@ class GfxPack:
|
|||
def switch_pressed(self, switch):
|
||||
if switch < 0 or switch >= self.NUM_SWITCHES:
|
||||
raise ValueError("switch out of range. Expected SWITCH_A (0), SWITCH_B (1), SWITCH_C (2), SWITCH_D (3), or SWITCH_E (4)")
|
||||
return not self.__switches[switch].value()
|
||||
return self.__switches[switch].is_pressed
|
||||
|
||||
def set_backlight(self, r, g, b, w=None):
|
||||
self.__rgb.set_rgb(r, g, b)
|
||||
if w is not None:
|
||||
self.display.set_backlight(w)
|
||||
self.display.set_backlight(w / 255)
|
||||
|
|
Ładowanie…
Reference in New Issue