Drawing Many Things

A single object can rarely outshine the masses

Real programs rarely draw only one shape. A game might have dozens of platforms, coins, and enemies. Writing a DRAW_RECT line for each would be slow and hard to change. Lists and loops let us describe many shapes as data, and draw them all with a few lines of code.

Drawing with a Loop

Suppose we want a row of ten squares. Using REPEAT TIMES and a counter, we only need to write the drawing code once.

csp
You may also type <- !=>= <= for auto complete.

On each pass i goes up by one, so each square lands 38 pixels to the right of the last. Changing the 10 to a 30 gives a row of thirty squares, with no extra code.

Storing Shapes in a List

When shapes are not evenly spaced, we describe each one in a list. A good way to do this is to make a list of lists, where each inner list holds the numbers for one shape.

*csp
You may also type <- !=>= <= for auto complete.

Each inner list is [x, y, width, height]. To draw them all, we use a FOR EACH loop. Each time through the loop, r is one of the inner lists, and we read its parts by index. Remember that indices start at 1.

csp
You may also type <- !=>= <= for auto complete.

To add another shape, we only add another inner list. The drawing code does not change.

It is easy to forget what each number in the inner lists means. A comment above the list, such as // [x, y, width, height], saves a lot of confusion later.

Parallel Lists

Another approach is to use one list for each property, and keep them lined up so that index 1 of every list describes the first shape.

csp
You may also type <- !=>= <= for auto complete.

Here we need the index i, because we have to look in two lists at once. LENGTH tells the loop how many times to run, so adding a star to both lists is all it takes.

Either style works. A list of lists keeps everything about one shape together. Parallel lists are simpler when each property is a single number.

Random Shapes

The RANDOM function picks a random whole number. RANDOM(1, 400) gives a number between 1 and 400, and both ends are allowed. Both arguments must be whole numbers, with the first no bigger than the second.

We can use it to fill a list with a random star field.

csp
You may also type <- !=>= <= for auto complete.

Notice that the random positions are made once, before the animation loop. If we called RANDOM inside the loop, the stars would jump to new places every frame and flicker. The positions are state, which means data that must be remembered from frame to frame.

Moving Many Things

Since the positions are in a list, we can move every shape by changing the list. We assign to an index, as we learned in the lists chapter.

csp
You may also type <- !=>= <= for auto complete.

Each frame has two parts. First we update the data by moving the stars, and when one falls off the bottom, we wrap it back to the top. Then we draw the data. Keeping those two parts separate keeps programs easy to read, and every game in the next lessons is built this way.

Practice


Next: Keyboard Input