Review Callback Function in CodeCraft
Use callback functions in CodeCraft
First, let's define some functions that can be used as callbacks:
# callback functions
def put_brick(point):
block(point, 'brick')
def put_red(p):
block(p, 'linen_red')
def put_blue(p):
block(p, 'linen_blue')
# action(p, callback), p is a point in CodeCraft
def action(p, callback):
callback(p)
# call action() on some points:
p1 = Position(10, 10, -10)
p2 = Position(12, 12, -20)
action(p1, put_brick)
action(p2, put_red)
action(Position(3,3, -10), put_blue)
action(p, callback)
shows the usage of callback function in CodeCraft. In action()
, parameter p
is a Position object, it's intended for callback
to use as input. Some functions can be used for callbacks: put_brick(), put_red(), put_blue()
.
We invoke action()
three times: the first time with point p1
and put_brick
as a callback; The third time with a Position()
object directly and put_blue
as callback.
Below are some other functions that can be used as callback for action()
:
def put_column(p):
def put_wall(p):
def put_cube(p):
After you try these callback functions with action()
, comment out all the lines of function call to action()
. Put this aside, we'll move on to `Mouse Click Events' in the game.