求助大佬 我想要把每个球都有他们自己的运动轨迹 碰到边缘
processing吧
全部回复
仅看楼主
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
level 2
👻白首 楼主
dd
2020年10月07日 15点10分 2
level 1
Qiu[] qiu=new Qiu[20];
void setup() {
size(400, 400);
// x=40;
// y=40;
// r=20;
//xspeed=random(-3, 3);
//yspeed=random(-3, 3);
for (int i=0; i<20; i++) {
qiu[i]=new Qiu();
}
}
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;
float xspeed;
float yspeed;
Qiu() {
x= random(r, width-r);
y=random(r, height-r);
r=10;
xspeed=random(-3, 3);
yspeed=random(-3, 3);
}
void display() {
noStroke();
fill(0, 255, 0);
ellipse(x, y, r, r);
}
void move() {
x+=xspeed;
y+=yspeed;
}
void moves() {
if (x-r<0||x+r>width)
{
x-=xspeed;
xspeed*=-1;
}
if (y-r<0||y+r>height)
{
y-=yspeed;
yspeed*=-1;
}
}
}
2020年10月12日 20点10分 4
1