// The basic ball class...
class ball {
// Attributes
float radius; // size
float x, y; // location
color ballColor;
float dx = 1;
float dy;
float speed = 5.0;
float gravity = 0.1;
float dampen = -0.9;
// Constructor(s)
ball() {
radius = 25;
x = random(width);
y = random(height);
ballColor = color(59, 90, 255);
//dx = random(-3, 3);
dy = random(-3, 3);
} // ball()
ball(float r, color c) {
radius = r;
x = random(width);
y = random(height);
ballColor = c;
//dx = random(-3, 3);
dy = random(-3, 3);
} // ball()
ball(float _x, float _y, float _r, color _c) {
x = _x;
y = _y;
radius = _r;
ballColor = _c;
dy = random(-3, 3);
} // ball()
// Behaviors
void display() {
// display the ball object
noStroke();
fill(ballColor);
ellipse(x, y, 2*radius, 2*radius);
} // display()
// move the ball
void move() {
x += dx;
y += speed;
speed = speed + gravity;
bounce(); // check bounces
} // move()
void bounce() {
if (x < radius) {
x = radius;
dx = -dx;
}
if (x > width-radius) {
x = width-radius;
dx = -dx;
}
if (y < radius) {
y = radius;
dy = -dy;
}
if (y > height - radius) {
y = height-radius;
dy = -dy;
speed = speed*dampen;
}
} // bounce()
} // class ball