Hardware Reference
In-Depth Information
Using “clear” and Setting a Start Point
You may have noticed that every time you press the
button, the image you have
just drawn remains on the screen. his can be frustrating when you are testing scripts.
To ensure you have a clear stage every time you run a script, add the
clear
block
from the pen palette underneath the starting control block
When clicked
, as
shown in Figure 4-4. his block tells the program to remove the previous action before
proceeding.
You will also notice that the turtle begins its drawing wherever it is located. his can
afect your drawing, and part of it may end up ofscreen if the turtle is positioned too
close to the edge of the stage. To avoid this, you can add the motion block
go to
x: 0 y:0
to set the start point of the pen, just like you did in Adventure 3 for the
Adventurer sprite in the inal game project. Remember that Scratch uses x and y coor-
dinates, with
x:0
and
y:0
being the middle of the stage. You can also set the direc-
tion that the sprite faces before it draws your shape by using the motion block
point
in direction 90
after the start coordinates. Figure 4-4 shows these blocks added
to the script.
Using Variables Instead of Values
It is more logical to set
variables
for values that you wish to use several times in pro-
gramming. In your pentagon drawing, you used values for the length of the side of the
shape (100 steps), the angle of the turn (72 degrees) and the number of sides (repeat
5). In this part of the project, you create variables that will make it easier to create
similar shapes in the future.
1.
In the blocks palette, click Variables
➜
Make a Variable. You need to make three
variables, called
Number_Sides
,
Angle
and
Side_Length
.
2.
Drag the three new variable blocks onto the script and set the value of
Number_
Sides
to 5,
Angle to
72, and
Side_Length
to 100.
3.
Underneath the variables blocks, add your simple shape script, but now instead
of the values that you typed into the boxes, add the variable names (see Figure
4-4).
4.
Now that you are using variables, you no longer need to calculate the angle of
rotation. Instead, you can set the
Angle
variable to divide 360 (the number of
degrees in a circle) by the number of sides that you set. To do this, drag the
operators block
0 / 0
and replace the value 72 with it. hen type 360 into the
left hand box, and drag the variable block
Number_Sides
into the right box.
5.
If you change the number of sides to 6, the script will draw a hexagon; if you
change the number of sides to 4, it will draw a square, and so on.