加工过程中如何改变球每次撞击墙壁的颜色?我的代码如下

问题描述 投票:0回答:2

这是我的代码。下一步如何对键盘按键的弹跳球产生影响。

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);
}
processing
2个回答
1
投票

每次球撞到墙壁时改变颜色

fill()

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);
}

0
投票

按照您对 void changeColor 所做的操作,但它仍然不起作用,我的球只是显示为白色。

© www.soinside.com 2019 - 2024. All rights reserved.