Delayed Construction
Let's use an CodeCraft examples to show the timer's basic use, you can see how things are related in slow motion:
timerDemo
def timerDemo():
block(0, 1, -20, "obsidian")
game.set_timer(2, column, 5, -20, 3, "dirt")
timerDemo()
Click Run
, you can see a black obsidian block is build first, then after 2 seconds a 3 blocks tall dirt column will appear at the right side.
Explanation
In timerDemo()
, function block()
is called to build a black block first.
Then timer function game.set_timer()
is called, the timer has parameters: (2, column, 5, -20, 3, "dirt")
, the first is a number 2, referring to delay time in seconds; the second is a function name (column
) that's going to be called after delay time 2 seconds, following are the arguments values (5, -20, 3, "dirt")
that's going to be used by the called function column(x, z, h, m)
.
timerDemo2
Build one by one after each second: columns: red, orange, yellow, green, blue, purple, magenta.
# timerDemo2
def timerDemo2():
column(2, -20, 1, 'linen_red')
game.set_timer(1, column, 4, -20, 2, 'linen_orange')
game.set_timer(2, column, 6, -20, 3, 'linen_yellow')
game.set_timer(3, column, 8, -20, 4, 'linen_green')
game.set_timer(4, column, 10, -20, 5, 'linen_blue')
game.set_timer(5, column, 12, -20, 6, 'linen_purple')
game.set_timer(6, column, 14, -20, 7, 'linen_magenta')
timerDemo2()
After the delaying time, the timer will call a function (second argument in timer) to start, and providing the updated input arguments' values that follow the function name.
Result: we can see different height and color columns appear one by one at each second in the game world.
((?? move to next lesson and hide the code?? delayed for loop)) Alternative way:
An better way to do the same thing, use list to store the color numbers, use for loop
to build the rainbow columns:
colorList = ['linen_red', 'linen_orange', 'linen_yellow', 'linen_green', 'linen_blue', 'linen_purple', 'linen_magenta']
def rainbowStep():
for i in range(7):
game.set_timer(i, column, i, -20, i+1, colorList[i])
rainbowStep()