/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.-MgvrqLvlp$/rev.960
*
* authors:
*
* Arki
* ij
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
final float tScale = 0.1;
final int ballSize = 20;
final int ballNum = 10;
int ballCaught = 0;
int points = 0;
float t = 1;
int lastTime = 0;
int currentTime = 0;
ArrayList<BounceBall> balls = new ArrayList<BounceBall>();
class BounceBall {
float x;
float y;
float xSpeed;
float ySpeed;
float tOff;
BounceBall() {
x = random (25, width-25);
y = random (25, height-25);
xSpeed = random(2, 5) * (random(1) < 0.5 ? -1 : 1);
ySpeed = random(2, 5) * (random(1) < 0.5 ? -1 : 1);
tOff = random(100);
}
void display() {
float c = (100*noise(t))+150;
fill(c, 0, c);
ellipse(x, y, ballSize, ballSize);
}
void step() {
float xDelta = (noise(t + tOff + 50)*xSpeed) * 3;
float yDelta = (noise(t + tOff + 25)*ySpeed) * 3;
if ((x > width - 10) || (x < 10)) {
xDelta *= -1;
xSpeed *= -1;
}
if ((y > height - 10) || (y < 10)) {
yDelta *= -1;
ySpeed *= -1;
}
x += xDelta;
y += yDelta;
}
}
void setup() {
size(800, 600);
background(0);
for (int i = 0; i < ballNum; i++) {
balls.add(new BounceBall());
}
}
void draw() {
t += tScale;
fill(0,25);
noStroke();
rectMode(CENTER);
rect(width/2,height/2,width,height);
translate((noise(t+10)-0.5)*points/20, (noise(t+53)-0.5)*points/20);
fill(255,25);
rect(width/4-(noise(t)*50),height/2,50+(noise(t)*50),10);
rect(width/2,height/2,50+(noise(t)*25),10+(noise(t)*25));
rect(width/4*3+(noise(t)*50),height/2,50+(noise(t)*50),10);
rect(width/2,height/4-(noise(t)*25),10,50+(noise(t)*25));
rect(width/2,height/4*3+(noise(t)*25),10,50+(noise(t)*25));
for (BounceBall ball : balls) {
ball.step();
ball.display();
}
fill(255);
textSize(32);
textAlign(LEFT, BASELINE);
text("Score: ", 10+((noise(t)-.5)*(points/20)), 35+((noise(t+50)-.5)*(points/20)));
fill((100*noise(t))+150, 0, (100*noise(t))+150);
text((int)points, 115+((noise(t)-.5)*(points/10)), 35+((noise(t+50)-.5)*(points/10)));
//text("Score: " + (int)points, 10, 50);
if (ballCaught >= ballNum) {
textAlign(CENTER, CENTER);
text("YOU WIN!", (width/2)+(((noise(t+27)-.5)*(points/20))*-1), (height/2)+(((noise(t+85)-.5)*(points/30)))*-1);
}
}
void mousePressed() {
ArrayList<BounceBall> dead = new ArrayList<BounceBall>();
for (BounceBall ball : balls){
if (dist(ball.x, ball.y, mouseX, mouseY) <= 16) {
fill(255);
float d = 100- dist(ball.x, ball.y, mouseX, mouseY);
ellipse(ball.x, ball.y, d, d);
dead.add(ball);
ballCaught++;
//points++;
int speedBonus = (int)dist(0, 0, ball.xSpeed, ball.ySpeed) * 3;
lastTime = currentTime;
currentTime = millis();
int timeBonus = max(((((currentTime-lastTime)/10)*-1)+250), 0);
points += timeBonus + speedBonus;
break;
}
}
balls.removeAll(dead);
println(dead.size());
}