In this project, we will write code to place a block of gold in a random location around the Minecraft world. We will then allow our character to move about the world and when he smashes the gold block, we'll end the game and let the user know that the gold has been found.

We'll add additional functionality in our next project as well.

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.

Our code for this project will be pretty simple; in our next project, we'll expand on it and make it more robust. For this project, besides the normal initialization we need to do for all Minecraft projects, we will:

1. Place a gold block in a random location

2. Test whether the player has smashed the gold block we placed

3. When the gold is found/smashed, indicate the status to the user

## Place gold in a random location

Next, we will pick a random block within the game world — and within the distance from our character that we defined above — and change it to gold:

GOLD_DEPTH = 0 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) gold_exists = True

This code first sets the position of the gold to be at the player's current position, and then modifies the location by a random amount for each coordinate (x, y, z). For the x and y coordinates, the new position is a random distance between -3 and 3 (our `ARENA_WIDTH`) from the current player position. `ARENA_WIDTH` sets the maximum distance the gold will be hidden from our character. In this project, we don't have any way of indicating where the gold is, so we don't want to place it too far from us; we'll fix that issue in the next project.

For the z position (the height of the gold), we place the gold at ground level — our code must determine the elevation of the particular block, since the elevation of the gold may be different than the elevation of the block where our character currently is. We use the `getHeight()` function for this. Note that we can change increase the depth of the buried gold by increasing our `GOLD_DEPTH` variable.

Once we've chosen a location for the gold block, we use the `setBlock()` function to change the block at that location from whatever material it currently is to gold.

Finally, we create a variable called `gold_exists` that will be used to track when the gold is found/smashed or not found. We set it to `True` to indicate that the gold has not yet been found. This variable will be needed later for our loop to determine when the gold has been found/smashed.

## Test if the gold block was smashed

Next, we need to write the code in our game loop to continually test whether the gold block has been smashed (we'll do this by actually testing whether it still exists). When it has been smashed (when it ceases to exist), we will exit the loop.

while gold_exists: player_pos = mc.player.getTilePos() gold_exists = block.Block(mc.getBlock(gold_pos)) == block.GOLD_BLOCK time.sleep(0.01)

The first line of the loop gets the player's position; the second line of the loop tests to see if the location where we originally placed the gold block is still a gold block. If it is still a gold block, `gold_exists` continues to be `True`; when the gold block is gone, `gold_exists` switches to `False`

Note that we've changed the loop from continuing forever (`while True`) to continuing only until the gold has been found (which we do by setting `gold_exists` to False).

## When gold is found, let the user know

Once the gold has been found and we've exited the loop, we need to let the user know that he found the gold:

mc.postToChat("You found the gold!") time.sleep(3)

## Full Code

Here is what the full code for this project should look like:

from rstem.mcpi import minecraft, control, block import time from random import randint control.show() mc = minecraft.Minecraft.create() ARENA_WIDTH = 3 GOLD_DEPTH = 0 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) while block.Block(mc.getBlock(gold_pos)) == block.GOLD_BLOCK: player_pos = mc.player.getTilePos() time.sleep(0.01) mc.postToChat("You found the gold!") time.sleep(3)

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've hidden the gold at the very surface — which isn't really hiding it at all! Increase the gold depth to hide the gold below the surface.

3. If you play the game more than once in the same spot, you may find that there is old gold there from a previous game. To prevent this, modify your code so that when the game starts, any old gold is converted to dirt.

4. Create an easier version of the game — instead of one hidden block of gold, hide two (or three or more), and end the game when any of them are found.

5. Create a harder version of the game — instead of one hidden block of gold, hide two (or three or more), and end the game when all of them are found.