Game Mechanics and Win/Lose ConditionsΒΆ
SummaryΒΆ
Builds complete game mechanics with collision detection, scoring, lives, and win/lose states. This chapter covers 20 concepts from the learning graph, building on prerequisite concepts from earlier chapters.
Concepts CoveredΒΆ
This chapter covers the following 20 concepts from the learning graph:
- List Item Access
- Testing Strategies
- Debugging Strategies
- Storytelling Flow
- Synchronous Broadcast
- Broadcast Storm
- Loop Termination
- Boolean Combination
- Win Condition
- Lose Condition
- Delete From List
- List Contains
- High Score List
- Storyboarding
- Clone Performance
- Inventory System
- Pair Programming
- Scene Planning
- Imagine Phase
- Flowchart
PrerequisitesΒΆ
This chapter builds on concepts from:
- 4. Events, Sequences, and First Scripts
- 7. Variables, Lists, and Data Management
- 8. Animation, Parallelism, and Debugging
- 9. Advanced Control and Operators
List Item Access β Getting Specific Items! πΒΆ
List Item Block (Variables β Dark Red)ΒΆ
Reporter: gets the value at a specific position in the list.
List Position NumbersΒΆ
Lists are 1-indexed β first item is position 1, not 0!
| Position | Item |
|---|---|
| 1 | First item |
| 2 | Second item |
| 3 | Third item |
| ... | ... |
| (length of list) | Last item |
List Item ExamplesΒΆ
Access Specific ItemΒΆ
Loop Through All ItemsΒΆ
Get Last ItemΒΆ
List Item SafetyΒΆ
Always check bounds before accessing!
if < (index) > (length of [list v]) > then
say [Invalid index!] for 2 secs
else
say (item (index) of [list v]) for 2 secs
end
List Item in Game MechanicsΒΆ
Use Item from InventoryΒΆ
when I receive [use-item v]
if < (length of [inventory v]) > 0 > then
set [item-to-use v] to (item (1) of [inventory v])
delete (1) of [inventory v]
// Use the item...
end
Equip Best WeaponΒΆ
set [best-damage v] to (0)
repeat (length of [weapons v])
if < (item (loop-counter) of [weapons v]) > (best-damage) > then
set [best-damage v] to (item (loop-counter) of [weapons v])
set [best-weapon-index v] to (loop-counter)
end
end
Delete From List β Removing Items! ποΈΒΆ
Delete Block (Variables β Dark Red)ΒΆ
Removes item at specified position β remaining items shift down!
Delete ExamplesΒΆ
Remove Used ItemΒΆ
Remove Specific ItemΒΆ
Clear Entire ListΒΆ
Remove by Value (Search + Delete)ΒΆ
repeat (length of [inventory v])
if < (item (loop-counter) of [inventory v]) = [rotten-apple] > then
delete (loop-counter) of [inventory v]
end
end
Delete + High Score ListΒΆ
when I receive [new-score v]
if < (score) > (item (1) of [high-scores v]) > then
insert (score) at (1) of [high-scores v]
if < (length of [high-scores v]) > 5 > then
delete (6) of [high-scores v] // Keep top 5
end
end
List Contains β Is It In There? πΒΆ
List Contains Block (Variables β Dark Red)ΒΆ
Boolean: TRUE if item exists in list, FALSE otherwise.
Contains ExamplesΒΆ
Check for ItemΒΆ
if < [key v] in [inventory v]? > then
say [Door unlocked!] for 2 secs
broadcast [door-open v]
else
say [You need a key!] for 2 secs
end
Check for Duplicate Before AddingΒΆ
if <not < [sword v] in [inventory v]? >> then
add [sword] to [inventory v]
say [Got a sword!] for 2 secs
else
say [Already have a sword!] for 2 secs
end
Check for Quest ItemΒΆ
Contains vs Item AccessΒΆ
| Block | Returns | Use For |
|---|---|---|
item (1) of [list] |
Value at position | Get the actual item |
[item] in [list]? |
True/False | Check existence |
High Score List β Top Players! πΒΆ
High Score List StructureΒΆ
high-scores list:
[0] 10500 (1st place)
[1] 9800 (2nd place)
[2] 8750 (3rd place)
[2] 7200 (4th place)
[3] 5500 (5th place)
High Score LogicΒΆ
when green flag clicked
delete all of [high-scores v]
repeat 5
add (0) to [high-scores v]
end
show list [high-scores v]
when I receive [game-over v]
if < (score) > (item (1) of [high-scores v]) > then
// New high score!
insert (score) at (1) of [high-scores v]
delete (6) of [high-scores v] // Keep top 5
say [NEW HIGH SCORE!] for 3 secs
end
High Score DisplayΒΆ
| Display Method | Code |
|---|---|
| List on stage | show list [high-scores] |
| Custom display | repeat 5: say (join [#] (join (loop-counter) (join [: ] (item (loop-counter) of [high-scores])))) for 2 secs |
| With names | Store join [name] (join [: ] (score)) in list |
High Score FeaturesΒΆ
| Feature | Implementation |
|---|---|
| Top 5/10 | delete (6) of [list] to trim |
| Names + Scores | Store join [name] (join [: ] (score)) |
| Date achieved | Add timestamp to entry |
| Level-specific | Separate list per level |
Testing Strategies β Quality Assurance! π§ͺΒΆ
What Are Testing Strategies?ΒΆ
Testing strategies = systematic ways to find bugs and verify your game works!
Testing LevelsΒΆ
| Level | What It Tests | When |
|---|---|---|
| Unit test | Single script/block | While coding |
| Integration test | Multiple scripts together | After adding feature |
| Playtest | Full game experience | Before sharing |
| Regression test | Old features still work | After changes |
| Edge case test | Extreme/unusual inputs | Before release |
Testing StrategiesΒΆ
| Strategy | How To Do It |
|---|---|
| Boundary testing | Test edges: 0, 1, max, min, -1 |
| Invalid input | Wrong keys, empty lists, divide by 0 |
| Stress test | Spawn 100 clones, run 10 minutes |
| Permutation test | Try all key combinations |
| Save/load test | Save, reload, continue game |
| Multiplayer test | Two keyboards, two players |
Testing ChecklistΒΆ
| Category | Tests |
|---|---|
| Movement | All directions, edges, collisions, jumping |
| Controls | All keys, key combos, rapid presses |
| Physics | Gravity, jumping, falling, slopes |
| Combat | Damage, knockback, invincibility, death |
| Scoring | Points, high scores, combos, multipliers |
| Lives | Lose, gain, game over, invincibility |
| Levels | Transitions, spawns, completion, secrets |
| UI | Menus, pause, menus, displays |
| Audio | Music, SFX, volume, mute |
| Performance | 100 clones, 10 min play, save/load |
Automated Testing (Custom Blocks)ΒΆ
define test-movement
// Test: Player moves right
go to x: 0 y: 0
repeat 10
change x by 5
end
if < (x position) = 50 > then
say [TEST PASS: Movement] for 2 secs
else
say [TEST FAIL: Movement] for 2 secs
end
define test-collision
// Test: Wall collision
go to x: 220 y: 0
if <touching color [#FF0000]? > then
say [TEST PASS: Collision] for 2 secs
else
say [TEST FAIL: Collision] for 2 secs
end
Debugging Strategies β Fix Bugs Like a Pro! πΒΆ
Debugging WorkflowΒΆ
1. REPRODUCE β Make bug happen every time
2. ISOLATE β Which script? Which block?
3. HYPOTHESIZE β "I think the 'change x by' is wrong"
4. TEST β Change ONE thing, test again
5. FIX β Apply correction
6. VERIFY β Test thoroughly, check for new bugs
Debugging Tools in ScratchΒΆ
| Tool | How to Use |
|---|---|
| Block Highlighting | Watch which block glows during execution |
| Variable Watchers | show variable β see values live on stage |
say Debugging |
say (variable) for 0.1 secs β live values |
wait for Timing |
wait 1 secs to slow down and observe |
| Single Step | Some Scratch versions: step through blocks |
| Clean Up | Right-click β "Clean up" β aligns blocks |
Debugging with say (Live Values!)ΒΆ
forever
say (join [x: ] (x position)) for 0.1 secs
say (join [y: ] (y position)) for 0.1 secs
say (join [vel-x: ] (velocity-x)) for 0.1 secs
say (join [onGround: ] (touching [ground v]?)) for 0.1 secs
wait 0.1 secs
end
Live dashboard of all important values! π
Common Bug Patterns & FixesΒΆ
| Bug Symptom | Likely Cause | Debug Steps |
|---|---|---|
| Sprite frozen | forever without wait |
Add wait 0.01 secs |
| Moves wrong way | Wrong sign (+/-) | Check change x by sign |
| Jumps infinitely | No ground check | Add touching ground? |
| Stuck in wall | No collision check | Add touching color? |
| Score not updating | Wrong variable name | Check score vs Score |
| Clone explosion | No spawn limit | Add cooldown/limit |
| Lag/freeze | Too many loops | Combine loops, add wait |
Debugging StrategiesΒΆ
| Strategy | How To Apply |
|---|---|
| Divide and conquer | Disable half the code, test, narrow down |
| Rubber duck debugging | Explain code to a rubber duck (or friend) |
| Binary search | Disable half scripts, test, narrow to one script |
| Minimal reproduction | Create new project with just the buggy part |
| Print everything | say all variables at each step |
| Check assumptions | "Is this variable actually 0?" β verify with say |
Storytelling Flow β Games That Tell Stories! πΒΆ
What Is Storytelling Flow?ΒΆ
Storytelling flow = how your game guides the player through a narrative experience!
Story Structure in GamesΒΆ
| Act | Purpose | Game Elements |
|---|---|---|
| Setup (Act 1) | Introduce world, character, goal | Tutorial, first level, intro cutscene |
| Confrontation (Act 2) | Challenges, rising action | Harder levels, new mechanics, mid-boss |
| Climax (Act 3) | Final challenge, resolution | Final boss, final level, ending cutscene |
Storytelling Through GameplayΒΆ
| Story Element | Game Mechanic |
|---|---|
| Character | Player sprite, customization |
| Setting | Backdrops, level design |
| Conflict | Enemies, obstacles, puzzles |
| Progression | Levels, upgrades, new abilities |
| Resolution | Win screen, credits, epilogue |
Storytelling Through CodeΒΆ
when green flag clicked
broadcast [chapter1 v] and wait // Setup
broadcast [chapter2 v] and wait // Rising action
broadcast [chapter3 v] and wait // Climax
broadcast [ending v] // Resolution
when I receive [chapter1 v]
switch backdrop to [village v]
speak [Our village was peaceful...] and wait
speak [Until the Shadow King came...] and wait
broadcast [chapter1-done v]
when I receive [chapter2 v]
switch backdrop to [forest v]
speak [You must find the three crystals...] and wait
// Gameplay: collect 3 crystals
Synchronous Broadcast β Wait for Everyone! π€ΒΆ
Broadcast And Wait RecapΒΆ
Pauses sender until ALL when I receive scripts FINISH.
When to Use Synchronous BroadcastΒΆ
| Scenario | Why Sync |
|---|---|
| Level start | All sprites initialized before gameplay |
| Cutscene steps | Each line finishes before next |
| Save game | All data saved before continuing |
| Multi-phase boss | Phase 1 done before phase 2 starts |
| Cutscene dialogue | Each character finishes speaking |
Synchronous Broadcast ExampleΒΆ
when green flag clicked
broadcast [initialize v] and wait
wait 1 secs
broadcast [spawn-player v] and wait
broadcast [spawn-enemies v] and wait
broadcast [spawn-ui v] and wait
broadcast [game-ready v]
when I receive [initialize v]
set [score v] to (0)
set [lives v] to (3)
when I receive [spawn-player v]
go to x: -200 y: -100
show
when I receive [spawn-enemies v]
repeat 5
create clone of [enemy v]
wait 0.2 secs
end
Broadcast Storm β Too Many Messages! β‘ΒΆ
What Is a Broadcast Storm?ΒΆ
Broadcast storm = too many broadcasts sent too quickly = lag, crashes, unpredictable behavior!
Causes of Broadcast StormsΒΆ
| Cause | Example |
|---|---|
| Broadcast in forever loop | forever { broadcast [update] } |
| Recursive broadcasts | A β B, B β A, A β B... |
| Too many clones broadcasting | 100 clones each broadcast |
Broadcast in when I receive |
Chain reaction |
Preventing Broadcast StormsΒΆ
| Prevention | How |
|---|---|
| Rate limit | wait 0.1 secs between broadcasts |
| Flags | if <not <broadcasting>> then broadcast |
| Cooldown | set [cooldown v] to (10), change by -1 each frame |
| Single broadcaster | One "manager" sprite handles all broadcasts |
Broadcast Storm Example (BAD!)ΒΆ
Broadcast Storm Fix (GOOD!)ΒΆ
Loop Termination β Exiting Loops Properly! πΒΆ
How Loops EndΒΆ
| Loop Type | Terminates When... |
|---|---|
repeat (N) |
After N iterations |
forever |
stop [this script] or stop [all] |
repeat until <cond> |
Condition becomes TRUE |
repeat while (custom) |
Condition becomes FALSE |
Loop Termination BlocksΒΆ
| Block | Stops |
|---|---|
stop [this script v] |
Current script only |
stop [all v] |
EVERYTHING in project |
stop [other scripts in sprite v] |
Other scripts in same sprite |
Loop Termination ExamplesΒΆ
Break Out of ForeverΒΆ
Exit Nested LoopΒΆ
repeat (10)
repeat (10)
if <touching [target v]?> then
stop [this script v] // Exits BOTH loops!
end
end
end
Stop All (Game Over)ΒΆ
Loop Termination Best PracticesΒΆ
| Practice | Why |
|---|---|
| Always have exit condition | forever must have stop or broadcast [stop] |
Use stop [this script] |
Prefer over stop [all] |
| Clean up before stop | hide, delete this clone, stop sounds |
Avoid stop [all] in libraries |
Kills everything including UI |
Boolean Combination β Complex Conditions! π§ ΒΆ
Combining Booleans (And/Or/Not)ΒΆ
| Operator | Block | True When... |
|---|---|---|
| AND | < > and < > |
BOTH true |
| OR | < > or < > |
AT LEAST ONE true |
| NOT | not < > |
Flips true/false |
Complex Boolean CombinationsΒΆ
Three Conditions (AND)ΒΆ
if < (score) > 100 > and < (lives) > 0 > and <not <touching [spike v]?>> then
broadcast [bonus-level v]
end
Complex ConditionΒΆ
if < < (distance) < 50 > or <touching [player v]?> > and <not < (invincible) >> then
broadcast [player-hit v]
end
De Morgan's Laws (Simplify!)ΒΆ
| Original | Equivalent (Simplified) |
|---|---|
not (A or B) |
(not A) and (not B) |
not (A and B) |
(not A) or (not B) |
Boolean Combination ExamplesΒΆ
Complex Jump ConditionΒΆ
if <key [space] pressed?> and <touching [ground v]?> and <not < (in-air) >> then
set [velocity-y v] to (15)
end
Enemy AI DecisionΒΆ
if < (distance to player) < 100 > and <not < (stunned) >> then
if < (player-x) > (x position) > then
set [velocity-x v] to (3)
else
set [velocity-x v] to (-3)
end
end
Win Condition β Victory! πΒΆ
What Is a Win Condition?ΒΆ
Win condition = the specific goal that makes the player win the game!
Common Win ConditionsΒΆ
| Type | Condition | Example |
|---|---|---|
| Score target | score > 1000 |
Collect 100 coins |
| All collected | length of [coins] = 0 |
Get all items |
| Reach location | x > 200 and y > 150 |
Reach exit |
| Defeat boss | boss-health = 0 |
Defeat final boss |
| Survive time | timer > 300 |
Survive 5 minutes |
| All enemies gone | length of [enemies] = 0 |
Clear room |
Win Condition ImplementationΒΆ
forever
// Check win conditions each frame
if < (score) > 1000 > then
broadcast [win v]
stop [this script v]
end
if < (length of [coins v]) = 0 > then
broadcast [win v]
stop [this script v]
end
if < (boss-health) = 0 > then
broadcast [win v]
stop [this script v]
end
end
Win SequenceΒΆ
when I receive [win v]
stop [all v]
switch backdrop to [victory v]
play sound [fanfare v]
say [YOU WIN!] for 5 secs
wait 3 secs
show variable [final-score v]
wait 5 secs
broadcast [return-to-menu v]
Multiple Win PathsΒΆ
| Game Type | Win Conditions |
|---|---|
| Platformer | Reach flag, all coins, defeat boss |
| Puzzle | All pieces placed, all switches hit |
| Survival | Timer reaches target |
| RPG | Defeat final boss, complete main quest |
| Racing | Cross finish line first |
Lose Condition β Game Over! πΒΆ
What Is a Lose Condition?ΒΆ
Lose condition = specific situation where the player fails and game ends!
Common Lose ConditionsΒΆ
| Type | Condition | Example |
|---|---|---|
| No lives | lives = 0 |
3 hits = game over |
| Time out | timer = 0 |
Didn't finish in time |
| Fall off world | y < -200 |
Fell in pit |
| Touch hazard | touching lava/spikes |
Instant death |
| Fail objective | failed to protect NPC |
Escort mission failed |
Lose Condition ImplementationΒΆ
forever
// Check lose conditions
if < (lives) = 0 > then
broadcast [game-over v]
stop [this script v]
end
if < (timer) = 0 > then
broadcast [game-over v]
stop [this script v]
end
if < (y position) < -200 > then
broadcast [game-over v]
stop [this script v]
end
if <touching [lava v]?> then
broadcast [game-over v]
stop [this script v]
end
end
Game Over SequenceΒΆ
when I receive [game-over v]
stop [all v]
switch backdrop to [game-over v]
play sound [game-over-sound v]
say [GAME OVER] for 3 secs
wait 2 secs
say (join [Final Score: ] (score)) for 5 secs
wait 3 secs
broadcast [return-to-menu v]
Lose Condition Best PracticesΒΆ
| Practice | Why |
|---|---|
| Clear feedback | Show WHY player lost |
| Quick restart | One click to try again |
| Progress saving | Don't lose all progress |
| Fair warning | "Low health!" before death |
| Learn from failure | Show what killed you |
Inventory System β Player's Bag! πΒΆ
What Is an Inventory System?ΒΆ
Inventory = a list that holds items the player collects!
Inventory List StructureΒΆ
Inventory OperationsΒΆ
| Operation | Code |
|---|---|
| Add item | add [potion] to [inventory v] |
| Remove item | delete (1) of [inventory v] |
| Use item | item (1) of [inventory v], then delete (1) |
| Check item | [potion] in [inventory v]? |
| Count items | length of [inventory v] |
| Show inventory | show list [inventory v] |
Inventory SystemsΒΆ
Simple Inventory (Stack)ΒΆ
when I receive [pickup-item v]
add [item-name] to [inventory v]
say (join [Got: ] (item-name)) for 2 secs
Categorized InventoryΒΆ
Categories: weapons, potions, keys, quest
when I receive [pickup v]
if < (item-type) = [weapon] > then
add (item) to [weapons v]
else
if < (item-type) = [potion] > then
add (item) to [potions v]
end
end
Weight/Slot LimitΒΆ
if < (length of [inventory v]) < 20 > then
add [item] to [inventory v]
else
say [Inventory full!] for 2 secs
end
Inventory UIΒΆ
when green flag clicked
hide list [inventory v]
when [i] key pressed
if < (showing-inventory) > then
hide list [inventory v]
set [showing-inventory v] to (false)
else
show list [inventory v]
set [showing-inventory v] to (true)
end
SummaryΒΆ
In this chapter, you learned:
- β
List Item Access β
item (N) of [list]for specific items - β Delete From List β Remove items, trim lists
- β List Contains β Check if item exists
- β High Score List β Top scores with trimming
- β Testing Strategies β Unit, integration, playtest, regression
- β Debugging Strategies β Reproduce, isolate, hypothesize, test, fix
- β Storytelling Flow β Three-act structure in games
- β
Synchronous Broadcast β
broadcast and waitfor coordination - β Broadcast Storm β Prevention and fixes
- β
Loop Termination β
stopblocks for clean exits - β Boolean Combination β And/Or/Not for complex logic
- β Win Condition β Score, collection, boss, survival, location
- β Lose Condition β Lives, time, hazards, objectives
- β Inventory System β Lists for items, categories, limits
- β Testing Strategies β Unit, integration, playtest, edge cases
- β Debugging Strategies β Reproduce, isolate, hypothesize, test, fix
- β Storytelling Flow β Three-act game narrative
- β Synchronous Broadcast β Coordinated multi-sprite starts
- β Broadcast Storm β Rate limiting, cooldowns
- β
Loop Termination β Clean
stopusage - β Boolean Combination β And/Or/Not for complex conditions
Key Terms to RememberΒΆ
| Term | Definition |
|---|---|
| List Item Access | item (N) of [list] β get specific value |
| Delete From List | Remove item at position |
| List Contains | Boolean check for item existence |
| High Score List | Sorted top scores list |
| Testing Strategies | Systematic bug-finding approaches |
| Debugging Strategies | Reproduce, isolate, hypothesize, test, fix |
| Storytelling Flow | Narrative structure in gameplay |
| Synchronous Broadcast | broadcast and wait coordination |
| Broadcast Storm | Message overload, prevention methods |
| Loop Termination | stop this script/all/other scripts |
| Boolean Combination | And/Or/Not for complex conditions |
| Win Condition | Goal that ends game in victory |
| Lose Condition | Condition that ends game in defeat |
| Inventory System | List-based item management |
| List Item Access | Getting specific list values |
| Delete From List | Removing items from collections |
| List Contains | Boolean membership check |
| High Score List | Persistent top scores |
| Testing Strategies | Quality assurance methodologies |
| Debugging Strategies | Systematic bug fixing |
| Storytelling Flow | Narrative pacing in games |
| Synchronous Broadcast | Coordinated multi-sprite messaging |
| Broadcast Storm | Message flooding prevention |
| Loop Termination | Clean script exit patterns |
| Boolean Combination | Compound logical conditions |
Try It Yourself! π―ΒΆ
Challenge 1: Build a complete platformer with win (reach flag) and lose (3 lives) conditions
Challenge 2: Create an inventory system with categories (weapons, potions, keys)
Challenge 3: Implement high score list that saves top 10 scores
Challenge 4: Write test cases for your movement, collision, and scoring systems
Challenge 5: Debug a broken script using say to watch variable values
Challenge 6: Design a game story with 3 acts and implement with broadcasts
Challenge 7: Create a broadcast system for level loading without storms
Challenge 8: Design complex boolean conditions for enemy AI decisions
Challenge 9: Implement multiple win/lose conditions in one game
Challenge 10: Build a complete playable game with all mechanics learned!
What's Next?ΒΆ
In Chapter 11, you'll master the Design Process and Capstone Project β the complete creative journey from idea to polished game, including planning, iteration, testing, polishing, and sharing your masterpiece!