How to Detect the Maze Walls 

 

Last week, I introduced you to the Vita-Man maze game!  We learned how to program Vita-Man to move when the arrow keys are pressed, and also how to get Vita-Man to move more smoothly by always moving in the direction of the last arrow key pressed!  Today, I'm going to show you how to detect the walls of the maze!  

how to detect the maze walls

In order to detect the walls of the maze while simultaneously keeping Vita-Man moving smoothly, we'll need to find a way to detect the walls without actually bumping into them!  We’re going to do this using Vita-Man’s fifth costume - the ‘detector’ costume!

how to detect the maze walls

The ‘detector’ costume wraps around Vita-Man’s other costumes.  As we move ‘Vita-Man’ around the grid, we’re going to constantly switch its costume to this ‘detector’ costume, in order to check whether we’re getting too close to a wall!

The Freebie

 

When I teach my own students how to create games in Scratch, I always have different versions of the project stored on a flash drive I bring with me to the classroom.  As much as I believe in having kids debug their own programs, there are situations in which you just need to get everyone up to speed and on the same page.  These are situations such as when a student was absent or a sprite was accidentally deleted, in which case it is extremely helpful to have on hand, readily accessible, a version of the game completed up to a certain point.   

how to detect the maze walls

And that, my friends, is the freebie I've prepared for you today!  

Click the button below and enter your email address, and you'll receive a zip file with 7 versions of the project - one for the starter project and one for each of the six steps.  

And finally, it is time to continue where we left off and start using our 'detector' costume to detect the walls of the maze...  

Placeholder Image

Step 3:  Become a Detector!

 

In this video, we identify the location in the code where Vita-Man is going to need to detect the walls of the maze.  We then make sure Vita-Man is wearing its 'detector' costume at this point, and we switch the costume back to the normal one as soon as the task of detecting the walls is completed. 

divider

Step 4:  Pick the Colors!

 

To decide whether Vita-Man is too close to a blue wall, we'll check whether the blue of the walls is '"touching" a particular color from Vita-Man's 'detector' costume.  The 'detector' costume is composed of 4 different colors.  We’ll decide which of the 4 colors in the ‘detector’ costume to compare with the blue of the walls, based on the direction in which Vita-Man is trying to move!  In this video, I show you how to accurately pick both the colors in the 'detector' costume and the blue of the walls.

divider

Step 5:  Detect the Walls!

 

Now that Vita-Man is wearing its 'detector' costume and the 'touching' blocks are ready to be used, it's time to actually put together the code which will prevent Vita-Man from going through the walls of the maze.  Follow the directions in the video below, and you'll have Vita-Man moving inside the maze like a pro!

divider

Step 6:  Escape!

 

We finally have Vita-Man moving on the grid and fully detecting the walls of the maze!  Now, let's give Vita-Man a way to escape from one side of the screen to the other... 

divider

What's Next

And that is it, my friends!  Getting Vita-Man to move on the grid and avoid those maze walls was not a simple task, but we got it done, and Vita-Man is moving smoothly!  

The next step in building this game is to have Vita-Man actually consume the vitamins (or green dots on the screen).  This is easier than it might seem as all we have to do is paint over the dots when we encounter them.  We'll then move on to programming the candy crawlers, Choco, Cookie, Smartie, and Gummy.  

how to detect the maze walls

The movement of the candy crawlers is very similar to that of Vita-Man's except the crawlers are not controlled by the arrow keys.  Instead, they are programmed to follow Vita-Man.  Once we have all our characters moving, we'll be able to focus on programming the power snacks to actually give Vita-Man some power.  Then, we'll give Vita-Man the ability to use that power to eat the crawlers and send them to jail.  Finally, we'll give the crawlers the ability to eat Vita-Man, and we'll add both a losing and a winning condition to our game.   

how to detect the maze walls

The Vita-Man Course

This is a complex course, but could you imagine how much you'll learn and how much fun you'll have while building it with your students?!  The image below will take you to a page with a lot more information as well as a preview of the course...

Let's Become Better Friends...

Would you like to share your experience of teaching this lesson and/or connect with 3,500+ other educators who are passionate about technology and education?  Click here to join our private Facebook group for support, encouragement, and extra resources!

I'll see you on the inside and until then...  Keep creating, coding, and scratch'in away!

Placeholder Image