|
157 | 157 | # read it into cur_btn_val
|
158 | 158 | cur_btn_val = sys.stdin.read(available)
|
159 | 159 |
|
| 160 | + # change to lower-case before comparison |
| 161 | + # so that it's case-insensitive. |
| 162 | + cur_btn_val = cur_btn_val.lower() |
| 163 | + |
160 | 164 | else: # no keyboard input
|
161 | 165 | # set to None to clear out previous value
|
162 | 166 | cur_btn_val = None
|
|
173 | 177 | # if game is being played
|
174 | 178 | elif CURRENT_STATE == STATE_PLAYING:
|
175 | 179 | # if up button was pressed
|
176 |
| - if cur_btn_val == KEY_UP: |
| 180 | + if cur_btn_val == KEY_UP.lower(): |
177 | 181 | # if the snake is not already moving up or down
|
178 | 182 | if snake.direction not in (snake.DIRECTION_DOWN, snake.DIRECTION_UP):
|
179 | 183 | # change the direction to up
|
180 | 184 | snake.direction = snake.DIRECTION_UP
|
181 | 185 | # if down button was pressed
|
182 |
| - if cur_btn_val == KEY_DOWN: |
| 186 | + if cur_btn_val == KEY_DOWN.lower(): |
183 | 187 | # if the snake is not already moving up or down
|
184 | 188 | if snake.direction not in (snake.DIRECTION_DOWN, snake.DIRECTION_UP):
|
185 | 189 | # change the direction to down
|
186 | 190 | snake.direction = snake.DIRECTION_DOWN
|
187 | 191 | # if right button was pressed
|
188 |
| - if cur_btn_val == KEY_RIGHT: |
| 192 | + if cur_btn_val == KEY_RIGHT.lower(): |
189 | 193 | # if the snake is not already moving left or right
|
190 | 194 | if snake.direction not in (snake.DIRECTION_LEFT, snake.DIRECTION_RIGHT):
|
191 | 195 | # change the direction to right
|
192 | 196 | snake.direction = snake.DIRECTION_RIGHT
|
193 | 197 | # if left button was pressed
|
194 |
| - if cur_btn_val == KEY_LEFT: |
| 198 | + if cur_btn_val == KEY_LEFT.lower(): |
195 | 199 | # if the snake is not already moving left or right
|
196 | 200 | if snake.direction not in (snake.DIRECTION_LEFT, snake.DIRECTION_RIGHT):
|
197 | 201 | # change direction to left
|
198 | 202 | snake.direction = snake.DIRECTION_LEFT
|
199 | 203 | # if the pause button was pressed
|
200 |
| - if cur_btn_val == KEY_PAUSE: |
| 204 | + if cur_btn_val == KEY_PAUSE.lower(): |
201 | 205 | # change the state to paused
|
202 | 206 | CURRENT_STATE = STATE_PAUSED
|
203 | 207 |
|
|
245 | 249 | # if the game is paused
|
246 | 250 | elif CURRENT_STATE == STATE_PAUSED:
|
247 | 251 | # if the pause button was pressed
|
248 |
| - if cur_btn_val == KEY_PAUSE: |
| 252 | + if cur_btn_val == KEY_PAUSE.lower(): |
249 | 253 | # change the state to playing so the game resumes
|
250 | 254 | CURRENT_STATE = STATE_PLAYING
|
251 | 255 |
|
252 | 256 | # if the current state is game over
|
253 | 257 | elif CURRENT_STATE == STATE_GAME_OVER:
|
254 | 258 | # if the p button is pressed for play again
|
255 |
| - if cur_btn_val == "p": |
| 259 | + if cur_btn_val in {"p", "P"}: |
256 | 260 | # set next code file to this one
|
257 | 261 | supervisor.set_next_code_file(__file__)
|
258 | 262 | # reload
|
259 | 263 | supervisor.reload()
|
260 | 264 | # if the q button is pressed for exit
|
261 |
| - if cur_btn_val == "q": |
| 265 | + if cur_btn_val in {"q", "Q"}: |
262 | 266 | # break out of main while True loop.
|
263 | 267 | break
|
0 commit comments