Skip to content

Instantly share code, notes, and snippets.

@jkwok91
Created March 31, 2014 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/5e810f12498a7a7e3cfd to your computer and use it in GitHub Desktop.
Save jkwok91/5e810f12498a7a7e3cfd to your computer and use it in GitHub Desktop.
bouncing
/*
a thing bouncing in a box
*/
int radius = 5;
PVector ballPos;
PVector ballVel;
float boxW, boxH;
float boxX, boxY;
float r, g, b;
color ballCol;
void setup() {
size(300, 200);
background(0);
makeBox();
}
void makeBox() {
r = random(255);
g = random(255);
b = random(255);
boxW = random(40, 150);
boxH = random(40, 150);
boxX = random(width-boxW);
boxY = random(height-boxH);
ballCol = color(r, g, b);
ballPos = new PVector(random(boxX+radius, (boxX+boxW)-radius), random(boxY+radius, (boxY+boxH)-radius));
ballVel = new PVector(random(1, 3), random(1, 3));
}
void draw() {
background(0);
stroke(255);
fill(0);
rectMode(CORNER);
rect(boxX, boxY, boxW, boxH);
ballPos.add(ballVel);
if (ballPos.x-radius <= boxX || ballPos.x+radius >= boxX+boxW) {
ballVel.x *= -1;
}
if (ballPos.y-radius <= boxY || ballPos.y+radius >= boxY+boxH) {
ballVel.y *= -1;
}
stroke(ballCol);
fill(ballCol);
ellipse(ballPos.x, ballPos.y, 2*radius, 2*radius);
}
void mousePressed() {
makeBox();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment