Game Development Reference
In-Depth Information
13 - Sonar Treasure Hunt
71. smallestDistance = 100 # any chest will be closer than
100.
72. for cx, cy in chests:
73. if abs(cx - x) > abs(cy - y):
74. distance = abs(cx - x)
75. else:
76. distance = abs(cy - y)
77.
78. if distance < smallestDistance: # we want the
closest treasure chest.
79. smallestDistance = distance
80.
81. if smallestDistance == 0:
82. # xy is directly on a treasure chest!
83. chests.remove([x, y])
84. return 'You have found a sunken treasure chest!'
85. else:
86. if smallestDistance < 10:
87. board[x][y] = str(smallestDistance)
88. return 'Treasure detected at a distance of %s
from the sonar device.' % (smallestDistance)
89. else:
90. board[x][y] = 'O'
91. return 'Sonar did not detect anything. All
treasure chests out of range.'
92.
93.
94. def enterPlayerMove():
95. # Let the player type in her move. Return a two-item
list of int xy coordinates.
96. print('Where do you want to drop the next sonar
device? (0-59 0-14) (or type quit)')
97. while True:
98. move = input()
99. if move.lower() == 'quit':
100. print('Thanks for playing!')
101. sys.exit()
102.
103. move = move.split()
104. if len(move) == 2 and move[0].isdigit() and move
[1].isdigit() and isValidMove(int(move[0]), int(move[1])):
105. return [int(move[0]), int(move[1])]
106. print('Enter a number from 0 to 59, a space, then
a number from 0 to 14.')
107.
108.
109. def playAgain():
110. # This function returns True if the player wants to
play again, otherwise it returns False.
111. print('Do you want to play again? (yes or no)')
112. return input().lower().startswith('y')
113.
114.
115. def showInstructions():
Search WWH ::




Custom Search