3.4 Variables in Block Programming
Understanding Variables
Variables are like containers that store information (values) in your program. They can hold different types of data, such as numbers, text, or true/false values.
Why Use Variables?
- Store and update scores in games
- Keep track of user input
- Control program flow
- Make your code more flexible and reusable
Creating Variables in Scratch
1. Making a New Variable
- Click on the "Variables" category in the blocks palette
- Click "Make a Variable"
- Enter a name for your variable (e.g., "score", "lives", "highScore")
- Choose if the variable should be available to all sprites or just the current one
2. Variable Blocks
| Block | Purpose |
|---|---|
set [variable] to [0] |
Sets the variable to a specific value |
change [variable] by [1] |
Increases or decreases the variable by a number |
show variable [variable] |
Displays the variable on the stage |
hide variable [variable] |
Hides the variable from the stage |
Using Variables in Your Projects
1. Score Counter
when green flag clicked
set [score v] to [0]
show variable [score v]
when this sprite clicked
change [score v] by (1)
play sound (pop v)
2. Countdown Timer
when green flag clicked
set [time v] to (30)
show variable [time v]
repeat until <(time) = [0]>
wait (1) seconds
change [time v] by (-1)
end
say [Time's up!] for (2) seconds
3. High Score System
when green flag clicked
if <(score) > (high score)> then
set [high score v] to (score)
say [New high score!] for (2) seconds
end
Working with Lists
Lists (also called arrays) allow you to store multiple values in a single variable.
1. Creating a List
- Click on the "Variables" category
- Click "Make a List"
- Name your list (e.g., "inventory", "scores", "questions")
2. Common List Operations
| Block | Purpose |
|---|---|
add [thing] to [list] |
Adds an item to the end of the list |
delete (1) of [list] |
Removes an item from the list |
item (1) of [list] |
Gets an item at a specific position |
length of [list] |
Gets the number of items in the list |
3. List Example: Quiz Game
when green flag clicked
delete (all v) of [questions v]
add [What is 5 + 3?] to [questions v]
add [What is the capital of France?] to [questions v]
add [What color is the sky?] to [questions v]
set [current question v] to [1]
ask (item (current question) of [questions v]) and wait
change [current question v] by (1)
Variable Scope
Variables in Scratch can have different scopes:
1. Global Variables
- Available to all sprites
- Created by selecting "For all sprites" when making a variable
- Use for values that multiple sprites need to access (e.g., score, time)
2. Local Variables
- Available only to the sprite they belong to
- Created by selecting "For this sprite only"
- Use for values specific to one sprite (e.g., speed, health)
Practice Activity
Create a simple game that:
- Keeps track of the player's score
- Has a countdown timer (30 seconds)
- When the player clicks a moving target, increase their score
- When time runs out, show the final score and check for a new high score
- Display both the current score and high score on the screen
Use both global variables (for scores) and local variables (for target-specific behaviors).