-
-
Save jkwok91/5e810f12498a7a7e3cfd to your computer and use it in GitHub Desktop.
bouncing
This file contains 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
/* | |
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