Intro to Scratch Games 3: Collisions and Solid Walls

About this lesson

In this lesson we are going to learn how to code solid walls and check for collisions between sprites.

Concepts Reviewed:

Cartesian Coordinates
If/else statements
Step 1
In this lesson we are going to learn how to make solid walls and check for collisions between sprites.
Step 2
We can check for collisions by using the "touching color" block.

This block will be "true" if the sprite is touching the given color, and "false" if it is not touching the color.

Step 3
We will use this block to determine when the player hits an enemy.

By using this block as the condition of an "if" statement, we can have the code inside the "if" block only activate if the player is touching the given color.

In the example below, the player will go to a specific location if it is touching the color red.

Step 4
Add this block of code to your "Player" sprite.

Placing the "if touching color red" block inside a "forever" loop makes the game check for the collision over and over again as the game is being played. If the player touches the color red at any point, it will move the player back to the starting position.

Here is how to change the color in the "touching color" block:

Check Your Work
Step 5
Click the green flag and test your game!

The player should be sent back to the start of the level if they touch the color red.

Step 6
In the next part of the lesson we are going to create solid walls that the player can't pass through!

We will do this in the following way:

  1. Move the sprite using "change x" or "change y" as before
  2. Check to see if the sprite is touching the color of the walls (black)
  3. If we are touching a wall, move back in the opposite direction by 1 unit
  4. Repeat this process for each unit we moved so we end up flush with the wall
Step 7
Begin by expanding code for downward movement as shown.

This block of code now moves the player down by 5 when the down arrow is pressed as before, but it also then checks to see if it is touching the color black 5 times (once per unit moved) and if it is, it moves the player back up.

Check Your Work
Step 8
Test your solid wall!

You may notice that the wall only blocks your movement when you are moving down. This is because we only added code that handles solid walls to our downward movement block.

Step 9
CHALLENGE: Make the walls solid when the player is moving in any direction!
Step 10
Let's improve this by using functions!

The two blocks of code on the right do the same thing as the block of code on the left!

We define a new block that includes all of the code we need to move the sprite down and account for solid walls, then we activate only that custom block when we press the down key.

Step 11
Make a new block for "move down steps"
Step 12
Replace the old code for moving down with the new block that we created.
Check Your Work
Step 13
Make sure your downward movement and solid walls work properly using the new block!
Step 14
CHALLENGE: Create new blocks for all four directions and use the custom blocks for movement instead of the old code.
Step 15
In the next lesson we are going to learn how to make the movement look smoother and add scoring!
Intro to Scratch Games 3: Collisions and Solid Walls Info



Created By



Intro to Scratch: Game Development

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach