Magic Clicks

A few more functions are already defined for you to be used as callback functions:

# Lesson 5: Magic Clicks
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)

# Functions you can call to perform actions:
def put_brick(point):
    game.set_block(point, 17)

def put_diamond(p):
    game.set_block(p, 19)

def put_red(p):
    game.set_block(p, 56) 

def put_blue(p):
    game.set_block(p, 45)

Exercise:

In the previous chapter, we learned how to highlight the "air" block and place a brick upon a click of the mouse. Type the following code into your window:

# Highlight mouse's location:
game.highlight("air")

# Build a block when mouse is clicked:
game.on_click(put_brick)

Run the code. In the CodeCraft world, you should be able to build a brick block on a mouse click as in previous lesson.

Comment out (insert # at the front of the line) the last call of game.on_click(put_brick), then add new code to put a diamond block:

#game.on_click(put_brick)

game.on_click(put_diamond)

Next, try each of the callback functions:

game.on_click(put_red)

game.on_click(put_blue)

Note that the functions that work as input value for game.on_click (the callback functions) all have one thing in common: they all require one input argument, a Position(x, y, z) object indicating a point location in the 3D game world.

results matching ""

    No results matching ""