title: Array of Enemies, 2D Java Game
body: <p>Basically, I am making a simple asteroids-y style side-scroller, I have had a little Java experience, but am not great when it comes to certain implementations.  So, I want to create a bunch of asteroids that spawn at random times and at random y-positions.  I figure the best way to do that is to do some sort of array, but I don't know how to really implement that, and do collision  checking for the entire array.  Here are relevant parts of my enemy and playing field classes.</p>
  
  <p>Bonus points to whomever can help me get my theme music working!</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.awt.*;
  import javax.swing.ImageIcon;
  
  public class Enemy 
  {
      int x, y;
      Image img;
      boolean isAlive = true;
  
  
      public Enemy(int startX, int startY, String location)
      {
          x = startX;
          y = startY;
          ImageIcon l = new ImageIcon(location);
          img = l.getImage();
      }
  
      public Rectangle getBounds()
      {
          return new Rectangle(x, y, 60, 60);
      }
  </code></pre>
  
  <p>And</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Board extends JPanel implements ActionListener
  {
      Player p;
      Image img;
      Timer time;
      Thread animator;
      Enemy en1, en2;
      boolean lost = false;
      static Font font = new Font("SanSerif", Font.BOLD, 24);
      public AudioClip theme;
  
      public static int score = 0;
      public static int lives = 3;
  
      public Board() 
      {
          //theme = Applet.newAudioClip(getClass().getResource("images/theme.mid"));
          //theme.play();
          p = new Player();       
          addKeyListener(new ActionListener());
          setFocusable(true);
          ImageIcon i = new ImageIcon("images/background.png");
          img = i.getImage();
          time = new Timer(5, this);
          time.start();
          int v =  172;
          en1 = new Enemy(p.x + 600, 260, "images/enemy1.png");
          en2 = new Enemy(p.x + 600, 200, "images/asteroid.gif");
      }
  
      public void actionPerformed(ActionEvent e)
      {
          checkCollisions();
          ArrayList bullets = Player.getBullets();
  
          for(int i = 0; i &lt; bullets.size(); i++)
          {
              Bullet b = (Bullet)bullets.get(i);
  
              if(b.isVisible() == true)
              {
                  b.move();
              }
              else
              {
                  bullets.remove(i);
              }
          }
  
          p.move();
  
          if(p.x &gt; 400)
          {
              en1.move(p.getdx());
          }
          if(p.x &gt; 500)
          {
              en2.move(p.getdx());
          }
          repaint();
      }
  
      public void paint(Graphics g)
      {
          if(lost)
          {
              System.out.println("You Lose");
          }
          super.paint(g);
          Graphics2D g2d = (Graphics2D) g;
          if((p.getX() - 590) % 2400 == 0 || (p.getX() - 590) % 2400 == 1)
          {
              p.nx = 0;
          }
          if((p.getX() - 1790) % 2400 == 0 ||(p.getX() - 1790) % 2400 == 1)
          {
              p.nx2 = 0;
          }
  
          g2d.drawImage(img, 685-p.nx2, 0, null);
  
          if(p.getX() &gt;= 590)
          {
              g2d.drawImage(img, 685-p.nx, 0, null);
          }
  
          g2d.drawImage(p.getImage(), p.edge, p.getY(), null);
  
          ArrayList bullets = Player.getBullets();
          for(int i = 0; i &lt; bullets.size(); i++)
          {
              Bullet b = (Bullet)bullets.get(i);
              g2d.drawImage(b.getImg(), b.getX(), b.getY(), null);
          }
          if(p.x &gt; 400)
          {
              if(en1.isAlive == true)
              {
                  g2d.drawImage(en1.getImg(), en1.getX(), en1.getY(), null);
              }
          }
          if(p.x &gt; 500)
          {
              if(en2.isAlive == true)
              {
                  g2d.drawImage(en2.getImg(), en2.getX(), en2.getY(), null);
              }           
          }
      }
      public void checkCollisions()
      {
              Rectangle r1 = en1.getBounds();
              Rectangle r2 = en2.getBounds();
              ArrayList bullets = Player.getBullets();
  
              for (int i = 0; i &lt; bullets.size(); i++)
              {
                  Bullet m = (Bullet) bullets.get(i);
                  Rectangle m1 = m.getBounds();
  
                  if (r1.intersects(m1) &amp;&amp; en1.isAlive())
                  {
                      score++;
                      en1.isAlive = false;
                      m.visible = false;
                   }
                   else if (r2.intersects(m1)&amp;&amp; en2.isAlive())
                   {
                       en2.isAlive = false;
                       m.visible = false;
                   }
              }
  
              Rectangle d = p.getBounds();
              if (d.intersects(r1) || d.intersects(r2))
              {
              lives--;
                      if(lives &lt; 0)
                      {
                          lost = true;
                      }
              }
  
          }
  </code></pre>