Hardware Reference
In-Depth Information
continued
Secondly, what is f ? f is just another variable, but it stores a file handle. A file
handle is just something to “grab hold of” the file with—it is a kind of virtual
reference to the real file inside your computer's filing system. Whenever you
want to do anything with the open file, use f to refer to it. This is very handy,
because like any other variable you have used before, you can have multiple file
variables to allow multiple files to be open at the same time like this:
f1 = open("config.txt", "r")
f2 = open("levels.txt", "r")
f3 = open("score.txt", "rw")
The variables f1 , f2 and f3 can be used in the rest of your program to correctly
identify which file you want to read from or write to.
Building Mazes from.a Data File
Now that you know how to make your programs read from data files, you are ready to
get a bit more adventurous. You know from earlier adventures that it is easy to place
blocks in the Minecraft world. What if you could build blocks using lists of blocks
stored in a data file? That's exactly what you are going to do here: you are going to build
a complete 3D maze in Minecraft, where the maze data is stored in an external file! But
first, you need to decide how the maze data will be represented inside the file.
To watch a tutorial on how to build and play your maze game, visit the companion
website at www.wiley.com/go/adventuresinminecraft and choose the Adventure 6
Video.
The mazes that you design will be 3D because they are built in the Minecraft 3D world,
but really they are just 2D mazes built out of 3D blocks—there is only one layer to
your maze. This means that the data file needs to store x and z data for each block in
the maze, so it will be rectangular.
You need to decide how blocks themselves are represented in the data file. You will be
using walls and air. To keep things simple, use a 1 to represent a wall and a 0 to repre-
sent air. You can always change the block types inside your Python program that are
used for walls later.
Understanding CSV Files
For this program, you use a special type of text file called a CSV file, to represent your
mazes as files in your computer's filing system.
 
 
Search WWH ::




Custom Search