Skip to main content

Stage 2: Move the Player Fish

Course progressStage 2 of 10
~60 min
Your workspace

Keep your Scratch project tab open all week. Open in a new tab so you don’t leave the course.

Build

a forever loop with four key-press checks

Learn

how a sprite reacts to the player's keyboard

Ship

a fish that swims up, down, left, and right

Teacher demo

Show the room — five minutes, end to end:

  1. Click your Player sprite. Click the Code tab.
  2. Drag in when green flag clicked (Events, yellow).
  3. Drag in switch costume to Fish-a, set rotation style left-right, go to x: 0 y: 10, and show.
  4. Drag in forever (Control). Inside it, drag four if blocks.
  5. For each if, drag in key (up arrow) pressed? from Sensing. Set the four keys to up, down, left, right.
  6. Inside each if, drag in motion blocks: change y by 2, change y by -2, point in direction -90 + change x by -2, point in direction 90 + change x by 2.
  7. Click the green flag. Press arrow keys. The fish swims.

Then say "Notice the fish flips when you go left. That's the rotation style."

The big idea

Today the Player Fish gets a brain. Press the arrow keys and the fish swims. Let go and it stops.

The pattern is the same one every arcade game on Earth uses: a forever loop runs many times per second, checking "is a key pressed right now?" and moving the sprite if the answer is yes. Pros call this the game loop. In Scratch it's just three words: when green flag clickedforever → check the keys.

There's one Scratch-specific trick worth noticing today. When the fish moves left, we want it to face left — not slide sideways like a crab. We control this with rotation style. Setting it to left-right means the fish flips horizontally when it changes direction, the same way a real fish turns its body to swim the other way.

New words
event
something that happens — like clicking the green flag or pressing a key
forever loop
a Control block that runs the same code over and over with no stop
if/then
a Control block that only runs its inside code when a question is true
sensing
block category that asks questions like 'is key X pressed?'
rotation style
tells Scratch how the sprite should turn — 'left-right' flips it horizontally
X and Y
the sprite's position numbers — X is left/right, Y is up/down
Before you start

Stage 1 should be done — your Player sprite has two costumes (Fish-a, Party Hat-a) and is named Player in the sprite pane.

Build it

Step 1 — Open the code area for the Player

In the sprite pane, click the Player sprite. At the top of the editor, click the Code tab.

The code area is empty right now — no blocks. That's where we build today.

Step 2 — Make the game start clean

Every new game should start with the fish looking like a fish (not a party hat) and facing the right way. Drag these blocks into the code area:

  1. From Events (yellow), drag when green flag clicked. This is the start of every script.
  2. From Looks (purple), drag switch costume to (Fish-a). Set it to Fish-a using the dropdown.
  3. From Motion (blue), drag set rotation style (left-right). This is the magic flip block.
  4. From Motion, drag go to x: (0) y: (10). Center-ish of the stage.
  5. From Looks, drag show. (In case the fish was hidden at the end of the last game.)

Snap them together in that order. Your script should look like:

Player setup script

when green flag clicked
switch costume to Fish-a
set rotation style left-right
go to x: 0 y: 10
show

Click the green flag. The fish should jump to the center of the stage and look like Fish-a.

Step 3 — Add the forever loop

The fish is set up, but it doesn't do anything yet. We need a loop that watches the keyboard.

From Control (orange), drag forever. Snap it directly under the show block.

The forever block has an opening in the middle. Anything you put inside that opening runs again and again, many times per second.

Step 4 — Add the four key checks

Inside the forever loop, we add four if/then blocks — one for each arrow key.

From Control, drag an if/then block. Drop it inside the forever.

From Sensing (turquoise), drag key (space) pressed?. The dropdown lets you pick the key. Drop this block into the diamond slot of the if.

Change the dropdown to up arrow.

Inside that if-block's body, drag change y by (1) from Motion. Change the value from 1 to 2.

Now your inside-the-forever script reads:

if (key (up arrow) pressed?) then
change y by (2)

Repeat for the other three arrows. Make four if-blocks total inside the forever:

  • up arrowchange y by 2 (swim up)
  • down arrowchange y by -2 (swim down)
  • left arrowpoint in direction (-90) then change x by -2 (face left, swim left)
  • right arrowpoint in direction (90) then change x by 2 (face right, swim right)

For left and right we use TWO blocks — first point in direction (so the fish flips), then change x.

Keyboard loop

forever
if key up arrow pressed? then
change y by 2
if key down arrow pressed? then
change y by -2
if key left arrow pressed? then
point in direction -90
change x by -2
if key right arrow pressed? then
point in direction 90
change x by 2

Step 5 — Test the controls

Click the green flag. Use the arrow keys.

  • Up arrow → fish swims up.
  • Down arrow → fish swims down.
  • Left arrow → fish flips, then swims left.
  • Right arrow → fish flips back, then swims right.

If all four work, save the project (File → Save Now).

Understand it

The forever loop is the heartbeat of every Scratch game. Without it, the script runs once and stops. With it, the script runs over and over — many times per second. Every frame, Scratch asks all four questions ("is up pressed? is down pressed? is left pressed? is right pressed?") and only runs the matching code.

The rotation style left-right is a Scratch thing worth slowing down on. By default, sprites use all around rotation — if you point a fish at -90 degrees, it can visibly rotate instead of simply flipping left. That's fine for some top-down games, terrible for this fish. Left-right flips the sprite horizontally only, which makes a fish swimming left look like a normal fish facing the other way.

The point in direction blocks (-90 for left, 90 for right) work with the rotation style. The block tells Scratch which way the fish is facing. Combined with left-right rotation, the fish flips horizontally and faces the new direction visually.

The change y by 2 value is a tuning knob. Bigger number = faster fish = harder game. Smaller number = slower fish = easier game. We picked 2 because it feels right at the screen size Scratch uses. Game designers spend weeks tuning numbers like this for real games.

The setup blocks at the top (switch costume, set rotation, go to x:0 y:10, show) might feel unnecessary the first time you click the green flag. But after the fish wins a round (Stage 4), it'll be wearing a party hat — and the next game needs to start as a regular fish. Initialization at the top of the script is what makes the game restart cleanly.

Try this

Learning beat

Try this

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

Predict first

Right-click the set rotation style block and choose delete to remove it. Click the green flag. Predict what happens when you press the left arrow. Now try it. Was your prediction right? Put the block back when you're done.

Compare

Change change y by 2 to change y by 20. Press the up arrow. Now change it to change y by 1. Which speed feels right? Speed is part of how the game feels — not too zippy, not too slow.

Connect

Stage 3 makes the fish GROW each time it eats. The size needs to be stored somewhere. Look at your script. Where could the fish remember its size between key presses? (Hint: it's not in the motion blocks.)

Test your stage

  • Clicking the green flag jumps the fish to the center of the stage.
  • The fish shows the Fish-a costume (not Party Hat-a) when the game starts.
  • Up arrow swims up.
  • Down arrow swims down.
  • Left arrow flips the fish and swims left.
  • Right arrow flips the fish back and swims right.
  • The project is saved (File → Save Now).
  • Design check. Hold an arrow for two seconds. Does the speed feel like you are moving the fish, or like the fish is chasing the screen?

If it breaks

  • Nothing happens when I press arrow keys. Three suspects. First, did you click the green flag? Second, is the forever block there? Third, is each if-block snapped inside the forever (not next to it)?
  • The fish rotates strangely when I press left. The set rotation style (left-right) block is missing or set to all around. Fix the block, click the green flag again.
  • The fish moves sideways but doesn't flip. The point in direction block is missing from the left or right if-block. Add it before the change x by block.
  • The fish moves but only when I press the arrow once, not when I hold it. The forever loop is probably missing. Without it, the script only checks the key one time. The forever makes it check every frame.
  • The fish flies off the screen and doesn't come back. That's normal for now — Stage 6's enemy fish use the screen edges differently. Don't worry about wrap-around today.
  • Pressing up moves the fish down, or left moves it right. Check the + and - signs in your change blocks. Up = +, down = -. Right = +, left = -.
Coach notes

The single most common failure on this stage: the if-blocks placed next to the forever instead of inside it. Walk the room after Step 4 and look at every laptop. The four if-blocks should be visibly inside the forever's center opening, not floating beside it.

The second most common: forgetting point in direction before change x. The fish moves but doesn't flip. Fish-a will visually look like it's swimming backwards. Catch this in the design check.

If a camper accidentally drops the change y by value to a negative number (e.g., for up arrow), the fish moves in the opposite direction. Use the test list ("Up arrow swims up") to catch this.

For Step 2's setup blocks, kids often skip them, click the flag, and the fish doesn't show up. That's because show is missing. The setup section is what makes the game restart-able. Don't skip it.

If a camper finishes by minute 35, push them straight into the medium stretch (speed tuning). They'll spend the rest of the hour finding their favorite value — and that's exactly what game design feels like.