We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I still need one more help. I made a code of bullets that are fired from the red circle. I want to make it so that if the bullet collides with a grey circle, both the grey circle and the bullet are removed from the screen
float radius=10;
int n=100;
float velocityY=3;
int xship=250; //SHIP X POSITION
int yship=500; //SHIP Y POSITION
float bulletX[]; //BULLETS X POSITION
float bulletY[]; //BULLETS Y POSITION
float x[]; // OBSTACLES X POSITIONS
float y[]; //OBSTACLES Y POSITIONS
boolean space; //SPACESHIP BULLET FIRE
int ammo=1; //AMMO FOR BULLETS
void setup()
{
size(500, 600);
x = new float [n];
y = new float [n];
bulletX = new float [2];
bulletY = new float [2];
for (int i=0; i<x.length; i++)
{
x[i]=random(0, width); //RANDOMIZE OBSTACLES LOCATIONS
y[i]=random(-600, 0); //RANDOMIZE OBSTACLES Y LOCATIONS
}
noStroke();
ellipseMode(RADIUS);
}
void draw()
{
background(0);
for (int i=0; i<x.length; i++)
{
y[i]=y[i]+velocityY;
//////////////////////////////COLLISION START////////////////////////////////////////
if (contact(x[i], y[i])==true)
{
x[i]=random(0, width);
y[i]=random(0, 0);
xship=250; //restart spaceship position
yship=500;
}
/////////////////////////////COLLISION END/////////////////////////////////////
fill(120);
ellipse(x[i], y[i], radius, radius);
if (y[i]>height) //IF OBSTACLES HIT GROUND
{
//y[i]=0; //OBSTACLES START FROM TOP
x[i]=random(0, 600); //OBSTACLES RANDOMIZE AGAIN
y[i]=random(0, 0); //OBSTACLES RANDOMIZE AGAIN
}
}
fill(124, 255, 60);
ellipse(xship, yship, radius, radius); //SPACESHIP
//////////////////////////////////BULLET SPEED AND LOCATION//////START////////
for (int i=0; i<bulletX.length; i++)
{
fill(255, 0, 0);
ellipse(bulletX[i], bulletY[i], radius, radius);
bulletY[i]=bulletY[i]-2; ///BULLETS SPEED
if (bulletcontact(bulletX[i], bulletY[i], radius, radius)==true) //bullets contact with obstacles
{
x[i]=random(0, width);
y[i]=random(0, 0);
bulletY[i]=0;
}
}
////////////////////BULLET COMES OUT OF SPACESHIP /////////////////START////////////
if (space == true && ammo>0)
{
float _bulletX[]=append(bulletX, xship);
float _bulletY[]=append(bulletY, yship-20);
bulletX = _bulletX;
bulletY = _bulletY;
space = false;
ammo=ammo-1; //AMMO COUNT TO 3 MAXIMUM ON SCREEN
}
for (int i = 0; i < bulletY.length; i += 1)
{
if (bulletY[i] <= 0)
{
ammo += 1;
arrayCopy(bulletX, i+1, bulletX, i, bulletX.length-i-1);
bulletX = shorten(bulletX);
arrayCopy(bulletY, i+1, bulletY, i, bulletY.length-i-1);
bulletY = shorten(bulletY);
}
}
}
////////////////////BULLET COMES OUT OF SPACESHIP///////////////////END////////////////////////
boolean contact(float x2, float y2)
{
return (dist(xship, yship, x2, y2))<2*radius;
}
boolean bulletcontact(float x3, float y3, float x4, float y4)
{
return (dist(x3, y3, x4, y4))<=2*radius;
}
void keyPressed()
{
if (key == ' ')
{
space = true;
}
}
void keyReleased()
{
fill(255);
if (key == ' ')
{
space = false;
}
}
Answers
Oh, also, press space bar to shoot the bullet