Function block_m(x,y,z,m)

(I will skip typing CodeCraft Setup Part in each lesson, but you know all CodeCraft applications start with it, you can find it at the beginning of each chapter from now on)

Let's start with the simplest structure, just placing one block.

# put one brick block
game.set_block(Position(0,2,-20), 17)

Wrap up the above code into a function, then just call a name of the function to build the block.

block(): put a brick block at (0,2,-20)

def block0():
    game.set_block(Position(0,2,-20), 17)

# call the function to build a brick block

block0()       # run... and check the result

It doesn't look efficient to define a function that can only place one brick block at a fixed location. If it can perform only one task, it may not be worth it to build the tool at all.

Next, give the function flexibility to choose different materials:

block1(m)
m represents the number of the block material.

# block1(m)
def block1(m):
    game.set_block(Position(0,2,-20), m)

# call the function with required argument value
block1(20)       # a dirt block
block1(67)      # a obsidian block replaces the dirt block
block1(78)      # a quartz block replaces the obsidian block

Do you think this function might be a little useful?

Next give the function another argument, x position:

block2(x, m)

# block(x, m) 
def block2(x, m):
  game.set_block(Position(x, 2, -20), m)

# different blocks at different x location
block2(0, 67)    # obsidian block at the center
block2(3, 78)    # quartz block at the right
block2(-3, 18)    # stone block at the left

block_m(x, y, z, m)

Finally, give function all the flexibilities: 3 position arguments x,y,z and m for material:

block_m(x,y,z,m)

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

# blocks, your choice of location and material 
block_m(4,1,-12, 2)
block_m(5,4,-18, 54)
block_m(-2,6,-10, 53)

Check out the image and smile! Functions are getting useful!

results matching ""

    No results matching ""