/**
* Animating a square.
*
*
Elie Zananiri
* CART 253, Winter 2008
*/
/* global variables */
int rectX;
int rectY;
int rectSize = 50;
/* built-in functions */
void setup() {
size(300, 300);
noStroke();
smooth();
resetSquare();
}
void draw() {
background(0);
moveSquare();
drawSquare();
}
/* custom functions */
// resets the square's position
void resetSquare() {
rectX = 0;
rectY = 50;
}
// moves the square erratically
void moveSquare() {
if (rectX < width && rectY < height) {
// random() returns a float, so we have to transform it into an integer;
// this is done with casting, using the (int) operator.
rectX = rectX+(int)random(0, 5);
rectY = rectY+(int)random(0, 5);
} else {
resetSquare();
}
}
// draws the square
void drawSquare() {
fill(255, 128, 0); // orange
rect(rectX, rectY, rectSize, rectSize);
}