Function column_m(x,z,h,m)

Build columns with function

Build a column Recall that we built a column with for loop?

Let's try make a function out of it:

column10() A brick column at ground location x:0, z:-10, height 10 blocks tall:

def column10():
    for j in range(10):
        game.set_block(Position(0, j+1, -10), 17)

# a 10 blocks tall brick column at (x=0,z=-10)  
column10()

columnH(h) Add h as input argument indicating the column's height:

def columnH(h):
    for j in range(h):
        game.set_block(Position(0, j+1, -10), 17)

# build some brick columns at (x=0, z=-10) with varying height  
columnH(3)   
columnH(5)   
columnH(10)

Run the above function calls one by one, if you refresh the screen before each run, you will see different height columns each time; if you don't refresh, you'll see a 3 blocks tall column first, then a 5 blocks tall column replaces the previous shorter one, and a 10 blocks tall column replaces the second one, the column looks getting taller because the taller columns cover previous shorter columns, these columns are at the same ground location and of the same material.

Let's make columnHM(h, m) h: height, m: material

def columnHM(h, m):
    for j in range(h):
        game.set_block(Position(0, j+1, -10), m)

# some columns with varying height and materials
column(12, 17)
column(10, 18)
column(4, 19)

If you don't erase previous structures while running the three functions, what do you expect to see? A column at x:0, z:-10, with 3 different colors!

columnFlex(x,z,h,m)

Is it time for us to make a column function with the most flexibility?

// columnFlex(x,z,h,m), h-height, m-material
def columnFlex(x,z,h,m):
    for j in range(h):
        game.set_block(Position(x, j+1, z), m)

# some columns
columnFlex(0,-10, 5, 2)
columnFlex(3,-15, 10, 8)
columnFlex(-1,-12, 8, 18)

Function can call other functions

Function columnFlex(x,z,h,m) is good enough in the sense of function flexibility. While in the sense of programming design, reusing code is the spirit. Let's call the previous user-defined function block_m(x,y,z,m) to define column_m(x,z,h,m).

Function: column_m(x,z,h,m)

Note: the first two parameters of column_m is the ground location x,z, then followed by h,m (height and material).

# block_m(x,y,z,m)
def block_m(x,y,z,m):
    game.set_block(Position(x,y,z), m)

# column_m(x,z,h,m) 
def column_m(x,z,h,m):
    for j in range(h):
        block_m(x, j+1, z, m)

# some columns
column_m(5,-10, 5, 53)         # orange
column_m(6,-12, 10, 50)        # light_blue
column_m(8,-11, 8, 59)         # yellow

Enjoy!

results matching ""

    No results matching ""