Game Development Reference
In-Depth Information
13 - Sonar Treasure Hunt
The remaining chests were here:
0, 4
Do you want to play again? (yes or no)
no
Sonar's Source Code
Knowing about Cartesian coordinates, number lines, negative numbers, and absolute
values will help us out with our Sonar game. If you do not think you understand these
concepts, go back to chapter 11 to brush up. Below is the source code for the game. Type it
into a new file, then save the file as sonar.py and run it by pressing the F5 key. You do not
need to understand the code to type it in or play the game, the source code will be explained
later.
Also, you can download the source code from the topic's website at the URL
http://inventwithpython.com/chapter13.
sonar.py
This code can be downloaded from http://inventwithpython.com/sonar.py
If you get errors after typing this code in, compare it to the topic's code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al@inventwithpython.com
1. # Sonar
2.
3. import random
4. import sys
5.
6. def drawBoard(board):
7. # Draw the board data structure.
8.
9. hline = ' ' # initial space for the numbers down
the left side of the board
10. for i in range(1, 6):
11. hline += (' ' * 9) + str(i)
12.
13. # print the numbers across the top
14. print(hline)
15. print(' ' + ('0123456789' * 6))
16. print()
17.
18. # print each of the 15 rows
19. for i in range(15):
20. # single-digit numbers need to be padded with an
extra space
21. if i < 10:
22. extraSpace = ' '
23. else:
24. extraSpace = ''
25. print('%s%s %s %s' % (extraSpace, i, getRow
(board, i), i))
26.
Search WWH ::




Custom Search