CIT020 Index > Variables

Variables

Part 1

Screenshot of part 1 answer Using variables, draw the diagram you see here. The sketch window is 150 by 150 with a white background. Save the sketch with the name variable1.

Part 2

moving square In a sketch window that is 100 by 100 pixels with a white background, draw a square 20 pixels on a side whose initial location is (0,0). Use variables to store the location of the upper left corner of the square. In your setup() function, use frameRate(5); to set the frame rate to 5 frames per second so you can see the result more clearly.

In your draw() function, add 3 to the square’s x coordinate and 2 to its y coordinate. Do not fill the square. You may use any color you like for its stroke color.

Save the sketch with the name variable2.

Part 3

randomly moving square The “staggering square.” This is the same as part 2, except that you add a random number between 2 and 8 to each of the x and y coordinates of the square so that it staggers around instead of walking normally.

Save the sketch with the name variable3.

Part 4

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

Set up your sketch window to be 200 by 200, with a white background, and a frameRate(3); yes, I really want only three frames per second on this one. If the frame rate is too fast, the result is very annoying. Set noStroke() in your setup function.

In the draw() function, randomly assign a number between 10 and 50 to a variable named radius. Draw a circle with that radius, centered at the current mouse position. The circle’s color should be randomly assigned; you do not need to use variables for that, but you may if you wish.

Save the sketch with the name variable4.

When You Finish

Zip up all four sketches into a zip file named variable.zip and upload it.