We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am currently making a space invaders game, but now I'm struggling with how to make the Alien shot. I try to use floor(random()); method, But it doesn't work. Here is my main code below:
PImage background;
int by = 0;
Shooter Shooter1;
int AlienRow = 3;
int AlienCol = 8;
int firstNullBullet = 0;
Alien[][] alien = new Alien[AlienCol][AlienRow];
Bullet[] bullet = new Bullet[2];
Boolean keyLeftPress = false, keyRightPress = false, gameEnd = false, gameWin = false;
int score = 0;
void setup()
{
size(700, 600);
gameStart();
setAlien();
background = loadImage("Space.jpg");
background.resize(700, 600);
}
void draw()
{
background(255);
image(background, 0, by);
Score();
gameWin();
gameWinEnd();
}
void gameStart()
{
Shooter1 = new Shooter();
}
void setAlien()
{
/** Set up the position of the image and how many rows and cols */
for (int i = 0; i < AlienCol; i++)
{
for (int j = 0; j < AlienRow; j++)
{
alien[i][j] = new Alien(30 * (2*(i+1)), 30 * (1*(j+1)));
}
}//end for loop
}
void displayAlienCheckHit()
{
/** display the alien from the procedure of Alien class and check the bullet hitting the
alien and go disappeared */
for (int i = 0; i < AlienCol; i++)
{
for (int j = 0; j < AlienRow; j++)
{
if (alien[i][j] != null)
{
alien[i][j].update();//display the alien and moving
for (int o = 0; o < bullet.length; o++)
{
if (bullet[o] != null && alien[i][j] != null)
{
Bullet b = bullet[o];
float dist = dist(b.x, b.y, alien[i][j].x, alien[i][j].y); //set the check point between the alien and bullet
if (dist < 30 && b.speed < 0)//if the check point is small than 30 the alien and the bullet will go disappeared
{
alien[i][j] = null;
bullet[o] = null;
score++;//Count the hitting
}
}
}
}
}
}//end for loop
}
void moveShooter1()
{
if (keyLeftPress && Shooter1.x > 10)
{
Shooter1.x -=10;
}
if (keyRightPress && Shooter1.x < width - 30)
{
Shooter1.x +=10;
}
}
void displayBullets()
{
/** If the bullet not null, do the procedure from the Bullets class.
If is less than 0, the bullet going to be null and disappear */
for (int i = 0; i < bullet.length; i++)
{
if (bullet[i] != null)
{
bullet[i].display();
bullet[i].move();
if (bullet[i].y > height || bullet[i].y < 0)//if the bullet out of the top of the screen and bullet go disappeared
{
bullet[i] = null;
}
} else //else the bullet keep going
{
firstNullBullet = i;
}
}
}
void keyPressed()
{
if (keyCode == LEFT && !gameEnd)
{
keyLeftPress = true;
}
if (keyCode == RIGHT && !gameEnd)
{
keyRightPress = true;
}
if (key == ' ' && !gameEnd)
{
Shooter1.shoot();
}
if (key == 'y' && gameEnd)
{
gameEnd = false;
setup();
}
if (key == 'e' && gameWin)
{
gameWin = true;
exit();
}
}
void keyReleased()
{
if (keyCode == LEFT)
{
keyLeftPress = false;
} else
{
if (keyCode == RIGHT)
{
keyRightPress = false;
}
}
}
// and here is my code for the Alien:
PImage Alien;
class Alien
{
float x, y;
float ximg = 0;
int speed = 2;
Boolean moveLeft = false;
Alien(float x, float y)
{
this.x = x;
this.y = y;
Alien = loadImage("invaderv3.png");
Alien.resize(40, 40);
}
void display()
{
image(Alien, x, y);
}
void move()
{
if (!moveLeft)
{
x += speed;
}
if (moveLeft)
{
x -= speed;
}
if (x <= 0)
{
moveLeft = false;
y += 90;
}
if (x >= width - 30)
{
moveLeft = true;
y += 90;
}
if (y >= 550)//When the alien go out of the button the game wiil end
{
gameEnd = true;
}
}
void update()
{
display();
move();
}
/**void AlienShoot()
{
Bullet b = new Bullet(this.x, this.y, 5);
}*/
}
Class for my bullet:
class Bullet
{
float x, y;
float speed;
Bullet(float x, float y, float speed)
{
this.x = x;
this.y = y;
this.speed = speed;
}
void display()
{
fill(random(255), random(255), random(255));
rect(x, y, 2, 15);
}
void move()
{
y += speed;
}
}
Answers
Just google it - many examples here in the forum
https://forum.processing.org/two/discussions/tagged?Tag=#space+invaders
https://www.google.de/webhp?nord=1&gws_rd=ssl#safe=off&nord=1&q=space+invaders+site:http://forum.processing.org/two&*
Anybody help? I tried to use the link Chrisir given. But because I'm using 2D array the code doesn't worked on mine.
my least favourite bug report. no information on what it's trying to do or what it does instead, just 'it doesn't work'. please be more explicit.
and we can't run your code without the image.
Sorry, here is the image.
I tried to use this bit code in the Alien class
void AlienShoot() { Bullet b = new Bullet(this.x, this.y, 5); }
And also tried to used this method below:
floor(random())
But the alien still won't shoot.
For shooting google shoot here on the forum
In short make a class Bullet and an ArrayList of that type
shooting: add to ArrayList
Display all bullets by for loop