Hardware Reference
In-Depth Information
Building Magic Bridges
In the previous program, you wrote code that sensed the block directly under your
player and posted a message on the chat. This is an interesting tiny step towards a big-
ger program but let's see if you can now turn this experiment into something real, by
stitching it together with some of the techniques you learned in Adventure 3 with the
setBlock() function.
By making a small change to safeFeet.py , you can turn it into a magic bridge builder
that places a glass bridge under your feet wherever you walk, making sure that your
player never falls into the sea or falls out of the sky! You will reuse this new function in
a later program in this adventure, so make sure you name it correctly.
1. Click File Save As and rename your safeFeet.py program as magic
Bridge.py .
2. Change the name of the safeFeet() function so that it is now called build
Bridge() , and modify the if/else statement as marked in bold by removing
the mc.postToChat() and replacing it with a mc.setBlock() . Every time
your player's feet are unsafe, this builds a glass bridge under him. Be careful of
the long line in the if statement:
def buildBridge():
pos = mc.player.getTilePos()
b = mc.getBlock(pos.x, pos.y-1, pos.z)
if b == block.AIR.id or b == block.WATER_FLOWING.id
or b==block.WATER_STATIONARY.id:
mc.setBlock(pos.x, pos.y-1, pos.z, block.GLASS.id)
3. As you can see in step 2, the else and the mc.postToChat() have been
removed from the code, as they are no longer needed.
4. If you build the bridge too slowly, your player will fall off, so you now need to remove
the time delay from the game loop. Make sure you use the new buildBridge()
function:
while True:
buildBridge()
5. Save your program by clicking File Save from the editor menu.
Run your program, and walk around the world, jumping up into the sky and walking
on water. As your player walks around, whenever his feet are not on safe ground, a
magic glass bridge appears to keep him from falling, as in Figure 4-2. It is now possible
for him to walk on water. It's a miracle!
 
 
Search WWH ::




Custom Search