Hardware Reference
In-Depth Information
Using postToChat to.Change Where
Your Position Displays
It's unusual, but in the whereAmI.py program, you play the game in the Minecraft
window but your position appears on the Python Shell window. You can easily fix that
by using a technique you have used already, in Adventure 1: postToChat ! Here's how:
1. Change the print() statement to mc.postToChat() , like this:
print("x="+str(pos.x) +" y="+str(pos.y) +" z="+str(pos.z))
mc.postToChat("x="+str(pos.x) +" y="+str(pos.y) +
" z="+str(pos.z))
2. Click File Save to save your program, and run it again by choosing Run Run
Module from the editor menu.
Well done! Your player position should now be displayed on the Minecraft chat, which
is much more convenient for you as you play the game.
Introducing a Game Loop
Having to run your whereAmI.py program every time you want to find out the player
position is not very helpful. Luckily, you can get round this by modifying your program
to add a game loop. Almost every other program you write in this topic will need a game
loop to make sure that the program continues to run and interact with your Minecraft
gaming. Most games you play will continue to run forever until you close them down,
and this is a useful technique to learn. In computing, this is called an infinite loop ,
because it loops forever.
An infinite loop is a loop that never ends—it goes on and on to infinity. The
only way to break out of an infinite loop is to stop the Python program. You can
stop the program in IDLE by choosing Shell Restart Shell, from the Python Shell
menu or by pressing CTRL then C on the keyboard at the Python Shell.
You can add the game loop by modifying your existing program; the new program will
be very similar, so you will save a bit of time this way. Be sure to save the new file under
a different file name so you don't lose your first program.
1. Start by saving your program as a new file by choosing File Save As from the
editor menu and calling it whereAmI2.py . Make sure you save it in your
MyAdventures folder; otherwise it will not work.
 
Search WWH ::




Custom Search