CIT020 Index > Loops

Loops

Part 1: String of Squares

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

Make a window that is 200 by 200 with a white background. Set the frame rate to 15 frames per second. Starting at the mouseX and mouseY coordinates, draw a horizontal row of squares. Each square is 10 pixels by 10 pixels, and there are 10 pixels of space between the squares. Draw as many squares as will fit in the sketch window (i.e., don’t just draw 15 squares and let the edge of the window cut off the drawing). If your program works correctly, there will never be a partial square at the right side of the screen. You may make the squares any color you like. You must use a while loop for this program. Save this sketch with the name squares.

Extra credit: Use constrain() or an if statement to make sure that you never see a partial square at the bottom of the screen.

Part 2: X of Circles

Make a window that is 200 by 200 with a white background. Set the frame rate to 15 frames per second. Your program will draw an “X” made of circles, centered at (mouseX, mouseY), as shown in the diagram below at the left. Each square in the diagram represents ten pixels. The gray circle represents the mouse position. In your program, make all the circles the same color (white is a reasonable choice). You must may use either a while loop or a for loop for this program. Save this sketch with the name circles.

diagram showing how circles are positioned.

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

Extra challenge: change the color of each the circles to a random color when the mouse moves. If the mouse doesn’t move, the circles all stay the same color. Use an if statement to make this happen.

Part 3: Creative Exercise—The Alańeðian Flag

The government of the fictional country of Alańeðia has hired you to design its new flag. Here are their requirements.

Save your sketch in a file named flag.

When You Finish

Zip up all the sketches into a zip file named loops.zip and upload it.