Strange comportment of Threads
in
Programming Questions
•
2 years ago
Hello,
I try to use Threads in order to firtst move several balls.
You will find here my code and the result.
I don't understand why i obtain those ghost !
Maybe someone can help me...
I try to use Threads in order to firtst move several balls.
You will find here my code and the result.
I don't understand why i obtain those ghost !
Maybe someone can help me...
- Balle b,b1,b2;
int fond=155;
color rouge=color(255,0,0);
color vert=color(0,255,0);
void setup() {
size(200,300);
background(fond);
ellipseMode(RADIUS);
b=new Balle(0,0,10,10,0);
b.start();
b1=new Balle(0,0,5,2,rouge);
b1.start();
b2=new Balle(0,0,8,5,vert);
b2.start();
}
void draw(){
}
class Balle extends Thread{
int X;
int dx;
int Y;
int dy;
int r;
int v;
color couleur;
Balle (int x,int y,int ra,int vi,color c){
X=x;
Y=y;
dx=1;dy=1;
r=ra;
v=vi;
couleur=c;
}
void start(){
super.start();
}
void run(){
while (X<2000){
stroke(fond);fill(fond);
ellipse(X,Y,r,r);
Deplace(X,Y);
stroke(couleur);fill(couleur);
ellipse(X,Y,r,r);
stroke(fond);fill(fond);
try {
sleep((long)(100/v));
} catch (Exception e) {
}
}
}
void Deplace(int x,int y){
X=x+dx;
Y=y+dy;
if (X+r>width)dx=-1;
if (X<r)dx=1;
if (Y+r>height)dy=-1;
if (Y<r)dy=1;
}
}
1