Help with space invaders game

edited February 2016 in Questions about Code
//NOT PROPERLY WORKING ARRAY
int cols= 3; 
int rows=2; 
//int cols_select; 
//int rows_select; 

Alien[][] aliens = new Alien[cols][rows]; 

PImage background;
int y=0;
int bulletX;
int bulletY;
int score=0;
int alienx;
int alieny;

final int NORMAL=0;
final int CRASHED=1;
int gameMode=NORMAL;

//calling the constractors
   //!ALIENS!
   //Alien alien1=new Alien(200,30);
   //Alien alien2=new Alien(400,30);
   //Alien alien3=new Alien(600,30);
   //Alien alien4=new Alien(200,130);
   //Alien alien5=new Alien(400,130);
   //Alien alien6=new Alien(600,130);

   //!DEFENDER!
   Defender defender1;

   //!BULLET!
   bullet  bullet1;

void setup()
{
  bulletX=0;
  bulletY=-100;
  size(1000,800);
  background=loadImage("bg1.jpg");
  image(background,0,0);
  background.resize(width,height);//resize the image to the size of my background
 // bullets=new ArrayList();

    //NOT PROPERLY WORKING ARRAY
    //creating the array
     for (int i=0; i< cols; i++){ 
      for (int j=0; j<rows; j++){ 
        aliens[i][j] = new Alien(alienx,alieny); 
        alienx=alienx+70;
        alieny=alieny+20;
       }
      } 
  bullet1=new bullet(bulletX,bulletY);
  defender1=new Defender(320,450);  

}//end of setup

void draw()
{
  if (gameMode==NORMAL)
  {
    background(255);
    image(background,0,y);
    image(background,0,y+height);
    y=y-10;

    if (y==-background.height)
    {
      y=0;
    }

  //  for (int i=0; i<bullets.size(); i++)
    //{
      //bullet.update();
    //}

    //alien1.update();
    //alien2.update();
    //alien3.update();
    //alien4.update();
    //alien5.update();
    //alien6.update();

    //NOT WORKING PROPERLY ARRAY
   for (int i=0; i< cols; i++){ 
    for(int j=0; j<rows; j++){ 
      aliens[i][j].update();
   }
    }    


   // boolean AlienOnScreen;
   // AlienOnScreen=alien1.update();
    //AlienOnScreen=alien2.update();
    //AlienOnScreen=alien3.update();


    //  if (AlienOnScreen==false)
      //{
        //alien1=new Alien(200,30);
        //alien2=new Alien(400,30);
        //alien3=new Alien(600,30);
      //}

      defender1.render();
      bullet1.update();

      textSize(30);
      fill(255);
      text("SCORE:"+score,30,30);

      if (defender1.crash()||(bullet1.crash())==true)
      {
        //fill(255);
        //text("YOUR SCORE IS:",score,220,420);

        fill(255,0,0);
        textSize(30);
        text("GAME OVER PRESS R TO TRY AGAIN",240,400);
        gameMode=CRASHED;
      }    

       if (defender1.crash()||(bullet1.crash())==true)
      {
        score=score+50;
      }
  }
}

  void keyPressed()
  {
    if (key==CODED)
    {
      if (keyCode==UP && defender1.y>=400)
      {
        defender1.y=defender1.y-10;
      }
      else if (keyCode==DOWN && defender1.y<=height-260)
      {
        defender1.y=defender1.y+10;
      }
      else if (keyCode==RIGHT && defender1.x<width-130)
      {
        defender1.x=defender1.x+20;
      }
      else if (keyCode==LEFT && defender1.x>15)
      {
       defender1.x=defender1.x-20;
      }
    }
          if (key=='r' && gameMode==CRASHED)
          {
              gameMode=NORMAL;

             // alien1=new Alien(200,30);
             // alien2=new Alien(400,30);
             // alien3=new Alien(600,30);
              //alien4=new Alien(200,130);
              //alien5=new Alien(400,130);
              //alien6=new Alien(600,130);

              //aliens[i][j].update();    

          bullet1=new bullet(bulletX+1000,bulletY+1000);
          defender1=new Defender(320,450);

         }

    if (key==' ')
    {
      bulletX=defender1.x+75;
      bulletY=defender1.y;

      bullet1=new bullet(bulletX, bulletY);
     // bullet2=new bullet(bulletX, bulletY);

    }  
} 

Here is my alien class:

class Alien 
{
  int y=100;
  int x=10;
  int v=5;

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

//boolean move()
//{
  //y=y+1;
  //x=x;

  //if (y>=0)
  //{
    //return true;
  //}
  //else 
 // {
   //return false;

  //}
//}//end of the boolean

   void move ()
  {

    x=x+v;  // which direction the alien is moving

    if(x> width - 25)

    { 
      v = -5;
      y = y+30;
    }
    if (x==25)
    {
      v =+5;
      y= y+30;
    }

  }//end of move

void create()
{
    fill(255,255,255);
    ellipse(x,y,45,50);
    fill(255,0,0);
    ellipse(x,y,60,25);
}

void update()
{
  create();
  move();
  //y=y+50;

}
}//end of class

i want to get 3 columns with 2 rows of aliens in fixed place with a simple moving pattern for some reason tho i cant get them to display properly with are going diagonally and the other problem that i have is when i try to press "r" and restart the game and set the aliens in the same place where they start it wont work they just keep going from where they left

Any help would be appreciated thanks in advance can put all the code if someone wants to check it

Answers

  • excuse the horrible preview its my first time posting here

  • edited February 2016

    edit post, highlight code, hit ctrl-o.

  • thanks for the tip :)

  • creating the array

    you need to reset alienx

      //creating the array
      for (int i=0; i< cols; i++) {
        alienx=0;
        for (int j=0; j<rows; j++) { 
          aliens[i][j] = new Alien(alienx, alieny); 
          alienx=alienx+70;
          alieny=alieny+20;
        }
      } 
    

    when i try to press "r" and restart the game and set the aliens in the same place where they start it wont work they just keep going from where they left

    here you want to do the double for-loop above in keyPressed : if (key=='r'...

    and, yes, in future please post the entire code. We can't run it otherwise.

  • edited February 2016

    omg thank you very much chrissir there is one more thing aliens for some reason keep kinda going diagonally and when i press the r it resets them but lower everytime?

    Thanks in advance

  • edited February 2016

    are we talking about these lines:

          if (key=='r' && gameMode==CRASHED)
          {
              gameMode=NORMAL;
    
             // alien1=new Alien(200,30);
             // alien2=new Alien(400,30);
             // alien3=new Alien(600,30);
              //alien4=new Alien(200,130);
              //alien5=new Alien(400,130);
              //alien6=new Alien(600,130);
    
              //aliens[i][j].update();    
    
          bullet1=new bullet(bulletX+1000,bulletY+1000);
          defender1=new Defender(320,450);
    
         }
    

    nothing (no alien) gets resetted here

    did you insert the double for-loop as I told you to?

  • edited February 2016

    yes ive done this

        if (key=='r' && gameMode==CRASHED)
              {
                  gameMode=NORMAL;
    
                 // alien1=new Alien(200,30);
                 // alien2=new Alien(400,30);
                 // alien3=new Alien(600,30);
                  //alien4=new Alien(200,130);
                  //alien5=new Alien(400,130);
                  //alien6=new Alien(600,130);
    
                  for (int i=0; i< cols; i++) {
                  alienx=0;
                  for (int j=0; j<rows; j++) { 
                  aliens[i][j] = new Alien(alienx, alieny); 
                  alienx=alienx+100;
                  alieny=alieny+50;
          }
        } 
    
  • edited February 2016

    you need to start with alieny = 0; in line 11

  • yeah that fixed the issue thanks do you happen to know why aliens go diagonally?

  • because you tell them to??

    x=x+v;  // which direction the alien is moving
    

    (or do you mean the layout of the grid right after the start? )

  • that is a pattern that they follow once they hit the width of the screen to go diagonally 40 pixels down but before hitting it i want them to be in a straight line (right at the start)

  • Answer ✓
      //creating the array
      for (int i=0; i< cols; i++) {
        for (int j=0; j<rows; j++) { 
          alienx=j*70+110;
          alieny=i*120+110;
          aliens[i][j] = new Alien(alienx, alieny);
        }
      } 
    
  • Answer ✓

    this calculates the alienx and alieny from i and j; so you get better values

    110 is distance from screen border, 70 and 120 the distance from each other

  • it worked you are amazing but how? haha i keep reading the code wont understand why it worked thank you very much anyways

  • Ohh alright thank you thank you very much you just saved me from a sleepless night :)

  • well, you have rows and columns

    in your version with

        alienx=alienx+70;
        alieny=alieny+20;
    

    the x-position AND the y-position was increased EVERY alien. So it becomes diagonal.

    In my version :

     alienx=j*70+110;
     alieny=i*120+110;
    

    the value of alienx and alieny are calculated from the i and j.

    This means, alieny is changed when the inner for-loop is closed and we re-enter the for j loop with a new i-value. And alienx is this situation gets the same value as in the last column

  • edited February 2016

    also please hit ctrl-t often in processing; it gives you auto-indent in processing

    and some house-cleaning: size should be first line in setup()

    you have an image you use as background. Good. Better name it backgroundImg

    but just say background(backgroundImg); instead of using the command image() here (or using background(255);), please

    delete lines that are not in use (which start with //) and make comments where needed please

    swap line 42 and 43 above (in the first post); replace the command image() with the command background(backgroundImg); as mentioned

    [EDITED]

  • Great explanation thanks for your valuable time!! Gotta work on the bullets array now and hitting and making the aliens disappear

  • shall i post all the program here without background and defender image?

  • not now

    only when you have more questions

    and then the entire program without remarks in between ("Here is my alien class:")

    ;-)

  • int cols= 2; 
    int rows=4; 
    Alien[][] aliens = new Alien[cols][rows]; 
    
    //PImage background;
    int y=0;
    int bulletX;
    int bulletY;
    int score=0;
    int alienx=0;
    int alieny=0;
    
    final int NORMAL=0;
    final int CRASHED=1;
    int gameMode=NORMAL;
    
    //calling the constractors
    
       //!DEFENDER!
       Defender defender1;
    
       //!BULLET!
       bullet  bullet1;
    
    void setup()
    {
      size(1000,800);
      bulletX=0;
      bulletY=-100;
      background(0);
      //background=loadImage("bg1.jpg");
      //image(background,0,0);
      //background.resize(width,height);//resize the image to the size of my background
     // bullets=new ArrayList();
    
    //NOT PROPERLY WORKING ARRAY
    
    //creating the array
      for (int i=0; i< cols; i++) {
      for (int j=0; j<rows; j++) { 
        alienx=j*100+110;
        alieny=i*120+110;
        aliens[i][j] = new Alien(alienx, alieny);
      }
    } 
    
      bullet1=new bullet(bulletX,bulletY);
      defender1=new Defender(410,500);  
    
    }//end of setup
    
    void draw()
    {
      if (gameMode==NORMAL)
      {
        background(0);
       // image(background,0,y);
        //image(background,0,y+height);
        y=y-10;
    
       // if (y==-background.height)
        //{
         // y=0;
       // }
    
      //  for (int i=0; i<bullets.size(); i++)
        //{
          //bullet.update();
        //}
    
    
        //NOT WORKING PROPERLY ARRAY
       for (int i=0; i< cols; i++){ 
        for(int j=0; j<rows; j++){ 
          aliens[i][j].update();
       }
        }    
    
          defender1.render();
          bullet1.update();
    
          textSize(30);
          fill(255);
          text("SCORE:"+score,30,30);
    
          if (defender1.crash()||(bullet1.crash())==true)
          {
            fill(255,0,0);
            textSize(30);
            text("GAME OVER PRESS R TO TRY AGAIN",240,400);
            gameMode=CRASHED;
          }    
    
           if (defender1.crash()||(bullet1.crash())==true)
          {
            score=score+50;
          }
      }
    }
    
      void keyPressed()
      {
        if (key==CODED)
        {
          if (keyCode==UP && defender1.y>=400)
          {
            defender1.y=defender1.y-10;
          }
          else if (keyCode==DOWN && defender1.y<=height-260)
          {
            defender1.y=defender1.y+10;
          }
          else if (keyCode==RIGHT && defender1.x<width-130)
          {
            defender1.x=defender1.x+20;
          }
          else if (keyCode==LEFT && defender1.x>15)
          {
           defender1.x=defender1.x-20;
          }
        }
          if (key=='r' && gameMode==CRASHED)
          {
              gameMode=NORMAL;
    
              for(int i=0; i<cols; i++){
                for (int j=0; j<rows; j++){
                  alienx=j*100+110;
                  alieny=i*120+110;
                  aliens[i][j]=new Alien (alienx,alieny);
                }
              }
    
          bullet1=new bullet(bulletX+1000,bulletY+1000);
          defender1=new Defender(320,450);
    
         }
    
        if (key==' ')
        {
          bulletX=defender1.x+75;
          bulletY=defender1.y;
    
          bullet1=new bullet(bulletX, bulletY);
         // bullet2=new bullet(bulletX, bulletY);
    
        }  
    } 
    

    Alien class:

    class Alien 
    {
      int y=100;
      int x=10;
      int v=5;
      boolean imaDeadAlien=false;
    
      Alien (int x ,int y)
      {
        this.x=x;
        this.y=y;
      }
    
       void move ()
      {
    
        x=x+v;  // which direction the alien is moving
    
        if(x> width - 25)
    
        { 
          v = -5;
          y = y+45;
        }
        if (x==25)
        {
          v =+5;
          y= y+45;
        }
    
      }//end of move
    
    void create()
    {
        fill(255,255,255);
        ellipse(x,y,45,50);
        fill(255,0,0);
        ellipse(x,y,60,25);
    }
    
    void destroyed()
    {
      imaDeadAlien=true;
    }
    
    void removeAlien()
    {
      if(imaDeadAlien==true){
        x=10000;
        y=10000;
      }
    }
    
    void update()
    {
      create();
      move();
      //y=y+50;
    
    }
    }//end of class
    

    bullet class:

    class bullet
    {
      int x;
      int y;
    
      bullet(int x,int y)
      {
        this.x=x;
        this.y=y;
      }
    
      void render()
      {
        fill(0,255,255);
        rect(x-5,y,10,20);
        fill(255);
        rect(x-5,y-10,10,10);
    
        //fill(255);
        //ellipse(x,y,20,20);
        //fill(0);
        //ellipse(x,y,5,5);
      }
    
      void move()
      {
        y=y-10;
      }
    
      void update()
      {
        render();
        move();
      }
    
     boolean crash()
      {
        color detectedColour;
        for (int i=y;  i<=y+3-0; i++)
        {
          detectedColour=get(x+30,i);
          if (detectedColour==color(255))
          {
            return true;
          }
        }
        return false;
    }  
    }//end of class
    

    Defender class:

    class Defender
    {
      //PImage image1;
      int x;
      int y;
    
      Defender(int x,int y)
      {
        this.x=x;
        this.y=y;
       // image1=loadImage("shooter.png");
      }
    
      void render(){
    
       // image1.resize(150,150);
        //image (image1,x,y);
      rect(x,y,100,100);
      }
    
      //{
        //fill(255);
        //rect(x,y,20,80);
        //fill(0,0,255);
        //rect(x-35,y+80,90,20);
      //}
    
    
      boolean crash()
      {
        color detectedColour;
        for (int i=y; i<=y+3-0; i++)
        {
          detectedColour=get(x+10,i);
    
          if (detectedColour==color(255))
          {
            return true;
          }
        }
            return false;
      }
    
    }//end of the class
    
  • ohh ok sorry i misunderstood you then thanks for the tip you are a very helpful man thank you thank you so much for your time i never thought id get a help from a forum i was always ignored thanks again!

  • edited February 2016

    oh what I meant was:

    you still need the background image, you just can use it with the command background

    so please restore line 5 etc. ....

    also kill line 17 and 72

Sign In or Register to comment.