Game Development Reference
In-Depth Information
The second place is the break statement on line 205. That statement is executed if the
player has found all the treasure chests before running out of sonar devices. In that case,
sonarDevices would be some value greater than 0 .
Lines 210 to 212 will tell the player they've lost. The for loop on line 213 will go
through the treasure chests remaining in theChests and show their location to the player
so that they know where the treasure chests had been lurking.
Asking the Player to Play Again, and the sys.exit() Function
216. if not playAgain():
217. sys.exit()
Win or lose, we call the playAgain() function to let the player type in whether they
want to keep playing or not. If not, then playAgain() returns False . The not operator
changes this to True , making the if statement's condition True and the sys.exit()
function is executed. This will cause the program to terminate.
Otherwise, execution jumps back to the beginning of the while loop on line 171.
Summary: Review of our Sonar Game
Remember how our Tic Tac Toe game numbered the spaces on the Tic Tac Toe board 1
through 9? This sort of coordinate system might have been okay for a board with less than
ten spaces. But the Sonar board has nine hundred spaces! The Cartesian coordinate system
we learned in the last chapter really makes all these spaces manageable, especially when
our game needs to find the distance between two points on the board.
Game boards in games that use a Cartesian coordinate system are often stored in a list of
lists so that the first index is the x-coordinate and the second index is the y-coordinate. This
make accessing a coordinates look like board[x][y] .
These data structures (such as the ones used for the ocean and locations of the treasure
chests) make it possible to have complicated concepts represented as data in our program,
and our game programs become mostly about modifying these data structures.
In the next chapter, we will be representing letters as numbers using their ASCII
numbers. (This is the same ASCII term we used in "ASCII art" previously.) By
representing text as numbers, we can perform mathematically operations on them which
will encrypt or decrypt secret messages.
Search WWH ::




Custom Search