Hardware Reference
In-Depth Information
your program and simply type in a number representing the size of the space you want
to clear, and then your program will clear all that space for you. This means you don't
have to keep modifying your program every time you want to clear a different size of
area in the Minecraft world.
There are two main versions of Python—Python 2 and Python 3. The Minecraft
programming interface is written specifically to work with Python 2, and you
use Python 2 for all the adventures in this topic. It is possible to write Minecraft
Python programs in Python 3 but you need an updated mcpi module. If you
see any Python 3 programs on the Internet, they will use input() instead of
raw_input() to read from the keyboard. At the time of writing, the precise
version numbers of the latest versions are 2.7.6 and 3.3.3. There are significant
differences between Python 2 and Python 3, which is why it's important to stick
to using Python 2 for this topic.
Now that you understand what raw_input() does, it's time to add it to your program.
To do this, you only have to make two small modifications to your clearSpace.py
program. Follow these steps:
1. First, use raw_input() to ask the user to type in a number that represents the
size of the space to clear, and store that number inside a variable called size .
Just like in Adventure 2 where you used str() to convert something to a string,
here you use int() to convert the string from raw_input() into a number
that you can perform calculations with. Try this line without the int() to see
what happens! Type in only the new lines that are marked in bold:
pos = mc.player.getTilePos()
size = int(raw_input("size of area to clear? "))
2. Next, modify the setBlocks() line so that instead of using the number 50, it
uses your new variable size :
mc.setBlocks(pos.x, pos.y, pos.z, pos.x+size, pos.y+size,
pos.z+size, block.AIR.id)
3. Save your program and run it by choosing Run Run Module from the menu.
Now move to somewhere in the Minecraft world where there are lots of trees or moun-
tains. Type a number into the Python Shell Window when prompted—perhaps choose
100 or something like that—and press the Enter key on the keyboard. All the trees and
mountains near your player should magically disappear and, as in Figure 3-4, you will
have a nice space to build other structures!
Keep this little utility program, as it will be useful later when you need to clear a bit of
space in the Minecraft world.
 
Search WWH ::




Custom Search