انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Block: home
0. Lab tour1
1. Getting started5
2. Motion5
3. Looks and sound5
4. Events4
5. Loops5
6. Conditions and sensing4
7. Variables and randomness4
8. Project: the “Catch Robo” game3
9. Messages: characters talk to each other4
10. Clones: one character, many copies4
11. My Blocks: make a new block3
12. Operators and time4
13. The pro project: a jumping game4

What is a variable?

Section 7: Variables and randomnessLesson 7.1 · ⏱ 7 min

A variable is a box with a name that keeps a number that can change. The best-known example: the score in games.

🧩 At the start: the score is zero
Blocks: when 🚩 clicked, set (score) to (0)
🧩 When Robo is clicked: one more point
Blocks: when this sprite clicked, change (score) by (1), play sound (Pop) until done
🖥️ What happens on the stage?
The score starts at zero, every click on Robo adds one, and the number shows on the stage.
📌 Note
We always start with set at the flag, so every new game starts from zero.

📌 Another example: a step counter

The variable steps starts at 0, and every press of the arrow adds 1. join sticks the word to the number.

Blocks: when 🚩 clicked, go to x: (-150) y: (0), set (steps) to (0)
Blocks: when (right arrow) key pressed, change x by (20), change (steps) by (1), say (join My steps:  steps)
After 3 steps
After 3 steps
✍️ Practise
Make a variable called score, and make the two scripts.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

The variable lives starts at 3, every click on Robo takes 1 away, and Robo says how many are left.

👀 Show the solution
Blocks: when 🚩 clicked, set (lives) to (3)
Blocks: when this sprite clicked, change (lives) by (-1), say (join Lives left:  lives) for (1) seconds
After two clicks: 1 left
After two clicks: 1 left
Medium

Count from 1 to 10 with a variable count inside repeat, and Robo says the number.

👀 Show the solution
Blocks: when 🚩 clicked, set (count) to (0), repeat (10)
The result on the stage
The result on the stage
Hard

The variable s starts at 50. In every round make Robo's size = s, then add 20 to s. What is Robo's size at the end?

👀 Show the solution
Blocks: when 🚩 clicked, set (s) to (50), repeat (5)
The size is 130
The size is 130
Finished the lesson? Mark it so your progress counts.