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:

from rstem.mcpi import minecraft, control, block import time from random import randint *from rstem.sound import Note *from math import log control.show() mc = minecraft.Minecraft.create() *beep = Note('A5') *BEEP_DURATION = 0.05 *ARENA_WIDTH = 10 *GOLD_DEPTH = 2 gold_pos = mc.player.getTilePos() gold_pos.x += randint(-ARENA_WIDTH, ARENA_WIDTH) gold_pos.z += randint(-ARENA_WIDTH, ARENA_WIDTH) gold_pos.y = mc.getHeight(gold_pos.x, gold_pos.z) - GOLD_DEPTH mc.setBlock(gold_pos, block.GOLD_BLOCK) *next_beep_time = time.time() while block.Block(mc.getBlock(gold_pos)) == block.GOLD_BLOCK: player_pos = mc.player.getTilePos() * vector_to_gold = gold_pos - player_pos * vector_to_gold.y = 0 * distance_to_gold = vector_to_gold.length() * if time.time() > next_beep_time: * if distance_to_gold <= 1: * if not beep.is_playing(): * beep.play(duration=None) * else: * beep.play(duration=BEEP_DURATION).wait() * next_beep_time = time.time() + log(distance_to_gold/100 + 1) time.sleep(0.01) *beep.stop() mc.postToChat("You found the gold!") time.sleep(3)

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!

1. 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.

2. 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?

3. 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.