Asks the player for the direction of their next move (or quit). Ensures they enter a valid move: either 'W', 'A', 'S' or 'D'.
()
| 203 | |
| 204 | |
| 205 | def askForPlayerMove(): |
| 206 | """Asks the player for the direction of their next move (or quit). |
| 207 | |
| 208 | Ensures they enter a valid move: either 'W', 'A', 'S' or 'D'.""" |
| 209 | print('Enter move: (WASD or Q to quit)') |
| 210 | while True: # Keep looping until they enter a valid move. |
| 211 | move = input('> ').upper() |
| 212 | if move == 'Q': |
| 213 | # End the program: |
| 214 | print('Thanks for playing!') |
| 215 | sys.exit() |
| 216 | |
| 217 | # Either return the valid move, or loop back and ask again: |
| 218 | if move in ('W', 'A', 'S', 'D'): |
| 219 | return move |
| 220 | else: |
| 221 | print('Enter one of "W", "A", "S", "D", or "Q".') |
| 222 | |
| 223 | |
| 224 | def addTwoToBoard(board): |