Game Development Reference
In-Depth Information
locate the next
159. closest sunken treasure chest.
160. Press enter to continue...''')
161. input()
162. print()
This is the rest of the instructions in one multi-line string. After the player presses Enter,
the function returns. These are all of the functions we will define for our game. The rest of
the program is the main part of our game.
How the Code Works: Lines 165 to 217
Now that we are done writing all of the functions our game will need, let's start the main
part of the program.
The Start of the Game
165. print('S O N A R !')
166. print()
167. print('Would you like to view the instructions? (yes/no)')
168. if input().lower().startswith('y'):
169. showInstructions()
The expression input().lower().startswith('y') asks the player if they want
to see the instructions, and evaluates to True if the player typed in a string that began with
'y' or 'Y' . If so, showInstructions() is called.
171. while True:
172. # game setup
173. sonarDevices = 16
174. theBoard = getNewBoard()
175. theChests = getRandomChests(3)
176. drawBoard(theBoard)
177. previousMoves = []
This while loop is the main game loop. Here are what the variables are for:
Table 13-2: Variables used in the main game loop.
Variable
Description
sonarDevices
The number of sonar devices (and turns) the player has left.
The board data structure we will use for this game. getNewBoard
() will set us up with a fresh board.
theBoard
The list of chest data structures. getRandomChests() will return
Search WWH ::




Custom Search