Game Development Reference
In-Depth Information
makeMove() will return False .
If the coordinates land directly on the treasure, makeMove() will return the string
'You have found a sunken treasure chest!' . If the XY coordinates are
within a distance of 9 or less of a treasure chest, we return the string 'Treasure
detected at a distance of %s from the sonar device.' (where %s is
the distance). Otherwise, makeMove() will return the string 'Sonar did not
detect anything. All treasure chests out of range.' .
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
Given the XY coordinates of where the player wants to drop the sonar device, and a list
of XY coordinates for the treasure chests (in the chests list of lists), how do we find out
which treasure chest is closest?
An Algorithm for Finding the Closest Treasure Chest
While the x and y variables are just integers (say, 5 and 0 ), together they represent the
location on the game board (which is a Cartesian coordinate system) where the player
guessed. The chests variable may have a value such as [[5, 0], [0, 2], [4,
2]] , that value represents the locations of three treasure chests. Even though these
variables are a bunch of numbers, we can visualize it like this:
Search WWH ::




Custom Search