In this project, we will improve upon the previous project, where we wrote code to place a piece of gold in a random location around the Minecraft world. In this project, we'll add sound to make a true metal detector. As you get closer to the gold, we'll beep the speaker at a faster and faster rate, until your character is right on top of it, at which point we'll continuously play the beep.
Other than ensuring that your speaker is connected, there is no specific hardware setup for this project. You will need to be able to move throughout the Minecraft world to use the code you'll be writing, and to do that, you are welcome to either use a keyboard/mouse or to use the Minecraft controller you built earlier with your CREATOR Kit.
Here is what the code for this project should look like when we add the audio to indicate the user's proximity to the gold. The new code is highlighted with arrows, and we'll discuss that code in more detail below:
The important highlighted changes are:
On Lines 24 through 26, we compute the distance between the gold and
Steve. With vectors, this is easy, because vectors provide a function
length()
that is the length of the vector. However, we only
want to know the distance in the X and Z directions (along the ground), so
we zero out the Y direction.
On Lines 28 through 34, we beep the speaker, if it's time to do so. We
need to determine the next_beep_time
based on the
distance from the gold: if we're right on top of the good, beep forever;
otherwise, we calculate the time to wait using the log()
function. If you haven't seen it before, log()
is a mathematical
function that basically returns the magnitude of a number.
On Line 38, if the beep was playing forever when we ended the game loop, we need to stop it.
Give it a try, and have fun searching for the gold!
Assuming you still have your controller wired up, add this metal detector code to the Minecraft controller code ( project) so that you can play the metal detector game using the CREATOR Kit as a controller.
We used the log()
function to compute the time to the next
beep. Can you modify that calculation to be based on
distance_to_gold
instead (in other words, not use log()
function)?
How does this affect the game?
In the project we challenged you to add more than one hidden gold block to make the game easier. With this version of the game, the gold is hidden and its location is hinted at only by beeping, so it can often seem harder when there is more than one gold block. Can you add a second gold block and end the game if you find either? Note: You'll want to compute the distance to each gold block, and use the shorter distance to calculate the beep frequency.