Unit 1 · Lesson 4

Make Collisions Fair with Lives and a Cooldown

  • Build the Rescue Systems
  • 30 minutes
  • Advanced Scratch · Ages 8–11

Open Space Rescue 3. Give the ship three lives and one second of protection after each hit.

Prerequisite: Scratch Meteor Challenge. Complete Meteor Challenge before adding rescue missions, custom blocks, and clones.

Your goal

Lose one life per hit and protect the ship briefly afterward.

Do one step. Match the picture. Then move on.

  1. Select Stage’s thumbnail

    Make Lives and HurtUntil

    In Variables, make Lives and HurtUntil. Stage makes them shared. Tick Lives; leave HurtUntil hidden.

    Actual Scratch Variables palette with Stage selected in Protect Lives with Collision Rules.
    1. Click: Make Lives and HurtUntil.
    2. Click: Show Lives.
    3. Check: Leave HurtUntil hidden.
    Actual Scratch editor · step 1
  2. Select Stage’s thumbnail

    Give a new game three lives

    In receive new game, add set Lives to 3, then Sensing → reset timer, immediately before broadcast prepare mission. Do not reset Lives inside each mission.

    Actual Scratch close-up of Stage's Code area: when I receive [new game]; stop [other scripts in sprite]; set [GameState] to (resetting); set [Mission] to (1); set [TotalRescued] to (0); set [Lives] to (3); reset timer; broadcast [prepare mission].
    1. Add: Set Lives to 3, then reset the timer.
    Actual Scratch editor · step 2
  3. Select Stage’s thumbnail

    Reset the protection value

    Inside define prepare mission values, add set HurtUntil to 0 after set Carrying to 0. Keep the backdrop choice below it.

    Actual Scratch close-up of Stage's Code area: set [HurtUntil] to (0).
    1. Add: Reset HurtUntil for each mission.
    Actual Scratch editor · step 3
  4. Select Spaceship

    Add the hit sound

    Open Sounds. Upload hit.wav and name it Hit. Return to Code and build receive player hit → start sound Hit.

    Actual Scratch close-up of Spaceship's Code area: when I receive [player hit]; start sound [Hit].
    1. Add: Build this Spaceship message stack.
    Actual Scratch editor · step 4
  5. Select Meteor

    Add the collision question

    In the existing clone loop, after change y and before the bottom-edge check, add if. Use GameState = playing and another and containing touching Spaceship? and timer > HurtUntil.

    Actual Scratch close-up of Meteor's Code area: if <<GameState = playing> and <<touching [Spaceship]?> and <timer > HurtUntil>>> then.
    1. Add: Build this Meteor code stack.
    Actual Scratch editor · step 5
  6. Select Meteor

    Protect the next second

    Inside that collision if, first add set HurtUntil to timer + 1. Put the blue timer oval inside the left input of a green + block.

    Actual Scratch close-up of Meteor's Code area: set [HurtUntil] to (timer + 1); change [Lives] by (-1).
    1. Add: Build this Meteor code stack.
    Actual Scratch editor · step 6
  7. Select Meteor

    Take exactly one life

    Next add change Lives by -1 → respawn Mission + 3 → broadcast player hit. Leave the existing bottom-edge respawn check below this collision if.

    Actual Scratch close-up of Meteor's Code area: call respawn (Mission + 3); if <GameState = playing> then; if <<GameState = playing> and <<touching [Spaceship]?> and <timer > HurtUntil>>> then; call respawn (Mission + 3); broadcast [player hit]; if <Lives = 0> then; set [GameState] to (lost); broadcast [end mission].
    1. Add: Take one life, respawn, then announce player hit.
    Actual Scratch editor · step 7
  8. Select Meteor

    End the game at zero lives

    Still inside the collision if, add if Lives = 0 → set GameState to lost → broadcast end mission. Put this after broadcast player hit.

    Actual Scratch close-up of Meteor's Code area: call respawn (Mission + 3); if <GameState = playing> then; if <<GameState = playing> and <<touching [Spaceship]?> and <timer > HurtUntil>>> then; call respawn (Mission + 3); broadcast [player hit]; if <Lives = 0> then; set [GameState] to (lost); broadcast [end mission].
    1. Add: Lose only when Lives reaches zero.
    Actual Scratch editor · step 8
  9. Select Spaceship

    Show the temporary shield

    After the Carrying message if/else in play mission, add if/else timer < HurtUntil. Top: set ghost effect to 45. Else: clear graphic effects.

    Actual Scratch close-up of Spaceship's Code area: if <timer < HurtUntil> then / else; set [ghost] effect to (45); clear graphic effects.
    1. Add: Build this Spaceship code stack.
    Actual Scratch editor · step 9
  10. Select Stage’s thumbnail

    Choose the losing screen

    Build receive end mission → if GameState = lost → if/else Lives = 0. Top: switch backdrop to Out of lives. Else: Time is up. The timer arrives next lesson.

    Actual Scratch close-up of Stage's Code area: when I receive [end mission]; if <GameState = lost> then; if <Lives = 0> then / else; switch backdrop to [Out of lives]; switch backdrop to [Time is up].
    1. Add: Build this Stage message stack.
    Actual Scratch editor · step 10
  11. In Scratch

    Test the shield

    Click the green flag. Touch a meteor. Lives should go from 3 to 2 and the ship should look faded for about one second. It can still move.

    Actual Scratch Stage during the collision and lives lesson with a visible falling Meteor clone.
    1. Click: Click the green flag to test.
    2. Click: Use Start / Restart for a fresh game.
    Actual Scratch editor · step 11
  12. In Scratch

    Test losing and restarting

    After three separate hits, Out of lives should appear. Click Start / Restart: Lives returns to 3. Do not use stop all; the button must keep working.

    Actual Scratch close-up of Stage's Code area: when I receive [new game]; stop [other scripts in sprite]; set [GameState] to (resetting); set [Mission] to (1); set [TotalRescued] to (0); set [Lives] to (3); reset timer; broadcast [prepare mission].
    1. Check: Check that new game restores Lives to 3.
    Actual Scratch editor · step 12
  13. In Scratch

    Save your progress

    Choose File → Save to your computer. Name your file Space Rescue 4. Keep your own save for the next lesson.

    Actual Scratch open File menu including Load from your computer and Save to your computer.
    1. Click: Open File.
    2. Click: Choose Save to your computer.
    Actual Scratch editor · step 13
Lost your place? Optional comparison files

Save your own work first. Loading an .sb3 replaces the project on screen.

Previous lesson comparison

Finished lesson 4 comparison

Optional: full block guide and finished game

Build with blocks · Lesson 4

See the blocks. Try the finished game.

Build these blocks in Scratch. This guide is read-only.

Your lesson’s block guide

Choose the matching sprite in Scratch, then build its stack from top to bottom.

Indented rows fit inside the block above. else marks the second slot of one if/else block.

My Blocks inputs pass values into one call. FallSpeed is a sprite-only variable: each meteor clone keeps its own value.

StageStage: define prepare mission values
  1. My Blocksdefine prepare mission values
  2. Variablesset [Goal] to (2)
  3. Variablesset [RescuedMission] to (0)
  4. Variablesset [Carrying] to (0)
  5. Variablesset [HurtUntil] to (0)
  6. Controlif <Mission = 1> then
  7. Looksswitch backdrop to [Mission 1]
  8. elseSecond slot of the same if/else block
    Controlif <Mission = 2> then
  9. Looksswitch backdrop to [Mission 2]
  10. elseSecond slot of the same if/else block
    Looksswitch backdrop to [Mission 3]
StageStage: when green flag clicked
  1. Eventswhen green flag clicked
  2. Eventsbroadcast [new game]
StageStage: when I receive [new game]
  1. Eventswhen I receive [new game]
  2. Controlstop [other scripts in sprite]
  3. Variablesset [GameState] to (resetting)
  4. Variablesset [Mission] to (1)
  5. Variablesset [TotalRescued] to (0)
  6. Variablesset [Lives] to (3)
  7. Sensingreset timer
  8. Eventsbroadcast [prepare mission]
StageStage: when I receive [prepare mission]
  1. Eventswhen I receive [prepare mission]
  2. Controlstop [other scripts in sprite]
  3. Variablesset [GameState] to (resetting)
  4. Eventsbroadcast [reset world] and wait
  5. My Blockscall prepare mission values
  6. Variablesset [GameState] to (playing)
  7. Eventsbroadcast [play mission]
StageStage: when I receive [mission clear]
  1. Eventswhen I receive [mission clear]
  2. Controlif <GameState = between> then
  3. Looksswitch backdrop to [Mission complete]
  4. Eventsbroadcast [end mission] and wait
StageStage: when I receive [end mission]
  1. Eventswhen I receive [end mission]
  2. Controlif <GameState = lost> then
  3. Controlif <Lives = 0> then
  4. Looksswitch backdrop to [Out of lives]
  5. elseSecond slot of the same if/else block
    Looksswitch backdrop to [Time is up]
SpaceshipSpaceship: define move ship (dx) (dy)
  1. My Blocksdefine move ship (dx) (dy)
  2. Motionchange x by (dx)
  3. Motionchange y by (dy)
  4. Controlif <x position > 210> then
  5. Motionset x to (210)
  6. Controlif <x position < -210> then
  7. Motionset x to (-210)
  8. Controlif <y position > 125> then
  9. Motionset y to (125)
  10. Controlif <y position < -155> then
  11. Motionset y to (-155)
SpaceshipSpaceship: when I receive [reset world]
  1. Eventswhen I receive [reset world]
  2. Controlstop [other scripts in sprite]
  3. Lookshide
  4. Lookssay []
  5. Looksclear graphic effects
  6. Motiongo to x: (0) y: (-130)
SpaceshipSpaceship: when I receive [play mission]
  1. Eventswhen I receive [play mission]
  2. Looksshow
  3. Controlforever
  4. Controlif <GameState = playing> then
  5. Controlif <key [right arrow] pressed?> then
  6. My Blockscall move ship (7) (0)
  7. Controlif <key [left arrow] pressed?> then
  8. My Blockscall move ship (-7) (0)
  9. Controlif <key [up arrow] pressed?> then
  10. My Blockscall move ship (0) (7)
  11. Controlif <key [down arrow] pressed?> then
  12. My Blockscall move ship (0) (-7)
  13. Controlif <Carrying = 1> then
  14. Lookssay [Passenger aboard!]
  15. elseSecond slot of the same if/else block
    Lookssay []
  16. Controlif <timer < HurtUntil> then
  17. Looksset [ghost] effect to (45)
  18. elseSecond slot of the same if/else block
    Looksclear graphic effects
SpaceshipSpaceship: when I receive [end mission]
  1. Eventswhen I receive [end mission]
  2. Controlstop [other scripts in sprite]
  3. Lookshide
  4. Lookssay []
  5. Looksclear graphic effects
SpaceshipSpaceship: when I receive [player hit]
  1. Eventswhen I receive [player hit]
  2. Soundstart sound [Hit]
AstronautAstronaut: when I receive [reset world]
  1. Eventswhen I receive [reset world]
  2. Controlstop [other scripts in sprite]
  3. Lookshide
  4. Lookssay []
  5. Motiongo to x: (pick random (-190) to (190)) y: (pick random (0) to (45))
AstronautAstronaut: when I receive [play mission]
  1. Eventswhen I receive [play mission]
  2. Looksshow
  3. Controlforever
  4. Controlif <<GameState = playing> and <<Carrying = 0> and <touching [Spaceship]?>>> then
  5. Variablesset [Carrying] to (1)
  6. Lookshide
  7. Soundstart sound [Rescue]
AstronautAstronaut: when I receive [rescue delivered]
  1. Eventswhen I receive [rescue delivered]
  2. Controlif <GameState = playing> then
  3. Motiongo to x: (pick random (-190) to (190)) y: (pick random (0) to (45))
  4. Looksshow
AstronautAstronaut: when I receive [end mission]
  1. Eventswhen I receive [end mission]
  2. Controlstop [other scripts in sprite]
  3. Lookshide
BaseStationBaseStation: when I receive [reset world]
  1. Eventswhen I receive [reset world]
  2. Controlstop [other scripts in sprite]
  3. Lookshide
  4. Motiongo to x: (0) y: (-140)
BaseStationBaseStation: when I receive [play mission]
  1. Eventswhen I receive [play mission]
  2. Looksshow
  3. Controlforever
  4. Controlif <<GameState = playing> and <<Carrying = 1> and <touching [Spaceship]?>>> then
  5. Variablesset [Carrying] to (0)
  6. Variableschange [RescuedMission] by (1)
  7. Variableschange [TotalRescued] by (1)
  8. Controlif <RescuedMission = Goal> then
  9. Variablesset [GameState] to (between)
  10. Eventsbroadcast [mission clear]
  11. elseSecond slot of the same if/else block
    Eventsbroadcast [rescue delivered]
  12. Soundstart sound [Rescue]
BaseStationBaseStation: when I receive [end mission]
  1. Eventswhen I receive [end mission]
  2. Controlstop [other scripts in sprite]
  3. Lookshide
MeteorMeteor: define respawn (speed)
  1. My Blocksdefine respawn (speed)
  2. Variablesset [FallSpeed · this sprite only] to (speed)
  3. Motiongo to x: (pick random (-205) to (205)) y: (180)
  4. Looksshow
MeteorMeteor: when I receive [reset world]
  1. Eventswhen I receive [reset world]
  2. Lookshide
  3. Controlstop [other scripts in sprite]
  4. Controldelete this clone
MeteorMeteor: when I receive [end mission]
  1. Eventswhen I receive [end mission]
  2. Lookshide
  3. Controlstop [other scripts in sprite]
  4. Controldelete this clone
MeteorMeteor: when I receive [play mission]
  1. Eventswhen I receive [play mission]
  2. Lookshide
  3. Controlrepeat (1)
  4. Controlcreate clone of [myself]
MeteorMeteor: when I start as a clone
  1. Controlwhen I start as a clone
  2. My Blockscall respawn (Mission + 3)
  3. Controlforever
  4. Controlif <GameState = playing> then
  5. Motionchange y by (0 - FallSpeed)
  6. Controlif <<GameState = playing> and <<touching [Spaceship]?> and <timer > HurtUntil>>> then
  7. Variablesset [HurtUntil] to (timer + 1)
  8. Variableschange [Lives] by (-1)
  9. My Blockscall respawn (Mission + 3)
  10. Eventsbroadcast [player hit]
  11. Controlif <Lives = 0> then
  12. Variablesset [GameState] to (lost)
  13. Eventsbroadcast [end mission]
  14. Controlif <<GameState = playing> and <y position < -175>> then
  15. My Blockscall respawn (Mission + 3)
Start / RestartStart / Restart: when green flag clicked
  1. Eventswhen green flag clicked
  2. Motiongo to x: (155) y: (150)
  3. Looksshow
Start / RestartStart / Restart: when this sprite clicked
  1. Eventswhen this sprite clicked
  2. Eventsbroadcast [new game]
Download lesson 4 checkpoint

Save your current Scratch project first. Then choose File → Load from your computer and select this .sb3 file.

Open the Scratch editor ↗

Opening the editor does not load the download automatically.

Finished game preview

This is the completed game, including features from later lessons. It does not run this lesson’s checkpoint.

Choose the green flag, then use all four arrow keys inside the Scratch player. Open your checkpoint in Scratch to test the blocks for this lesson.

Open the finished game in a new tab ↗
END OF LESSON

Ready for the next step?

Return here anytime to review.

Comments

Lesson discussion

Ask a question or help a classmate. Mr. S can reply here.

Loading discussion…