Using the Random Block

Next, you'll add blocks so that every time Alonzo is clicked, he moves to a random position on the stage.

  1. Find the pick random (1) to (10) block in the green Operators palette, and click it several times to try it out.
  2. Talk with Your Partner What values did the random block report?
  3. Add a line of code so that every time Alonzo is clicked, he not only turns, but also moves to a random position.
    • Use two pick random () to () blocks and one go to x=0 y=0 block together at the end of your script. The animation below shows how to snap these together, but you will need to do more than it shows.
    • Make Alonzo move to a random position between -190 and 190 in the x direction and between -130 and 130 in the y direction.

    Placing random block in goto block

    Right now, the Snap! stage (the white rectangle where sprites act out their scripts) doesn't show axes or grid lines, but locations on it are still specified by coordinates. The center of the stage is (0, 0), and the stage runs from x = -240 to x = 240 and from y = -180 to y = 180.

    Why limit Alonzo's moves between -190 and 190 in the x direction and between -130 and 130 in the y direction? So that he stays completely visible on the stage.

  4. Click on Alonzo. If your script works, Alonzo should face the other way and move to a random spot on the stage.
THE FOLLOWING WAS REMOVED BECAUSE IT IS TOO MUCH TOO SOON, BUT KEPT VISIBLE, IN THIS COMMENT, SO THAT WE CAN USE IT SOMEWHERE.
Coordinate Grid

The Snap! stage (the white rectangle where sprites act out their scripts) has coordinates. The x axis shows from -240 to 240, and the y axis shows from -180 to 180.

You don't have to make your stage look like this picture. But if you want to, click the Stage thumbnail picture in the lower right, choose "Backgrounds..." from the Snap! file menu, and import "XY Grid". Then, turn on reporter watchers for x position, y position, and direction by clicking the checkboxes beside them in the blue Motion palette on the left.

On this page, we let Alonzo move only between -190 and 190 in the x direction and between -130 and 130 in the y direction so he doesn't go off the edges of the stage.