Delayed for Loops
With the help of the timer, we can easily see how for
loop and range
functions work together to build structures in slow motion.
Three Blocks One by One
for i in range(3):
game.set_timer(i, block, i, 1, -20, 'box_lime')
range(3)
produce 3 integers: [0,1,2];
at 0 second, make a block at(0, 1, -20);
at 1 second, make one at (1, 1, -20);
at 2 second, make one at (2, 1, -20);
together, they make a black line of 3 blocks.
vertical dash line
for j in range(2, 20, 2):
game.set_timer( j, block, 0, j, -20, 'box_blue')
range(1,10,2)
creates integers in list: [1,3,5,7,9]
at 2 second, make a block at (0, 2, -20);
at 4 seconds, a new block at (0, 4, -20)
at 6 seconds, a new block at (0, 6, -20);
.....
So you will see each new block appears on top of the previous one every 2 seconds.
Grow some diagonals:
Add some variations to the arguments' values, we can get different structures, check them out in CodeCraft to help understand how loops and range
function work step by step.
# increase x, y, a face diagonal
for j in range(5):
game.set_timer(j, block, j, (1+j), -20, 'box_red')
#increase x,y,z, a space diagonal
for j in range(5):
game.set_timer(j, block, j, (1+j), (-20+j), 'box_green')
Make sure you test some examples in CodeCraft to see the construction in slow motion
Nesting loops with timer
Grow walls
2 layer nesting loops, link the timer with x, or y variables so the wall will grow taller or wider
# wall grows wider, delayed time link with i
for j in range(5):
for i in range(4):
game.set_timer(i, block, i+10, (j+2), -30, 'box_orange')
# wall grows taller, the timer linked with j
for j in range(5):
for i in range(4):
game.set_timer(j, block, i, (j+2), -30, 'box_pink')
# slanted
for j in range(5):
for i in range(4):
game.set_timer(j, block, i+j, (j+10), -30, 'box_purple')
# separate y
for j in range(3):
for i in range(4):
game.set_timer(j, block, i+10, (2*j+2), -30,'box_light_blue')
for j in range(3):
for i in range(4):
game.set_timer(j,block, (10+i+j), (2*j+10), -30, 'box_lime')
# separate x,y
for j in range(3):
for i in range(4):
game.set_timer(j,block, (20+2*i), (2*j+2), -30,'box_red')
for j in range(3):
for i in range(4):
game.set_timer(j, block, (20+2*i+j), (2*j+10), -30,'box_yellow')
Grow a cube
3 layer nesting loops, build a cube in slow motion or build a blocks array
# loop x,y,z, grow a cube
for j in range(3):
for i in range(4):
for k in range(5):
game.set_timer(j,block, i, (j+2), (-20+k),'box_lime')
for j in range(3):
for i in range(4):
for k in range(5):
game.set_timer(j,block, (i+10), (2*j+2), (-20+k),'glass_brown')
for j in range(3):
for i in range(4):
for k in range(5):
game.set_timer(j,block, (2*i+20), (2*j+2), (-20+k),'glass_orange')
# grow blocks array
for j in range(3):
for i in range(4):
for k in range(5):
game.set_timer(j,block, (2*i+30), (2*j+2), (-20+2*k),'glass_blue')
Skyscraper
# grow skyscraper
def skyscraper(x,z,w,l,h,material):
for j in range(h):
for i in range(w):
for k in range(l):
game.set_timer(j/4, block, (x+2*i), (2*j+1), (z+2*k), material)
# some buildings
skyscraper(0,-30,4,6,10,'diamond')
skyscraper(20,-40,6,6, 25,'obsidian')
skyscraper(-10, -50,8,2, 8, 'brick')