Arraylist problems

Hey! I started coding a game for my project and I thought I could write it to Processing IDE to test it out. Just one problem: I can't get the bullet system right. I tried using ArrayList(), but it keeps crashing. Also, this is one of my first Java (well, kind of) codes. I usually go with C++ but the graphics are kind of hard to accomplish. Hope someone could help.

(is there any way to make the code smaller? like linking it as .txt or even collapsing it? I tried the C button but I quess it ain't for that :P)

draw d = new draw();
hw hw = new hw();

//every bitmap used in this code

int[][] bitmap = {
  {0, -1}, 
  {-1, -1}, 
};

int[][] smile = {
  {0, -1, -1, -1, 0, 0, 0, 0}, 
  {-1, -1, 0, 0, 0, 0, 0, 0}, 
  {-1, 0, 0, 0, -1, -1, -1, -1}, 
  {-1, 0, 0, 0, 0, 0, 0, 0}, 
  {-1, 0, 0, 0, 0, 0, 0, 0}, 
  {-1, 0, 0, 0, -1, -1, -1, -1}, 
  {-1, -1, 0, 0, 0, 0, 0, 0}, 
  {0, -1, -1, -1, 0, 0, 0, 0}, 
};

/**
This is the ship
0 0 0 # # 0 0 0
0 0 0 # # 0 0 0
0 0 # # # # 0 0
0 0 # # # # 0 0
0 # # # # # # 0
0 # # # # # # 0
# # # # # # # #
0 # # # # # # 0
*/

int[][] ship = {
  {0, 0, 0, -1, -1, 0, 0, 0}, 
  {0, 0, 0, -1, -1, 0, 0, 0}, 
  {0, 0, -1, -1, -1, -1, 0, 0}, 
  {0, 0, -1, -1, -1, -1, 0, 0}, 
  {0, -1, -1, -1, -1, -1, -1, 0}, 
  {0, -1, -1, -1, -1, -1, -1, 0}, 
  {-1, -1, -1, -1, -1, -1, -1, -1}, 
  {-1, -1, 0, 0, 0, 0, -1, -1}, 
};

int[][] enemy = {
  {-1, -1, 0, 0, 0, 0, -1, -1}, 
  {-1, -1, -1, -1, -1, -1, -1, -1}, 
  {0, -1, -1, -1, -1, -1, -1, 0}, 
  {0, -1, -1, -1, -1, -1, -1, 0}, 
  {0, 0, -1, -1, -1, -1, 0, 0}, 
  {0, 0, -1, -1, -1, -1, 0, 0}, 
  {0, 0, 0, -1, -1, 0, 0, 0},
  {0, 0, 0, -1, -1, 0, 0, 0}, 
};

//public game's instructions
boolean gamestarted = true;

//draw class public variables
color bgcolor = 0;

//avatar variables
int a_x = 0;
boolean shootammo = false;
int ammo_x = 0;
int ammo_y = 0;

//enemy variables
int e_x = 0;
int e_dir = 1;

ArrayList <Bullet> bullets;

void setup() {

  bullets = new ArrayList();

  //set resolution
  size(640, 480);

  //set fps to 200
  frameRate(200);
}

class Bullet{
  int x;
  int y;

  Bullet(int tx, int ty) {
    x = tx;
    y = ty;
  }
  void display() {
    d.pixel(x, y, -1);
    println(x,y);
  }
  void move() {
    y -= 1;
    if(y<1) {
      bullets.remove(0);
    }
      //println(e_x);
      if(y<18) {
        if((x >= e_x) && (x <= e_x+8)) {
          println("HIT");
          exit();
        }

      }
  }
  void movebullets() {
  for(Bullet ammo : bullets) {
    move();
  }
  }
}

void draw() {

  /**  
  tests for the commands in class draw
  d.circle(320,240, 20, color(0,255,0));
  d.pixel(320, 240, color(255,0,0));
  d.bitmap(ship,320,240,8,8);
  */

  //run the space invaders test code every cycle
  playdatgame();
}

//space invaders prototype void
void playdatgame() {
  if (gamestarted == true) {
    d.bg(0);
    a_x = hw.getMouseX();
    if(hw.pressed()) {
      if(shootammo == false) {
        //shootammo = true;
        Bullet ammo = new Bullet(a_x+4,450);
        bullets.add(ammo);
        ammo.movebullets();
        //ammo.move();
        ammo.display();
        //ammo_x = a_x+4;
        //ammo_y = 450;
      }
    }
    if(shootammo == true) {
      //d.pixel(ammo_x, ammo_y, -1);
      ammo_y--;
      if(ammo_y<0) {
       shootammo = false; 
      }
      //println(e_x);
      if(ammo_y<18) {
        if((ammo_x >= e_x) && (ammo_x <= e_x+8)) {
          println("HIT");
          exit();
        }
      }
    }
    d.bitmap(ship,a_x,450,8,8);
    d.bitmap(enemy,e_x,10,8,8);
    if(e_dir == -1) {
      e_x--;
    }
    if(e_dir == 1){
      e_x++;
    }
    if(e_x>width-8) {
      e_dir = -1;
    }
    if(e_x<0) {
      e_dir = 1;
    }
  }
}

//hardware control class
public class hw {
  int getMouseX() {
    return mouseX;
  }
  boolean pressed() {
    return mousePressed;
  }
}

//drawing control class
public class draw {

  //pixel drawing void
  void pixel(int px, int py, color pcolor) {
    stroke(pcolor);
    point(px, py);
  }

  //circle drawing void
  void circle(int cx, int cy, int crad, color ccolor) {
    fill(bgcolor);
    stroke(ccolor);
    ellipse(cx, cy, crad, crad);
  }

  //background color control void (wipes everything to bg color)
  void bg(color bgc) {
    background(bgc);
    bgcolor = bgc;
  }

  //bitmap control void (uses 2d arrays to get the image)
  void bitmap(int[][] array, int x, int y, int w, int h) {
    for (int xc=0; xc<w; xc++) {
      for (int yc=0; yc<h; yc++) {
        //print(array[xc][yc]);
        stroke(color(array[yc][xc]));
        point(xc+x, yc+y);
      }
    }
  }

}

Answers

  • Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    When it crashes, what errors do you get?

    Kf

  • I would avoid calling your class 'draw', that's just asking for confusion.

  • edited May 20

    If the bullet goes below 1 in y axis, it says ConcurrentModificationException. Sometimes it says "The value of the local variable ammo is not used (in line 112)" And I finally got the code showing right.

  • edited May 20 Answer ✓

    Hello,

    some ideas:

    • don't use draw as a class name

    • you even have hw hw = new hw();, where the object has the same name as the class. Bad idea.

    • advice: objects small letter (e.g. smallLetter), classes start with a Capital (Bullet)

    • this if (gamestarted == true) { same as if (gamestarted) {

    • I would place all classes at the end in one go and not have one class definition between setup() and draw()

    • This //circle drawing void is not correct. Void is just the return type of the function. Write //circle drawing function instead.

    Your handling of the bullet ArrayList

    You have this

     void movebullets() {
        for (Bullet ammo : bullets) {
          move();
        }
      }
    

    inside the class. This is wrong. The class represents only one bullet and what it can do (display, collide). So everything concerning all bullets should be in playdatgame() or in the function movebullets(). Also you fail to remove bullets correctly; remove() makes the error ConcurrentModificationException btw. To remove you need to loop backward over the list , see reference on ArrayList please.

    With this: ammo.display(); you display the last bullet but not all in the list. To display all, for loop over them. ammo_x and ammo_y not needed. It's in the class as x,y.

    All in all, your understanding of using an ArrayList of objects of a class seems not to be complete yet.

    Best, Chrisir ;-)

    Full new version:

    ClassDraw d = new ClassDraw();
    Hardware hw = new Hardware();
    
    //every bitmap used in this code
    
    int[][] bitmap = {
      {0, -1}, 
      {-1, -1}, 
    };
    
    int[][] smile = {
      {0, -1, -1, -1, 0, 0, 0, 0}, 
      {-1, -1, 0, 0, 0, 0, 0, 0}, 
      {-1, 0, 0, 0, -1, -1, -1, -1}, 
      {-1, 0, 0, 0, 0, 0, 0, 0}, 
      {-1, 0, 0, 0, 0, 0, 0, 0}, 
      {-1, 0, 0, 0, -1, -1, -1, -1}, 
      {-1, -1, 0, 0, 0, 0, 0, 0}, 
      {0, -1, -1, -1, 0, 0, 0, 0}, 
    };
    
    /**
    This is the ship
     0 0 0 # # 0 0 0
     0 0 0 # # 0 0 0
     0 0 # # # # 0 0
     0 0 # # # # 0 0
     0 # # # # # # 0
     0 # # # # # # 0
     # # # # # # # #
     0 # # # # # # 0
     */
    
    int[][] ship = {
      {0, 0, 0, -1, -1, 0, 0, 0}, 
      {0, 0, 0, -1, -1, 0, 0, 0}, 
      {0, 0, -1, -1, -1, -1, 0, 0}, 
      {0, 0, -1, -1, -1, -1, 0, 0}, 
      {0, -1, -1, -1, -1, -1, -1, 0}, 
      {0, -1, -1, -1, -1, -1, -1, 0}, 
      {-1, -1, -1, -1, -1, -1, -1, -1}, 
      {-1, -1, 0, 0, 0, 0, -1, -1}, 
    };
    
    int[][] enemy = {
      {-1, -1, 0, 0, 0, 0, -1, -1}, 
      {-1, -1, -1, -1, -1, -1, -1, -1}, 
      {0, -1, -1, -1, -1, -1, -1, 0}, 
      {0, -1, -1, -1, -1, -1, -1, 0}, 
      {0, 0, -1, -1, -1, -1, 0, 0}, 
      {0, 0, -1, -1, -1, -1, 0, 0}, 
      {0, 0, 0, -1, -1, 0, 0, 0}, 
      {0, 0, 0, -1, -1, 0, 0, 0}, 
    };
    
    //public game's instructions
    boolean gamestarted = true;
    
    //draw class public variables
    color bgcolor = 0;
    
    //avatar variables
    int a_x = 0;
    boolean shootammo = false;
    //int ammo_x = 0;
    //int ammo_y = 0;
    
    //enemy variables
    int e_x = 0;
    int e_dir = 1;
    
    ArrayList <Bullet> bullets;
    
    //------------------------------------------------------
    
    void setup() {
      //set resolution
      size(640, 480);
    
      bullets = new ArrayList();
    
      //set fps to 200
      frameRate(200);
    }
    
    void draw() {
    
      /**  
       tests for the commands in class draw
       d.circle(320,240, 20, color(0,255,0));
       d.pixel(320, 240, color(255,0,0));
       d.bitmap(ship,320,240,8,8);
       */
    
      //run the space invaders test code every cycle
      playdatgame();
    }
    
    // -------------------------------------------------------------
    
    //space invaders prototype function
    void playdatgame() {
    
      if (gamestarted) {
    
        d.bg(0);
        a_x = hw.getMouseX();
    
        movebullets();
    
        if (hw.pressed()) {
          if (!shootammo) { // the ! means not 
            //shootammo = true;
            Bullet newBullet = new Bullet(a_x+4, 450);
            bullets.add(newBullet);
            //ammo.move();
            //   ammo.display();
            //ammo_x = a_x+4;
            //ammo_y = 450;
          }
        }
    
        //if (shootammo) {
        //  //d.pixel(ammo_x, ammo_y, -1);
        //  ammo_y--;
        //  if (ammo_y<0) {
        //    shootammo = false;
        //  }
        //  //println(e_x);
        //  if (ammo_y<18) {
        //    if ((ammo_x >= e_x) && (ammo_x <= e_x+8)) {
        //      println("HIT");
        //      exit();
        //    }
        //  }
        //}
    
        d.bitmap(ship, a_x, 450, 8, 8);
        d.bitmap(enemy, e_x, 10, 8, 8);
    
        if (e_dir == -1) {
          e_x--;
        }
        if (e_dir == 1) {
          e_x++;
        }
        if (e_x>width-8) {
          e_dir = -1;
        }
        if (e_x<0) {
          e_dir = 1;
        }
      }
    }
    
    void movebullets() {
      for (Bullet currentBullet : bullets) {
        currentBullet.display();
        currentBullet.move();
      }
    
      for (int i = bullets.size() - 1; i >= 0; i--) {
        Bullet currentBullet = bullets.get(i);
        if (currentBullet.isDead) {
          bullets.remove(i);
        }
      }
    }
    
    // ============================================
    
    class Bullet {
    
      // represents one bullet
    
      int x; 
      int y; 
      boolean isDead=false; 
    
      Bullet(int tx, int ty) {
        x = tx; 
        y = ty;
      }
    
      void display() {
        d.pixel(x, y, -1); 
        // println(x, y);
      }
    
      void move() {
    
        y -= 1; 
    
        if (y<-2) {
          // bullets.remove(0);
          isDead=true;
        }
    
        //println(e_x);
        if (y<18) {
          if ((x >= e_x) && (x <= e_x+8)) {
            println("HIT"); 
            exit();
          }
        }
      }
    }
    
    //=============================================
    
    //hardware control class
    public class Hardware {
      int getMouseX() {
        return mouseX;
      }
      boolean pressed() {
        return mousePressed;
      }
    }
    
    // =====================================
    //drawing control class
    public class ClassDraw {
    
      //pixel drawing void
      void pixel(int px, int py, 
        color pcolor) {
        stroke(pcolor); 
        point(px, py);
      }
    
      //circle drawing void
      void circle(int cx, int cy, int crad, color ccolor) {
        fill(bgcolor); 
        stroke(ccolor); 
        ellipse(cx, cy, crad, crad);
      }
    
      //background color control void (wipes everything to bg color)
      void bg(color bgc) {
        background(bgc); 
        bgcolor = bgc;
      }
    
      //bitmap control void (uses 2d arrays to get the image)
      void bitmap(int[][] array, int x, int y, int w, int h) {
        for (int xc=0; xc<w; xc++) {
          for (int yc=0; yc<h; yc++) {
            //print(array[xc][yc]);
            stroke(color(array[yc][xc])); 
            point(xc+x, yc+y);
          }
        }
      }
    }//class
    //
    
  • If the bullet goes below 1 in y axis, it says ConcurrentModificationException. Sometimes it says "The value of the local variable ammo is not used (in line 112)"

    This would've been useful information to put in the original post given that it tells us exactly what is wrong and where. So much more descriptive than 'it keeps crashing'.

  • Wow thank you so much Chrisir! I couldn't believe my eyes when I looked up for your answer! You fixed the entire code! THANK YOU! :D <3

Sign In or Register to comment.