Controlling the Agent with Ordered Instructions
Sequencing, events, and Agent movement in Minecraft MakeCode.
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.
STEP 1 Warm-up: Computers Are Literal
Read this set of instructions for making a peanut butter sandwich:
- Put the peanut butter on the bread.
- Eat the sandwich.
- Open the jar.
- 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
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.
- In the Minecraft world, press C on your keyboard to open the Code Builder.
- In the Code Builder window, choose MakeCode.
- Click New Project and give it a name —
Lesson 1 - Agentworks. - Click Code options, then select Python Only.
- Click Create. The project will open straight into the Python editor (no blocks view to switch from).
- In the toolbox on the left, find the Agent category. You should see commands like
agent.move,agent.turn, andagent.place.
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 namedon_chatthat 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 1STONEin 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 runon_chatwhenever the player typesroadin chat.
- If you skip
agent.set_item(...), the Agent has nothing in its hand andplacesilently does nothing. - If you skip
agent.destroy(DOWN), the Agent tries to place a block where one already exists (the ground), soplacesilently does nothing.
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
- Switch back to your Minecraft world.
- Open the chat window (press T) and type
road, then press Enter. - Watch the Agent move. Did it do what you predicted?
/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
- Change how far the Agent moves on at least one line. (Change a
5to something else.) - Change the turn direction from
LEFT_TURNtoRIGHT_TURN— or the other way around. - Add at least two more movement commands to make the path longer or more interesting.
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)
CHALLENGE Build Something of Your Own
Now create an original Agent program. Pick one of the three options below — or invent your own.
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)thenagent.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:
- Screenshot of your code in the MakeCode editor.
Use Windows + Shift + S (Snipping Tool) or your normal screenshot method. Capture the whole code block. - 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. - 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?
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.