java - Writing space invaders code, can't get bullets to destroy aliens -


i apologize in advance question potentially taking effort solve. group project i've been working on while scripts bit complicated, , me , 2 others i'm working total beginners java. unfortunately problem don't know problem is, question bit vague. i'll best show relevant bits of code , indicating might problem appreciated :)

this class, spritealien, creates alien , has methods define alien's movement.

import javax.swing.jpanel;  public class spritealien extends jpanel{      public int hposalien; // alien's horizontal coordinate     public int vposalien; // alien's vertical coordinate      public int hposbullet, vposbullet;       public int life; // aliens life, may have more 1      int dx = 1; // number of pixels alien moves horizontally every timer cycle     int dy = 10; // number of pixels alien moves down when hits boundary     boolean direction = true; // false - left, true - right     boolean shoot, defeated = false;      // constructor     public spritealien (int horizontalpos, int verticalpos){         hposalien = horizontalpos;         vposalien = verticalpos;     }       public void movealien()     {            //this creates alien's movement          // if alien has not reached right wall , moving right         if ((hposalien <= 740) && (direction == true)) {              moveright();         }          // if alien has reached right wall , moving right         else if ((hposalien >= 740) && (direction == true)) {              movedown();             moveleft();             direction = false;         }          // if alien has not reached right wall , moving left         else if ((hposalien <= 740) && (hposalien >= 0) && (direction == false)) {             moveleft();         }          // if alien has reached left wall , moving left         else if ((hposalien < 0) && (direction == false)) {             movedown();             moveright();             direction = true;         }                }      // methods used alien movement     void moveleft() {         hposalien -= dx;     }      void moveright() {         hposalien += dx;     }      void movedown() {         vposalien += dy;     } } 

this class, spritearray, creates array of aliens move formation

// create array of aliens make our alien army public class spritearray extends random{

spritealien[] alienarmy = new spritealien[10]; // array of alien objects     int index; // index of array      int x, y; // coordinates of alien      // constructor     spritearray(){         // nested loop position aliens 2 dimensional arrays on screen         for(int = 0; < 2; i++){             for(int j = 0; j < 5; j++)             {                 index = i*5 + j; // (0*1 + 0) (0*1 + 1) etc                  x = j*120 + 100; // each column 120 pixels apart                 y = i*80 + 100; // each row 80 pixels apart                   alienarmy[index] = new spritealien(x, y);              }         }     } 

this main class involved, sprite, , 1 have error because i've written code fires bullets , supposed make aliens destroyed. i'm aware piece of code annoyingly long, make easier i've put 'look here here' comment before relevant bit :p

import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.timer; import java.util.timertask;  import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception;  public class sprite extends jpanel implements actionlistener{      // game animation     public int hposshooter; // shooter's horizontal coordinate     public int vposshooter; // shooter's vertical coordinate     public int hposbullet; // bullet's horizontal coordinate     public int vposbullet = 490; // bullet's vertical coordinate     public int movementspeed; // shooter's speed(describe shooter movement)     public timer looptime = new timer(9, this); // used loop through movements , repaint     public timer generatebullet;     public boolean started = false; // whether or not game has started     public boolean fired = false; // whether or not shot should exist     public int numberenemies = 1; // more enemies need edited     public boolean[] destroyed = new boolean[10]; //array of boolean variables tell whether or not alien matching index has been destroyed      public int shootingindex;      // display text information     playerinfo pinfo = new playerinfo();     jlabel scorelabel = new jlabel("score: " + integer.tostring(pinfo.getplayerscore()));     jlabel lifelabel = new jlabel("lives: " + integer.tostring(pinfo.getplayerlives()));      // load images     string imagenameshooter; //the shooter     string imagenamealien; //the alien     string imagenamebarrier; //the barrier      protected bufferedimage imageshooter;     protected bufferedimage imagealien;     protected bufferedimage imagebarrier;       spritearray enemies = new spritearray();    //  spritebarrier barrier1 = new spritebarrier(400, 400);     spritebarrierarray barrier1 = new spritebarrierarray(160, 360);     spritebarrierarray barrier2 = new spritebarrierarray(360, 360);     spritebarrierarray barrier3 = new spritebarrierarray(560, 360);  //  int shootingindex = enemies.alienshoot();       public sprite (string imagenameshooter, string imagenamealien, string imagenamebarrier){         try {             imageshooter = imageio.read(new file(imagenameshooter));         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          try {             imagealien = imageio.read(new file(imagenamealien));         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          try {             imagebarrier = imageio.read(new file(imagenamebarrier));         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          scorelabel.sethorizontaltextposition(jlabel.right);         lifelabel.sethorizontaltextposition(jlabel.left);         add(scorelabel);         add(lifelabel);     }      public static void spriteimage(jframe frame, jpanel panel) throws ioexception     {                   frame.add(panel);     }      // paint sprite     protected void paintcomponent(graphics g)     {         super.paintcomponent(g);         g.drawimage(imageshooter, hposshooter, vposshooter, null); //draws shooter in correct position          //draws bullet         if (fired)         {             g.fillrect (hposbullet, vposbullet, 8, 12);         }          //draws aliens         for(int = 0; < 10 ; i++ ){             g.drawimage(imagealien, enemies.alienarmy[i].hposalien, enemies.alienarmy[i].vposalien, null);             if(destroyed[i] == false)             {                 g.fillrect (enemies.alienarmy[i].hposbullet, enemies.alienarmy[i].vposbullet, 8, 12);             }         }          //draw barriers         for(int = 0; < 12 ; i++ ){             g.drawimage(imagebarrier, barrier1.barrier[i].hposbarrier, barrier1.barrier[i].vposbarrier, null);             g.drawimage(imagebarrier, barrier2.barrier[i].hposbarrier, barrier2.barrier[i].vposbarrier, null);             g.drawimage(imagebarrier, barrier3.barrier[i].hposbarrier, barrier3.barrier[i].vposbarrier, null);         }          looptime.start(); // refreshes image make animation work     }       //makes enemies drop missiles     void enemyattack() {          generatebullet = new timer(1000, new actionlistener() {             public void actionperformed(actionevent e){                 shootingindex = enemies.alienshoot();             }         });         generatebullet.start();     }      //shooter movement mechanics     public void actionperformed(actionevent e)     {            //conditional allows movement once game has started         if (started)         {             //this allows movement right if right border hasn't been reached             if (hposshooter < 733 && movementspeed > 0)             {                 hposshooter = hposshooter + movementspeed;             }             //this allows movement left if left border hasn't been reached             if (hposshooter > 0 && movementspeed < 0)             {                 hposshooter = hposshooter + movementspeed;             }          }          //fire!! here here here!!!!!         if (fired)         {             vposbullet = vposbullet - 6; //bullet moves upwards             //if alien hit... (should) destroyed!             for(int = 0; < 10 ; i++ ){                 if (!(destroyed[i]))                 {                     if (vposbullet == (enemies.alienarmy[i].vposalien + 20) && hposbullet > enemies.alienarmy[i].hposalien && hposbullet < (enemies.alienarmy[i].hposalien + 50))                     {                         destroyed[i] = true;                         repaint();                         fired = false;                         vposbullet = 490;                     }                 }             }             for(int i=0; i<12; i++)             {                 if (vposbullet == barrier3.barrier[i].vposbarrier && hposbullet > barrier3.barrier[i].hposbarrier && hposbullet < (barrier3.barrier[i].hposbarrier + 20))                 {                     fired = false;                     vposbullet = 490;                 }                 if (vposbullet == barrier2.barrier[i].vposbarrier && hposbullet > barrier2.barrier[i].hposbarrier && hposbullet < (barrier2.barrier[i].hposbarrier + 20))                 {                     fired = false;                     vposbullet = 490;                 }                 if (vposbullet == barrier1.barrier[i].vposbarrier && hposbullet > barrier1.barrier[i].hposbarrier && hposbullet < (barrier1.barrier[i].hposbarrier + 20))                 {                     fired = false;                     vposbullet = 490;                 }             }         }         //okay important bit's on now!!!          //when bullet reaches top of screen 'fired' = false         if (vposbullet < 0)         {             fired = false;             vposbullet = 490;         }          //this bit of makes aliens shoot missiles @ user's 'shooter'         for(int j = 0; j < 10 ; j++ ){             enemies.alienarmy[j].movealien();             if(enemies.alienarmy[j].shoot == true)             {                 if(enemies.alienarmy[j].vposbullet > 600)                 {                     enemies.alienarmy[j].shoot = false;                 }                 else                 {                     enemies.alienarmy[j].vposbullet += 2;                 }              }         }           //the position reset         repaint();     } } 

thank can provide!

i not sure but

if (vposbullet == (enemies.alienarmy[i].vposalien + 20) && hposbullet > enemies.alienarmy[i].hposalien && hposbullet < (enemies.alienarmy[i].hposalien + 50)) 

there in first test

vposbullet == (enemies.alienarmy[i].vposalien + 20) 

i not sure coords exact this, there should lower , greater maybe.

because of this:

vposbullet = vposbullet - 6; //bullet moves upwards 

maybe not soulution code long ;p

edit:

as @user3659404 said in comment, update if statement to:

if (vposbullet < (enemies.alienarmy[i].vposalien + 20) && vposbullet > enemies.alienarmy[i].vposalien && hposbullet > enemies.alienarmy[i].hposalien && hposbullet < (enemies.alienarmy[i].hposalien + 50)) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -