space invaders game code, please help

edited March 2017 in Questions about Code

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;
  }
}
Tagged:

Answers

Sign In or Register to comment.