Skip to main content

Stage 6: Control a Sprite with AI

Course progressStage 6 of 10
~90 min
Your two tools

Keep both tabs open all week. Open in a new tab — don’t use the buttons in this page to leave the course.

Build

four if/then blocks that make the sprite move based on your hand

Learn

how to turn an AI guess into an action

Ship

a sprite that moves up, down, sideways, or stands still

Teacher demo

Show the room — this is the demo stage that hooks the room:

  1. Open your project with the Stage 5 say-script. Point at the say block.
  2. Replace say with a move action wrapped in an if block.
  3. Show four if-blocks, one per class: Rock → move down, Paper → move up, Scissors → move sideways, Nothing → stand still.
  4. Click the green flag. Move your hand. Watch the sprite move.
  5. Crank up the speed value. Crank it down. "Tuning is part of the game."

The big idea

Yesterday the sprite said what the AI saw. Today the sprite does something about it. This is the moment a "cool AI demo" becomes a "real game."

The new tool is the if/then block — Scratch's way of saying "if this is happening, do that."

Starting control map
Rockmove down
Papermove up
Scissorsmove right
Nothingstand still

These are the first mappings. Campers can tune them after the movement works.

The forever loop is the same one from Stage 5 — it never stops. Inside it, we now ask the AI's prediction four questions instead of just one. Whichever question's answer is "yes" runs that action.

The mapping — which hand sign does which thing — is a design choice. Today we use a starting map (Rock = down, Paper = up, Scissors = sideways, Nothing = still). You can change it. In Stage 7 the right map will matter a lot, because the sprite will need to catch falling things.

How the two tools connect
Photosexamples in Teachable Machine
Modelthe AI after training
Model URLthe address of the AI
RAISEloads the model
PredictionRock, Paper, Scissors, Nothing
Game actionsay, move, score, win

Stage 5 used the prediction as words. Stage 6 uses that same prediction as a controller: if the AI says Rock, the sprite moves down.

New words
if/then
a block that says 'if a thing is happening, do this'
conditional
code that only runs when something is true
X position
where the sprite is left or right (negative = left, positive = right)
Y position
where the sprite is up or down (positive = up, negative = down)
control map
your plan for which hand sign does which action
Before you start

Stage 5 should be open. The sprite should currently say what the AI sees when you press the green flag.

Connect the dots

Do not change the model today. The AI already knows the hand signs. Today you decide what the game should do when each prediction appears.

Build it

Step 1 — Take the say block out

You don't need the speech bubble anymore. Drag the say block (and the AI prediction block inside it) to the trash. Keep the when green flag clicked and forever blocks — you'll reuse them.

Your script should now be just:

Keep the event and loop

when green flag clicked
forever
end

Step 2 — Add four if blocks inside the forever

From the Control category, drag the if block. Snap it inside the forever loop.

Add three more if blocks below the first one, all inside the same forever loop. You should see four empty if-blocks stacked vertically.

Step 3 — Ask "is the AI seeing Rock?" inside the first if

The if block has an empty diamond slot at the top — that's where the question goes. The question is an Operators block.

  1. From Operators, drag the equals block: [ ] = [ ].
  2. Drop it into the first if's diamond slot.
  3. In the left side of the equals, drag your AI prediction block.
  4. In the right side, type the word Rock (with a capital R, exact match to your class name).

The whole thing should now read:

Rock question

if <(prediction) = [Rock]> then
end
block

equals comparison

The word on the right must match your Teachable Machine class name exactly. Rock and rock are different words to the computer.

Step 4 — Make the sprite move when the AI sees Rock

Inside that first if block's body, drag a change y by block from Motion. Set it to -10. That means "move 10 pixels down" (because Y is up-positive).

Repeat the same pattern for the other three ifs:

  • Paperchange y by 10 (move up)
  • Scissorschange x by 10 (move sideways)
  • Nothing → no block (the sprite stays still)

Your finished script should look like:

Finished AI movement script

when green flag clicked
forever
if <(prediction) = [Rock]> then
change y by (-10)
end
if <(prediction) = [Paper]> then
change y by (10)
end
if <(prediction) = [Scissors]> then
change x by (10)
end
if <(prediction) = [Nothing]> then
end
end

Step 5 — Test and tune

Click the green flag. Make a Rock sign — the sprite should drift down. Make Paper — the sprite climbs. Make Scissors — it slides right. Hold up Nothing — it stops.

If the sprite moves too slow, change the 10 values to 15 or 20. Too fast? Try 5. Tuning is part of the game.

Save your project.

Pacing Lab

This lab is required before Stage 7. You are designing a controller, not just making blocks work.

Part A — Control map design lab (20 minutes)

Draw or write your control map before changing anything else:

Rock moves: ________________________
Paper moves: _______________________
Scissors moves: ____________________
Nothing does: ______________________

Now answer:

The easiest sign for me is __________, so it should control __________.
The hardest sign for me is __________, so it should control __________.

Update your blocks only after the map makes sense.

Part B — Speed tuning pass (15 minutes)

Test movement speeds 5, 10, and 20.

Speed 5 felt: ______________________
Speed 10 felt: _____________________
Speed 20 felt: _____________________
My chosen speed is ______ because ______________________________.

Use the chosen speed in the project before Stage 7.

Understand it

The forever loop is exactly the same one you wrote in Stage 5. The pattern doesn't change. What grew is what's inside the loop. Every frame, the loop now asks the AI four questions, and the answers steer the sprite.

We use four separate ifs because they are easier to read. One if block handles one hand sign. Later you can learn shorter ways to write this. For now, four ifs is the right choice because it says: "for each class, here is its action."

The map is your choice. Rock could mean up, down, left, right, jump, or anything else. We picked Rock = down because rocks fall. Paper = up because paper can float. Scissors = sideways because we needed one more move. Pick controls that fit your game.

In Stage 7 the map matters because the sprite needs to catch falling things. The hand signs you press most often should map to the actions you need most.

Try this

Learning beat

Try this

Three short experiments. Predict before you run, then test your guess.

Predict first

Change the Rock mapping from change y by -10 to change y by -50. Predict what will happen. Try it. Did the sprite teleport or zoom? What does that tell you about how big each "frame's move" should be?

Compare

Comment out (drag out) the Scissors if-block. Now you only have three controls. Test — does the game still feel playable? Or does it feel like something is missing?

Connect

Stage 7 adds falling objects that the sprite has to catch. If most objects fall on the left side of the screen, which hand sign do you want to be the easiest to make? Look at your control map — does it need a redesign?

Test your stage

  • Pressing green flag starts the program.
  • Rock sign moves the sprite down.
  • Paper sign moves the sprite up.
  • Scissors sign moves the sprite sideways.
  • Nothing (no hand) leaves the sprite still.
  • The sprite stays on the stage — it doesn't fly off forever.
  • Design check. Hold each sign for 3 seconds. Does the speed feel right, or does the sprite zoom past where you want it?

If it breaks

  • The sprite doesn't move at all. Three suspects. First, is the if's diamond slot filled with the equals block? Second, is the AI prediction block on the left side of the equals? Third, did you spell the class name exactly the way it's spelled in Teachable Machine (capital first letter)?
  • The sprite only moves when I press a key. You probably used a when key pressed event instead of when green flag clicked. The green flag is what starts the AI's prediction loop.
  • The sprite zooms off the screen and doesn't come back. The movement numbers are too big. Drop them to 5 or smaller. Real games also stop sprites at the edges. We won't do that today, but Stage 7's hard stretch covers it.
  • The sprite moves in the wrong direction. Check the + and - in your change blocks. Up is positive Y. Down is negative Y. Right is positive X. Left is negative X.
  • Two signs move the sprite at once. The AI is flickering between two predictions. That's a Stage 3 problem — go back to Teachable Machine, add 5–10 more photos to whichever two classes are getting confused, retrain, and re-export the URL.
Coach notes

This is the second "wow" moment of the week (after Stage 5's green-flag-talking moment). Build 5 minutes of free-play time in after Step 5 — campers will want to chase their sprite around the stage.

The biggest bug here is class name spelling. If a camper named the Teachable Machine class rock but typed Rock in the if-block, the sprite never moves. Walk the room and confirm spelling matches exactly.

If the sprite moves in two directions, the AI model probably has a weak spot. Do not keep changing the if-blocks. Send the camper back to Stage 3 for a quick retrain. Fix the model, not the workaround.

Fast finishers should do the medium stretch (per-direction speeds) — it primes them for thinking about feel in Stage 8. The hard stretch (stage boundaries) is a good before-snack-break activity for the fastest 10% of campers.