I’m new to Processing and I cannot solve the problem below :
In my game, why some of the bullets can smash the meteorites, but some of the bullets cannot?
The code:
PImage mete;
PImage bg;
PImage spship;
PImage bulletB;
int totalNum = 15;
int PlayerW, PlayerH;
float[] x = new float [totalNum];
float[] y = new float [totalNum];
float[] w = new float [totalNum];
float[] h = new float [totalNum];
boolean[] alive = new boolean [totalNum];
int totalNum2 = 30;
float[] bx = new float[totalNum2];
float[] by = new float[totalNum2];
boolean[] visible = new boolean[totalNum2];
int bW = 10;
int bH = 60;
float BLeft;
float BRight;
float BTop;
float BBottom;
float meteLeft;
float meteRight;
float meteTop;
float meteBottom;
void setup()
{
size(800,1000);
background(255);
mete = loadImage("meteorite.gif");
bg = loadImage("galaxy.jpg");
spship = loadImage("spaceship.png");
bulletB = loadImage("Bullet_blue.png");
PlayerW = 110;
PlayerH = 110;
for(int i = 0; i < x.length; i=i+1)
{
float scale = random(0.2, 0.3);
w[i] = mete.width * scale;
h[i] = mete.height * scale;
x[i] = random(0, width-w[i]);
y[i] = random(-1000, -h[i]);
alive[i] = true;
}
for(int i = 0; i < visible.length; i=i+1)
{
visible[i] = false;
}
}
void draw()
{
image(bg, 0, 0, width, height);
for(int i = 0; i < visible.length; i=i+1)
{
if(visible[i] == true)
{
image(bulletB, bx[i], by[i], bW, bH);
by[i] = by[i] - 5;
}
if(by[i] <= 0)
{
visible[i] = false;
}
}
drawmeteorite(5, 21, 3);
}
void keyPressed()
{
if( key == ' ')
{
boolean found = false;
for(int i = 0; i < visible.length; i=i+1)
{
if(found == false && visible[i] == false)
{
bx[i] = mouseX-5;
by[i] = mouseY-110/2;
visible[i] = true;
found = true;
}
}
}
}
void drawmeteorite(int lives, int startTime, int speed)
{
for(int i = 0; i < visible.length; i=i+1)
{
if(visible[i] == true)
{
BLeft = bx[i];
BRight = bx[i] + bW;
BTop = by[i];
BBottom = by[i] + bH;
}
}
for(int i = 0; i < x.length; i=i+1)
{
if(alive[i])
{
meteLeft = x[i];
meteRight = x[i] + w[i];
meteTop = y[i];
meteBottom = y[i] + h[i];
if(BTop < meteBottom && meteTop < BBottom && BLeft < meteRight && meteLeft < BRight)
{
alive[i] = false;
}
}
}
for(int i = 0; i < x.length; i=i+1)
{
if(alive[i])
{
image(mete, x[i], y[i], w[i], h[i]);
y[i] = y[i] + speed;
if(y[i] >= height)
{
float scale = random(0.2, 0.3);
w[i] = mete.width * scale;
h[i] = mete.height * scale;
x[i] = random(0, width-w[i]);
y[i] = random(-100, -h[i]);
lives = lives - 1;
}
}
}
image(spship, mouseX-PlayerW/2, mouseY-PlayerH/2, PlayerW, PlayerH);
fill(255,34, 0);
textSize(40);
text("Lives: "+ lives, 20, 50);
text("Time: "+ (startTime - millis()/1000) + "s", 20, 100);
}