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: Stretchy Lines

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

This sketch is in a window that is 150 by 150 with a white background. Set the frame rate to 15 frames per second. Your program will draw ten lines starting at the current mouse position. The first line will be from (mouseX, 0) to (mouseX, mouseY). The second line will be five pixels to the right and 0.9 times as long as the first line; in other words, (mouseX + 5, 0, mouseX + 5, 0.9 * mouseY). The third line will be five pixels more to the right and 0.8 times as long as the first line: (mouseX + 10, 0, mouseX + 10, 0.8 * mouseY), and so forth. You must use a for loop for this program. Save this program with the name lines.

symmetric triangle of lines Extra credit: Have lines that go to the left and right of the mouse x position to make a symmetric triangle, as shown in the screenshot at the left. You must use only one for loop.

Part 3: 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 use 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 credit: change the color of all 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.

When You Finish

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