Hardware Reference
In-Depth Information
4. Create a bridge list that is empty. There are no blocks in your bridge at the
moment, so it always starts empty:
bridge = []
5. Define a buildBridge() function that will build the bridge for you. You will
use this buildBridge() function in the final program in this adventure, so
make sure you name this function correctly. Most of the code at the start of this
function is the same as in the magicBridge.py program, so you could copy
that if you want to save some typing time. Be careful to get the indents correct:
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)
6. When you build part of the bridge, you have to remember where you built the
block, so that the block can be removed later on when your player's feet are safely
on the ground. You will use another list to keep the three parts of the coordinate
together, and add this to the bridge list. See the following Digging into the Code
sidebar for a fuller explanation of what is going on here:
coordinate = [pos.x, pos.y-1, pos.z]
bridge.append(coordinate)
7. To make your bridge vanish when your player is no longer standing on it, you need
an else statement to check if he is standing on glass or not. If he is not, your pro-
gram will start deleting blocks from the bridge. The program has to check that there
is still some bridge left, otherwise it will raise an error if you try to pop from an
empty list. The elif is short for “ else if ”, and the != means “not equal to”. Be
careful with the indents here: the elif is indented once, as it is part of the build
Bridge() function; the next if is indented twice as it is part of the elif :
elif b != block.GLASS.id:
if len(bridge) > 0:
8. These next lines are indented three levels, because they are part of the if that is
part of the elif that is part of the buildBridge() function! Phew!
Remember that earlier you appended a list of three coordinates to the bridge
list? Here, you have to index into that list with coordinate[0] for x, coordi
nate[1] for y and coordinate[2] for z. Adding the time.sleep() also
makes the bridge vanish slowly, so that you can see it happening:
coordinate = bridge.pop()
mc.setBlock(coordinate[0], coordinate[1],
coordinate[2], block.AIR.id)
time.sleep(0.25)
 
Search WWH ::




Custom Search