🟢
Coding🚀 Ages 7-10Beginner 6 min read

Events and Buttons in Scratch

A primary-school coding lesson on Scratch events: learn how hat blocks start code when you click the flag, press a key, or click a sprite, with examples and a quiz.

Key takeaways

  • An event is something that happens, like a click or a key press
  • Scratch starts code when an event happens, using hat blocks
  • The green flag block starts your project
  • You can broadcast messages so one sprite tells another to act

What is an event?

An event is something that happens. Clicking a button is an event. Pressing a key is an event. Clicking a sprite is an event.

In Scratch, code can wait for an event and then spring into action. This is called event-driven coding, and it's what makes projects feel alive and interactive.

Hat blocks start the action

In Scratch, events use special blocks called hat blocks. They have a rounded top, like a little hat, and they sit on top of a script.

A hat block waits quietly. When its event happens, it starts all the blocks beneath it.

The most famous one is the green flag block:

when green flag clicked
  say "Hello!" for 2 seconds

When you click the green flag above the stage, this script runs. The sprite says "Hello!" for 2 seconds. The green flag is how most projects begin.

Reacting to key presses

You can make a sprite move when a key is pressed:

when up arrow key pressed
  change y by 10

Now, every time you tap the up arrow, the sprite jumps up by 10. (Remember, y is the up-and-down position.) You could add more scripts for the down, left, and right arrows to move in every direction.

Clicking a sprite

You can also start code when a sprite is clicked, which turns it into a button:

when this sprite clicked
  play sound "pop"
  next costume

Click the sprite, and it makes a pop sound and changes its look. This is a simple way to build buttons in a game.

Talking between sprites: broadcast

Sometimes one sprite needs to tell another, "It's your turn now!" Scratch does this with broadcast and when I receive.

One script sends a message:

when green flag clicked
  broadcast "start game"

Another sprite listens for it:

when I receive "start game"
  show
  glide 1 secs to x: 0 y: 0

When the first script broadcasts start game, the second script wakes up and runs. This is how sprites work together, like actors taking cues in a play.

Putting it together

Events are often paired with loops and if statements. For example:

when green flag clicked
  forever
    if <key space pressed?> then
      change y by 10

This waits for the green flag, then loops forever, jumping up whenever the space key is held. Together, events, loops, and decisions let you build real games where the player is in control.

Quick quiz

Test yourself and earn XP

What is an event in Scratch?

What does the 'when green flag clicked' block do?

Which block shape is an event block in Scratch?

What does 'broadcast' do in Scratch?

FAQ

A hat block is a Scratch block with a rounded top that sits on top of a script. It waits for an event, then starts the blocks underneath it.

Yes. In Scratch, several event scripts can start and run together, which is great for animating more than one thing at once.