1

This is the code I have. And in next step how can I make effect on the bouncing ball from keyboard keys.

float circleX = 0;
float circleY = 0;

float xSpeed = 2.5;
float ySpeed = 2;

void setup() {
 size(500, 500); 
}

void draw() {
  background(0,200,0);

  circleX += xSpeed;
  if (circleX < 0 || circleX > width) {
    xSpeed *= -1;
  }

  circleY += ySpeed;
  if (circleY < 0 || circleY > height) {
    ySpeed *= -1;
  }

  ellipse(circleX, circleY, 100, 100);
}
Spektre
  • 49,595
  • 11
  • 110
  • 380
oden
  • 37
  • 4
  • what is the question? title suggest color change on collision and the text implies keyboard handling so what is it? – Spektre Apr 30 '22 at 07:12
  • oh actually it has two different questions. The first part(color change) is solved. Can you help with the keyboard handling part. Please. – oden Apr 30 '22 at 08:51
  • not coding in processing ... so no as keyboard is environment dependent and color is already handled ... – Spektre Apr 30 '22 at 09:49

1 Answers1

1

Change the color fill() each time the ball hits a wall:

float circleX = 0;
float circleY = 0;
float xSpeed = 2.5;
float ySpeed = 2;

void setup() {
 size(500, 500); 
}

void changeColor() {
  fill(random(255), random(255), random(255));
}

void draw() {
  background(0,200,0);

  circleX += xSpeed;
  if (circleX < 0 || circleX > width) {
    xSpeed *= -1;
    changeColor();
  }

  circleY += ySpeed;
  if (circleY < 0 || circleY > height) {
    ySpeed *= -1;
    changeColor();
  }

  ellipse(circleX, circleY, 100, 100);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174