Hardware Reference
In-Depth Information
When the for loop finishes, the block friend has reached her target, so set the
target to be the block friend's own current position. This way she won't try to
move again.
The program moves the block friend by clearing her from her previous last posi-
tion (which it does by setting the block to AIR ), updating the block friend's
position to be the next block in the line, then recreating the block friend in that
position.
Note the difference between blockBetween and blocksBetween , the first
contains the block where the friend is, the second contains a list of all blocks
between where the friend started and where she is moving too.
The speed the block friend travels is set by how long the program sleeps between
each block move— time.sleep(0.25) . If this wait is too short, the block will
move too quickly and the player will never be able to get away from the block. If
it's too long, the block will move so slowly it will drive you mad with frustration!
16. Put in a small delay at the end of the loop so the program doesn't overload
Minecraft by constantly looping and asking for the player's position:
time.sleep(0.25)
17. Run the program.
The block friend will appear next to your player and follow him until the pair get too far
away from each other. When that happens, the block friend will come to a stop and
your player will have to go back and stand next to her before she will start following
him again.
You can download the complete code for the block friend program from the Adventures
in Minecraft companion website at www.wiley.com/go/adventuresinminecraft.
DIGGING INTO THE CODE
To calculate the distance between the block and the player, you created a function
called distanceBetweenPoints(point1, point2) . This uses Pythagoras'
theorem to calculate an accurate distance between two points. You may have
investigated how Pythagoras' theorem would work in the challenge in “Adding a
Homing Beacon” in Adventure 4?
 
Search WWH ::




Custom Search