Hardware Reference
In-Depth Information
Interesting Things You Can
Do With Data Files
Another interesting way to make computer programs do different things every time
you run them is to store the input data in a text file and have the computer read that
file when it starts. You can then create any number of text files and even have a menu
so you can choose which file to open depending on what you want to do with the pro-
gram. This is called a data-driven program, because it is mostly the data in the external
text file that defines what the program does.
If you think about it, many of the programs you already use on your computer use data
files. The word processor you use to type up your homework stores your homework in
a data file; when you take a photo with a digital camera it is stored in a data file; and
your image editor program reads that photo from the data file. Even Minecraft uses
data files behind the scenes for tasks like saving and loading the world, and for the
texture packs used to build all of the different blocks in the world from. Using data files
with a program is a much more flexible way of working, because it means that the pro-
gram can be used for lots of different things without forcing you, the user, to modify
the program every time you want it to do something slightly different. You can also
share those data files with your friends, if they have the same programs as you.
An early example program that I wrote that demonstrates the use of data files
with Minecraft is a program on my blog, called the Minecraft BMP builder ( http://
blog.whaleygeek.co.uk/minecraft-pi-with-python/) , which reads an image from a
bitmap picture file (BMP file) and builds it block by block inside the Minecraft
world. With it, I built a huge Raspberry Pi logo that is so big it looks a bit foggy
when you look up at it! This program can read any BMP image file and then build
that image out of blocks inside the Minecraft world, without any need to modify
the program.
Your first step in writing a data-driven program is to learn how to use Python to open
and read text files from the computer filing system.
Making a Hint-Giver
To learn how to open and read text files, you are going to write a simple hint-giver pro-
gram. This program will read a file you have prepared of useful Minecraft hints and tips,
and display one of the hints on the Minecraft chat at random intervals. You're going to
need a text file with tips in it, so your first task is to create this file. You can create this
file in the normal Python editor, just like you do with your Python programs.
 
Search WWH ::




Custom Search