Restart speed when mouse clicked
in
Programming Questions
•
8 months ago
I am super new to programming. This is what my professor want me to do: Create an animation where a ball is moving inside the display window whose speed gradually slows down. When somebody click the mouse, this ball will be set with a new random speed and move again (will gradually stop again). The ball will bounce if it hits the boundary of the window.
I can't figure out how to get the ball to reset with a new speed.
This is what I have so far:
float x = 50;
float y = 50;
float xspeed = 1.5;
float yspeed = 1;
void setup () {
size (400, 400);
smooth();
noStroke ();
}
void draw () {
x = x + xspeed;
y = y + yspeed;
if ( (x > width-25) || (x < 25) ) {
xspeed = xspeed*(-0.9);
}
if ( (y > height-25) || (y < 25) ) {
yspeed = yspeed*(-0.9);
}
background (255);
ellipseMode (CENTER);
fill (150);
ellipse (x, y, 50, 50);
}
void mouseClicked () {
x = mouseX;
y = mouseY;
xspeed = -xspeed;
yspeed = -yspeed;
}
Any suggestions would be wonderful
1