Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

3. Stay Inside the Lines

  • 1. Remix the Maze Starter Project
  • 2. Following the Mouse Pointer
  • 3. Stay Inside the Lines
  • 4. Using If-Statements to Win
  • 5. Add-Ons
  • 6. Reflection
  • 7. Wrap-up: Maze Game
  • 8. Wrap-up: Share Your Project
  • 9. Wrap-up: Show Your Project
arrow_backward Back Next arrow_forward

In this video, you’ll use “if” statements to keep the player sprite inside the boundaries of your game. Start by adding an “if” block inside the “forever” block already in this program. If statements run code if something is true or false. Every “if” block needs a condition -- which can either be true or false. As you figure out which condition to use, it can be helpful to either say or think about what you want the program to do next, for example, “IF the sprite touches the boundary, THEN do something.”

use a “touching color” block from the sensing menu.

Then, change the color by clicking on the color swatch, then on the boundary in your project. Great! Now the computer can make a decision: If touching the boundary color, then… In the then portion of the if-then block, program the player sprite to go back to its starting position if it touches a boundary.

To do this, use a “go to xy” block from the motion menu.

The “go to xy” block has two values, an x and a y. These numbers represent the sprite’s position on the screen. If you move the player sprite to where it will start, the values in the block will change to that position.

When you click and run this block, the sprite goes to the position it was in originally when you dragged out this block. Before you go any further, test this if statement to make sure it’s working correctly. Click on the sprite, and it should begin to follow the mouse. If it touches the boundary, then the sprite goes to the position you set with the “go to” block. Great! But, there is one problem. The sprite continues to follow the mouse. Remember, the computer will do only what you tell it to do, and you didn’t tell the sprite to stop following the mouse.

Fix this bug by adding a ‘stop all’ block to the “if” statement. When this block runs, all code in the program will stop. Now, try it out one more time.

Look at that! Now when the sprite touches the boundary, it goes back to the beginning and no longer follows the mouse. To play again, the user clicks the sprite to run this block stack again.

This is a difficult problem! Keep trying to solve it, and don’t get discouraged if the first few solutions you try don’t work. Now it’s your turn: Make the sprite return to the starting position by adding “touching,” “go to x y,” “stop all” and “if” blocks.

  1. Add an "if" block.
  2. Make the "if" block check if the sprite is touching the the color of the boundary.
  3. If the sprite is touching the boundary, make it return to its starting point.
  4. If the sprite touches the boundary, stop all code from running.