22 - Growing Tree

Back in Volume 3, we've defined a tree() function.

Next, we will start with a small tree(), use the timer to design a recursion function, bigger_tree(), to imitate a growing tree. game.set_timer() will update the arguments' values for tree() after each time delay, so the tree size will grow in height and width until the terminating condition stop the recursion loop.

# bigger_tree, a tree grows steadly
def bigger_tree(x,z,h,w):
    tree(x,z,h,w)

    if  w < 3:
        game.set_timer(0.2, bigger_tree, x, z, h+1, w+1)

bigger_tree(0,-20,4,1)
bigger_tree(10,-20,4,1)
bigger_tree(20,-20,4,1)

See the 3 trees grow bigger together? By adjusting the increasing steps of arguments in timer, we can control the growing ratio of h and w. From the image of the 3 trees, what do you think? Hmnn, tree triplets?

So, I'm thinking, trees grow naturally with different speed. I'll try using a random number as the increasing step value for h in the timer.

grow_tree()

# grow_tree (random height)
def grow_tree(x,z,h,w):
    tree(x,z,h,w)

    if  w < 3:
        game.set_timer(0.2, grow_tree, x, z, (h + randint(1,4)), w+1)


tree(-10,-20,4,1)
tree(-20,-20,4,1)
tree(-30,-20,4,1)

Grow line of trees

Tree planting business: let's plant a line trees one by one, the distance between trees are 10 blocks, each tree grows taller on it's own.

def tree_n(x, z, h, w, n):
  grow_tree(x, z, h, w)
  if n > 1:
    game.set_timer(1, tree_n, x+10, z, h, w, n-1)

tree_n(10,-20, 5, 2, 5)    # grow 5 trees, the first tree is at x

Challenge:

Can you define a function tree10 that plant a line of 10 trees along x line? How about along z line? Or x-z diagonal?

results matching ""

    No results matching ""