//exercise 2b
//move mouse horizontally to change the color of the background
float angle = 0.0;
float offset = 0.0;
int num = 20;
Ball[] balls = new Ball[num];
void setup()
{
size(300, 300);
colorMode(RGB, 255, 255, 255, 100);
noStroke();
angleMode(DEGREES);
ellipseMode(CENTER);
smooth();
framerate(60);
for(int i=0; i<num; i++)
{
color tempcolor = color(random(255), random(255), random(255), random(50, 100));
balls[i] = new Ball(tempcolor, random(-300, 0), random(275, 300), random(15, 50), random(7.5, 10));
}
}
void draw()
{
background(mouseX*0.95);
for(int i=0; i<num; i++)
{
balls[i].update();
balls[i].display();
}
}
class Ball
{
color c;
float xpos, ybounce, bsize, speed;
Ball(color c_, float x, float y, float ballsize, float speedi)
{
c=c_;
xpos = x;
ybounce = y;
bsize = ballsize;
speed = speedi;
}
void display()
{
float sinval =abs(sin(angle));
fill(c);
ellipse(xpos, 300 + (sinval * -ybounce-(bsize/2)), bsize, bsize);
angle=angle+speed;
xpos++;
ybounce--;
}
void update()
{
if (xpos>300)
{
xpos=0;
ybounce=random(0, 300);
}
if(ybounce<0)
{
ybounce=0;
}
}
}