Scratch Project · Grade 5 · Chapter 7
Scratch Mini-Game Part 1: Player & Obstacles
Designing the engine for "Space Dodger" — responsive player movement and falling asteroid physics!
Learning Goals
What We'll Build Today
GOAL 1
Game Engine
Set up coordinate bounds, player movement, and smooth keyboard responsiveness.
GOAL 2
Random Spawning
Use pick random to drop falling obstacles from random top positions.
GOAL 3
Obstacle Resetting
Build infinite falling loops by resetting Y coordinates when reaching bottom.
Game Architecture
Deconstructing Arcade Games
Think of games like Space Invaders or Subway Surfers. What are the 3 main visual elements running constantly on screen?
Discussion: Player character, obstacles/enemies, and background backdrop!
Project Brief
"Space Dodger" Mini-Game Concept
Our hero, Astro-Cat, is navigating through an asteroid belt in space! Space rocks drop continuously from above. Astro-Cat must dodge left and right to survive!
Today in Part 1, we write the code for Astro-Cat's thrusters and the falling asteroid system!
Scratch Math
Understanding Stage Coordinates
The Scratch stage is a grid: X moves horizontally (-240 left to +240 right), Y moves vertically (-180 bottom to +180 top).
⬅️ Horizontal X-Axis
Center = 0 | Far Left = -240 | Far Right = 240
⬆️ Vertical Y-Axis
Center = 0 | Top Edge = 180 | Bottom Edge = -180
Setup Flowchart
Initializing the Player Sprite
1
Start Position
Set X: 0, Y: -130 (Bottom center of stage).
2
Set Size
Set size to 60% so dodging feels crisp.
3
Control Loop
Start forever loop reading left/right keys.
Code Implementation
Astro-Cat Movement Script
Smooth left/right movement with screen boundary checks:
when green flag clicked
go to x: (0) y: (-130)
forever {
if <key [right arrow v] pressed?> then { change x by (8) }
if <key [left arrow v] pressed?> then { change x by (-8) }
}
Game Physics
Preventing Off-Screen Escape
If player holds right arrow, Astro-Cat might disappear off screen! We add boundary caps:
if <x position > 210> then { set x to (210) }
if <x position < -210> then { set x to (-210) }
Obstacle Mechanics
How Falling Objects Work
To make an asteroid fall down continuously, we decrease its Y coordinate inside a forever loop!
1
Spawn Top
Set Y to 180 (top edge) & X to random (-200 to 200).
2
Fall Down
change y by (-6) repeatedly.
3
Reset Loop
If Y < -170, send back to top!
Code Implementation
Asteroid Falling & Respawning Script
when green flag clicked
forever {
set y to (180)
set x to (pick random (-200) to (200))
repeat until <y position < -170> {
change y by (-7)
turn right (3) degrees // Spinning effect
}
}
Scratch Operators
The Magic of pick random
Without randomness, games become predictable and boring. pick random (-200) to (200) generates a new surprise X position every single drop!
Fun Fact: Game developers call this Procedural Generation!
Side-by-Side Comparison
Predictable Drop vs. Dynamic Surprise Drop
❌ PREDICTABLE DROP
Asteroid drops down middle (X=0) every time.
Player can just stand on the side and win effortlessly!
✅ DYNAMIC SURPRISE DROP
Asteroid picks new random X coordinate for every spawn.
Keeps player actively dodging and engaged!
Debunking Myths
Common Mini-Game Glitches
Glitch: "Stuck at Bottom"
If the check is y position = -180, fast falling objects might jump from -176 to -183 and miss the exact match! Always use < (less than)!
Glitch: "Sluggish Dodge"
Using move 2 steps feels super slow. Increase change step to 8 or 10 for snappy arcade controls!
Smartboard Voting
Predict the Speed!
Which script drops the asteroid faster?
Script A: change y by (-4)
Script B: change y by (-12)
Vote: Thumbs UP for A, Thumbs DOWN for B!
Spot the Bug
Why Is the Asteroid Teleporting Mid-Air?
forever [ set y to (180), change y by (-5) ]
Bug Analysis: Every cycle of the loop resets Y back to 180! Why is it missing the repeat until inner loop?
Think-Pair-Share
Designing Obstacle Variations
Imagine adding a second obstacle: a fast-falling Lightning Bolt!
Partner Task: How would the Lightning Bolt script differ from the Asteroid? (Speed, size, sound effects?)
Guided Practice
Assembling Player & Obstacle Live
Let's connect our scripts together on the Scratch stage!
- Step 1: Choose Space backdrop & Astro-Cat sprite.
- Step 2: Add smooth arrow controls & boundary limits.
- Step 3: Create Rock sprite and attach falling random loop.
Hands-on Challenge
The Double Obstacle Rush!
Mission: Duplicate the Asteroid sprite. Give Asteroid 2 a smaller size (40%) and faster fall speed (Y change by -10)!
Scratch Goal: Test dodging TWO falling objects at different speeds simultaneously!
Quick Check · Question 1
Which block combination spawns an obstacle at the top edge at a random horizontal position?
Aset x to (180), set y to (0)
Bset y to (180), set x to (pick random (-200) to (200))
Cmove (10) steps, turn (90) degrees
Dgo to [mouse pointer v]
Click to reveal answer
Quick Check · Question 2
Why do we use change y by (-8) instead of change y by (8) to make an object fall?
APositive numbers make things fall down
BNegative Y changes move down toward the bottom of the screen
CNegative numbers make sprites bigger
DScratch doesn't allow positive Y numbers
Click to reveal answer
Summary
Part 1 Milestone Achieved!
- Player Engine: Responsive left/right arrow controls with bounds.
- Obstacle Engine: Infinite falling loop using random top X coordinates.
- Game Loop: Smooth 60 FPS animation cycle.
Looking Ahead
Next Stop: Mini-Game Part 2!
In Chapter 8, we add collision detection, scoring points, 3 lives, sound effects, and GAME OVER screens!
Exit Ticket: Save your Scratch project as SpaceDodger_Part1!