So i am recreating space invaders to teach myself some game programming.
For the first time i have started to use ArrayLists.
But every time i run it i get an java.lang.IndexOutOfBoundsException when the last bullet that you have shot hits a target.
Thanks for your intrest and help.
Here a full error message :
Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.spaceinvader.Main.colision(Main.java:172)
at com.spaceinvader.Main.update(Main.java:165)
at com.spaceinvader.Main.run(Main.java:108)
at java.lang.Thread.run(Unknown Source)
My source Code :
Main.java
package com.spaceinvader; import com.spaceinvader.render.Render; import com.spaceinvader.entity.*; import javax.swing.JFrame; import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferStrategy; import java.util.ArrayList; public class Main extends Canvas implements Runnable, KeyListener { private static final long serialVersionUID = 1L; static Main main; static Render render; static public JFrame frame; Thread thread; private final static int WIDTH = 400; private final static int HEIGHT = 600; boolean running = false; static String Titel = "Space Invader"; private boolean[] keys = new boolean[65536]; public boolean left, right, use, back; public static EntityPlayer player; public static EntityBullet bullet; public static ArrayList<EntityBullet> bullets; public static ArrayList<EntityAlien> aliens; public int timer = 0; public int timed = 30; public static void main(String[] args) { frame = new JFrame(); main = new Main(); render = new Render(); frame.setResizable(false); frame.setTitle(Titel); frame.add(main); frame.pack(); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); main.start(); } public Main() { Dimension size = new Dimension(WIDTH, HEIGHT); setPreferredSize(size); setMinimumSize(size); setMaximumSize(size); addKeyListener(this); player = new EntityPlayer(); player.setAlive(true); player.setWidth(40); player.setHeight(20); player.setX(WIDTH / 2 - player.getWidth() / 2); player.setY(HEIGHT - 50); player.setVelX(5); bullets = new ArrayList<EntityBullet>(); aliens = new ArrayList<EntityAlien>(); } public void start() { if (running) return; thread = new Thread(this); thread.start(); running = true; } public void stop() { if (!running) return; running = false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } public void run() { int frames; long lastTime = System.nanoTime(); long lastTimer = System.currentTimeMillis(); double ns = 1000000000.0 / 60.0; double delta = 0; frames = 0; int updates = 0; requestFocus(); while (running) { long now = System.nanoTime(); delta += (now - lastTime) / ns; lastTime = now; if (delta >= 1) { update(); updates++; delta--; timer++; } render(); frames++; while (System.currentTimeMillis() - lastTimer > 1000) { lastTimer += 1000; frame.setTitle("Space Invader " + updates + " ups, " + frames + " fps"); frames = 0; updates = 0; } } } private void render() { BufferStrategy bs = getBufferStrategy(); if (bs == null) { createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); render.render(g); g.dispose(); bs.show(); } public void updateKey() { left = keys[KeyEvent.VK_LEFT]; right = keys[KeyEvent.VK_RIGHT]; use = keys[KeyEvent.VK_SPACE] || keys[KeyEvent.VK_ENTER]; back = keys[KeyEvent.VK_ESCAPE]; } public void keyPressed(KeyEvent e) { keys[e.getKeyCode()] = true; } public void keyReleased(KeyEvent e) { keys[e.getKeyCode()] = false; } public void keyTyped(KeyEvent e) { } public void releaseAll() { left = right = use = back = false; } private void update() { updateKey(); MovementPlayer(); Shooting(); SpawnAliens(); colision(); } private void colision() { if (aliens.size() > 0 && bullets.size() > 0) { for (int i = 0; i < bullets.size(); i++) { for (int i2 = 0; i2 < aliens.size(); i2++) { if (bullets.get(i).getBounds().intersects(aliens.get(i2).getBounds())) { System.out.println("collision"); bullets.remove(i); aliens.remove(i2); } } } } } private void SpawnAliens() { int ALIENSWIDTH = 8; int ALIENSHEIGHT = 4; if (timer >= timed) { if (back) { timer = 0; for (int i = 0; i < ALIENSWIDTH; i++) { for (int i2 = 0; i2 < ALIENSHEIGHT; i2++) { aliens.add(new EntityAlien()); aliens.get(aliens.size() - 1).setHeight(10); aliens.get(aliens.size() - 1).setWidth(10); aliens.get(aliens.size() - 1).setX(i * 30 + 100); aliens.get(aliens.size() - 1).setY(i2 * 30 + 100); } } } } } private void Shooting() { if (timer >= timed) { if (use) { timer = 0; bullets.add(new EntityBullet()); bullets.get(bullets.size() - 1).setHeight(10); bullets.get(bullets.size() - 1).setWidth(10); bullets.get(bullets.size() - 1).setX(player.getX() + player.getHeight() / 2 + bullets.get(bullets.size() - 1).getWidth() / 2); bullets.get(bullets.size() - 1).setY(player.getY() - bullets.get(bullets.size() - 1).getHeight()); bullets.get(bullets.size() - 1).setVelY(3.0); } } if (bullets.size() > 0) { for (int i = 0; i < bullets.size(); i++) { bullets.get(i).incY(-bullets.get(i).getVelY()); } for (int i = 0; i < bullets.size(); i++) { if (bullets.get(i).getY() < 0 - bullets.get(bullets.size() - 1).getHeight()) { bullets.remove(i); } } } } private void MovementPlayer() { if (left && player.getX() > 0) { player.incX(-player.getVelX()); } if (right && player.getX() + player.getWidth() < WIDTH) { player.incX(player.getVelX()); } } }
Render.java
package com.spaceinvader.render; import com.spaceinvader.Main; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; public class Render { Main main; Font font = new Font("Verdana", Font.BOLD, 22); public Render() { main = new Main(); } public void render(Graphics g) { g.setColor(Color.black); g.fillRect(0, 0, Main.frame.getWidth(), Main.frame.getHeight()); g.setColor(Color.red); g.fillRect((int)Main.player.getX(), (int)Main.player.getY(), (int)Main.player.getWidth(), (int)Main.player.getHeight()); if (Main.bullets.size() > 0) { for (int i = 0; i < Main.bullets.size(); i++) { g.setColor(Color.blue); g.fillRect((int)Main.bullets.get(i).getX(), (int)Main.bullets.get(i).getY(),(int) Main.bullets.get(i).getWidth(), (int)Main.bullets.get(i).getHeight()); } } if (Main.aliens.size() > 0) { for (int i = 0; i < Main.aliens.size(); i++) { g.setColor(Color.green); g.fillRect((int)Main.aliens.get(i).getX(), (int)Main.aliens.get(i).getY(),(int) Main.aliens.get(i).getWidth(), (int)Main.aliens.get(i).getHeight()); } } } }
BaseEntity.java
package com.spaceinvader.entity; public class BaseEntity { private double x, y; private double height, width; private double velX, velY; private boolean alive; //accessor public boolean getAlive(){return alive;} public double getX(){ return x;} public double getY(){ return y;} public double getHeight(){return height;} public double getWidth(){return width;} public double getVelX(){return velX;} public double getVelY(){return velY;} //mutators public void setAlive(boolean alive){this.alive = alive;} public void setX(double x){this.x = x;} public void setY(double y){this.y = y;} public void incX(double i){this.x += i;} public void incY(double d){this.y += d;} public void setHeight(double height){this.height = height;} public void setWidth(double width){this.width = width;} public void incHeight(double i){this.height += i;} public void incWidth(double i){this.width += i;} public void setVelX(double velX){this.velX = velX;} public void setVelY(double velY){this.velY = velY;} BaseEntity(){ setAlive(false); setX(0); setY(0); setWidth(0); setHeight(0); } }
EntityAlien.java
EntityBullet.java
EntityPlayer.java