Unit 2 · Lesson 1

Meet an enemy

  • Enemies and battle choices
  • 60 minutes
  • Advanced Scratch · After Monster Quest

Start a battle and introduce its health counters.

Recommended preparation: Scratch Monster Quest. Complete Monster Quest or bring equivalent Scratch experience. Build from a blank project, one small pictured action at a time.

Your goal

Switch from exploration to a separate battle state.

A turn-based battle waits for a choice. The world stops moving while the hero and enemy take turns.

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

  1. Select Stage’s thumbnail

    Upload the battle background

    Select Stage → Backdrops. Upload battle-backdrop.png. Rename the backdrop Battle Arena. Keep the earlier backdrops.

    Actual Scratch editor: Upload the battle background. Select Stage → Backdrops. Upload battle-backdrop.png. Rename the backdrop Battle Arena. 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 1
  2. Add Enemy

    Upload the first enemy

    Upload slime-idle.png as a new sprite. Rename the sprite Enemy. Rename its first costume Slime 1.

    Actual Scratch editor: Upload the first enemy. Upload slime-idle.png as a new sprite. Rename the sprite Enemy. Rename its first costume Slime 1.
    1. Click: Point at Choose a Sprite
    2. Click: Click Upload Sprite
    Actual Scratch editor · step 2
  3. Select Enemy

    Add its second costume

    Select Enemy → Costumes. Use Upload Costume to add slime-blink.png. Rename it Slime 2. Both pictures belong to Enemy.

    Actual Scratch editor: Add its second costume. Select Enemy → Costumes. Use Upload Costume to add slime-blink.png. Rename it Slime 2. Both pictures belong to Enemy.
    1. Click: Open Costumes
    2. Click: Point at Choose a Costume
    3. Click: Click Upload Costume
    4. Click: Rename this picture
    Actual Scratch editor · step 3
  4. Select Enemy

    Place the enemy

    Select Enemy. Set x to 125, y to −30, size to 200 and rotation style to do not rotate. The hero begins on the opposite side of the map.

    Actual Scratch editor: Place the enemy. Select Enemy. Set x to 125, y to −30, size to 200 and rotation style to do not rotate. The hero begins on the opposite side of the map.
    1. Click: Set the pictured x and y
    2. Click: Set the pictured size
    3. Click: Open Direction to choose no rotation
    Actual Scratch editor · step 4
  5. Select Stage’s thumbnail

    Create area and health variables

    Create Area, Cleared, HP and MaxHP, each For all sprites. Uncheck their monitors. HP means health points; MaxHP is the most health the hero can have.

    Actual Scratch editor: Create area and health variables. Create Area, Cleared, HP and MaxHP, each For all sprites. Uncheck their monitors. HP means health points; MaxHP is the most health the hero can have.
    1. Click: Type the variable name
    2. Check: This variable is shared by all sprites
    3. Click: Click OK
    Actual Scratch editor · step 5
  6. Select Stage’s thumbnail

    Create battle-strength variables

    Create Power, EnemyHP and EnemyPower, each For all sprites. Uncheck their monitors. Power is the damage an attack causes; EnemyPower is the damage returned by the enemy.

    Actual Scratch editor: Create battle-strength variables. Create Power, EnemyHP and EnemyPower, each For all sprites. Uncheck their monitors. Power is the damage an attack causes; EnemyPower is the damage returned by the enemy.
    1. Click: Type the variable name
    2. Check: This variable is shared by all sprites
    3. Click: Click OK
    Actual Scratch editor · step 6
  7. Select Enemy

    Give Enemy its own animation counter

    Select Enemy. Create AnimTicks For this sprite only. Uncheck its monitor. This is a separate counter from Hero’s AnimTicks, even though the names match.

    Actual Scratch editor: Give Enemy its own animation counter. Select Enemy. Create AnimTicks For this sprite only. Uncheck its monitor. This is a separate counter from Hero’s AnimTicks, even though the names match.
    1. Click: Type the variable name
    2. Click: Choose For this sprite only
    3. Click: Click OK
    Actual Scratch editor · step 7
  8. Select Stage’s thumbnail

    Reset the starting area

    Select Stage. Extend the existing green-flag stack after its earlier resets. Set Area to 1 and Cleared to 0. A cleared area has already awarded its crystal.

    Actual Scratch editor: Reset the starting area. Select Stage. Extend the existing green-flag stack after its earlier resets. Set Area to 1 and Cleared to 0. A cleared area has already awarded its crystal.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 8
  9. Select Stage’s thumbnail

    Set the hero’s starting strength

    Continue that stack. Set MaxHP to 10 + (2 × Area), Power to Area + 2, HP to MaxHP, EnemyHP to 0 and EnemyPower to 0. Use the green Operators blocks for the calculations.

    Actual Scratch editor: Set the hero’s starting strength. Continue that stack. Set MaxHP to 10 + (2 × Area), Power to Area + 2, HP to MaxHP, EnemyHP to 0 and EnemyPower to 0. Use the green Operators blocks for the calculations.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 9
  10. Select Stage’s thumbnail

    Reset battle values for New Game

    In Stage’s new game stack, copy the seven pictured Area-through-EnemyPower resets after QuestReady = 0 and before ask hero name. New Game must begin with fresh battle values. Keep its existing broadcasts at the bottom.

    Actual Scratch editor: Reset battle values for New Game. In Stage’s new game stack, copy the seven pictured Area-through-EnemyPower resets after QuestReady = 0 and before ask hero name. New Game must begin with fresh battle values. Keep its existing broadcasts at the bottom.
    1. Add: Build these blocks in this order
    Actual Scratch editor · step 10
  11. Select Stage’s thumbnail

    Update the area’s instructions

    In Stage’s load area stack, change Message to Click Guide. Type YES to begin. Keep the Meadow Trail backdrop block.

    Actual Scratch editor: Update the area’s instructions. In Stage’s load area stack, change Message to Click Guide. Type YES to begin. Keep the Meadow Trail backdrop block.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 11
  12. Select Hero

    Keep the hero visible in battle

    Select Hero’s existing visibility loop. Extend its or condition to include GameState = battle and GameState = turn, as pictured. Keep the single show / hide pair.

    Actual Scratch editor: Keep the hero visible in battle. Select Hero’s existing visibility loop. Extend its or condition to include GameState = battle and GameState = turn, as pictured. Keep the single show / hide pair.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 12
  13. Select Guide

    Allow a quest only for an unbeaten foe

    In Guide’s YES branch, add if Cleared = 0. Accept with QuestReady = 1 and Message Walk to the monster to battle.; otherwise show This area is already clear. Set HINT’s Message to Arrows move. Touch foe after YES.

    Actual Scratch editor: Allow a quest only for an unbeaten foe. In Guide’s YES branch, add if Cleared = 0. Accept with QuestReady = 1 and Message Walk to the monster to battle.; otherwise show This area is already clear. Set HINT’s Message to Arrows move. Touch foe after YES.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 13
  14. Select Enemy

    Reset Enemy on the map

    Select Enemy. Add when I receive load area → go to x: 125 y: −30 → set AnimTicks to 0 → switch costume to Slime 1.

    Actual Scratch editor: Reset Enemy on the map. Select Enemy. Add when I receive load area → go to x: 125 y: −30 → set AnimTicks to 0 → switch costume to Slime 1.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 14
  15. Select Enemy

    Show an undefeated enemy

    Build Enemy’s pictured visibility loop. On the map, show only when Cleared = 0. Also show during battle or turn. The extra loading battle state keeps the enemy visible while the scene changes.

    Actual Scratch editor: Show an undefeated enemy. Build Enemy’s pictured visibility loop. On the map, show only when Cleared = 0. Also show during battle or turn. The extra loading battle state keeps the enemy visible while the scene changes.
    1. Add: Build these blocks in this order
    Actual Scratch editor · step 15
  16. Select Enemy

    Begin the enemy’s blink animation

    Give Enemy a new green-flag loop. During explore or battle, change AnimTicks by 1. If AnimTicks = 18, add next costume. The second slime picture makes the enemy blink.

    Actual Scratch editor: Begin the enemy’s blink animation. Give Enemy a new green-flag loop. During explore or battle, change AnimTicks by 1. If AnimTicks = 18, add next costume. The second slime picture makes the enemy blink.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 16
  17. Select Enemy

    Return to its open-eyed costume

    Under the first animation test, add if AnimTicks > 21. Inside it, switch costume to Slime 1 and set AnimTicks to 0. Keep both tests inside the same loop.

    Actual Scratch editor: Return to its open-eyed costume. Under the first animation test, add if AnimTicks > 21. Inside it, switch costume to Slime 1 and set AnimTicks to 0. Keep both tests inside the same loop.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 17
  18. Select Hero

    Move the hero into battle

    Select Hero. Add when I receive start battle → go to x: −105 y: −15 → switch costume to Stand → point in direction 90. Create the start battle message.

    Actual Scratch editor: Move the hero into battle. Select Hero. Add when I receive start battle → go to x: −105 y: −15 → switch costume to Stand → point in direction 90. Create the start battle message.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 18
  19. Select Enemy

    Move Enemy into battle

    Select Enemy. Add when I receive start battle → go to x: 105 y: −15 → switch costume to Slime 1. The two characters should face each other across the arena.

    Actual Scratch editor: Move Enemy into battle. Select Enemy. Add when I receive start battle → go to x: 105 y: −15 → switch costume to Slime 1. The two characters should face each other across the arena.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 19
  20. Select Stage’s thumbnail

    Prepare battle values

    Select Stage. Add when I receive start battle. Set GameState to turn, EnemyHP to 8 × Area, and EnemyPower to Area + 1. The temporary turn state prevents premature button clicks.

    Actual Scratch editor: Prepare battle values. Select Stage. Add when I receive start battle. Set GameState to turn, EnemyHP to 8 × Area, and EnemyPower to Area + 1. The temporary turn state prevents premature button clicks.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 20
  21. Select Stage’s thumbnail

    Open the battle arena

    Finish that stack with switch backdrop to Battle Arena, set Message to Choose one battle action., and set GameState to battle. The Attack button arrives in the next lesson.

    Actual Scratch editor: Open the battle arena. Finish that stack with switch backdrop to Battle Arena, set Message to Choose one battle action., and set GameState to battle. The Attack button arrives in the next lesson.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 21
  22. Select Stage’s thumbnail

    Show health while playing

    Update Stage’s existing information loop to match the pictured state condition. Add show HP and MaxHP in its first slot, and hide HP and MaxHP in its else slot. Keep the existing name and separate message check.

    Actual Scratch editor: Show health while playing. Update Stage’s existing information loop to match the pictured state condition. Add show HP and MaxHP in its first slot, and hide HP and MaxHP in its else slot. Keep the existing name and separate message check.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 22
  23. Select Stage’s thumbnail

    Show enemy health only in battle

    At the bottom of that forever loop, add a separate if / else. If GameState is battle or turn, show EnemyHP; otherwise hide EnemyHP. The enemy’s counter should disappear on the map.

    Actual Scratch editor: Show enemy health only in battle. At the bottom of that forever loop, add a separate if / else. If GameState is battle or turn, show EnemyHP; otherwise hide EnemyHP. The enemy’s counter should disappear on the map.
    1. Add: Add or update the highlighted blocks
    Actual Scratch editor · step 23
  24. Select Hero

    Trigger the first encounter

    Select Hero. Build the pictured green-flag loop. Require explore, QuestReady = 1, Cleared = 0 and touching Enemy. Then set GameState to loading battle and broadcast start battle. All four conditions must be true.

    Actual Scratch editor: Trigger the first encounter. Select Hero. Build the pictured green-flag loop. Require explore, QuestReady = 1, Cleared = 0 and touching Enemy. Then set GameState to loading battle and broadcast start battle. All four conditions must be true.
    1. Add: Build these blocks in this order
    Actual Scratch editor · step 24
  25. Select Hero

    Test accepting a quest

    Start a new game. Walk into Enemy before speaking to Guide: no battle should start. Move away, click Guide and type YES, then touch Enemy again.

    Actual Scratch editor: Test accepting a quest. Start a new game. Walk into Enemy before speaking to Guide: no battle should start. Move away, click Guide and type YES, then touch Enemy again.
    1. Click: Run your test with the green flag
    2. Check: Check the visible game counters
    3. Check: Read the result in Message
    Actual Scratch editor · step 25
  26. Select Stage’s thumbnail

    Test the battle scene

    Battle Arena shows Hero left and Enemy right. HP is 12; EnemyHP is 8. Drag HP and MaxHP onto the top row, and EnemyHP to the second row, matching the picture. Arrow keys must stop movement. Attack comes next lesson.

    Actual Scratch editor: Test the battle scene. Battle Arena shows Hero left and Enemy right. HP is 12; EnemyHP is 8. Drag HP and MaxHP onto the top row, and EnemyHP to the second row, matching the picture. Arrow keys must stop movement. Attack comes next lesson.
    1. Click: Run your test with the green flag
    2. Check: Arrange these counters to match
    3. Check: Read the Message after testing
    Actual Scratch editor · step 26
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 5.

Download lesson 5 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. Looksswitch backdrop to [Title Screen]
  4. Variablesset [HeroName] to (Explorer)
  5. Variablesset [Message] to ()
  6. Variablesset [QuestReady] to (0)
  7. Variablesset [Area] to (1)
  8. Variablesset [Cleared] to (0)
  9. Variablesset [MaxHP] to ((10) + ((2) * (Area)))
  10. Variablesset [Power] to ((Area) + (2))
  11. Variablesset [HP] to (MaxHP)
  12. Variablesset [EnemyHP] to (0)
  13. Variablesset [EnemyPower] to (0)
StageReset progress and begin
  1. Eventswhen I receive [new game]
  2. Variablesset [QuestReady] to (0)
  3. Variablesset [Area] to (1)
  4. Variablesset [Cleared] to (0)
  5. Variablesset [MaxHP] to ((10) + ((2) * (Area)))
  6. Variablesset [Power] to ((Area) + (2))
  7. Variablesset [HP] to (MaxHP)
  8. Variablesset [EnemyHP] to (0)
  9. Variablesset [EnemyPower] to (0)
  10. Eventsbroadcast [ask hero name] and wait
  11. Eventsbroadcast [load area] and wait
  12. Variablesset [GameState] to (explore)
StageShow the current area
  1. Eventswhen I receive [load area]
  2. Looksswitch backdrop to [Meadow Trail]
  3. Variablesset [Message] to (Click Guide. Type YES to begin.)
StageAsk for a short hero nickname
  1. Eventswhen I receive [ask hero name]
  2. Variablesset [GameState] to (naming)
  3. Sensingask [Choose a made-up hero nickname (1–12 letters).] and wait
  4. Variablesset [HeroName] to (answer)
  5. Controlif <<length of [HeroName] = 0> or <length of [HeroName] > 12>> then
  6. Variablesset [HeroName] to (Explorer)
StageShow information only while playing
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <<<GameState = explore> or <GameState = dialogue>> or <<<GameState = battle> or <GameState = turn>> or <GameState = loading>>> then
  4. Variablesshow variable [HeroName]
  5. Variablesshow variable [HP]
  6. Variablesshow variable [MaxHP]
  7. elseSecond slot of the same if/else block
    Variableshide variable [HeroName]
  8. Variableshide variable [HP]
  9. Variableshide variable [MaxHP]
  10. Controlif <not <Message = >> then
  11. Variablesshow variable [Message]
  12. elseSecond slot of the same if/else block
    Variableshide variable [Message]
  13. Controlif <<GameState = battle> or <GameState = turn>> then
  14. Variablesshow variable [EnemyHP]
  15. elseSecond slot of the same if/else block
    Variableshide variable [EnemyHP]
StageSet up a fair battle
  1. Eventswhen I receive [start battle]
  2. Variablesset [GameState] to (turn)
  3. Variablesset [EnemyHP] to ((8) * (Area))
  4. Variablesset [EnemyPower] to ((Area) + (1))
  5. Looksswitch backdrop to [Battle Arena]
  6. Variablesset [Message] to (Choose one battle action.)
  7. Variablesset [GameState] to (battle)
StartShow New Game on the title screen
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <GameState = menu> then
  4. Looksshow
  5. elseSecond slot of the same if/else block
    Lookshide
StartLock New Game before starting
  1. Eventswhen this sprite clicked
  2. Controlif <GameState = menu> then
  3. Variablesset [GameState] to (loading)
  4. Eventsbroadcast [new game]
HeroPlace the hero at a safe starting point
  1. Eventswhen I receive [load area]
  2. Motiongo to x: (-180) y: (-80)
  3. Variablesset [XSpeed] to (0)
  4. Variablesset [YSpeed] to (0)
  5. Variablesset [AnimTicks] to (0)
  6. Looksswitch costume to [Stand]
  7. Motionset rotation style [left-right]
  8. Motionpoint in direction (90)
HeroShow the hero only inside the adventure
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <<<GameState = explore> or <GameState = dialogue>> or <<GameState = battle> or <GameState = turn>>> then
  4. Looksshow
  5. elseSecond slot of the same if/else block
    Lookshide
HeroRead arrow keys and move in four directions
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <GameState = explore> then
  4. Variablesset [XSpeed] to (0)
  5. Variablesset [YSpeed] to (0)
  6. Controlif <key [left arrow] pressed?> then
  7. Variableschange [XSpeed] by (-4)
  8. Controlif <key [right arrow] pressed?> then
  9. Variableschange [XSpeed] by (4)
  10. Controlif <key [up arrow] pressed?> then
  11. Variableschange [YSpeed] by (4)
  12. Controlif <key [down arrow] pressed?> then
  13. Variableschange [YSpeed] by (-4)
  14. Motionchange x by (XSpeed)
  15. Motionchange y by (YSpeed)
  16. My Blocksstay on map
HeroStay inside the map
  1. My Blocksdefine stay on map
  2. Controlif <x position > 215> then
  3. Motionset x to (215)
  4. Controlif <x position < -215> then
  5. Motionset x to (-215)
  6. Controlif <y position > 85> then
  7. Motionset y to (85)
  8. Controlif <y position < -120> then
  9. Motionset y to (-120)
HeroFace the way you move and animate your feet
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <GameState = explore> then
  4. Controlif <XSpeed < 0> then
  5. Motionpoint in direction (-90)
  6. Controlif <XSpeed > 0> then
  7. Motionpoint in direction (90)
  8. Controlif <<XSpeed = 0> and <YSpeed = 0>> then
  9. Looksswitch costume to [Stand]
  10. Variablesset [AnimTicks] to (0)
  11. elseSecond slot of the same if/else block
    Variableschange [AnimTicks] by (1)
  12. Controlif <AnimTicks > 3> then
  13. Looksnext costume
  14. Variablesset [AnimTicks] to (0)
HeroStand on the left for battle
  1. Eventswhen I receive [start battle]
  2. Motiongo to x: (-105) y: (-15)
  3. Looksswitch costume to [Stand]
  4. Motionpoint in direction (90)
HeroBegin a quest battle by touching the enemy
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <<GameState = explore> and <<<QuestReady = 1> and <Cleared = 0>> and <touching [Enemy]?>>> then
  4. Variablesset [GameState] to (loading battle)
  5. Eventsbroadcast [start battle]
GuidePlace the guide beside the path
  1. Eventswhen I receive [load area]
  2. Motiongo to x: (-75) y: (25)
GuideShow the guide on the map
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <<GameState = explore> or <GameState = dialogue>> then
  4. Looksshow
  5. elseSecond slot of the same if/else block
    Lookshide
GuideListen to the player’s typed choice
  1. Eventswhen this sprite clicked
  2. Controlif <GameState = explore> then
  3. Variablesset [GameState] to (dialogue)
  4. Sensingask [Type YES to accept the quest, or HINT for help.] and wait
  5. Controlif <answer = YES> then
  6. Controlif <Cleared = 0> then
  7. Variablesset [QuestReady] to (1)
  8. Variablesset [Message] to (Walk to the monster to battle.)
  9. elseSecond slot of the same if/else block
    Variablesset [Message] to (This area is already clear.)
  10. elseSecond slot of the same if/else block
    Controlif <answer = HINT> then
  11. Variablesset [Message] to (Arrows move. Touch foe after YES.)
  12. elseSecond slot of the same if/else block
    Variablesset [Message] to (Try YES or HINT.)
  13. Variablesset [GameState] to (explore)
EnemyChoose the monster for this area
  1. Eventswhen I receive [load area]
  2. Motiongo to x: (125) y: (-30)
  3. Variablesset [AnimTicks] to (0)
  4. Looksswitch costume to [Slime 1]
EnemyHide a defeated enemy
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <<<<<GameState = explore> or <GameState = dialogue>> or <GameState = loading battle>> and <Cleared = 0>> or <<GameState = battle> or <GameState = turn>>> then
  4. Looksshow
  5. elseSecond slot of the same if/else block
    Lookshide
EnemyBlink while waiting for the hero
  1. Eventswhen green flag clicked
  2. Controlforever
  3. Controlif <<GameState = explore> or <GameState = battle>> then
  4. Variableschange [AnimTicks] by (1)
  5. Controlif <AnimTicks = 18> then
  6. Looksnext costume
  7. Controlif <AnimTicks > 21> then
  8. Looksswitch costume to [Slime 1]
  9. Variablesset [AnimTicks] to (0)
EnemyFace the hero across the battle arena
  1. Eventswhen I receive [start battle]
  2. Motiongo to x: (105) y: (-15)
  3. Looksswitch costume to [Slime 1]
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…