Week 3 of 6 ยท Lesson for Students

Loops, Conditionals & Sensing

Today you will use repeat loops, make your program make decisions with if/else, and detect when sprites touch each other โ€” the key ingredients of every game.

1
2
3
โœ“
4
5
โœ“
6
7
๐Ÿ†
1
Open your Week 2 project
You need your keyboard-controlled player sprite from last week
  1. Open Scratch and load your Week 2 project (the chase game with arrow key controls).
  2. Test it โ€” pressing arrow keys should still move your player sprite.
  3. You need at least two sprites: your player and one other sprite (enemy/friend).
โš ๏ธ Watch out!

If you cannot find last week's project, ask your teacher. As a fallback: create a new project, add two sprites, and copy the four arrow-key scripts from memory.

2
Understand the forever loop
A forever loop runs its contents over and over โ€” permanently
  1. Click on your player sprite.
  2. In the Block Palette, click the yellow-orange "Control" section.
  3. Find the forever block โ€” it looks like a C-shape with no bottom.
  4. Drag it to an empty space in the Scripts Area. Do not connect it to anything yet.
  5. Look at its shape: blocks you drop inside the C will run forever, one after another, looping back to the top.
๐Ÿ‘€ Key idea

A forever loop never stops on its own โ€” it keeps running until you press the red stop button. We will use it to constantly check whether the player has touched something.

3
Add collision detection
Use a forever loop with an if block to check if sprites are touching
  1. With your player sprite selected, add this new script to an empty area of the Scripts Area:
  2. Drag when ๐Ÿšฉ clicked from Events. Then snap a forever block below it.
  3. From the Control section, drag an if <> then block and drop it inside the forever block (into the C-shaped gap).
  4. From the "Sensing" section (light blue), find touching [Sprite2]?. Drag it into the hexagonal gap on the if block.
  5. Click the dropdown on the touching block and choose your second sprite's name.
  6. Inside the if block, add say [Ouch!] for 1 second.
  7. Click the green flag, then move your player into the other sprite. It should say "Ouch!"
๐Ÿ‘€ What you should see

Your script looks like: flag โ†’ forever โ†’ if touching [other sprite] then say "Ouch!". When the two sprites overlap, the speech bubble appears.

โš ๏ธ Watch out!
  • The if block must be inside the forever loop โ€” dragged into the C-shaped gap. If it sits below the forever block instead of inside it, Scratch will only check once at the start, not continuously.
  • The touching block goes in the hexagonal slot in the if block โ€” not snapped below it.
  • If it says "touching [mouse-pointer]?" by default, click the dropdown and choose your sprite's name.
  • If the sprites are on top of each other when the flag starts, the "Ouch!" will fire immediately. Drag them apart before testing.
๐Ÿ” Check Your Progress โ€” After Step 3

Before moving on, check your collision script looks correct:

  • The forever loop is connected to a "when flag clicked" hat block
  • The if block is inside the forever loop
  • The "touching" condition is inside the if's hexagonal slot
  • Moving the player into the other sprite triggers a reaction
๐Ÿ†˜ Something looks wrong?
  • The if block is below the forever, not inside it: Drag the if block and drop it directly into the C-shaped gap of the forever block. Look for the white highlight before releasing.
  • The touching block is snapped below the if, not inside: The hexagonal slot on the "if" block is where it goes. Drag the touching block to that diamond-shaped gap.
  • No reaction when sprites touch: Check the sprite name in the dropdown matches your second sprite exactly. Open the Sprite Pane and note the exact name shown.
4
Broadcast "game-over" on collision
Instead of just saying something, end the game properly
  1. Remove the say [Ouch!] block from inside the if (right-click โ†’ Delete Block).
  2. Replace it with broadcast [game-over] (from Events โ€” create a new message called "game-over").
  3. Below the broadcast, add stop [this script] from the Control section.
  4. Now add a new separate script to react to game over: when I receive [game-over] โ†’ say [Game Over!] for 2 seconds.
  5. Test: run the game and move your player into the enemy.
โš ๏ธ Watch out!
  • stop [this script] only stops the collision-checking loop. The player can still be moved with arrow keys. That is fine for now โ€” we fix this in Week 6 with a proper state machine.
  • Make sure "game-over" is spelled the same in both the broadcast and the receive block (with a hyphen, lowercase).
5
Build the Maze backdrop
Draw a simple maze and use colour-sensing to stop the player going through walls
  1. Click the backdrop square at the bottom-right of the stage area.
  2. Click the Paint icon (the pencil) to create a new blank backdrop.
  3. Choose the Rectangle tool and pick a bright colour (e.g. dark blue). Draw thick walls to make a simple maze.
  4. Leave a start area, an end area, and paths wide enough for your sprite to fit through.
  5. Note down the exact wall colour โ€” you will need to sense it in the next step.
  6. Click "Code" at the top-left to return to the Scripts Area.
โš ๏ธ Watch out!
  • Keep walls thick (at least 20px). If walls are thin, a fast-moving sprite can skip through them in one frame.
  • Use a single solid colour for all walls โ€” do not use gradients or multiple colours. Scratch's colour sensing needs an exact match.
  • Leave the floor/background white or a clearly different colour from the walls.
๐Ÿ” Check Your Progress โ€” After Step 5
  • You have a maze backdrop with clear, thick walls in one solid colour
  • Your player sprite is small enough to fit through the maze paths
  • Collision detection with the enemy sprite is still working
๐Ÿ†˜ Something looks wrong?
  • Sprite is too big for the maze: Click the player sprite, go to its Properties (top of the Sprite Pane), and reduce the size number โ€” try 50 or less.
  • I can't get back to the code from the backdrop editor: Click your sprite in the Sprite Pane first, then click the "Code" tab at the top.
  • My maze looks bad: That's OK โ€” functionality matters more than art at this stage. A simple rectangle maze is completely fine.
6
Add wall collision using colour sensing
Send the player back to the start when they touch a wall
  1. Click your player sprite. Find your existing collision forever loop.
  2. Add a second if block inside the forever loop, below the first one (for enemy collision).
  3. From Sensing, drag touching color [โ– ]? into its hexagonal slot.
  4. Click the colour square on the "touching color" block. A colour picker opens.
  5. Click the eyedropper icon (bottom of the picker), then click on a wall in your maze on the stage. The colour will be captured exactly.
  6. Inside this new if block, add go to x: [start x] y: [start y] with the coordinates of your start position.
  7. Test: run the game and walk into a wall. The sprite should jump back to start.
โš ๏ธ Watch out!
  • You must use the eyedropper to pick the wall colour โ€” do not try to pick it manually from the colour wheel. The colour must be an exact pixel match.
  • After clicking the eyedropper, your cursor becomes a crosshair. Click directly on a wall pixel in the stage. If you click the floor by mistake, the sprite will warp back whenever it touches the floor instead of the wall!
  • If the sprite teleports back immediately when the game starts, it may already be touching wall colour. Drag the sprite to the centre of the start area first.
  • The "go to x y" coordinates should be your start position. Drag the sprite to the start area and read the x/y values from the Sprite Pane.
7
Add a goal and win condition
Place a sprite at the end of the maze โ€” reaching it wins the game
  1. Add a new sprite as the "goal" (a star, trophy, or any sprite). Place it at the end of your maze.
  2. On your player sprite, add a third if block inside the forever loop: touching [Goal sprite]?.
  3. Inside it, add broadcast [you-win] and stop [this script].
  4. On the goal sprite, add: when I receive [you-win] โ†’ say [You did it!] for 3 seconds.
  5. Test the full maze: navigate from start to the goal without touching walls.
โš ๏ธ Watch out!
  • The goal sprite must be placed in an area the player can actually reach without going through walls.
  • If touching the goal also triggers the wall collision (because the goal overlaps a wall), move the goal slightly so it sits entirely on the floor colour.
  • The order of your three if blocks inside the forever loop matters if sprites overlap in complex ways โ€” but for this project any order is fine.
๐Ÿ”

Fantastic โ€” Week 3 complete!

You have used forever loops, if blocks, and colour sensing to build a working maze game. Next week you will add variables to track score and lives.