Game Development Reference
In-Depth Information
13 - Sonar Treasure Hunt
move, and keep asking until they type in the coordinates
correctly.
enterPlayerMove()
Place a sonar device on the game board, and update the
board data structure then return a string that describes
what happened.
makeMove()
Ask the player if they want to play another game of
Sonar.
playAgain()
Print out instructions for the game.
showInstructions()
These might not be all of the functions we need, but a list like this is a good idea to help
you get started with programming your own games. For example, when we are writing the
drawBoard() function in the Sonar game, we figure out that we also need a getRow()
function. Writing out a function once and then calling it twice is preferable to writing out
the code twice. The whole point of functions is to reduce duplicate code down to one place,
so if we ever need to make changes to that code we only need to change one place in our
program.
How the Code Works: Lines 1 to 38
1. # Sonar
2.
3. import random
4. import sys
Here we import two modules, random and sys . The sys module contains the exit()
function, which causes the program to immediately terminate. We will call this function
later in our program.
Drawing the Game Board
6. def drawBoard(board):
The back tick (`) and tilde (~) characters are located next to the 1 key on your keyboard.
They resemble the waves of the ocean. Somewhere in this ocean are three treasure chests,
but you don't know where. You can figure it out by planting sonar devices, and tell the
game program where by typing in the X and Y coordinates (which are printed on the four
sides of the screen.)
The drawBoard() function is the first function we will define for our program. The
sonar game's board is an ASCII-art ocean with coordinates going along the X- and Y-axis,
and looks like this:
 
Search WWH ::




Custom Search