Function block()

(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.setBlock(new 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.

block0( )
Put a brick block at (0,2,-20)

function block0() {
    game.setBlock(new Position(0,2,-20), 17);

// call the function to build a brick block


// 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 block() flexibility to choose different materials:

m represents the number of the block material.

// block1(m)
function block1(m) {
    game.setBlock(new Position(0,2,-20), m);

// call the function and click run after each line:

block1(20)       // a dirt block
block1(67)      // a obsidian block replaces the dirt block
block1(78)      // a quartz block replaces the obsidian block

Click run after each block1(m) call, and you will see first you have a dirt block at (0,2,-20), then it's replaced by a black obsidian, then replaced again by a quartz block.

Do you think this function might be a little useful?

Next give the function another argument, x position:

block2(x, m)

// block2(x, m)
function block2(x,m) {
    game.setBlock(new Position(0,2,-20), m);

// different blocks at different x location
block2(0, 67);     // obsidian block at (0,2,-20)
block2(3, 78);     // quartz block at (3,2,-20)
block2(-3, 18);    // stone block at (-3, 2,-20)

block_m(x, y, z, m)

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


// block_m(x,y,z, m)
function block_m(x,y,z,m) {
    game.setBlock(new Position(x,y,z), m);

// blocks, your choice of location and material 
block_m(3,1,-16, 2);        
block_m(5,5,-18, 54);        
block_m(-2,3,-10, 53);

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

results matching ""

    No results matching ""