Lesson 2: Wall Collisions

Lesson Overview

In the Previous Lesson, we programmed the Cat to move when we pressed the Arrow Keys. In this Lesson, we will be programming the Cat to collide with the walls of the Maze, rather then going through them. We will also be adding a way for the player to “Win” the game after retrieving the treasure.

Continue from the Previous Template, or open the one linked below. Hover Mouse below to reveal template link

https://scratch.mit.edu/projects/495977975/


Step 1: Detecting Color

To begin, select Sprite1, the Cat. The movement code from the previous Lesson should still be there.

Underneath our last If-Then, we will be adding in additional If-Then. Inside of it, place the Touching Color? block from Sensing.

The Sensing blocks are used to detect things. They can be used to determine the location of the mouse-pointer, its distance from other sprites, or even if one of our Sprites is touching a specific color.

In this case, we will be using this to detect when our Sprite comes into contact with the walls of the Maze.

e will need to adjust the Touching Color? blocks Color to match the color of our Walls. This will allow our Player to detect when they have come into contact with the Walls.

Left-Click inside of the Color Bubble. Look for the Bottom Option, the Color Matcher.

We will need to adjust the Touching Color? blocks Color to match the color of our Walls. This will allow our Player to detect when they have come into contact with the Wallls.

Left-Click inside of the Color Bubble. Look for the Bottom Option, the Color Matcher.


Using the Color Matcher, click on the Black Walls of the Maze. This should change the Color for the Touching Color to Black

This will allow us to constantly check if the Player is touching the walls of the Maze. If the answer is ever “True”, the code placed inside of this conditional statement will trigger.


Step 2: Moving to the Start

Inside of the If-Then, add in the Go To X: Y: block from Motion. This block allows us to ‘teleport’ the player to a specified coordinates.

We will be adding in the coordinates for the Starting Location. This will make it so that when the Player bumps into a Wall, they are teleported back to the Start.

Make sure to Test your game, and ensure that this is working as intended!