Game Development Reference
In-Depth Information
list. The first item will be stored in the x variable and the second will be stored in the y
variable. We then put these two variables into another two-item list, which we store in the
previousMoves list with the append() method. This means previousMoves is a
list of XY coordinates of each move the player makes in this game.
The x and y variables, along with theBoard and theChests (which represent the
current state of the game board) are all sent to the makeMove() function. As we have
already seen, this function will make the necessary modifications to the game board. If
makeMove() returns the value False , then there was a problem with the x and y values
we passed it. The continue statement will send the execution back to the start of the
while loop that began on line 179 to ask the player for XY coordinates again.
Finding a Sunken Treasure Chest
195. else:
196. if moveResult == 'You have found a sunken
treasure chest!':
197. # update all the sonar devices currently
on the map.
198. for x, y in previousMoves:
199. makeMove(theBoard, theChests, x, y)
200. drawBoard(theBoard)
201. print(moveResult)
If makeMove() did not return the value False , it would have returned a string that
tells us what were the results of that move. If this string was 'You have found a
sunken treasure chest!' , then that means we should update all the sonar devices
on the board so they detect the second closest treasure chest on the board. We have the XY
coordinates of all the sonar devices currently on the board stored in previousMoves . So
we can just pass all of these XY coordinates to the makeMove() function again to have it
redraw the values on the board.
We don't have to worry about this call to makeMove() having errors, because we
already know all the XY coordinates in previousMoves are valid. We also know that
this call to makeMove() won't find any new treasure chests, because they would have
already been removed from the board when that move was first made.
The for loop on line 198 also uses the same multiple assignment trick for x and y
because the items in previousMoves list are themselves two-item lists. Because we
don't print anything here, the player doesn't realize we are redoing all of the previous
moves. It just appears that the board has been entirely updated.
Checking if the Player has Won
203. if len(theChests) == 0:
Search WWH ::




Custom Search