Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions Metro/Metro_RP2350_Snake/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@
# read it into cur_btn_val
cur_btn_val = sys.stdin.read(available)

# change to lower-case before comparison
# so that it's case-insensitive.
cur_btn_val = cur_btn_val.lower()

else: # no keyboard input
# set to None to clear out previous value
cur_btn_val = None
Expand All @@ -173,31 +177,31 @@
# if game is being played
elif CURRENT_STATE == STATE_PLAYING:
# if up button was pressed
if cur_btn_val == KEY_UP:
if cur_btn_val == KEY_UP.lower():
# if the snake is not already moving up or down
if snake.direction not in (snake.DIRECTION_DOWN, snake.DIRECTION_UP):
# change the direction to up
snake.direction = snake.DIRECTION_UP
# if down button was pressed
if cur_btn_val == KEY_DOWN:
if cur_btn_val == KEY_DOWN.lower():
# if the snake is not already moving up or down
if snake.direction not in (snake.DIRECTION_DOWN, snake.DIRECTION_UP):
# change the direction to down
snake.direction = snake.DIRECTION_DOWN
# if right button was pressed
if cur_btn_val == KEY_RIGHT:
if cur_btn_val == KEY_RIGHT.lower():
# if the snake is not already moving left or right
if snake.direction not in (snake.DIRECTION_LEFT, snake.DIRECTION_RIGHT):
# change the direction to right
snake.direction = snake.DIRECTION_RIGHT
# if left button was pressed
if cur_btn_val == KEY_LEFT:
if cur_btn_val == KEY_LEFT.lower():
# if the snake is not already moving left or right
if snake.direction not in (snake.DIRECTION_LEFT, snake.DIRECTION_RIGHT):
# change direction to left
snake.direction = snake.DIRECTION_LEFT
# if the pause button was pressed
if cur_btn_val == KEY_PAUSE:
if cur_btn_val == KEY_PAUSE.lower():
# change the state to paused
CURRENT_STATE = STATE_PAUSED

Expand Down Expand Up @@ -245,19 +249,19 @@
# if the game is paused
elif CURRENT_STATE == STATE_PAUSED:
# if the pause button was pressed
if cur_btn_val == KEY_PAUSE:
if cur_btn_val == KEY_PAUSE.lower():
# change the state to playing so the game resumes
CURRENT_STATE = STATE_PLAYING

# if the current state is game over
elif CURRENT_STATE == STATE_GAME_OVER:
# if the p button is pressed for play again
if cur_btn_val == "p":
if cur_btn_val in {"p", "P"}:
# set next code file to this one
supervisor.set_next_code_file(__file__)
# reload
supervisor.reload()
# if the q button is pressed for exit
if cur_btn_val == "q":
if cur_btn_val in {"q", "Q"}:
# break out of main while True loop.
break