MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / askForPlayerMove

Function askForPlayerMove

src/gamesbyexample/twentyfortyeight.py:205–221  ·  view source on GitHub ↗

Asks the player for the direction of their next move (or quit). Ensures they enter a valid move: either 'W', 'A', 'S' or 'D'.

()

Source from the content-addressed store, hash-verified

203
204
205def 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
224def addTwoToBoard(board):

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected