How to make the invader can shoot by theirself?

Hi there, I'm making a game where something comes across the screen, from one side to the other. What I'd like to do is that the invader can shoot the bullet itself at a random moment by using 1D array not the arraylist, and I'm really stuck with it... Here is what i got so far:

    //Main class
    int AlienRow = 4;
    int AlienCol = 8;
    int AfirstNullBullet = 0;
    Alien[][] alien = new Alien[AlienCol][AlienRow];
    BulletAlien[] bulletAlien = new BulletAlien[5];

    void setup()
    {
      size(700, 600);
      gameStart();
      setAlien();
    }

    void draw()
    {
      background(255);
    }

    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 displayBullets() 
    {
      for (int a = 0; a < bulletAlien.length; a++)
      {
        if (bulletAlien[a] != null)
        {
          bulletAlien[a].Adisplay();
          bulletAlien[a].Amove();

          if (bulletAlien[a].ay < height || bulletAlien[a].ay > 0)
          {
            bulletAlien[a] = null;
          } else
          {
            AfirstNullBullet = a;
        }
      }
    }

    //Alien class
    class Alien
    {
      float x, y;
      float ximg = 0;
      float speed = 2;

      Boolean moveLeft = false;

      Alien(float x, float y)
      {
        this.x = x;
        this.y = y;
      }

      void display()
      {
        eclipse(x, y, 40, 40);
      }

      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() 
      {
        if (int (random(100)) > 0) 
        {
          bulletAlien[AfirstNullBullet] = new BulletAlien(x, y, 20);
        }
      }
    }

    //Bullet class
    class BulletAlien 
    {
      float ax, ay;
      float aspeed;

      BulletAlien(float ax, float ay, float aspeed) 
      {
        this.ax = ax;
        this.ay = ay;
        this.aspeed = aspeed;
      }

      void Adisplay()
      {
        fill(255, 255, 255);
        rect(ax, ay, 4, 15);
      }

      void Amove()
      {
        ay -= aspeed;
      }
    }

Answers

  • Copy and paste the code. Immediately see there are Too many opening curley braces - you're missing one on line 50, it looks like. Ctrl + t to automatically format the code. Add in missing bracket and Ctrl + t again.

    Try to run it. The function gameStart() can not be found. Whoops! Did you try to run the code you posted? Remove the call to gameStart() by commenting it out.

    Try to run the code again. The function eclipse() does not exist. Now I know you haven't tried running your own code. Change that to ellipse().

    Try to run the code again. Now gameEnd is undefined. Remove reference to it.

    Try to run the code again. Finally it starts. Nothing is drawn. Just a white screen. Oh, that's because all you're drawing in draw() is a white background.

    Maybe you should get the aliens to be drawn before you try to get them to shoot?

  • As for your actual question, to have up to five bullets at once, I would do the following:

    1) When a new bullet is to be created (and this happens at random for each Alien), look for a position in the bullets array that isn't already a bullet, and add a new bullet at that position. If there are no free positions, forget about adding the new bullet.

    2) To update and display the bullets, look at every bullet in the array and move it down, and draw it. If it is below the bottom of the screen, set the bullet in that position back to null, freeing up that spot.

    Tidied up code:

    //Main class
    int AlienRow = 4;
    int AlienCol = 8;
    int AfirstNullBullet = 0;
    Alien[][] alien = new Alien[AlienCol][AlienRow];
    BulletAlien[] bulletAlien = new BulletAlien[5];
    
    void setup()
    {
      size(700, 600);
      //gameStart();
      setAlien();
    }
    
    void draw()
    {
      background(255);
      for (int i = 0; i < AlienCol; i++) 
      {
        for (int j = 0; j < AlienRow; j++) 
        {
          alien[i][j].update();
        }
      }
      displayBullets();
    }
    
    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 displayBullets() 
    {
      for (int a = 0; a < bulletAlien.length; a++)
      {
        if (bulletAlien[a] != null)
        {
          bulletAlien[a].Adisplay();
          bulletAlien[a].Amove();
    
          if (bulletAlien[a].ay < height || bulletAlien[a].ay > 0)
          {
            bulletAlien[a] = null;
          } else
          {
            AfirstNullBullet = a;
          }
        }
      }
    }   
    
    //Alien class
    class Alien
    {
      float x, y;
      float ximg = 0;
      float speed = 2;
    
      Boolean moveLeft = false;
    
      Alien(float x, float y)
      {
        this.x = x;
        this.y = y;
      }
    
      void display()
      {
        ellipse(x, y, 40, 40);
      }
    
      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();
        Alienshoot();
      }
    
      void Alienshoot() 
      {
        if (int (random(100)) > 0) 
        {
          bulletAlien[AfirstNullBullet] = new BulletAlien(x, y, 20);
        }
      }
    }
    
    //Bullet class
    class BulletAlien 
    {
      float ax, ay;
      float aspeed;
    
      BulletAlien(float ax, float ay, float aspeed) 
      {
        this.ax = ax;
        this.ay = ay;
        this.aspeed = aspeed;
      }
    
      void Adisplay()
      {
        fill(255, 255,255);
        rect(ax, ay, 4, 15);
      }
    
      void Amove()
      {
        ay -= aspeed;
      }
    }
    
  • edited April 2017

    I'm sorry about the mess of the code, cause is only a part of my code. The background and the alien were imaged so that I change it for an easy post. Here is my full working code (image at the end):

        //Main
        PImage background;
        int by = 0;
        Shooter Shooter1;
    
        int AlienRow = 4;
        int AlienCol = 8;
        int firstNullBullet = 0;
        int AfirstNullBullet = 0;
        Alien[][] alien = new Alien[AlienCol][AlienRow];
        Bullet[] bullet = new Bullet[4];
        BulletAlien[] bulletAlien = new BulletAlien[5];
    
        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 += 10;//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;
            }
          }
          for (int a = 0; a < bulletAlien.length; a++)
          {
            if (bulletAlien[a] != null)
            {
              bulletAlien[a].Adisplay();
              bulletAlien[a].Amove();
    
              if (bulletAlien[a].ay < height || bulletAlien[a].ay > 0)
              {
                bulletAlien[a] = null;
              } else
              {
                AfirstNullBullet = a;
              }
            }
          }
        }
    
        void Score()
        {
          fill(255, 0, 0);
          textAlign(CENTER);
          textSize(20);
          text("Your score is: " + score, 90, 25);
        }
    
        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;
            }
          }
        }
    
        //Alien
        PImage Alien;
    
        class Alien
        {
          float x, y;
          float ximg = 0;
          float 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() 
          {
            if (int (random(100)) > 0) 
            {
              bulletAlien[AfirstNullBullet] = new BulletAlien(x, y, 20);
            }
          }
        }
    
        //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, 4, 15);
          }
    
          void move()
          {
            y += speed;
          }
        }
    
        //Alien bullet
        class BulletAlien 
        {
          float ax, ay;
          float aspeed;
    
          BulletAlien(float ax, float ay, float aspeed) 
          {
            this.ax = ax;
            this.ay = ay;
            this.aspeed = aspeed;
          }
    
          void Adisplay()
          {
            fill(255, 255, 255);
            rect(ax, ay, 4, 15);
          }
    
          void Amove()
          {
            ay -= aspeed;
          }
        }
    
        //Shooter
        class Shooter
        {
          float x, y;
          int Shoot;
          int LastShoot;
          int coolDown;
    
          Shooter()
          {
            this.x = 350;
            this.y = 570;
            this.LastShoot = 0;
            this.coolDown = 600;
          }
    
          void display()
          {
            fill(255, 0, 0);
            rect(x - 15, y, 50, 15);
            fill(255, 0, 0);
            rect(x, y - 10, 20, 10);
          }
    
          void shoot() 
          {
            if (millis() - LastShoot > coolDown) 
            {
              bullet[firstNullBullet] = new Bullet(x + 10, y - 25, -5);
              LastShoot = millis();
            }
          }
        }
    
        //WinEnd
        void gameWinEnd()
        {
          if (!gameEnd && !gameWin) {
            Shooter1.display();
            moveShooter1();
            displayBullets();
            displayAlienCheckHit();
          }
          if (gameEnd) 
          {
            gameEndText();
          }
          if (gameWin)
          {
            gameWinText();
          }
        }
    
        void gameWin()
        {
          if (score == 320)//if the score reach the top then the game will stop
          {
            gameWin = true;
          }
        }
    
    
        void gameEndText() 
        {
          fill(255, 0, 0);
          textAlign(CENTER);
          textSize(25);
          text("You LOSE!! Press Y to start a new game.", 350, 300);
        }
    
        void gameWinText()
        {
          fill(255, 0, 0);
          textAlign(CENTER);
          textSize(25);
          text("You WIN!! Press E to exit the game.", 350, 300);
        }
    

    invaderv3 Space

  • edited April 2017

    You can provide a smaller version of your code centered around your current problem + any additional detail related to it. It is important to post a minimum runnable sketch demonstrating your current approach and provide details of what it does now and what it suppose to be doing instead, in case you are looking for feedback.

    Going smaller is important in your case. You don't want to try to debug all the process at once. Simple code provides clarity and it is always good to go back to revise concept or implement bigger changes.

    This next tutorial provides an idea of how to generate a moving object: https://processing.org/examples/bounce.html

    You should consider using an ArrayList as well so to manage multiple bullets: https://processing.org/examples/arrayobjects.html

    This next post could also be helpful: https://forum.processing.org/two/discussion/comment/95303/#Comment_95303

    *****EDIT: @LeegeeRicky Ok, after I follow your code, I figure that this is a working version of your program and that you solved your issue. Never mind my previous comment. Thanks for sharing your solution.

    Kf

  • No is not working because the Alien still won't shoot itself. Thanks for the comment.

  • You mean, like when you press space?

    Kf

  • No, when I press the space the shooter will shoot. Now what I want to do is make the alien to shoot automatically.

  • Ok, so what should it do? Aliens will fire all at once? Or randomly every so often?

    I have an observation. Check this next code:

    //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, 4, 15);
      }
    
      void move()
      {
        y += speed;
      }
    }
    
    //Alien bullet
    class BulletAlien 
    {
      float ax, ay;
      float aspeed;
    
      BulletAlien(float ax, float ay, float aspeed) 
      {
        this.ax = ax;
        this.ay = ay;
        this.aspeed = aspeed;
      }
    
      void Adisplay()
      {
        fill(255, 255, 255);
        rect(ax, ay, 4, 15);
      }
    
      void Amove()
      {
        ay -= aspeed;
      }
    }
    

    Both classes are almost almost identical. The only thing that is different is this:

    y += speed; vs. ay -= aspeed;

    My proposal. Reduce your two classes two one. Remove BulletAlien class. When you create a bullet, you have two options:

    • Make the bullet's speed positive when is fired by the shooter
    • Make it negative if it is fired by an alien.

    Then your program can iterate over the array of bullets and based on the way you wrote your code, alien and shooter bullets are managed by the same array (Yes, you need to eliminate one of the arrays as well)

    However, you should reconsider rewriting the section of the code where you remove bullets in your program that are out of the sketch. Your are setting the array entry to null. This might work but it is not a good idea. You are better off using an ArrayList and removing the actual item in the list when it leaves the sketch. Notice that to remove an item, you have to iterate the list backwards. Check the reference for ArrayList as they show an example of how to do that.

    Kf

Sign In or Register to comment.