User-Defined Functions
Build a Brick Block
In this chapter, we have a function already defined for us: put_brick(point)
. Your job is to learn how to call this function and use it to build brick blocks.
Make sure the code editor has the following code:
// Lesson 2: Function put_brick(point)
// CodeCraft Setup
let game = new Game();
let materials = game.materials;
console.log(materials);
// A black block for your reference:
let p = new Position(0, 1, -20);
game.setBlock(p, 1);
// Define function put_brick, point is a Position object:
function put_brick(point) {
game.setBlock(point, 17);
}
// Call put_brick to build a brick block:
This function put_brick
has one input parameter point
which is a Position
object.
To call put_brick(point)
, we need to create a Position(x, y, z)
object indicating a point in CodeCraft world. Now add the following code to a new line at the end:
let s = new Position(1, 2, -20);
then call put_brick
with s
as input value:
put_brick(s);
Run the program and enter the CodeCraft world to see a brick block at x=1, y=2, z=-20
Next create two more Position
objects, t, r
and call put_brick
on them:
let t = new Position(5, 5, -18);
let r = new Position(-2, 4, -19);
// call put_brick() on t, r
put_brick(t);
put_brick(r);
See the picture, you should have the same result in the CodeCraft world.