Reproduce Blocks/ Blocks Production

We can use the timer function in CodeCraft to design a recursive situation to help explain this concept.

Demo recursion in CodeCraft

Attention!!

Terminating condition must be known in advance to avoid infinite loop.

Let's use doubleBlock() as example to show the simplest application of recursion in CodeCraft:

doubleBlock(y)

# doubleBlock, step 10
def doubleBlock(y):      
    block(0, y, -20, 'brick')

    if y < 10:
        game.set_timer(1, doubleBlock, y+10)

doubleBlock(1)

Run the code in CodeCraft, you will see the first brick block is set at y=1, then after a second, the second block will appear at y=11.

Explaination:

doubleBlock(1) first call block() to build a brick at y=1, then it checks the condition (y<10) to be True and continues to call the timer function, while in timer function, doubleBlock itself is called again after time delay with new input value (y+10) = 11, so doubleBlock(11) runs to build another brick at y=11. After this the condition is checked again, this time it is False (y is not smaller than 10), so the program stops here. We get two brick blocks one after another.

moreBlocks(x)

Increase the terminating condition to a larger range, we can build more blocks one by one

# moreBlocks (along y, distance 2)
def moreBlocks(y):
    block(0, y, -20, 'linen_blue')
    if y < 19:
        game.set_timer(0.2, moreBlocks, y+2)

moreBlocks(1)

This program will build blue blocks every 0.2 seconds, totally 10 blocks along y axis.

Blocks Show

Similar to previous functions, we can design other recursive functions to reproduce blocks along many directions. We can use more arguments, so the first block has more flexibility with location and material. From now on we define blocks_...() series, that take (x,y,z,m) as input arguments, m indicates the material name string.

blocks_x10(x, y, z, m)

Your turn to try this one: the first block is at (x,y,z), then a few more blocks grow along x axis 1 second each, separated by 10. (Suggest the terminating condition: x<60)

(answer hints:)

# blocks_x10  (along x, step 10)
def blocks_x10(x, y, z, m):
    block(x,y,z,m)

    if x < 60:
        game.set_timer(1, blocks_x10,  x+10, y, z, m)

blocks_x10(0, 3, -20, 'quartz')

Let's try adding another input parameter, d as the separation distance for the blocks:

blocks_x(x,y,z,m,d)

# blocks_x (along x, step d)
def blocks_x(x, y, z, m, d):
    block(x, y, z, m)

    if x < 60:
        game.set_timer(1, blocks_x, x+d, y, z, m, d)

blocks_x(0, 2, -20, 'brick', 5)
blocks_x(0, 4, -20, 'cobblestone', 2)

Again, pay attention to terminating condition!

Your turn to define other blocks_...(x,y,z,m,d) functions along y, z directions and face diagonal(x-y) and space diagonal(x-y-z)

(Code reference)

# blocks_y (along y, step d, pay attention to terminating condition)
def blocks_y(x,y,z,m,d):
    block(x,y,z,m)

    if y<60:    # Attention!! terminal condition related to y
        game.set_timer(1, blocks_y, x, y+d, z, m, d)

blocks_y(0,0,-20,'obsidian', 10)

# blocks_z (along z, step d)
def blocks_z(x,y,z,m,d):
    block(x,y,z,m)
    if z<0:            # Attention!! terminal condiction related to z
        game.set_timer(1,blocks_z, x,y,z+d,m,d)

blocks_z(0,1,-20,'obsidian', 10)
blocks_z(0,4,-20,'cobblestone',2)

# blocks_xy (x,y step up d)
def blocks_xy(x,y,z,m,d):
    block(x,y,z,m)
    if x<60:
        game.set_timer(0.5, blocks_xy, x+d, y+d, z,m,d)

blocks_xy(0,2,-20,'brick', 5)
blocks_xy(0,4,-20,'cobblestone',2)   

# blocks_xyz, (x,y,z all step up d)
def blocks_xyz(x,y,z,m,d):
    block(x,y,z,m)
    if x<30:
        game.set_timer(0.5, blocks_xyz, x+d, y+d, z+d, m, d)

blocks_xyz(0,0,-20,'linen_orange', 2)

Please try the programs yourself and run to see the 3D visual, it's not easy to put so many videos in the book.

==

Gold Rain

A drop of gold!

# rainGold, timer, dictionary, randint
def rainGold(x, y, z):
    block(x, y, z, 'gold')
    block(x, y+2, z, 'air')

    if y > 0:
        game.set_timer(0.2, rainGold, x, y-2, z)

rainGold(0, 20, -20)

Run to see one line of the gold rain.

Let's use randint() to make some gold rain. Don't forget to put from random import randint at the front of all code.

for i in range(20):
    rainGold(randint(1, 20), randint(20, 30), randint(-20, 0))

You can check out the scene in motion when you run the code.

results matching ""

    No results matching ""