Minecraft Education – Lesson 4

Agent Programming with Python

Objectives

Vocabulary


Part A – Moving the Agent

The Agent is like a robot we can program with Python.

Sample Code:

# Move the Agent in front of the player
agent.teleport_to_player()

# Move 3 steps forward
for i in range(3):
    agent.move(FORWARD, 1)

Part B – Agent Placing Blocks

The Agent can place blocks as it moves to build paths or structures.

Sample Code:

agent.teleport_to_player()

# Build a 5-block path in front of the Agent
for i in range(5):
    agent.place(FORWARD)
    agent.move(FORWARD, 1)

Make sure the Agent has blocks in its inventory (e.g., stone or planks).


Part C – Building a Mini House with Functions

We can write functions so we can build a small house with one chat command.

Sample Code:

def build_floor(size):
    for x in range(size):
        for z in range(size):
            blocks.place(PLANKS_OAK, pos(x, 0, z))

def build_walls(size, height):
    # Front and back walls
    for x in range(size):
        for y in range(1, height + 1):
            blocks.place(PLANKS_OAK, pos(x, y, 0))
            blocks.place(PLANKS_OAK, pos(x, y, size - 1))

    # Left and right walls
    for z in range(size):
        for y in range(1, height + 1):
            blocks.place(PLANKS_OAK, pos(0, y, z))
            blocks.place(PLANKS_OAK, pos(size - 1, y, z))

def on_chat_house():
    build_floor(5)
    build_walls(5, 3)
    player.say("Mini house complete!")

player.on_chat("house", on_chat_house)

Type house in the chat and watch the mini house appear near the origin (0,0,0) area.


Extension / Project Ideas