Hardware Reference
In-Depth Information
DIGGING INTO THE CODE
In your new program, by putting all of your setBlocks() statements into the
function house() , there is an extra little bit of magic that is taking place.
The variables x, y, z, and the constant SIZE are all called global variables.
They are global because they are first given a value inside the main program
(the non-indented part). Because they are global, it means they can be used
anywhere in the program, including inside the house() function. So, if you did
some experiments and drew a few different houses, by changing the value of
x, y and z in the main program, because these variables are global and can be
used anywhere in the whole program, this works.
In Adventure  6 you will learn that global variables can make a large program
very hard to fix when it goes wrong, and there is a better way to share informa-
tion with functions. However, for now, this use of global variables is good
enough and your program is small enough that it won't cause a problem.
A global variable is a variable that can be used anywhere in the program. Any
variables (and also constants) defined without a left-hand indent are global, and
can be accessed from anywhere in the program. So, if you use a = 1 and it has
no left-hand indent, that variable can be used anywhere in that program.
However, any variables that are defined with some left-hand indent are not global
(often called local variables). So, if you use a = 1 inside the indented region
under a def statement (that is inside the function), then that variable cannot be
accessed from code anywhere except from within the indented region of that
function. There is more to learn about global variables, but that's all you need to
know about them at this point.
Building a Street of.Houses
with.a.for.Loop
You are now ready to put together all the things you have learned in this chapter, and
build a huge street of houses. If you were building all of these houses manually by
choosing items from the inventory, it might take you hours to build them, and you
might make mistakes in building them or make some of them slightly smaller or larger
by accident. By automating the building of houses and other structures with Minecraft
programming, you can speed things up and make sure that they are perfectly built to a
very precise size!
 
Search WWH ::




Custom Search