Scratch Code Examples: Learn Scratch Programming (2024)

Scratch is a simple, fun coding platform designed especially for kids. We teach Scratch in our elementary school coding program, but even adults can enjoy coding with Scratch. The Scratch platform does a pretty good job of making it easy to jump into coding, but even the simplest programs are intimidating for someone who is just getting started. It’s helpful to see Scratch code examples and try them out for yourselves. After all, it’s a brand-new interface with buttons, blocks, and more.

📌 [Download] Printable Scratch Coding Tutorials Get 2 printable Scratch tutorials, Rocket Landing and Flying Space Cat, to code your own games step by step. Download Now

We’re going to take a look at different Scratch code examples and what they do, but first, what is Scratch and why should you care?

Learn Programming in Scratch

Scratch is a free platform for learning how to code. It’s popular in large part because MIT put a ton of work into making Scratch programming language easy to understand and use. It’s a block-based coding language and allows users to drag and drop colorful blocks of code to build animations or games.

While the way learners interact with the code is simplified, the code itself is not. Each block represents a chunk of real code, which means kids are learning real coding concepts when they work in Scratch. As such, Scratch is a great platform for teaching your child coding concepts like conditionals and loops, exploring how (X,Y) coordinates work, or what exactly an “event” is in coding.

If that sounds interesting, you can also check out our Scratch coding classes for kids!

Recommended:Kids Coding Websites

Scratch Code Examples: Learn Scratch Programming (1)

Scratch Code Examples for Beginners

Scratch builds complete programs by connecting a series of code blocks together. Each block represents a piece of real code inside that users can string together to make things happen. In a Scratch program, different kinds of blocks have different colors and names that tell you the type of block it is and what job it does.

Let’s take a look at a few Scratch code examples. These are some of the most common blocks in beginner Scratch programs and what they do.

  • Event blocks
  • Motion blocks
  • Looks Blocks
  • Sound Blocks
  • Control Blocks

You can also see how these example blocks work together to build a complete project, like our Scratch Valentine’s Day tutorial.

Scratch Code Examples: Learn Scratch Programming (2)

Event Blocks

The orange blocks in Scratch, usually with a rounded top, are event blocks.

In most cases, the event blocks are the ones that begin each sequence of code. They usually can’t be added below other blocks on a chain. Their job is to wait for a specific event to happen or send a message to other blocks so they can tell the code blocks below them to go!

Scratch code blocks that aren’t attached to an event block won’t run, so event blocks are a necessary part of every program.

When Green Flag Clicked

The “When Green Flag Clicked” event block starts a chain of code when the green flag button is clicked. This is commonly used as the way to start the code for an animation or game that doesn’t need other interaction from the person playing it. Essentially, it’s a start button to get your code going!

Scratch Code Examples: Learn Scratch Programming (3)

When Key Pressed

The “When Key Pressed” event block starts a section of code when a specific key is pressed on the keyboard. It’s useful for when you want something to happen when the user presses a key, like getting a sprite (a character or image) to move around.

Scratch Code Examples: Learn Scratch Programming (4)

When This Sprite Clicked

In Scratch programming, sprites are elements and characters in your project. The “when this sprite clicked” event block starts the code when the user clicks on the chosen sprite. This is useful for any Scratch game that involves collecting items by clicking on them or for letting users click on buttons using the mouse. On touchscreens, a click event happens when the user taps on the sprite with their finger.

Scratch Code Examples: Learn Scratch Programming (5)

Motion Blocks

The motion blocks are the blue blocks. These are the blocks that control the movement of the different sprites in your program. Any time you want a sprite to change its position on the screen, these are the blocks that you’ll add.

Move

TThe “move” block is the most basic of the motion blocks. When activated, it will move its sprite the indicated number of steps in the direction the sprite is facing. So, if you wanted to move your sprite 10 steps to the right, you would make sure it is facing 90° (the right side of the screen) and activate the move block.

The white circles inside these blocks have numbers that you can change so you can move the sprite the specific distance that you want.

Scratch Code Examples: Learn Scratch Programming (6)

Turn

The “turn” blocks change the direction your sprite is facing by the number of degrees shown in the white circle. In a maze game, your sprite might need to turn 90° to travel the right path. Remember that the direction a sprite is facing affects the direction that the “move” block takes it.

Scratch Code Examples: Learn Scratch Programming (7)

Go to

The “go to” block will instantly move its sprite to the coordinates shown in its circles.

Scratch Code Examples: Learn Scratch Programming (8)

Glide

The “glide” block will also take its sprite to the set coordinates, but will do it over a number of seconds instead of doing it instantly. This is useful if you want to show the movement instead of just moving the sprite instantaneously.

Scratch Code Examples: Learn Scratch Programming (9)

Looks Blocks

Looks blocks affect how sprites and backgrounds appear to the user. This is great for making sprites look like they are Looks blocks affect how sprites and backgrounds appear to the user. This is great for making sprites look like they are moving or for changing the scenery in animations. Changing the looks of a block is a separate task from changing its position. If you want to make a sprite appear to be walking, you’ll need to activate both a motion block and a looks block.

Switch Costume

Changing the looks of a sprite is done by switching between costumes. Costumes are different pictures that are all connected to a single sprite. When the “switch costume” block is activated, its sprite will change to the specific costume shown in the dropdown menu.

Scratch Code Examples: Learn Scratch Programming (10)

Switch Backdrop

Changing the looks of the scenery is done by switching between backdrops. When the “switch backdrop” block is activated, the picture used as the background will change to the backdrop indicated in the dropdown.

Scratch Code Examples: Learn Scratch Programming (11)

Show and Hide

The show and hide blocks affect the visibility of sprites. If there is a sprite that you don’t want visible on the screen, the “hide” block can make it invisible, but still a part of your program. Likewise, if there is an invisible sprite on the screen that you want to make visible, the “show” block will make it visible.

Scratch Code Examples: Learn Scratch Programming (12)

Sound Blocks

Sound blocks are blocks that affect the audio playing instead of anything visual. You can choose specific sound files included on Scratch, upload your own files, or even record directly in the program if you like.

Of the three most commonly used sound blocks, two will play your chosen sound file, but there’s a major difference between them.

Play Sound Until Done

The “play sound until done” block will play the file until it finishes before activating the next block. This keeps sounds from overlapping each other, which could make any dialogue difficult to understand. It’s also useful if you want a sprite to finish saying what he’s saying before moving.

Scratch Code Examples: Learn Scratch Programming (13)

Start Sound

In contrast, the “start sound” block will play a sound, but will also instantly activate the next block in line. This is useful if you want to stack sounds on top of each other, or activate another block while the sound is playing.

Scratch Code Examples: Learn Scratch Programming (14)

Stop All Sounds

The “stop all sounds” button does just what it says. When this block is activated, it will stop all sounds from playing. Note that this doesn’t pause or mute them, it stops them from playing. That means that sound files will start over from the beginning if they are activated again.

Scratch Code Examples: Learn Scratch Programming (15)

Control Blocks

Control blocks don’t directly affect sprites, backdrops, or audio. Instead, they control when and how often other blocks get activated. That gives you the ability to code loops, add in delays, and generally increase the amount of control you have over your code.

Wait

The “wait” block keeps code blocks from activating directly after the block ahead of them. This is useful for making sure block actions happen exactly when you intend them to.

Scratch Code Examples: Learn Scratch Programming (16)

Repeat

“Repeat” blocks are used to create code loops. When a “repeat” block is activated, it will repeat whatever other blocks it contains for the specified number of times before activating any block underneath. This helps a ton in keeping the overall length of code down and helps in avoiding small mistakes in repeating code.

Scratch Code Examples: Learn Scratch Programming (17)

Forever

The “forever” block is a special loop block that will continue activating whatever blocks it contains until it is made to stop. This is especially useful for things like creating background music or other actions that you don’t want to stop.

Scratch Code Examples: Learn Scratch Programming (18)

💻 Scratch classes for kids ages 8-10. Learn Scratch with fun and enagaging, live coding classes. View Elementary School Program.

Scratch Game & App Tutorials

Learn how to create a simple game or application with these free projects. Find full Scratch code examples in our programming tutorials.

Learn Coding5 Popular Game Mechanics in Scratch [60 Second Lessons]January 12, 2024
Learn CodingHow to Make Flappy Bird on ScratchApril 28, 2023
Learn CodingScratch Tutorial For Kids: Flying Space CatDecember 5, 2022
Learn CodingSimple Scratch Tutorial for Kids: Code a Rocket Landing GameUpdated on September 14, 2023

Download Free Printable Scratch Coding Tutorials PDF

Get the Rocketship Landing game and Flying Space Cat Scratch tutorials in a printable format.

Make Games & Apps in Scratch

Now that you have the hang of the basic kinds of Scratch blocks, it’s time to put them to use!

Everything you can make in Scratch is created through combinations of the different kinds of blocks, and there is no better way to learn than by jumping in and trying it out yourself. But if you’re interested in getting a head start on learning, be sure to check out CodeWizardsHQ’s Scratch coding classes.

Good luck, and happy Scratching!

Scratch Code Examples: Learn Scratch Programming (2024)
Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 6418

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.