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.

The circle and line must each use their own set of variables. Thus, the variables for the starting point of the line must be separate from the variables used for the center of the circle. You must use the width and height variables only when drawing the square.

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(15); 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 diameter. Draw a circle with that diameter, centered at the current mouse position. The circle’s red, green, and blue values must be randomly assigned and must have a transparency value of 192. You do not need to use variables for the random color, but you may do so if you wish.

Erase the sketch in the setup() function. Do not erase the sketch unless the user clicks the mouse button. Use the mousePressed() function to do this.

Save the sketch with the name variable4.

When You Finish

Create a new folder called variable and put all four sketch folders into that new folder. Zip up the “big folder” into a zip file named variable.zip and upload that to Moodle.