Skip to content

Instantly share code, notes, and snippets.

@raheelahmad
Created October 10, 2011 17:51
Show Gist options
  • Save raheelahmad/1275956 to your computer and use it in GitHub Desktop.
Save raheelahmad/1275956 to your computer and use it in GitHub Desktop.
Veritcal Boxes
class Box {
float x, y, w, h, speed;
boolean movingForward, movingUp;
void drawItself() {
rect(x, y, w, h);
}
void update() {
if (movingForward)
x += speed;
else
x -= speed;
if (movingUp)
y -= speed;
else
y += speed;
}
void check() {
if (x + w >= width)
movingForward = false;
if (x <= 0)
movingForward = true;
if (y + h >= height)
movingUp = true;
if (y <= 0)
movingUp = false;
}
}
Box[] myBoxes;
void setup() {
size (500, 500);
int m = 10;
for () {
myBoxes[i] = new Box();
myBoxes[i].x = 10;
myBoxes[i].y = 50;
myBoxes[i].w = m;
myBoxes[i].h = 50;
myBoxes[i].speed = 2;
myBoxes[i].movingForward = true;
myBoxes[i].movingUp = false;
}
}
void draw() {
background(150);
myBox.drawItself();
myBox.update();
myBox.check();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment