Hardware Reference
In-Depth Information
DIGGING INTO THE CODE
When you created the blocks for the wall, you used two for loops—one inside
the other. This is a really useful prgramming technique known as nesting. You
can also say that the loops are nested. The first for loop (for x in range(0,
ARENAX + 1):) loops once for each block in across the width (x) of the arena.
The second loop (for y in range(1, ARENAY):) does the same, but for
each block from 1 to the height (y) of the arena.
The x and y variables are used to create a ShapeBlock, minecraftstuff.
ShapeBlock(x,y,0,block.BRICK_BLOCK.id) for each block in the wall.
When you use nested for loops and the ARENAX and ARENAY constants, it
means that even if the arena is a different size, the wall will always fill the area.
Running More Than One Obstacle
You now have a problem! The program you created is stuck. It will loop around, moving
the wall up and down forever and is never going to do anything else. This is because the
program you have written is sequential , meaning that each command in your pro-
gram is run in turn, and the next command will wait for the previous one to finish
before running. Your program is stuck, because it never gets to the next command
after the while loop.
Sequential means to follow a sequence or order, usually one thing after the
other.
How can you create more obstacles and run the rest of the game if the program is stuck
and won't do anything other than move the wall up and down?
There is a solution. Meet multi-threading! Until now, all the programs you have writ-
ten in these adventures have been single-threaded; in other words, they have been
sequential, with one command running after the other.
If you imagine your program as a piece of string laid out flat and your commands as
knots in that string, you can see how your program runs from one end of the string to
the other, running a command whenever it gets to a knot. If you include a loop, it will
make your program go back to a previous knot, and if you include an if statement it
might make the program miss a knot—but it can still only run one command at a time.
 
Search WWH ::




Custom Search