Hardware Reference
In-Depth Information
8. Create a target for the block friend. This will be the position she will move toward.
Initially, set the target as the block friend's current position, this is so the block
friend doesn't try and move anywhere when the program starts:
target = friend.clone()
To copy positions in Minecraft, you use the clone() function. This is because the
positions returned by the Minecraft API are Python objects and they are different
to normal variables. For example, if you had used the code target = friend
and later changed the friend.x value in the friend object, the target.x
value would be changed in target too.
9. Start an endless loop, so the program will run forever. (Note that all the code
after this point is indented under this loop.)
while True:
10. Get the player's position and calculate the distance between the player and the
block friend using the distanceBetweenPoints() function:
pos = mc.player.getTilePos()
distance = distanceBetweenPoints(pos, friend)
11. Apply the rules you want the block friend to use to work out what to do next. If
it's “happy”, tell it to compare the distance between the friend and the “too far
away” constant. If the distance is less than the “too far away” constant, set the
target to be the position of the player. If it's greater than the “too far away” con-
stant, change the block's mood to “sad” (see Figure 8-2):
if blockMood == "happy":
if distance < TOO_FAR_AWAY:
target = pos.clone()
elif distance >= TOO_FAR_AWAY:
blockMood = "sad"
mc.postToChat("<block> Come back. You are too far
away. I need a hug!")
12. Now you need to tell the program that otherwise (that is, if the distance is not
less than the "too far away" constant), the block friend is "sad" and in that
case to wait until the player is within one block's distance (a hug) before chang-
ing the block's mood to "happy" :
elif blockMood == "sad":
if distance <= 1:
blockMood = "happy"
mc.postToChat("<block> Awww thanks. Let's go.")
 
Search WWH ::




Custom Search