Interactive Screen Saver… what?

So I wanted to make something that would allow the bouncing ball to vary speed, direction, and size. However, once I started I tried messing with speed when I would click, but I could not get the effect I wanted and though I knew why I didn’t know how to fix it. But I was able to change direction and location of the ball. So I made a screensaver that is the stereo typical bouncing ball, but if you were to click on the screen the ball would move to where you clicked and would move in the opposite direction of that which it was bouncing in before. I also wanted it to erase after a while after you let it sit for a while or if you click so that it doesn’t get too crazy.

Here is the code:

float circlex = 0;
float xSpeed = 2;
float circley =0;
float ySpeed = 2;
float i = 0;
void setup() {
  size(600, 370);
  background (75, 100, 200);
}
void mouseClicked() {
  background (75, 100, 200);
  circley = mouseY;
  circlex = mouseX;
  xSpeed = xSpeed *-1;
  ySpeed = ySpeed *-1;

}

void draw() {

  stroke(0);
  fill(200, 40, 10);
  ellipse(circlex, circley, 20, 20);
  circlex = circlex + xSpeed;
  circley = circley + ySpeed;
  if (circlex > width || circlex <= 0) {
    xSpeed = xSpeed * -1;
  }
  if (circley > height || circley <= 0) {
    ySpeed = ySpeed * -1;
  }
  i = i+1;
  if (i > 2500){  
    background (75, 100, 200);
    i = i - 2500;
   }

}

And I made it color-changing too!

float circlex = 0;
float xSpeed = 2;
float circley =0;
float ySpeed = 2;
float i = 0;
float red = random(255);
float green = random(255);
float blue = random(255);
void setup() {
  size(600, 370);
  background (75, 100, 200);
}
void mouseClicked() {

  red = random(255);
  green = random(255);
  blue = random(255);
  background (75, 100, 200);
  circley = mouseY;
  circlex = mouseX;
  xSpeed = xSpeed *-1;
  ySpeed = ySpeed *-1;
}

void draw() {

  stroke(0);
  fill(red, green, blue);
  ellipse(circlex, circley, 20, 20);
  circlex = circlex + xSpeed;
  circley = circley + ySpeed;
  if (circlex > width || circlex <= 0) {
    xSpeed = xSpeed * -1;
  }
  if (circley > height || circley <= 0) {
    ySpeed = ySpeed * -1;
  }
  i = i+1;
  if (i > 2500) {  
    background (75, 100, 200);
    i = i - 2500;
  }
  if (circlex <= 0 || circlex >= width || circley <= 0 || circley >= height) {
    red = random(255);
    green = random(255);
    blue = random(255);
  }
}

 

Leave a Reply