How to Get Started
This article will teach you how to make a controllable sprite in Scratch coding, a free visual programming language. The script we write will tell a sprite to move in the four cardinal directions when a player presses the arrow keys.
To follow along, make sure to first do these steps:
- Make an account at the Scratch website.
- Create a new Scratch project, and use the “Choose a Sprite” button to create a new sprite.
- Practice programming basic left and right sprite movement with our how to program simple movement in Scratch tutorial.
Once you’ve learned the simple method of making sprites move, making them move smoothly is a natural next step. As with other programming languages, it’s always a good goal to try and learn how to iterate on code you’ve made before and find ways to make it run better or more efficiently.
Why is Smooth Movement Important?
Movement of some sort is present in almost every game made in Scratch. It’s usually the primary way the player interacts with your game. By pressing the arrow keys, players tell their character to move around on the screen. They can control the character in this way in order to collect items or complete objectives in the game.
Because it’s what the player spends the most time doing, it’s important that our character moves as smoothly and intuitively as possible.
In our how to make a sprite move in Scratch tutorial, we used a very simple system of movement. It works, but you may have noticed that the character kind of stutters around the screen. In this article, we will introduce a more advanced piece of code that works much better.
This is perfect for Scratchers who have tried the simple system, and want to step their game up a notch. This style of movement feels good. It’s continuous and smooth, and still pretty easy to create. Let’s check it out!
How to Program Smooth Movement
Step 1. Write Code for 1 Direction Movement
Here’s a more advanced way to add movement to your game. Let’s start by writing code to move in just one direction.
This code begins running
when the Green Flag is clicked. It’s inside of a forever loop in order to make sure it stays running for the entire game. When the right arrow key is pressed, the sprite moves to the right. The blue motion blocks inside the if statement do that for us.
point in direction block tells our sprite to look to the right. Because of this block, the sprite will always look in the direction that it is moving. Next, the
change x by 10 block is what actually moves the sprite. We can make it move faster / slower by increasing / decreasing the value.
If you want to make your code simpler, you can optionally use a
move 10 steps block instead of the
change x by 10 block. For moving around, there’s not any major differences.
- A sprite’s x-value (x coordinate) determines its horizontal position (x position) on the screen. If we want a sprite to move to the right, we increase its x position. To move to the left, we decrease it instead.
- Similarly, a sprite’s y-value (y coordinate) determines its vertical position (y position) on the screen. if we want a sprite to move up / down, we tell it to change its y position by a positive / negative amount.
Step 2. Write Code for All 4 Directions
Now that we understand how to move in one direction, let’s apply this to extend our code. The following script allows movement in all four primary directions.
This is pretty much just an extension of the original code. Now we have an if statement for all four directions! Notice that all of them reside inside of the same forever loop. We could split these off into separate loops, but it’s not necessary.
Each if statement here is in charge of moving in one direction. These if statements all use similar code, but the numbers are slightly different for each.
Notice that for each if statement:
point in directionblock specifies a unique direction. This tells the sprite to point towards each direction it’s supposed to move in.
- For the
right arrowif statements, we use a
- For the
down arrowif statements, we use a
change yblock instead.
If you only want to move left and right, you can simply remove the
up arrow and
down arrow sections.
Once you recreate this code, you should find that your sprite moves around super smoothly!
Debugging: However, we’re not done just yet. There’s a bug here that needs fixing. As with any style of movement in Scratch, you might find that your sprite flips upside down when moving to the left! Fear not, as this is a common issue that most Scratchers experience.
There’s a simple solution to this, which we discuss in our article on how to make a sprite move in Scratch. Check out the “Fixing Bugs” section there for more information.
Comparing Our Finished Code
Now that you’ve fixed any bugs, you should find that your sprite moves around flawlessly! Compared to the simpler method of movement, this is much more continuous and satisfying.
Smooth movement offers several advantages that the simpler method does not:
- More responsive control
- Fast and continuous movement
- Diagonal Movement
For all of these reasons, we believe that smooth movement is objectively superior to simple movement. Once you have learned this method, there’s no reason to look back!
For a more intuitive understanding of the advantages that smooth movement offers, check out these two Scratch projects for a side-by-side comparison:
Smooth movement is the foundation of almost every game in Scratch. Now that you’ve got a controllable character, you can create anything you want!
Your next challenge should be to make a simple game which uses this movement. You can check out the following articles for more help.
- How to make a simple game in Scratch
- How to make a Scratch sprite jump (another essential game mechanic)
Additionally, stay tuned for our step-by-step coding tutorials on how to make specific types of games in Scratch. This movement will fit into all of them!
Scratch Coding Classes for Kids
For structured Scratch learning, Juni Learning offers project-based Scratch coding courses for kids 8-11 to get started with coding. Our Scratch curriculum prepares students with full mastery of the Scratch environment, and prepares them to advance into coding with more advanced, text-based coding languages like Python.
- Game Superstar (Scratch Level 1 course): introduces computer science fundamentals such and teaches students how to build and design their own Scratch games.
- Game Master (Scratch Level 2 course): covers more complex concepts like nested loops, complex conditionals, cloning and more in preparation for learning more advanced coding languages.
Check out our coding for kids courses, or speak with a Juni Advisor by calling (650) 263-4306 or emailing email@example.com.