Hardware Reference
In-Depth Information
Sensing that a Block
Has.Been.Hit
The last sensing ability that you need in your tool-bag for this adventure, is the ability
to sense when your player hits a block. Block-hit detection will allow you to create
some really exciting games and programs of your own, because it allows your player to
interact directly with each and every block inside the Minecraft world.
Start a new program for this adventure, as it will begin life as a little self-contained
experiment but will later make its way into your final game of this adventure.
1. Create a new file by choosing File New File from the editor window. Click File
Save As and name the new file blockHit.py .
2. Import the necessary modules:
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
3. Connect to the Minecraft game:
mc = minecraft.Minecraft.create()
4. Work out the position of the player and move very slightly to one side. Use this
as the position of the diamond that you are going to create—this is your magic
treasure:
diamond_pos = mc.player.getTilePos()
diamond_pos.x = diamond_pos.x + 1
mc.setBlock(diamond_pos.x, diamond_pos.y, diamond_pos.z,
block.DIAMOND_BLOCK.id)
5. Define a function called checkHit() . You will reuse this function in your final
program, so make sure you name it correctly:
def checkHit():
6. Ask the Minecraft API for a list of events that have happened. This is just a nor-
mal Python list, like the one you used in your vanishingBridge.py program
earlier:
events = mc.events.pollBlockHits()
7. Process each event in turn, using a for loop. See the following Digging into the
Code sidebar for a more detailed explanation of this new form of the for loop:
for e in events:
pos = e.pos
 
 
Search WWH ::




Custom Search