Exercise 3: Asking a question
Make a similar program to draw regular polygons
but this time use an 'ask' block (in the sensors set) to allow
the user to specify how many sides the polygon is to have. The
answer typed in by the user is stored in a special variable
called 'answer' which can then be used in later blocks. For
example, the repeat loop can be set up so that it repeats for a
number of times equal to the number stored in 'answer'.
To get this program we need to be able to
calculate how many degrees to turn between drawing the lines of
the polygon. For a square we turned 90°. For a triangle we
turned 120°. Can you see that the angle can always be found by
dividing 360° by the number of sides? The following script works
well for polygons up to about 6 sides but much larger polygons
will hit the edge of the stage and become distorted. Can you
find a solution for this? Can you also modify the script so that
each time a shape is drawn the program automatically re-poses
the question? Note that the cat has been shrunk and placed in
the top left corner of the stage.

Exercise 4:
Broadcasting a message
Next we will try
making scripts (stacks) that use or 'call' other scripts. In
Scratch jargon we say that the first script 'broadcasts a
message' and the other script receives the message and runs its
scripts.
In
our script we will make a script to draw a house which simply
'calls' our square-drawing script and then 'calls' our
triangle-drawing script to place a roof on top of the square to
make a simple house shape. We'll just have to be careful to make
sure that the triangle is drawn in the right place so that it
does indeed sit on top of the square.
Look at the following scripts. They are the same
as the script that
you saw in a previous exercise except that this time the 'green
flag' script simply broadcasts the message 'square' and the
other script receives the message and draws the square. Create
the scripts and check that they behave as expected (shrink the
cat and put it in the top left corner before you run the
script).
The advantage is that now we have a script that
can be called from inside other scripts, such as this
'house-drawing script':

I've left it up to you to make the script for
drawing a triangle with sides of 150 pixels. You are probably
expecting to get a square and a triangle when you click the
green flag, not necessarily with th
e
triangle correctly placed on top of the square. But I think you
will get something much worse, perhaps a mess like I got:
This shape contains neither the square nor the
equilateral triangle we expected. What went wrong? What happened
is that the drawing of the square takes a little time but
Scratch does not wait for the square drawing to finish before
beginning to draw the triangle, so the two shapes get mixed into
one. The solution then is to force Scratch to wait for the
square to be completed before allowing the triangle to be drawn.
Scratch has a special 'broadcast and wait' block that broadcasts a message and then
waits for the other script(s) triggered by that message to
finish before allowing the next block to run. So modify
the green flag script so the it looks like this (note that I
have also added a block to erase any existing drawing):

If you run the new scripts you should get a
shape as above, with the square and triangle visible but with
the triangle in the wrong place. To make the triangle appear in
the right place we can set the direction to 30° (direction is
measured clockwise from the vertical) just before the triangle
is drawn so modify your 'triangle' script accordingly and then
test and save your project ('house' would be a good name').
You might be wondering whether it wouldn't be
easier to draw the house shape WITHOUT using broadcast messages.
The answer is yes, but the above exercise is mainly designed to
introduce broadcast messages which you will certainly find
useful later, such as in the next exercise. One reason to use
messages in this way is that it allows you to split your program
into reusable 'modules' and also to stop the main structure (the
green flag script above) from getting cluttered with distracting
detail.
Exercise 5
In this exercise you will make a new script that
will call (trigger) the house script three times to make this 'village':

Here is the beginning of the solution:

You really need to study closely only the first
of these scripts. You can see it will go to a specific location,
trigger the house drawing script, then go to a different
location and trigger the house drawing script again. Therefore
this script is incomplete for it draws only two houses, not
three.
The other scripts are basically the same as what
you saw before except that:
It's your job now to create the above scripts
and to check that they work as expected, then extend the main
script so that it draws the third house in the right location
(the houses should be evenly spaced). As usual, it's a good idea
to shrink the cat sprite so that it doesn't hide too much of the
drawing. Please save this project with the name 'village'.
Exercise 6: Loops within
loops: a flower
Try this neat little script to make the
attractive flower shown. Take a good look at the flower and note
that it is in fact made up simply of 10 squares. For this script
the cat should be placed in the centre of the stage before the
script is run. There is no 'green flag cap' on this script so
you must run it by single-clicking it. Notice that the cat is
hidden by this script but even the invisible cat can still trace
lines. Click the 'show' block in the left panel to make the cat
visible again.

It's interesting to realise that we can put a
loop inside a loop like this, but we can also use this flower to
get more practice in broadcasting a message and responding to
it. We can use our 'square' script again as shown below. Notice
how easy it is to understand the green flag script.

Challenge: design your own flower and save it.
You can use either the 'loop within loop' method or the
'broadcast' method.
Exercise 7: Making
a 'squiral'
using variables,
How would you call this shape? it's a 'square
spiral' so let's call it a 'squiral'... Now how would you
make this shape? Notice that each line being drawn
(starting at the centre) is slightly longer than the previous one. We can draw
this by modifying our 'square' script so that the each line
drawn has a length longer than the previous one. This means the
length of the line is variable and we must use a 'variable'
to contain it. Think of a variable as like a named container
- the container can contain numbers, letters or a combination.
Before
you can use a variable you have to create it. Open the
'Variables' group and click 'Make a variable' and give the
variable the name 'length'. You will be asked whether this
variable will be used only by this sprite or by all sprites. If
the variable is used by this sprite only you SHOULD choose that
option because that means that other sprites could use a
different variable with the same name and also because it
reduces the likelihood of mistakes later. So choose 'only this
sprite' in this case.
Now that you have created the variable you can
create a script like the one shown. Inside the repeat loop we
have blocks to move the (invisible) cat forwards and turn it
through 90°, just like for a square, but this time the cat moves
forward a distance equal to the number stored in the variable
'length' and that variable is increased ('incremented') by 3 after
each line is drawn. There are many lines in the above drawing,
50 in fact, so
the loop repeats 50 times. So the first line is drawn with a
length of three pixels, the next is drawn with a length
of 6 pixels, the next with 9 and so on until 50 lines have been
drawn.
Note also that there is a block which ensures
the line drawing always begins in the centre of the stage (the
centre has horizontal position x = 0 and vertical position y =
0).
Challenge: Make a script for drawing a shape
like this and save it with the name 'squiral2'. Hint: I only
changed the number of repeats (from 50 to 80) and one other
number...
Exercise 8
onwards
That's the end of the Logo-like drawing
exercises that we will do in Scratch (though you are welcome to
experiment with drawing until the cows come home in your own
time). Click HERE to jump to
the next set of lessons in Scratch.