Events: Make Your Sprite Listen
Kids learn what events are, then use arrow keys and clicks to control their Scratch sprite like a real game character.
CarnielSeriesCoding for Children4 of 4
On this page13 sections
Welcome back, coder! Last time you built your first Scratch program. Your sprite said hello and walked back and forth, forever.
But did you notice something? Your sprite never listened to you. It did the same thing every time, no matter what you pressed. Today we fix that. You are going to make your sprite move when you press a key, just like a character in a video game.
What Is an Event?
An event is something that happens. When it happens, some code starts running.
You already know lots of events from real life:
- When the doorbell rings, you open the door.
- When the school bell rings, everyone goes outside to play.
- When your alarm goes off, you wake up (slowly!).
Computers work the same way. They wait for something to happen, then they do the job you gave them.
In Scratch, event blocks are yellow and have a round top, like a hat. That round top means "this block goes first." You used one last time: when green flag clicked. Today you will meet two more.
Warm-Up Game: Robot Commands
Try this with a friend or a grown-up before you touch the computer. One person is the coder. The other person is the robot.
- The coder makes up three rules, like: "When I clap, jump. When I say beep, spin around. When I wave, sit down."
- The robot stands still and waits.
- The coder claps, beeps or waves in any order, and the robot follows the right rule.
The robot only moves when an event happens. That's exactly how your sprite will work today. Swap places and play again!
Make Your Sprite Walk with the Arrow Keys
Open Scratch at scratch.mit.edu and click Create. You can also open your project from last time with File, then Load from your computer.
Before we start, here is one new idea. The stage is like a big map. x tells Scratch how far left or right your sprite is. y tells Scratch how far up or down it is. Make x bigger and the sprite goes right. Make y bigger and it goes up.
Step 1: The right arrow
Click the yellow Events circle. Drag when space key pressed into the code area. Click the little arrow next to space and choose right arrow.
Now click the blue Motion circle. Snap these two blocks under it:
- point in direction 90 (90 means "face right")
- change x by 10
Press the right arrow key on your keyboard. Your sprite moves right! Hold the key down and it keeps going.
Step 2: The left arrow
Make a new script the same way, but choose left arrow. Snap in point in direction -90 (that means "face left") and change x by -10. The minus sign makes the sprite go the other way.
Uh oh. Is your sprite upside down when it faces left? You already know the fix from last time. Click on the sprite's code and add set rotation style left-right under a when green flag clicked block, then click the green flag once.
Step 3: Up and down
Make two more scripts:
- when up arrow key pressed with change y by 10
- when down arrow key pressed with change y by -10
Now try all four arrow keys. You are driving your sprite around the stage!
No keyboard? On a tablet, you can use when this sprite clicked instead, or add the arrow keys to the screen later when you're ready for a bigger challenge.
Make Your Sprite Ticklish
Here's one more event. Click the yellow Events circle and drag out when this sprite clicked. Snap a purple say Hello! for 2 seconds block under it and change the words to That tickles!
Now click on your sprite on the stage. It giggles at you!
Want it sillier? Add a change color effect by 25 block from Looks. Every click turns your sprite a new color. To get the normal color back, click clear graphic effects.
Lots of Scripts, All at Once
Look at your code area. You now have five or six little scripts, each with its own hat block on top. That's normal! Each script sits quietly and waits for its own event. When you press the up arrow, only the up arrow script runs. The others keep waiting.
Real games are built exactly like this. One script for jumping, one for running, one for when you get hit. You just built the controls for your very first game.
Challenges
- Turbo mode: change all the 10s to 20s. What happens? Remember to change -10 to -20 too.
- Space jump: make a script for the space key with change y by 50, then wait 0.3 seconds, then change y by -50. Your sprite jumps!
- Back to the middle: make the c key run go to x: 0 y: 0. Now one key brings your sprite home.
- Add a sound: click the pink Sound circle and put start sound in your tickle script.
New Words
- Event: something that happens, like a key press or a click, that starts some code.
- Hat block: a block with a round top. It always goes at the top of a script.
- Script: a stack of blocks that snap together.
- x: how far left or right a sprite is.
- y: how far up or down a sprite is.
Quick Quiz
- What is an event?
- Which way does your sprite move if you change x by -10?
- Which block makes something happen when you click the sprite?
- You have six scripts. You press the up arrow. How many scripts run?
Answers: 1. Something that happens and starts some code. 2. Left. 3. When this sprite clicked. 4. Just one, the up arrow script.
What's Next?
You can drive your sprite now. But it still can't tell when it bumps into something. Next time you will learn if-then, which lets your sprite make choices: if it touches a star, then something fun happens. You will use your arrow-key controls to build a real catching game. Keep this project saved!
For Grown-Ups
This episode introduces event-driven programming, the idea behind every app and game: code waits for input, then reacts. Your child also meets x and y coordinates for the first time. They don't need to understand graphs yet. "x is left and right, y is up and down" is enough for now. Ask your child to save the project (File, then Save to your computer), because Episode 5 builds on these exact controls.