Hardware Reference
In-Depth Information
Writing the.Magic Doormat
LED.Program
The last thing you are going to do with your LED is to link it up to the Minecraft game.
Fortunately, this final step is quite easy and builds on everything you already know
how to do.
1. Save your existing testLED.py program under a different name by choosing
File Save As from the menu and calling it welcomeLED.py .
2. At the top of the file, add the imports for the Minecraft modules you need, and
connect to the Minecraft game:
import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
3. Below this, set some constants that will be the coordinates of your welcome
home mat. You need to decide where you want the mat to appear in your world,
and choose these constants accordingly. On the Raspberry Pi you can see your
coordinates in the top left of the Minecraft window. On PC/Mac you can press
F3 to find out the coordinates of your player:
HOME_X = 0
HOME_Y = 0
HOME_Z = 0
4. Below these constants, create a block out of wool at the mat home location. This
is your doormat—just like the doormat you created in Adventure 2:
mc.setBlock(HOME_X, HOME_Y, HOME_Z, block.WOOL.id, 15)
5. Change your game loop so it now looks like this. Take special care over the
indents. This code repeatedly reads the player position and geo-fences it to see if
you are on the doormat. If you are, it flashes the LED to let you know you are
home:
try:
while True:
pos = mc.player.getTilePos()
if pos.x == HOME_X and pos.z == HOME_Z:
flash(0.5)
finally:
GPIO.cleanup()
Run the program using the instructions in the section on running a GPIO program
earlier in this adventure, and make sure that when you stand on your doormat, the
LED flashes to welcome you home!
 
 
Search WWH ::




Custom Search