go to Part:   previous   next   1   2   3   4   5   6   7   8   9   10   11 

Patterns Part 9

Making the pattern

Change the code of the DrawPattern handler to this: (add three lines after the switch statement)

on DrawPattern

repeat with i=1 to 10

put "C"&i into lName

create graphic lName

put random(4) into lShapeChoice

switch lShapeChoice

case 1

set the style of graphic lName to "rectangle"

break

case 2

set the style of graphic lName to "roundrect"

break

case 3

set the style of graphic lName to "oval"

break

case 4

set the style of graphic lName to "line"

break

end switch

set the width of graphic lName to 40

set the height of graphic lName to 40

set the loc of graphic lName to i*40,40

end repeat

end DrawPattern

 

 

The line set the width of graphic lName to 40 is easy to understand, so is the second line set the height of graphic lName to 40 .

The next line is not so easy: set the loc of graphic lName to i*40,40 .

Let's see what it does:  go to run mode and press the Go! button just once.  You will get something like:

Since it is random, your screen will look different from mine, but that's not important.
Let's see if we can explain what we see:

First, we have set the width and height of each graphic object to 40 pixels.  This you can see at the blue double arrows in the picture.  We do the repeat 10 times, and the first time the variable i is equal to 1.

Therefore, the line set the loc of graphic lName to i*40,40 sets the loc to 40,40 because * means "multiply" and i is 1 and 1 times 40 is 40.

So we effectively do: set the loc of graphic lName to 40,40 .  The loc is the centre of an object, the green dot I put in the picture.  From the red double arrows you can see it is at 40 pixels from the left of the window and at 40 pixels down from the top.

The next time round i will be 2, and 2 times 40 is 80.
That means we do: set the loc of graphic lName to 80,40 for the second graphic. After that i will be 3 and we set it at 3*40=120, and so on.  Each time round the repeat loop we will put the graphic 40 pixels more to the right.  There are 10 in total so we end at 400.

So far so good.  If you want to try again, go to program mode, select all the graphics and delete them, then go back to run mode and press Go! once.

Error

Why are some positions empty?  There seem to be no lines!  We asked for rectangles, ovals, rounded rectangles and lines, but we see no lines.  I'll solve that mystery later.

Rows

Can we also make rows?  Sure:  we have the code for one row, so we repeat it:

on DrawPattern

repeat with y=1 to 10

repeat with i=1 to 10

put "R"&y&"C"&i into lName

create graphic lName

put random(4) into lShapeChoice

switch lShapeChoice

case 1

set the style of graphic lName to "rectangle"

break

case 2

set the style of graphic lName to "roundrect"

break

case 3

set the style of graphic lName to "oval"

break

case 4

set the style of graphic lName to "line"

break

end switch

set the width of graphic lName to 40

set the height of graphic lName to 40

set the loc of graphic lName to i*40,y*40

end repeat

end repeat

end DrawPattern

We have added a repeat loop that is wrapped around the first one:  repeat with y=1 to 10 and we also changed the loc value of the graphic to i*40,y*40.  So now we do the ten graphics ten times, and each time we change rows the variable y will be one higher so the loc will be 40 pixels more down from the top. 

BUT:  we should not use the same names for the objects in each row, we have to make names that are also different by row.  Thus we change the name to "R"&y&"C"&i which means:  take the letter R (for row), write the value of y after it, then write the letter "C" after that, finally write the value of i after that, and take the whole thing as the name.  When y is 5 and i is 8 the object will get the name R5C8 and that will be the object of the fifth row and the eight position in that row.  I chose R for row and C for column, but I could have chosen anything, as long as all names of all objects are different.

We get:

This is more interesting, but I don't like it for many reasons.  I'm sure you don't like it either.

A lot of work to be done!

Nesting

Before we make it much better again, let's look quickly at the structure of our DrawPattern handler:

After adding the new repeat, there are three levels of indentation (place the insertion point in the handler and press tab if things have not lined up properly).

Indentation is the amount by which some parts of the code are shifted to the right.  Revolution does it for you so that you can easily see which things "sit inside" which other things.  We already had the switch statement sit inside the repeat, and now we have that repeat sit inside the new repeat.  I put red boxes in the picture so that you can see better the blocks of code.  These blocks are inside each other, like boxes within boxes.  This is called nesting of code.  It is very important that you can see the nesting.  When things do not line up properly, just place the insertion point in the code and press the tab key, Revolution will align the code properly and it will be easier to read.

The Documentation

How do I know so well what I can write as code and what not?

Select Help—>Documentation:

You will get a large panel:

In the top row you see Getting Started, User Guide, Dictionary, Glossary, Search.

Don't bother with User Guide, but click Dictionary:

Then type random in the search box:  two lines show up, click the first one.

The bottom pane of the window then shows you all about the random function and gives some examples.

You can browse in the glossay and elsewhere, but the dictionary is the most helpful part of the Revolution documentation.  It gives you examples and related stuff too.

Many words are synonyms.  A synonym is another word for the same thing, for example "car" is a synonym for "automobile", "light" is for "lamp", "telly" for "television", "spectacles" for "glasses" and so on.  If you look in the dictionary for loc you will not find it in the list, but you will find location because loc is a short synonym for location.

In the next part we're going to solve all our problems :-)