Coches que se chocan con una bola
boolean r, l, d, u;
float x = 50, y = 50;
int numcoches = 30;
int xbola=30;
int ybola=30;
Car[] cars = new Car[numcoches]; // Declaramos el arreglo de coches
void setup() {
size(800, 800);
frameRate(80);
for (int i = 0; i < numcoches; i++) {
cars[i] = new Car(color(random(255), random(255), random(255)), random(10, width - 10), random(10, height - 10), random(-5, 5));
}
}
void draw() {
background(0);
ellipse(x, y, xbola, ybola);
if (r) x += 1;
if (l) x -= 1;
if (u) y -= 1;
if (d) y += 1;
for (int i = 0; i < numcoches; i++) {
cars[i].drive();
if(dist(x,y,cars[i].xpos,cars[i].ypos )<27){
noLoop();
}
cars[i].display();
}
}
void keyPressed() {
if (keyCode == RIGHT) r = true;
if (keyCode == LEFT) l = true;
if (keyCode == UP) u = true;
if (keyCode == DOWN) d = true;
}
void keyReleased() {
if (keyCode == RIGHT) r = false;
if (keyCode == LEFT) l = false;
if (keyCode == UP) u = false;
if (keyCode == DOWN) d = false;
}
class Car {
color c;
float xpos;
float ypos;
float xspeed;
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos, ypos, 20, 10);
}
void drive() {
xpos += xspeed;
if (xpos > width) {
xpos = 0;
}
if (xpos < 0) {
xpos = width;
}
}
}
Comentarios
Publicar un comentario