Hardware Reference
In-Depth Information
Save and run your program, and check that when your player hits the treasure it disap-
pears. You should also find that when you hit the treasure and it disappears, a new
piece of treasure is created at a random position close to your player.
Adding a Homing Beacon
The homing beacon will display the score and the approximate distance to the treasure
every second on the Minecraft chat. Here is how to add this.
1.
Create a
timer
variable. As the main game loop will eventually run 10 times
per second, you will have to count 10 loops for every second. This
timer
will
help you to do that. If you change the speed of the game loop, you will have to
adjust this
TIMEOUT
value as well. Make sure you put this code just above the
homingBeacon()
function (note, there is no indent at all here):
TIMEOUT = 10
timer = TIMEOUT
2.
Remove the
print()
from inside the
homingBeacon()
function and list the
timer
as a global variable, as this function will want to change its value:
def homingBeacon():
global timer
3.
Treasure will be present in the sky if the
treasure_x
variable has a value in it.
You have to check here if treasure has been created, otherwise you will get hom-
ing beacon messages on the Minecraft chat when there is no treasure to find:
if treasure_x != None:
4.
This function will be called 10 times per second from the game loop, so you only
want to update the homing beacon every 10 times:
timer = timer - 1
if timer == 0:
timer = TIMEOUT
5.
When the
timer
times out (every 10 calls to this function, or once every sec-
ond), calculate a rough number that tells you how far away from the treasure you
are. he
abs()
function will find the absolute value (a positive value) of the dif-
ference between two positions. By adding all the positive differences together,
you get a number that is bigger when you are further away from the treasure,
and smaller when you are nearer to it. Check your indents here, as this code all
belongs to the body of the most recent
if
statement: