Threads
in
Share your Work
•
2 years ago
ArrayList luciernagas;
Creador crear;
void setup () {
size (400, 400);
luciernagas = new ArrayList();
crear = new Creador ();
crear.start();
smooth ();
}
void draw () {
background (0);
for (int i=0 ; i<luciernagas.size(); i++) {
Luciernaga temp = (Luciernaga) luciernagas.get (i);
temp.pintar ();
}
}
////////////////////////////////////////////////////////////////////// Luciernagas
public class Creador extends Thread {
int tiempo;
Luciernaga l;
public Creador () {
tiempo = 1000 ;
}
public void run () {
while (true) {
l = new Luciernaga ();
l.start();
luciernagas.add(l);
try {
sleep (tiempo);
}
catch (InterruptedException ie){
}
}
}
}
////////////////////////////////////////////////////////////// Creador
public class Luciernaga extends Thread {
int vida;
int posX;
int posY;
int tam;
int tono;
public Luciernaga () {
vida = 200;
posX = (int) random (10, width);
posY = (int) random (10, height);
tam = (int) random (10, 20);
tono= (int) random (100, 255);
}
public void run () {
while (vida>0) {
vida--;
mover ();
try {
sleep (30);
}
catch (InterruptedException ie) {
println("interrumpido!");
}
}
luciernagas.remove (this);
}
public void pintar () {
fill (255, tono, 8);
ellipse (posX, posY, tam, tam);
}
public void mover () {
tono+= (int) random (-20, 20);
posX+= (int) random (-5, 5);
posY+= (int) random (-5, 5);
}
}