/**
* Animating a square.
*
*
Elie Zananiri
* CART 253, Winter 2008
*/
/* global variables */
int rectX = 0;
int rectY = 50;
int rectSize = 50;
// we'll use an integer to represent direction:
// 1 -> left to right
// -1 -> right to left
int direction = 1;
/* built-in functions */
void setup() {
size(300, 300);
noStroke();
smooth();
}
void draw() {
background(0);
moveSquare();
drawSquare();
}
/* custom functions */
// moves the square
void moveSquare() {
if (rectX > (width-rectSize)) {
// the square is too far to the right, change direction to left
direction = -1;
} else if (rectX < 0) {
// the square is too far to the left, change direction to right
direction = 1;
}
rectX = rectX + direction;
}
// draws the square
void drawSquare() {
fill(255, 128, 0); // orange
rect(rectX, rectY, rectSize, rectSize);
}