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)
from codecraft import Game, Position
game = Game()
materials = game.materials
print(materials)

# A black block for your reference:
p = Position(0, 1, -20)
game.set_block(p, 1)

# Define function put_brick, point is a Position object:
def put_brick(point):
    game.set_block(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:

s = Position(1, 2, -20)

Then call put_brick with s as the 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:

t = Position(5, 5, -18)
r = Position(-2, 4, -19)

# call put_brick() on t, r

put_brick(t)
put_brick(r)

You should have the same result as this picture in your CodeCraft world.

results matching ""

    No results matching ""