Hi Everyone,
So my code is progressing well. I am looking for a way to erase the aliens when bullets touch them.
I am a bit lost regarding the boolean variable using the distance between the bullets and the round body of the aliens.
I want to kill the aliens when a bullet hits them, erase them from the screen and from the list.
A solution for it?
Thank you,
Rémy
Alien[]aliens = new Alien[1000];
SpaceShip spaceship;
ArrayList<Bullet> bullets;
Timer timer;
int totalAliens = 0;
void setup(){
size(600,400);
smooth();
frameRate(60);
timer = new Timer(1000);
timer.start();
spaceship = new SpaceShip(10);
bullets = new ArrayList<Bullet>();
bullets.add(new Bullet(mouseX,mouseY));
}
void draw(){
background(255);
if(timer.isFinished()){
aliens[totalAliens] = new Alien();
totalAliens++;
if (totalAliens >= aliens.length){
totalAliens = 0;
}
timer.start();
}
for (int i = bullets.size()-1; i >= 0; i--) {
Bullet b = bullets.get(i);
b.move();
b.display();
if (b.finished()){
bullets.remove(i);
}
}
for (int i = 0; i < totalAliens ; i++){
aliens[i].move();
aliens[i].display();
}
spaceship.move(mouseX, mouseY);
spaceship.display();
}
void mousePressed(){
bullets.add(new Bullet(mouseX,mouseY));
}
class Timer{
int savedTime;
int totalTime;
Timer(int _tempTotalTime){
totalTime = _tempTotalTime;
}
void start(){
savedTime = millis();
}
boolean isFinished(){
int passedTime = millis() - savedTime;
if(passedTime > totalTime){
return true;
} else {
return false;
}
}
}
class Alien {
float x;
float y;
float diam;
float speed;
Alien(){
x = random(width);
y = random(-100,-50);
diam = random(20,40);
speed = diam*0.1;
}
void move(){
y += speed;
}
void display(){
stroke(0);
strokeWeight(diam*0.1);
ellipseMode(CENTER);
noFill();
arc(x, y + diam*0.5, 2*diam, diam, -PI, 0);
arc (x-diam,y+diam*0.75,diam*0.5,diam*0.5,-PI,0);
arc (x+diam,y+diam*0.75,diam*0.5,diam*0.5,-PI,0);
fill(200);
ellipse(x,y,diam,diam);
}
boolean reachedBottom(){
if (y > height + diam*4){
return true;
} else {
return false;
}
}
}
class Bullet{
float bulletX1, bulletY1;
float bulletX2, bulletY2;
int r;
int bulletSpeed;
Bullet(float _x, float _y){
bulletX1 = _x - 20;
bulletY1 = _y - 15;
bulletX2 = _x + 20;
bulletY2 = _y - 15;
r = 6;
bulletSpeed = 2;
}
void move(){
bulletY1 -= bulletSpeed;
bulletY2 -= bulletSpeed;
}
boolean finished() {
if ((bulletY1<0) || (bulletY2<0)) {
return true;
} else {
return false;
}
}
void display(){
fill(255,0,0);
noStroke();
ellipse (bulletX1,bulletY1,r,r);
ellipse (bulletX2,bulletY2,r,r);
}
boolean intersect(Alien d){
float distance = dist(bulletX1,bulletY1,d.x,d.y);
if (distance < r + d.diam){
return true;
} else {
return false;
}
}
}
class SpaceShip{
float x;
float y;
int shipRatio;
SpaceShip(int tempShipRatio){
x = width/2;
y = height/2;
shipRatio = tempShipRatio;
}
void move (float tempX, float tempY){
x = tempX;
y = tempY + shipRatio*2;;
}
void display(){
stroke(0);
strokeWeight (3);
fill(210);
line (x - shipRatio*2, y - shipRatio, x - shipRatio*2, y - shipRatio*4);
line (x + shipRatio*2, y - shipRatio, x + shipRatio*2, y - shipRatio*4);
triangle (x - shipRatio*3, y - shipRatio, x, y - shipRatio*4, x + shipRatio*3, y-shipRatio);
}
}