IF THIS, THEN THAT

Teach your game to make decisions!

Lesson: T1 — L08 Time: ~75 min Tool: Scratch Outcomes: G5-1, G5-2, G5-4
LEARNING SUPPORT VERSION

1 Games Make Decisions

Words to Know

Conditional
A rule that checks if something is true
If-Then
If something happens, then do this
If-Then-Else
If something happens, do this; otherwise, do something else
Flowchart
A diagram that shows decisions using shapes and arrows
Games are constantly making decisions. Every moment, the game is checking: "Is the player touching the enemy? Has the timer run out? Did they collect enough coins?" These checks are called conditionals.
Real-life IF-THEN examples:
- IF it is raining THEN bring an umbrella
- IF you are hungry THEN eat lunch
- IF the light is red THEN stop
Game IF-THEN examples:
- IF player touches lava THEN lose a life
- IF score is greater than 100 THEN show "You Win!"
- IF you have enough coins THEN buy a power-up ELSE keep collecting
Tell your partner an IF-THEN rule from your favourite game. For example: "In Mario, IF you touch a mushroom THEN you grow bigger."
I can make up an IF-THEN sentence about a game.

2 Draw a Flowchart

A flowchart is a diagram that shows how decisions work. It uses special shapes:
Oval = Start or End
Diamond = Decision (yes or no question)
Rectangle = Action (something that happens)
Example flowchart:

[ Start ]

◊ Player touches enemy? ◊
↓ YES            ↓ NO
[ Lose 1 life ]     [ Keep playing ]

◊ Lives = 0? ◊
↓ YES            ↓ NO
[ Game Over ]     [ Keep playing ]
My Flowchart
Draw or write your own flowchart with at least 1 decision (diamond shape). It can be about any game rule, e.g. collecting a coin, touching an enemy, or reaching a goal.
Start → ... → Decision: ...? → YES: ... / NO: ... → ...
Not sure what to draw? Try this: Start → Player touches coin? → YES: Score +10 / NO: Keep moving. That's a complete flowchart!
I have drawn a flowchart with at least 1 decision (diamond shape).

3 If-Then in Scratch

Open your L07 Scratch project (the one with the score variable and the star).
First, let's add a bad character (an enemy) to the game:
Step 1: Click the Choose a Sprite button (bottom-right of stage, cat face with + icon). The sprite library opens.
Step 2: Search for Crab (or "Ghost" if you prefer). Click it to add it. The enemy sprite appears on the stage.
Step 3: Drag the crab to a spot on the stage away from the cat. The enemy is positioned and ready.
Now click the cat sprite thumbnail below the stage. Important: The code we write goes on the CAT, not the crab!
Build an if-then block that makes the cat react when it touches the enemy:
Step 4: You should already have a "when green flag clicked" with a "forever" block from last lesson. We will add inside the forever block. If you don't have one, create: Events → "when green flag clicked", then Control → "forever".
Step 5: From Control (orange section), drag "if < > then" and place it inside the forever block. This is a decision block — it checks a condition every frame.
Step 6: From Sensing (light blue section), drag "touching [mouse-pointer]?" into the diamond-shaped hole in the if block. Click the dropdown and change it to Crab. Now it reads: "if touching Crab? then".
Step 7: From Looks (purple section), drag "say [Hello!] for [2] seconds" inside the if block. Change "Hello!" to Ouch! and change 2 to 1. The cat will say "Ouch!" for 1 second when touching the crab.
Step 8: From Variables (orange section), drag "change Score by 1" inside the if block (under the say block). Change the 1 to -5. Negative number! The score goes DOWN by 5 when touching the enemy.
when green flag clicked
set Score to 0
forever
if touching Crab? then
say Ouch! for 1 seconds
change Score by -5
Test it! Click the green flag. Move the cat into the crab. The cat should say "Ouch!" and the score should drop by 5.
Stuck? Make sure you selected the CAT sprite (not the crab). The code goes on the cat. Check that the dropdown in the sensing block says "Crab" (not "mouse-pointer").
When the cat touches the enemy, it says "Ouch!" and the score decreases.

4 Add Lives

Now let's add a Lives variable so the game can end when the player runs out of chances.
Step 1: Click Variables (orange section) in the Block Palette.
Step 2: Click Make a Variable. Type Lives and click OK. A Lives display appears on the stage next to the Score display.
Step 3: In your "when green flag clicked" stack, add "set Lives to 3" right after "set Score to 0". Now the game starts with 3 lives every time.
Step 4: Inside the "if touching Crab? then" block, add "change Lives by -1" (after the "change Score by -5" block). Every time the cat touches the crab, it loses 1 life.
Now add a Game Over check. After the "if touching Crab" block (but still inside the forever), add another if block:
Step 5: From Control, drag another "if < > then" inside the forever block, below the first if block.
Step 6: From Operators (green section), drag "[ ] = [ ]" into the diamond hole. In the left box, drag the "Lives" variable (from Variables). In the right box, type 0. Now it reads: "if Lives = 0 then".
Step 7: Inside this new if block, add from Looks: "say [Game Over!] for [3] seconds".
Step 8: Below the say block (still inside the if), add from Control: "stop [all]". This ends the entire game.
when green flag clicked
set Score to 0
set Lives to 3
forever
if touching Crab? then
say Ouch! for 1 seconds
change Score by -5
change Lives by -1
if Lives = 0 then
say Game Over! for 3 seconds
stop all
Test it! Click the green flag. Move the cat into the crab 3 times. The lives should count down: 3, 2, 1, 0. When lives reach 0, the cat says "Game Over!" and everything stops.
Game ending too fast? The cat might be touching the crab multiple times in one second. Try dragging the crab further away so you have time to move between touches.
I have a Lives variable. When lives reach 0, the game stops.

5 Extension: Add a Simple Jump

Finished early? Try adding a simple jump to your cat! This is optional.
Step 1: From Events, drag "when [space] key pressed" into a new area of the Coding Area. This listens for the space bar.
Step 2: From Motion, drag "change y by [10]" and snap it underneath. Change 10 to 80. The cat jumps up 80 pixels.
Step 3: From Control, drag "wait [1] seconds" and snap it underneath. Change 1 to 0.3. The cat pauses in the air for a moment.
Step 4: From Motion, drag "change y by [10]" and snap it underneath. Change 10 to -80. The cat falls back down! This creates a simple jump effect.
when space key pressed
change y by 80
wait 0.3 seconds
change y by -80
Save your project: Click the project name at the top and rename it to: L08 - If Then - [Your Name]
What I Learned
Write 1-2 sentences about what you learned today.
Today I learned that games make decisions using... When the cat touches the enemy, it... I also added lives so...