CS 105 - Understanding Our Algorithmic World

CS 105 - Exercise Two

Goals

  • Learn how to draw with code
  • Start thinking spatially
  • Learn a little bit about debugging your code

Prerequisite

Objective

Aside: When you opened the project, you will have noticed that the stage isn't as plain as it was the last time. If you click on the "Stage" button on the right, you will see that the scripting area changes. Now, rather than a tab called "Costumes", there is a tab called "Backgrounds". You can add new backgrounds to your projects by just dragging images into the "Backgrounds" pane.

In this exercise, you are going to tell the sprite how to traverse this maze without hitting the walls. To keep you honest, we are going to tell the sprite to draw the path it follows so we can tell if it ran into the red or not. This is what it should look like at the end (you are welcome to have different turns, just so long as there is a line drawn from the start to the end that doesn't touch the red):

Goal

I have given you some code to get you started (if you don't see it, make sure that you have returned to the sprite and clicked on the "Scripts" tab).

Starter code

The first thing I would like you to add is the pen down block. When this is down, everywhere the sprite goes, it will leave a trail behind. Click this onto the end of the script I gave you (we refer to each linked collection of blocks as a script).

Now, add a combination of move blocks, turn left blocks, and turn right blocks to maneuver the sprite through the maze. Recall that you can run the script by clicking the green flag button or by clicking on the hat block itself on the top of your script.

Hint Don't try to get the sprite home all at once. Add a block, and then run the script to see if the sprite is where you want it to be before adding another. The incremental approach will save you a lot of headaches as our scripts get more complicated. When things go wrong, it is easier to fix if you know it is just the last block you added that is the problem...

Tidying up

As you run your script a few times, you will find that the pen lines all over the place are a problem. Especially since the sprite draws when it resets itself back to the start.

The solution is to add a clear block to your script. Add it right after the pen down block.

Debugging

The other thing you will find is that you can't see what the sprite is doing. When you start the script, you see the result immediately. It can be helpful to get the sprite to slow down and show the steps.

At the top of the window, you will find the "Show stepping" button ( stepping icon ).

Click it.

The button will highlight and you will see a slider appear next to it. The slider controls the step speed. Move the slider to the middle of its range.

When you run your script now, you will see that Snap! highlights each block before carrying out the action, and pauses between actions so you can see what the effect was.

Submitting

Share the project using the instructions from exercise 1.

Visit the exercise page on Canvas to submit the URL.