/**
* 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 directionX = 1;
int directionY = 1;
/* built-in functions */
void setup() {
size(300, 300);
noStroke();
smooth();
}
void draw() {
background(0);
moveSquare();
drawSquare();
}
/* custom functions */
// moves the square erratically
void moveSquare() {
if (rectX > (width-rectSize)) {
// the square is too far to the right, change direction to left
directionX = -1;
} else if (rectX < 0) {
// the square is too far to the left, change direction to right
directionX = 1;
}
if (rectY > (height-rectSize)) {
// the square is too far to the bottom, change direction to up
directionY = -1;
} else if (rectY < 0) {
// the square is too far to the top, change direction to down
directionY = 1;
}
int stepX = (int)random(5);
int stepY = (int)random(5);
rectX = rectX + (directionX*stepX);
rectY = rectY + (directionY*stepY);
}
// draws the square
void drawSquare() {
fill(255, 128, 0); // orange
rect(rectX, rectY, rectSize, rectSize);
}