Hardware Reference
In-Depth Information
Tracking the.Player
When the player has collected all the diamonds and reached the end of the arena, he
has completed the level. If he falls into the river or a hole, he is returned to the start
of the arena. To make these things happen, your program needs to know where the
player is.
After checking where the player is, the program can then either set the levelCom-
plete flag to True if he has collected all the diamonds or set his position back at the
start of the arena.
Your next task is to add the code to the level loop to track the player and either put
him back to the start or complete the level:
1. Indented under the level loop, get the player's position:
#level loop
while not gameOver and not levelComplete:
pos = mc.player.getTilePos()
2. Check to see if the player's height, y, is lower than the height of the arena. If it is,
he must have fallen into the river or a hole, so put him back to the start:
if pos.y < arenaPos.y:
mc.player.setPos(arenaPos.x + 1, arenaPos.y + 1,
arenaPos.z + 1)
3. Check to see if the player has reached the end of the arena and has collected all
the diamonds. This is done by seeing whether the player's z position is the same
as the end of the arena and, if it is, setting the levelComplete flag to True :
if pos.z == arenaPos.z + ARENAZ and diamondsLeft == 0:
levelComplete = True
When the levelComplete flag is set to True , the level loop ends, the dia-
monds are re-created and the game starts again.
4. Run the program. The game should reset when all the diamonds have been col-
lected and the player reaches the end of the area; and, if the player falls into the
river or a hole, he should be returned to the start of the arena.
At the moment the game is playing only one level—the first and easiest one. It
might be wise for you to practice your moves now, because in the next section
the difficulty is going to be cranked up!
 
Search WWH ::




Custom Search