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."

forever
if (AI sees Rock) then → sprite moves down
if (AI sees Paper) then → sprite moves up
if (AI sees Scissors) then → sprite moves sideways
if (AI sees Nothing) then → sprite stays still

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.

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.

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:

when 🟢 clicked
forever
(empty)

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:

if (AI prediction) = (Rock) then

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:

when 🟢 clicked
forever
if (AI prediction) = (Rock) → change y by -10
if (AI prediction) = (Paper) → change y by 10
if (AI prediction) = (Scissors) → change x by 10
if (AI prediction) = (Nothing) → (nothing)

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.

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.

The reason we use four separate ifs (instead of one big if/else chain) is that it reads cleanly for a beginner. Later you'll learn that if/else if/else if chains are slightly more efficient — the AI prediction only gets looked up once. For now, four ifs is the right choice because it shows the intent — "for each class, here's its action."

The mapping is arbitrary. Rock could mean up, down, left, right, jump, shoot, anything. We picked Rock = down because rocks fall. Paper = up because paper can float. Scissors = sideways because... we needed to put it somewhere. The map you pick should match your game's vibe. A flying game might want Paper as the only lift. A racing game might map Rock and Paper to left/right.

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. That's a sign the change values are too big. Drop them to 5 or smaller. Also: real games usually clamp the sprite 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 single biggest debugging issue here is class name spelling. If a camper named their Teachable Machine class "rock" lowercase but typed "Rock" capital in the if-block, the comparison fails silently and the sprite never moves. Walk the room and confirm spelling matches exactly.

The flickering issue (sprite moves in two directions) is real and usually means the AI model has a weak spot. Resist the urge to keep tuning 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.