level 2
👻白首
楼主
求助大佬
我想要把每个球都有他们自己的运动轨迹 碰到边缘会反弹的那种 现在的情况
Qiu[] qiu=new Qiu[20];
float x, y, r;
float xspeed, yspeed;
void setup() {
size(400, 400);
x=40;
y=40;
r=20;
xspeed=5;
yspeed=4;
for (int i=0; i<20; i++) {
qiu[i]=new Qiu(random(width), random(height), r);
}
}
void draw() {
background(0, 0, 255);
for (int i=0; i<20; i++) {
qiu[i].display();
qiu[i].move();
qiu[i].moves();
}
}
class Qiu {
float x, y, r;
Qiu(float x, float y, float r) {
this.x=x;
this.y=y;
this.r=r;
}
void display() {
noStroke();
fill(0, 255, 0);
ellipse(x, y, r, r);
}
void move() {
x+=xspeed;
y+=yspeed;
}
void moves() {
if (x<=r/2||x>=width-r/2)
{
xspeed*=-1;
}
if (y<=r/2||y>=height-r/2)
{
yspeed*=-1;
}
}
}
2020年10月07日 15点10分
1
我想要把每个球都有他们自己的运动轨迹 碰到边缘会反弹的那种 现在的情况
Qiu[] qiu=new Qiu[20];
float x, y, r;
float xspeed, yspeed;
void setup() {
size(400, 400);
x=40;
y=40;
r=20;
xspeed=5;
yspeed=4;
for (int i=0; i<20; i++) {
qiu[i]=new Qiu(random(width), random(height), r);
}
}
void draw() {
background(0, 0, 255);
for (int i=0; i<20; i++) {
qiu[i].display();
qiu[i].move();
qiu[i].moves();
}
}
class Qiu {
float x, y, r;
Qiu(float x, float y, float r) {
this.x=x;
this.y=y;
this.r=r;
}
void display() {
noStroke();
fill(0, 255, 0);
ellipse(x, y, r, r);
}
void move() {
x+=xspeed;
y+=yspeed;
}
void moves() {
if (x<=r/2||x>=width-r/2)
{
xspeed*=-1;
}
if (y<=r/2||y>=height-r/2)
{
yspeed*=-1;
}
}
}