Hardware Reference
In-Depth Information
If you accidentally use the wrong mouse button and delete your treasure, you
will never be able to hit it! You will either have to re-run the program to create
new treasure, or manually build a block of treasure in the same place before you
can hit it. This is because the block type AIR does not process block-hit events.
CHALLENGE
Modify your blockHit.py program so that it also reads the e.face variable
from the event that it is returned in the for loop. This e.face variable is a
different number depending on which of the block's six faces has been hit. Start
by displaying e.face on the Minecraft chat and then hitting each face to work
out which numbers are used for which faces. Finally, modify your program to
display a different message for each of the six faces of your diamond block.
DIGGING INTO THE CODE
In the block-hit detection program, you used a for loop, but you used a style of
it that you have not used before. Let's look a little bit into this extra magic, as it
is quite a powerful feature of Python.
The normal for loop you have used before looks like this:
for i in range(6):
print(i)
range(6) is actually a function that generates a list of numbers
[0,1,2,3,4,5] . You can prove this by typing the following at the Python Shell:
print(range(6))
You will see the following:
[0, 1, 2, 3, 4, 5]
That looks a bit familiar, doesn't it? Actually, all the range() function does is to
generate a new list of numbers.
The for/in statement in Python will loop through all items in a list, storing the
first item in the loop control variable ( i in the previous example), then running
the loop body, then storing the next item in the loop control variable, and doing
this until the list is exhausted.
continued
Search WWH ::




Custom Search