Exercise 04
Spawning a line of blocks - While Loop - Modulo
- Note : We already saw the “setBlocks” function in Exercice 3. It allows line creation in an easier (yet less modular) way *
The while construct consists of a block of code and a condition/expression. The condition/expression is evaluated, and if the condition/expression is true, the code within the block is executed. This repeats until the condition/expression becomes false. Because the while loop checks the condition/expression before the block is executed, the control structure is often also known as a pre-test loop. Compare this with the do while loop, which tests the condition/expression after the loop has executed. 1
First let’s create a line made of say 10 blocks of Spruce Wood Plank (id : 5:1)
And here is what you should get : 10 blocks line of Spruce Wood Plank
You can play with size to see this line growing. We can also play with the subid of the wood at each iteration using a modulo.
In computing, the modulo operation finds the remainder after division of one number by another (sometimes called modulus). 2
So for instance for modulo 6
Source | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | etc. |
Modulo 6 | 0 | 1 | 2 | 3 | 4 | 5 | 0 | 1 | 2 | 3 | 4 | 5 | 0 | 1 | etc. |
We’ll use this to alternate on the kind of wood. 5 modulo 6 in python will be written 5 % 6
You should get something like: 10 blocks line of alternating Wood Plank
We can do the same with Wool (35) and a module 16 (as there are 16 color of wool). We’ll also make a diagonal playing with the z coordinate as well
And voila ! 32 blocks diagonal line of wool alternating colors 04-line-diag-wool.png
exercises
exercises