Learn to Program with Minecraft by Craig Richardson

Learn to Program with Minecraft by Craig Richardson

Author:Craig Richardson
Language: eng
Format: mobi, epub
Publisher: No Starch Press, Inc.
Published: 2016-08-28T04:00:00+00:00


block = 57

➋ mc.setBlock(destX, destY, destZ, block)

mc.postToChat("Block set")

while True:

pos = mc.player.getPos()

➌ distance = math.sqrt((pos.x - destX) ** 2 + (pos.z - destZ) ** 2)

➍ if distance > 100:

mc.postToChat("Freezing")

elif distance > 50:

mc.postToChat("Cold")

elif distance > 25:

mc.postToChat("Warm")

elif distance > 12:

mc.postToChat("Boiling")

elif distance > 6:

mc.postToChat("On fire!")

elif distance == 0:

➎ mc.postToChat("Found it")

Listing 7-2: The start of the Hot and Cold program

Before randomly placing a block, the program makes sure that the block won’t be placed underground. To do so, it uses the getHeight() function ➊, which finds the block that is the highest y-coordinate (that is, on the surface) for any position in the game. Then it places a diamond block at a random position ➋.

The code at ➌ calculates the distance to the diamond block. It uses the sqrt() function, which is in the math module—this is why import math is needed at the beginning of the program. The sqrt() function calculates the square root of a number.

NOTE

Listing 7-2 uses a formula called the Pythagorean theorem. The formula uses two sides of a triangle to calculate the length of the third. In this case, I use the distance from the player to the hidden block on the x-axis and the z-axis to calculate the distance to the hidden block in a straight line.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.