🎮 Game Mechanics
Track scores, lives, levels, or any game state that changes as players progress through your creation.
Variables are like your sprite’s memory - they help your program remember important information like scores, names, or the number of lives left in a game. Think of them as labeled boxes where you can store different types of information!
A variable is a container that holds a value that can change during your program. Just like a box with a label, you can:
🎮 Game Mechanics
Track scores, lives, levels, or any game state that changes as players progress through your creation.
📊 Calculations
Store numbers for math operations, count how many times something happens, or track measurements.
💬 User Information
Remember player names, answers to questions, or choices made during interactive stories.
🎨 Dynamic Behavior
Control sprite behavior based on stored values - make characters smarter and more responsive!
Let’s learn how to create and work with variables step by step:
🏷️ Create a New Variable Go to the Variables category and click “Make a Variable”. Give it a descriptive name like “score” or “player_name”.
📝 Choose Visibility Decide if it’s “For all sprites” (global) or “For this sprite only” (local). Most game variables are global.
🎯 Set Initial Values Use “set [variable] to (value)” blocks to give your variable a starting value.
🔄 Update as Needed Use “change [variable] by (amount)” to increase or decrease values, or “set” to replace completely.
📺 Display if Helpful Check the box next to your variable name to show it on the stage - great for scores and timers!
set [score ▼] to (0) - Sets the variable to exactly this value
change [score ▼] by (10) - Adds (or subtracts) this amount
set [name ▼] to (answer) - Can store text, not just numbers
[score ▼] - The round reporter block gives you the current value
say (score) for (2) seconds - Display the value to users
if score > 100 then - Use in conditions and comparisons
show variable [score ▼] - Make it visible on the stage
hide variable [score ▼] - Remove it from the stage display
Change display style by right-clicking the stage display
Here are essential patterns you’ll use in almost every project:
when green flag clickedset [score ▼] to (0)
when [collectible ▼] clickedchange [score ▼] by (10)play sound (coin ▼) until done
if score = 100 then say [You win!] for (3) secondsend
when green flag clickedset [lives ▼] to (3)
when I receive (player hit ▼)change [lives ▼] by (-1)if lives = 0 then say [Game Over!] for (3) seconds stop [all ▼]end
when green flag clickedset [time ▼] to (60)repeat until time = 0 wait (1) seconds change [time ▼] by (-1)endsay [Time's up!] for (2) seconds
when green flag clickedask [What's your name?] and waitset [player_name ▼] to (answer)say (join [Hello, ] (player_name)) for (3) seconds
Lists are like variables that can hold multiple pieces of information at once - imagine a shopping list or a high score table!
📋 Make a List In Variables, click “Make a List” and give it a name like “high_scores” or “inventory”.
➕ Add Items Use “add (item) to [list ▼]” to put new information at the end of your list.
🔍 Access Items Use “item (1) of [list ▼]” to get specific items. Lists start counting from 1, not 0!
🗑️ Remove Items Use “delete (1) of [list ▼]” to remove specific items or “delete all of [list ▼]” to clear everything.
📊 Get Information Use “length of [list ▼]” to find out how many items are in your list.
when green flag clickedif length of [high_scores ▼] = 0 then add (0) to [high_scores ▼]end
when game endsif score > item 1 of [high_scores ▼] then replace item (1) of [high_scores ▼] with (score) say [New high score!] for (3) secondsend
when [treasure ▼] clickedadd (treasure name) to [inventory ▼]say (join [Found: ] (treasure name)) for (2) seconds
when [use item ▼] clickedif length of [inventory ▼] > 0 then say (join [Using: ] (item (1) of [inventory ▼])) for (2) seconds delete (1) of [inventory ▼]else say [No items to use!] for (2) secondsend
Scratch variables can store different types of information:
🔢 Numbers
Integers: Whole numbers like 5, -10, 0
Decimals: Numbers with decimal points like 3.14, -2.5
Perfect for scores, positions, timers, and calculations
📝 Text (Strings)
Words and phrases: “Hello”, “Player1”, “Game Over”
Mixed content: “Score: 100”, “Level 5”
Great for names, messages, and displaying information
✅ Boolean-like Values
True/False concepts: Use 1 for true, 0 for false
Status tracking: “on”, “off”, “ready”, “playing”
Helpful for game states and conditions
🎨 Special Values
Colors: Store color values for pen drawing
Coordinates: Store x,y positions as text like “100,50”
Complex data: JSON-like strings for advanced projects
when green flag clickedset [difficulty ▼] to (1)set [enemy_speed ▼] to (2)
every (30) secondschange [difficulty ▼] by (1)set [enemy_speed ▼] to ((difficulty) * (2))
when I receive (player action ▼)if player_level < 5 then say [Great job, beginner!] for (2) secondselse say [Excellent work, expert!] for (2) secondsend
// For shared projects onlywhen green flag clickedif ☁ global_high_score < local_score then set [☁ global_high_score ▼] to (local_score)end
when green flag clickedask [Easy (1) or Hard (2) mode?] and waitset [game_mode ▼] to (answer)
if game_mode = 1 then set [enemy_count ▼] to (3)else set [enemy_count ▼] to (8)end
Common problems and solutions when working with variables:
👀 Show Variables Temporarily Check variable boxes to see their values change in real-time during testing.
💬 Say Variable Values Use “say (variable) for (2) seconds” to check values at specific moments.
🎨 Use Different Display Styles Right-click stage variable displays to choose sliders, large readouts, or hidden displays.
📝 Add Debug Comments Use comment blocks to remind yourself what each variable is supposed to do.
when green flag clickedset [correct_answers ▼] to (0)set [total_questions ▼] to (10)set [current_question ▼] to (1)
repeat (total_questions) set [num1 ▼] to (pick random (1) to (10)) set [num2 ▼] to (pick random (1) to (10)) ask (join (join (num1) [ + ]) (num2)) and wait
if answer = (num1 + num2) then change [correct_answers ▼] by (1) say [Correct!] for (1) seconds else say [Try again next time!] for (1) seconds end
change [current_question ▼] by (1)end
say (join [You got ] (join (correct_answers) [ out of 10 correct!])) for (5) seconds
Ready to build more sophisticated programs with data?
Variables and lists are the foundation of smart, interactive programs. With Vibelf’s guidance, you’ll create projects that remember, learn, and grow with their users! 📊✨