Skip to content

Commit 05ca129

Browse files
authored
Merge pull request #2993 from FoamyGuy/metro_rp2350_snakegame
Update snake game key inputs
2 parents 1bebd9a + 2baf7aa commit 05ca129

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Metro/Metro_RP2350_Snake/code.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@
157157
# read it into cur_btn_val
158158
cur_btn_val = sys.stdin.read(available)
159159

160+
# change to lower-case before comparison
161+
# so that it's case-insensitive.
162+
cur_btn_val = cur_btn_val.lower()
163+
160164
else: # no keyboard input
161165
# set to None to clear out previous value
162166
cur_btn_val = None
@@ -173,31 +177,31 @@
173177
# if game is being played
174178
elif CURRENT_STATE == STATE_PLAYING:
175179
# if up button was pressed
176-
if cur_btn_val == KEY_UP:
180+
if cur_btn_val == KEY_UP.lower():
177181
# if the snake is not already moving up or down
178182
if snake.direction not in (snake.DIRECTION_DOWN, snake.DIRECTION_UP):
179183
# change the direction to up
180184
snake.direction = snake.DIRECTION_UP
181185
# if down button was pressed
182-
if cur_btn_val == KEY_DOWN:
186+
if cur_btn_val == KEY_DOWN.lower():
183187
# if the snake is not already moving up or down
184188
if snake.direction not in (snake.DIRECTION_DOWN, snake.DIRECTION_UP):
185189
# change the direction to down
186190
snake.direction = snake.DIRECTION_DOWN
187191
# if right button was pressed
188-
if cur_btn_val == KEY_RIGHT:
192+
if cur_btn_val == KEY_RIGHT.lower():
189193
# if the snake is not already moving left or right
190194
if snake.direction not in (snake.DIRECTION_LEFT, snake.DIRECTION_RIGHT):
191195
# change the direction to right
192196
snake.direction = snake.DIRECTION_RIGHT
193197
# if left button was pressed
194-
if cur_btn_val == KEY_LEFT:
198+
if cur_btn_val == KEY_LEFT.lower():
195199
# if the snake is not already moving left or right
196200
if snake.direction not in (snake.DIRECTION_LEFT, snake.DIRECTION_RIGHT):
197201
# change direction to left
198202
snake.direction = snake.DIRECTION_LEFT
199203
# if the pause button was pressed
200-
if cur_btn_val == KEY_PAUSE:
204+
if cur_btn_val == KEY_PAUSE.lower():
201205
# change the state to paused
202206
CURRENT_STATE = STATE_PAUSED
203207

@@ -245,19 +249,19 @@
245249
# if the game is paused
246250
elif CURRENT_STATE == STATE_PAUSED:
247251
# if the pause button was pressed
248-
if cur_btn_val == KEY_PAUSE:
252+
if cur_btn_val == KEY_PAUSE.lower():
249253
# change the state to playing so the game resumes
250254
CURRENT_STATE = STATE_PLAYING
251255

252256
# if the current state is game over
253257
elif CURRENT_STATE == STATE_GAME_OVER:
254258
# if the p button is pressed for play again
255-
if cur_btn_val == "p":
259+
if cur_btn_val in {"p", "P"}:
256260
# set next code file to this one
257261
supervisor.set_next_code_file(__file__)
258262
# reload
259263
supervisor.reload()
260264
# if the q button is pressed for exit
261-
if cur_btn_val == "q":
265+
if cur_btn_val in {"q", "Q"}:
262266
# break out of main while True loop.
263267
break

0 commit comments

Comments
 (0)