Hardware Reference
In-Depth Information
3. Use the random function to place the treasure at a position no more than RANGE
blocks away from the player, but set the y coordinate so that it is somewhere
above the player (which will probably place it in the sky):
treasure_x = random.randint(pos.x, pos.x+RANGE)
treasure_y = random.randint(pos.y+2, pos.y+RANGE)
treasure_z = random.randint(pos.z, pos.z+RANGE)
mc.setBlock(treasure_x, treasure_y, treasure_z,
block.DIAMOND_BLOCK.id)
Run your program and test that a piece of treasure is created up in the sky, near where
your player is standing.
Collecting Treasure when It Is Hit
Now you will use the code from the blockHit.py program with a few small modifica-
tions to detect when the treasure is hit by your player's sword.
1. Remove the print statement from the checkHit() function and replace it
with the code shown here. The score and treasure_x variables have to be
listed as global variables here, because the checkHit() function will change
their values. Python requires you to list inside a function, any global variables
that it changes the value of. If you don't do this, your program will not work:
def checkHit():
global score
global treasure_x
2. Read through any block-hit events and check if the position matches the posi-
tion of your treasure:
events = mc.events.pollBlockHits()
for e in events:
pos = e.pos
if pos.x == treasure_x and pos.y == treasure_y
pos.z == treasure_z:
mc.postToChat("HIT!")
3. Now you are going to tell your program to add points to the score for hitting
the treasure, then delete the treasure so it disappears. Finally, you must remem-
ber to set treasure_x to None (so that placeTreasure() can create a new
random piece of treasure later). Be careful with the indents here, as this code is
part of the body of the if statement:
score = score + 10
mc.setBlock(treasure_x, treasure_y, treasure_z,
block.AIR.id)
treasure_x = None
 
 
Search WWH ::




Custom Search