Back to Minecraft Lessons
Lesson 1 · Self-Paced

Controlling the Agent with Ordered Instructions

Sequencing, events, and Agent movement in Minecraft MakeCode.

ESSENTIAL QUESTION

How does the order of instructions affect what a computer program does?

By the end of this lesson, I will...

  • Explain why the order of code matters.
  • Use an event (a chat command) to start a program.
  • Write a sequence of commands to move the Minecraft Agent.
  • Predict what a short program will do before running it.
  • Debug a program when the Agent does not behave as expected.
  • Modify starter code to build something original.
Every Lesson

Start Here First — Every Single Time

Every Minecraft lesson begins with a brand-new world. Do not skip this. Open Lesson 0, follow the setup steps, then come back here and continue with Step 1.

Open Lesson 0 — World Setup

STEP 1 Warm-up: Computers Are Literal

Read this set of instructions for making a peanut butter sandwich:

  1. Put the peanut butter on the bread.
  2. Eat the sandwich.
  3. Open the jar.
  4. Get a knife.
Think about it first — what is wrong with this set of instructions?

Each step is technically a real action, but the order is wrong. You cannot put peanut butter on bread before opening the jar, and you cannot eat the sandwich before you've made it. A human reading this might mentally rearrange the steps. A computer cannot.

The big idea: Computers do not understand what you meant. They only do what you wrote, in the order you wrote it. This ordered set of instructions is called a sequence.

Today's Vocabulary

SequenceThe order in which instructions run.
EventSomething that starts code, like typing a chat command.
AgentA programmable helper in Minecraft Education.
DebuggingFinding and fixing mistakes in code.
AlgorithmA step-by-step set of instructions for solving a problem.

STEP 2 Open MakeCode

You should already be in your fresh flat world from Lesson 0. If not, go back and complete Lesson 0 first.

  1. In the Minecraft world, press C on your keyboard to open the Code Builder.
  2. In the Code Builder window, choose MakeCode.
  3. Click New Project and give it a name — Lesson 1 - Agent works.
  4. Click Code options, then select Python Only.
  5. Click Create. The project will open straight into the Python editor (no blocks view to switch from).
  6. In the toolbox on the left, find the Agent category. You should see commands like agent.move, agent.turn, and agent.place.
Heads up: Because you chose Python Only at project creation, the editor is already in Python. You will not need to switch language modes. All of the code samples below are in Python.

Check yourself

You have Minecraft open, the MakeCode window is on your screen, and you can see Agent blocks in the toolbox. If yes — move on. If no — ask a neighbor or your teacher.

STEP 3 Your First Agent Program

Your project is already in Python from Step 2. Delete anything that's already in the editor. Then copy the code below and paste it in:

def on_chat():
    agent.set_item(STONE, 1, 1)
    agent.move(FORWARD, 5)
    agent.turn(LEFT_TURN)
    agent.move(FORWARD, 5)
    agent.destroy(DOWN)
    agent.place(DOWN)
player.on_chat("road", on_chat)

What this program says

  • def on_chat(): — defines a function named on_chat that holds the instructions for the Agent. The indented lines below it belong to this function.
  • agent.set_item(STONE, 1, 1)loads the Agent's inventory. The Agent can't place a block out of thin air; you have to tell it which block to place. This puts 1 STONE in slot 1 of the Agent's inventory.
  • agent.move(FORWARD, 5) — the Agent moves forward 5 blocks.
  • agent.turn(LEFT_TURN) — the Agent turns left.
  • agent.move(FORWARD, 5) — the Agent moves forward 5 more blocks.
  • agent.destroy(DOWN)breaks the block underneath the Agent. We have to clear that space first, because the Agent can't place a new block where one already exists.
  • agent.place(DOWN) — the Agent places a block below itself from its inventory (the stone you loaded above).
  • player.on_chat("road", on_chat)this is the event. It tells Minecraft to run on_chat whenever the player types road in chat.
Two place-block gotchas:
  1. If you skip agent.set_item(...), the Agent has nothing in its hand and place silently does nothing.
  2. If you skip agent.destroy(DOWN), the Agent tries to place a block where one already exists (the ground), so place silently does nothing.
Both are common "it ran but didn't work" bugs — the program looks fine, but no block appears.
Predict before you run it — what shape will the Agent's path make in the world?

The Agent will move forward 5 blocks, turn left, and move forward 5 more blocks — an L-shape with a corner where it turned. Then it will place one block underneath itself at the end of the path.

Run it

  1. Switch back to your Minecraft world.
  2. Open the chat window (press T) and type road, then press Enter.
  3. Watch the Agent move. Did it do what you predicted?
If the Agent doesn't move at all: make sure the Agent is somewhere in the world (you can summon it by typing /agent in chat). If your chat command does nothing, double-check that the word in player.on_chat("road", on_chat) matches exactly what you typed.

STEP 4 Modify the Starter Code

Now make the program your own. Change the code in three ways. Run it after each change so you can see what each edit does.

Required modifications

  1. Change how far the Agent moves on at least one line. (Change a 5 to something else.)
  2. Change the turn direction from LEFT_TURN to RIGHT_TURN — or the other way around.
  3. Add at least two more movement commands to make the path longer or more interesting.
Pro tip: Change one thing at a time and test after every change. If your program breaks, you'll know exactly which edit caused it. This is how real programmers debug.

Need an idea?

Here is one version of what your modified code might look like (don't just copy it — make yours different):

def on_chat():
    agent.move(FORWARD, 4)
    agent.turn(RIGHT_TURN)
    agent.move(FORWARD, 6)
    agent.turn(LEFT_TURN)
    agent.move(FORWARD, 3)
player.on_chat("path", on_chat)

STEP 5 Debugging: Order Matters

Read the program below. It's supposed to make the Agent move forward, turn, and then continue along a new direction.

def on_chat():
    agent.move(FORWARD, 5)
    agent.move(FORWARD, 5)
    agent.turn(LEFT_TURN)
    agent.move(FORWARD, 5)
player.on_chat("turn", on_chat)
Predict: when this runs, the Agent doesn't turn until late. Why? Which line would you move?

The Agent moves forward 10 blocks total before it ever turns — because both agent.move(FORWARD, 5) lines run before the turn. The turn is in the wrong position in the sequence.

To make it turn between the two long moves, swap the order so the turn happens after the first move:

Try the broken version first. Then try the fixed version below to compare:

def on_chat():
    agent.move(FORWARD, 5)
    agent.turn(LEFT_TURN)
    agent.move(FORWARD, 5)
    agent.move(FORWARD, 5)
player.on_chat("turn", on_chat)
The big idea: Debugging is not guessing randomly. Debugging means comparing what you expected to what actually happened, finding the line that caused the difference, and changing only that.

CHALLENGE Build Something of Your Own

Now create an original Agent program. Pick one of the three options below — or invent your own.

Option A — Maze Entrance

Make a winding path that looks like the start of a maze. Include corners and at least two turns.

Option B — Obstacle Course

Move the Agent through an obstacle course path. Add stairs, corners, or zig-zags.

Option C — Building Outline

Use the Agent to trace the outline of a square, rectangle, or small building. Bonus: place blocks as it goes.

Minimum requirements

  • Your program starts with a chat command (an event).
  • The Agent moves at least 5 times.
  • The Agent turns at least 2 times.
  • Your code is different from the starter code — not just copied.
  • You can explain what your program does in one sentence.

Stretch goals (try if you finish early)

  • Use agent.destroy(DOWN) then agent.place(DOWN) after each move to leave a trail of stone blocks. If you want a long trail, raise the stone quantity at the top — e.g., agent.set_item(STONE, 64, 1) for a full stack.
  • Make a shape that another student would recognize.
  • Add comments (lines starting with #) above each section of your code to explain what it does.

Example with comments

def on_chat():
    # First side of the path
    agent.move(FORWARD, 5)

    # Turn toward the second side
    agent.turn(RIGHT_TURN)
    agent.move(FORWARD, 3)

    # Turn again to make the path more interesting
    agent.turn(LEFT_TURN)
    agent.move(FORWARD, 4)
player.on_chat("maze", on_chat)

SUBMIT Turn In Your Work

When you've finished the challenge, submit three items to the Schoology assignment:

  1. Screenshot of your code in the MakeCode editor.
    Use Windows + Shift + S (Snipping Tool) or your normal screenshot method. Capture the whole code block.
  2. Screenshot of the result in Minecraft — your Agent's path, build, or maze entrance.
    Inside Minecraft, press F2 to take a screenshot, or use the regular Windows screenshot shortcut.
  3. Short written reflection. Answer all four questions in 1–2 sentences each:
    • What did your program do?
    • Which line of code was the most important to your result?
    • Describe one bug you fixed or one thing that didn't work the first time.
    • How did changing the order of instructions change the result?
Important: The reflection counts for half the grade. "It worked" is not a reflection — show that you understand why it worked.

When You're Done

If you finish early, here are productive ways to spend the rest of class:

  • Trade computers with a classmate and run each other's programs.
  • Try one of the stretch goals above.
  • Explore the other Agent commands in MakeCode (agent.destroy, agent.attack, agent.detect).
  • Open a Minecraft Hour of Code tutorial and try the next concept on your own.
Back to Minecraft Lessons