Created
October 10, 2011 17:51
-
-
Save raheelahmad/1275956 to your computer and use it in GitHub Desktop.
Veritcal Boxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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