Unit 2 · Lesson 2

Add danger and three lives

  • Monsters and worlds
  • 75 minutes
  • Advanced Scratch · After Pixel Quest

Take one hit at a time and return to the menu after losing.

Recommended preparation: Scratch Pixel Quest. Complete Pixel Quest or bring equivalent Scratch platformer experience. Follow one short pictured action at a time.

Your goal

Make hazards and monsters cost one life, with brief protection after each hit.

The monsters can move. Now make touching a monster or spike cost one life. One touch must never take all three lives.

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

  1. Add Hazards

    Upload the spike map

    Upload meadow-hazards.png as a new sprite named Hazards. Set x 0, y 0 and Size 100. Keep the whole transparent canvas centered; it lines up with Platforms.

    Actual Scratch editor: Upload the spike map. Upload meadow-hazards.png as a new sprite named Hazards. Set x 0, y 0 and Size 100. Keep the whole transparent canvas centered; it lines up with Platforms.
    1. Click: Point at Choose a Sprite
    2. Click: Click Upload Sprite
    Actual Scratch editor · step 1
  2. Select Stage’s thumbnail

    Add the losing screen

    Select Stage → Backdrops → Upload Backdrop. Add loss-screen.svg and name it Out of lives. Keep the earlier backdrops.

    Actual Scratch editor: Add the losing screen. Select Stage → Backdrops → Upload Backdrop. Add loss-screen.svg and name it Out of lives. Keep the earlier backdrops.
    1. Click: Open Backdrops
    2. Click: Point at Choose a Backdrop
    3. Click: Click Upload Backdrop
    4. Click: Rename this picture
    Actual Scratch editor · step 2
  3. Add Menu

    Add a way back to the menu

    Upload menu-button.svg as a new sprite named Menu. Set x 0, y -88 and Size 100. This button will appear after losing.

    Actual Scratch editor: Add a way back to the menu. Upload menu-button.svg as a new sprite named Menu. Set x 0, y -88 and Size 100. This button will appear after losing.
    1. Click: Point at Choose a Sprite
    2. Click: Click Upload Sprite
    Actual Scratch editor · step 3
  4. Select Stage’s thumbnail

    Make shared Lives

    Select Stage and make Lives. It is shared automatically. Untick its counter for now; the game will show it while playing.

    Actual Scratch editor: Make shared Lives. Select Stage and make Lives. It is shared automatically. Untick its counter for now; the game will show it while playing.
    1. Click: Make each named variable
    2. Click: Untick the variable monitor boxes
    Actual Scratch editor · step 4
  5. Select Player

    Make brief hit protection

    On Player, make Invincible For this sprite only. Untick its counter. A value above 0 means another hit must wait.

    Actual Scratch editor: Make brief hit protection. On Player, make Invincible For this sprite only. Untick its counter. A value above 0 means another hit must wait.
    1. Click: Make each named variable
    2. Click: Untick the variable monitor boxes
    Actual Scratch editor · step 5
  6. Select Stage’s thumbnail

    Start every new adventure with three lives

    Add set Lives to 3 in Stage’s green-flag and new game stacks. Hide Lives on the menu; show Lives in load level. Keep the earlier Level resets.

    Actual Scratch editor: Start every new adventure with three lives. Add set Lives to 3 in Stage’s green-flag and new game stacks. Hide Lives on the menu; show Lives in load level. Keep the earlier Level resets.
    1. Add: Build stack 1 in this order
    2. Add: Build stack 2 in this order
    3. Add: Build stack 3 in this order
    Actual Scratch editor · step 6
  7. Select Player

    Start without hit protection

    In Player’s load level receiver, add set Invincible to 0 before show. Keep the other movement and animation resets.

    Actual Scratch editor: Start without hit protection. In Player’s load level receiver, add set Invincible to 0 before show. Keep the other movement and animation resets.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 7
  8. Select Player

    Make the respawn receiver

    Copy Player’s load level stack. Change its hat to when I receive respawn. In this copy, set Invincible to 30 before show. Keep the starting position and other resets.

    Actual Scratch editor: Make the respawn receiver. Copy Player’s load level stack. Change its hat to when I receive respawn. In this copy, set Invincible to 30 before show. Keep the starting position and other resets.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 8
  9. Select Player

    Replace the old free-fall reset

    In Player’s playing loop, remove only the old y position < -170 reset branch. In its place, add if Invincible > 0 → change Invincible by -1.

    Actual Scratch editor: Replace the old free-fall reset. In Player’s playing loop, remove only the old y position < -170 reset branch. In its place, add if Invincible > 0 → change Invincible by -1.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 9
  10. Select Player

    Send one shared hit message

    After the protection countdown, add the pictured danger check. It asks for Invincible = 0 and a spike, Crawler, Bat or fall. Inside it, set GameState to hurt, then broadcast player hit.

    Actual Scratch editor: Send one shared hit message. After the protection countdown, add the pictured danger check. It asks for Invincible = 0 and a spike, Crawler, Bat or fall. Inside it, set GameState to hurt, then broadcast player hit.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 10
  11. Select Stage’s thumbnail

    Lose one life and respawn

    On Stage, add when I receive player hit → if GameState = hurt → change Lives by -1. If Lives > 0, broadcast respawn and wait, then set GameState to playing.

    Actual Scratch editor: Lose one life and respawn. On Stage, add when I receive player hit → if GameState = hurt → change Lives by -1. If Lives > 0, broadcast respawn and wait, then set GameState to playing.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 11
  12. Select Stage’s thumbnail

    Stop the adventure at zero lives

    In the Lives check’s else branch, set GameState to lost, switch backdrop to Out of lives and broadcast game ended. Do not use stop all; Menu must still respond.

    Actual Scratch editor: Stop the adventure at zero lives. In the Lives check’s else branch, set GameState to lost, switch backdrop to Out of lives and broadcast game ended. Do not use stop all; Menu must still respond.
    1. Add: Build these blocks in this order
    Actual Scratch editor · step 12
  13. Select Stage’s thumbnail

    Return to the title screen safely

    On Stage, add when I receive show menu → set GameState menu → switch backdrop Title Screen → hide Level → hide Lives.

    Actual Scratch editor: Return to the title screen safely. On Stage, add when I receive show menu → set GameState menu → switch backdrop Title Screen → hide Level → hide Lives.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 13
  14. Select Menu

    Connect the Menu button

    On Menu, build the two pictured stacks. Its green-flag loop shows Menu only when GameState = lost; otherwise hide. After show, go to front layer. Clicking Menu while lost broadcasts show menu.

    Actual Scratch editor: Connect the Menu button. On Menu, build the two pictured stacks. Its green-flag loop shows Menu only when GameState = lost; otherwise hide. After show, go to front layer. Clicking Menu while lost broadcasts show menu.
    1. Add: Build this left stack
    2. Add: Build this right stack
    Actual Scratch editor · step 14
  15. Select Hazards

    Give the spike map its receivers

    Copy Platforms’ load level and show menu stacks onto Hazards. Keep x 0, y 0, switch costume to Level and show in the load receiver.

    Actual Scratch editor: Give the spike map its receivers. Copy Platforms’ load level and show menu stacks onto Hazards. Keep x 0, y 0, switch costume to Level and show in the load receiver.
    1. Add: Build this left stack
    2. Add: Build this right stack
    Actual Scratch editor · step 15
  16. Select Player

    Hide the world after losing

    On Player, add when I receive game ended → hide. Copy this two-block receiver onto Platforms, Hazards, Crawler and Bat. Do not copy it onto the menu buttons.

    Actual Scratch editor: Hide the world after losing. On Player, add when I receive game ended → hide. Copy this two-block receiver onto Platforms, Hazards, Crawler and Bat. Do not copy it onto the menu buttons.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 16
  17. Select Player

    Test a fair hit, the loss screen and save

    Start with Lives 3. Touch a spike or monster: lose one life and return safely. At 0, the world hides and Menu returns to the title. Save as Monster Quest Lesson 06.

    Actual Scratch editor: Test a fair hit, the loss screen and save. Start with Lives 3. Touch a spike or monster: lose one life and return safely. At 0, the world hides and Menu returns to the title. Save as Monster Quest Lesson 06.
    1. Click: Run your test with the green flag
    2. Check: Check the game counters and messages
    3. Check: Run the movement or adventure test here
    Actual Scratch editor · step 17
Optional: full block guide and lesson comparison

Save your own project first. Loading an .sb3 replaces the project on screen. This comparison shows the end of lesson 6.

Download lesson 6 comparison · Download all lesson comparisons

Choose the named sprite in Scratch and read its blocks from top to bottom. Indented rows fit inside the block above. Keep one copy of each existing stack.

StageOpen the title screen
  1. Eventswhen green flag clicked
  2. Variablesset [GameState] to (menu)
  3. Variablesset [Level] to (1)
  4. Variablesset [Lives] to (3)
  5. Looksswitch backdrop to [Title Screen]
  6. Variableshide variable [Level]
  7. Variableshide variable [Lives]
  8. Eventsbroadcast [show menu]
StageBegin a new game
  1. Eventswhen I receive [new game]
  2. Variablesset [GameState] to (loading)
  3. Variablesset [Level] to (1)
  4. Variablesset [Lives] to (3)
  5. Eventsbroadcast [load level] and wait
  6. Variablesset [GameState] to (playing)
StageLoad the selected level backdrop and counters
  1. Eventswhen I receive [load level]
  2. Looksswitch backdrop to [Meadow Steps]
  3. Variablesshow variable [Level]
  4. Variablesshow variable [Lives]
StageReturn safely to the title screen
  1. Eventswhen I receive [show menu]
  2. Variablesset [GameState] to (menu)
  3. Looksswitch backdrop to [Title Screen]
  4. Variableshide variable [Level]
  5. Variableshide variable [Lives]
StageLose one life and respawn or end
  1. Eventswhen I receive [player hit]
  2. Controlif <GameState = hurt> then
  3. Variableschange [Lives] by (-1)
  4. Controlif <Lives > 0> then
  5. Eventsbroadcast [respawn] and wait
  6. Variablesset [GameState] to (playing)
  7. elseSecond slot of the same if/else block
    Variablesset [GameState] to (lost)
  8. Looksswitch backdrop to [Out of lives]
  9. Eventsbroadcast [game ended]
PlatformsLoad the level platforms costume
  1. Eventswhen I receive [load level]
  2. Motiongo to x: (0) y: (0)
  3. Looksswitch costume to [Level]
  4. Looksshow
PlatformsHide platforms on the menu
  1. Eventswhen I receive [show menu]
  2. Lookshide
PlatformsHide platforms on result screens
  1. Eventswhen I receive [game ended]
  2. Lookshide
PlayerPlace the explorer at the level start
  1. Eventswhen I receive [load level]
  2. Motiongo to x: (-210) y: (-126)
  3. Variablesset [XSpeed] to (0)
  4. Variablesset [YSpeed] to (0)
  5. Variablesset [Grounded] to (0)
  6. Variablesset [JumpHeld] to (0)
  7. Variablesset [StepY] to (-1)
  8. Variablesset [AnimTicks] to (0)
  9. Looksswitch costume to [Walk 1]
  10. Motionset rotation style [left-right]
  11. Motionpoint in direction (90)
  12. Variablesset [Invincible] to (0)
  13. Looksshow
PlayerHide the explorer on the title screen
  1. Eventswhen I receive [show menu]
  2. Lookshide
PlayerMove one horizontal pixel at a time
  1. My Blocksdefine move horizontally
  2. Controlrepeat (abs of (XSpeed))
  3. Motionchange x by ((XSpeed) / (abs of (XSpeed)))
  4. Controlif <touching [Platforms]?> then
  5. Motionchange x by ((0) - ((XSpeed) / (abs of (XSpeed))))
  6. Controlif <x position > 225> then
  7. Motionset x to (225)
  8. Controlif <x position < -225> then
  9. Motionset x to (-225)
PlayerLand on solid platforms a pixel at a time
  1. My Blocksdefine move vertically
  2. Variablesset [Grounded] to (0)
  3. Variablesset [StepY] to (-1)
  4. Controlif <YSpeed > 0> then
  5. Variablesset [StepY] to (1)
  6. Controlrepeat (abs of (round (YSpeed)))
  7. Motionchange y by (StepY)
  8. Controlif <touching [Platforms]?> then
  9. Motionchange y by ((0) - (StepY))
  10. Controlif <StepY < 0> then
  11. Variablesset [Grounded] to (1)
  12. Variablesset [YSpeed] to (0)
PlayerRead keys, move, jump and check the active adventure
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <GameState = playing> then
  4. Variablesset [XSpeed] to (0)
  5. Controlif <key [left arrow] pressed?> then
  6. Variableschange [XSpeed] by (-4)
  7. Controlif <key [right arrow] pressed?> then
  8. Variableschange [XSpeed] by (4)
  9. My Blocksmove horizontally
  10. Controlif <key [space] pressed?> then
  11. Controlif <<JumpHeld = 0> and <Grounded = 1>> then
  12. Variablesset [YSpeed] to (11)
  13. Variablesset [JumpHeld] to (1)
  14. elseSecond slot of the same if/else block
    Variablesset [JumpHeld] to (0)
  15. Variableschange [YSpeed] by (-0.7)
  16. Controlif <YSpeed < -12> then
  17. Variablesset [YSpeed] to (-12)
  18. My Blocksmove vertically
  19. Controlif <XSpeed < 0> then
  20. Motionpoint in direction (-90)
  21. Controlif <XSpeed > 0> then
  22. Motionpoint in direction (90)
  23. Controlif <Grounded = 0> then
  24. Looksswitch costume to [Walk 2]
  25. elseSecond slot of the same if/else block
    Controlif <XSpeed = 0> then
  26. Looksswitch costume to [Walk 1]
  27. Variablesset [AnimTicks] to (0)
  28. elseSecond slot of the same if/else block
    Variableschange [AnimTicks] by (1)
  29. Controlif <AnimTicks > 3> then
  30. Looksnext costume
  31. Variablesset [AnimTicks] to (0)
  32. Controlif <Invincible > 0> then
  33. Variableschange [Invincible] by (-1)
  34. Controlif <<Invincible = 0> and <<<touching [Hazards]?> or <touching [Crawler]?>> or <<touching [Bat]?> or <y position < -170>>>> then
  35. Variablesset [GameState] to (hurt)
  36. Eventsbroadcast [player hit]
PlayerReset motion and give brief hit protection
  1. Eventswhen I receive [respawn]
  2. Motiongo to x: (-210) y: (-126)
  3. Variablesset [XSpeed] to (0)
  4. Variablesset [YSpeed] to (0)
  5. Variablesset [Grounded] to (0)
  6. Variablesset [JumpHeld] to (0)
  7. Variablesset [StepY] to (-1)
  8. Variablesset [AnimTicks] to (0)
  9. Looksswitch costume to [Walk 1]
  10. Motionset rotation style [left-right]
  11. Motionpoint in direction (90)
  12. Variablesset [Invincible] to (30)
  13. Looksshow
PlayerHide the explorer when a run ends
  1. Eventswhen I receive [game ended]
  2. Lookshide
CrawlerPlace and reset Crawler
  1. Eventswhen I receive [load level]
  2. Motiongo to x: (170) y: (-132)
  3. Variablesset [AnimTicks] to (0)
  4. Variablesset [Speed] to (2)
  5. Looksswitch costume to [1]
  6. Looksshow
CrawlerHide Crawler on the menu
  1. Eventswhen I receive [show menu]
  2. Lookshide
CrawlerPatrol the floor and animate the crawler
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <GameState = playing> then
  4. Motionchange x by (Speed)
  5. Controlif <x position > 206> then
  6. Variablesset [Speed] to (-2)
  7. Controlif <x position < 154> then
  8. Variablesset [Speed] to (2)
  9. Variableschange [AnimTicks] by (1)
  10. Controlif <AnimTicks > 3> then
  11. Looksnext costume
  12. Variablesset [AnimTicks] to (0)
CrawlerHide Crawler when the run ends
  1. Eventswhen I receive [game ended]
  2. Lookshide
BatPlace and reset Bat
  1. Eventswhen I receive [load level]
  2. Motiongo to x: (0) y: (45)
  3. Variablesset [AnimTicks] to (0)
  4. Variablesset [Phase] to (0)
  5. Looksswitch costume to [1]
  6. Looksshow
BatHide Bat on the menu
  1. Eventswhen I receive [show menu]
  2. Lookshide
BatFly in a loop and animate the bat
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <GameState = playing> then
  4. Variableschange [Phase] by (8)
  5. Motiongo to x: ((70) * (sin of (Phase))) y: ((45) + ((18) * (cos of (Phase))))
  6. Variableschange [AnimTicks] by (1)
  7. Controlif <AnimTicks > 3> then
  8. Looksnext costume
  9. Variablesset [AnimTicks] to (0)
BatHide Bat when the run ends
  1. Eventswhen I receive [game ended]
  2. Lookshide
HazardsLoad the level hazards costume
  1. Eventswhen I receive [load level]
  2. Motiongo to x: (0) y: (0)
  3. Looksswitch costume to [Level]
  4. Looksshow
HazardsHide hazards on the menu
  1. Eventswhen I receive [show menu]
  2. Lookshide
HazardsHide hazards on result screens
  1. Eventswhen I receive [game ended]
  2. Lookshide
StartShow New Game only on the menu
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <GameState = menu> then
  4. Looksshow
  5. elseSecond slot of the same if/else block
    Lookshide
StartStart from the menu
  1. Eventswhen this sprite clicked
  2. Controlif <GameState = menu> then
  3. Eventsbroadcast [new game]
MenuShow Menu only when it can be used
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <GameState = lost> then
  4. Looksshow
  5. Looksgo to [front] layer
  6. elseSecond slot of the same if/else block
    Lookshide
MenuUse the Menu control
  1. Eventswhen this sprite clicked
  2. Controlif <GameState = lost> then
  3. Eventsbroadcast [show menu]
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…