Anna Gustafson
  • Portfolio
    • Art Gallery >
      • Dog Portraits
      • Cat Portraits
      • Landscapes
      • Commission me >
        • Prices
        • Photo Tips
    • Stage Management
  • Blog

Simple Wall Detection in Scratch


​Below are three methods to create walls that your character can't go through! ​
​
This post is using projects created on Scratch, which is a free, open-source software found at https://scratch.mit.edu/.
In this post I'll explain three different ways to detect walls in Scratch.
​
  1. Walls & The Mouse Pointer
  2. Semi-permeable Walls & The Mouse Pointer
  3. Walls & Arrow Keys (using either X / Y or directional based movement)

But first... what blocks are used to tell whether you're touching a wall in Scratch or not?

Blocks for Touching a Wall In Scratch

There are three main blocks you can use to detect if you're touching a wall in scratch.

Touching Sprite

Picture

This block is useful if your walls are more than one color. 

Touching Color

Picture

Useful if your walls are a solid color.

Color Touching Color

Picture

Useful for semi-permeable walls.

The examples below will show you how to use these blocks in different scenarios.

1) Walls & The Mouse Pointer

In this example this block is a solid color, therefore we'll use the "touching color" block to detect the wall.

Step 1:
​Create a wall, either as a sprite or as a backdrop on the stage. No need to add any code to it.
​

Step 2:

Add this code to your character that's not supposed to go through walls. 

Picture

Test out this example.

Instructions: Click the flag at the top of the player, and use your mouse to move the cat around. Notice that you cannot go through the block in the middle of the stage.

2) Semi-permeable Walls & The Mouse Pointer

This example is useful for racing kind of games - where you want to discourage players from driving off the main course. In the example below, the cars can't drive directly onto the grass, but if the back half touches the grass, the car will keep going.

Step 1:

Picture

Create a level with a solid color for the "walls", in this case it'll be grass.

Step 2:

Picture

Create a character with a certain color ONLY on the front.

Step 3:

Picture

Add this code to your character. Make sure to adjust:
- the starting size so your character fits inside the walls
-the starting position and starting direction, again so you don't spawn inside the walls

 Test out this example below

Instructions: Click the flag at the top of the player, and use your mouse to move the car around. Notice that you cannot go directly through the grass, but the back & sides of the car will clip the edges with no problem.

3) Walls & Arrow Keys

These kind of walls are useful if you're making a maze! It uses the same concepts as the mouse-pointer based movement, but for arrow keys instead.

Step 1:

​Create a wall sprite. No need to add any code to it

Step 2:

Add this stack of code to your character that's not supposed to go through walls.

Picture
Choose 1 of the following stacks for your arrow key controls and give it to your character. One uses X/Y the other uses direction-based movement.
Picture
This stack uses X and Y

Test out this example below.

Instructions: Click the flag at the top of the player, and use the arrow keys to move the dinosaur around.

Picture
This stack uses "point in direction" and "move 10 steps"
Honolulu, HI
  • Portfolio
    • Art Gallery >
      • Dog Portraits
      • Cat Portraits
      • Landscapes
      • Commission me >
        • Prices
        • Photo Tips
    • Stage Management
  • Blog