Code:
import java.util.ArrayList;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
public class Cardrive extends BasicGame {
private Image bg; // Attribut für das Hintergrundbild
private ArrayList<Car> autos = new ArrayList<Car>();
private Player player; // Der Spieler
private int neuesAutoTimer = 0;
private boolean gameOver = false; // Die GameOver Variable
private Input input; // Spieler Input
@Override
public void init(GameContainer gc) throws SlickException {
bg = new Image("bg.png");
player = new Player(gc.getInput());
input = gc.getInput();
}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
neuesAutoTimer += delta;
player.bewegen(delta);
if (neuesAutoTimer >= 750 && gameOver == false) {
autos.add(new Car((int) (Math.random() * (2 - 0) + 0)));
neuesAutoTimer = 0;
}
for (Car auto_now : autos) {
auto_now.bewegen(delta);
}
if (autos.size() > 0 ) {
for (Car auto_now : autos) {
if (player.getHitbox().intersects(auto_now.getHitbox())) {
autos.remove(auto_now);
gameOver = true;
}
}
}
if (input.isKeyPressed(Input.KEY_ENTER) && gameOver == true) {
autos.clear();
neuesAutoTimer = 0;
gameOver = false;
}
}
@Override
public void render(GameContainer gc, Graphics gfx) throws SlickException {
// Der Hintergrund wird gezeichnet
bg.draw();
// Alle Autos in der Liste werden gezeichnet
for (int i = 0; i < autos.size(); i++) {
Car auto = autos.get(i);
auto.zeichnen();
gfx.draw(auto.getHitbox());
}
// Den Spieler mit der Hitbox zeichen
player.zeichnen();
gfx.draw(player.getHitbox());
// Anzeige für den momentanen delta-Wert
gfx.drawString("delta: " + neuesAutoTimer, 25, 50);
// Anzeige für die momentanen Autos auf dem Bildschirm
gfx.drawString("Autos: " + autos.size(), 25, 75);
// Gameover anzeigen
if (gameOver) {
gfx.setColor(Color.red);
gfx.drawString(
"Du hast verloren!! \nDrücke Enter für einen Neustart!",
200, 300);
} else {
gfx.setColor(Color.white);
}
}
public Cardrive(String title) {
super(title);
}
public static void main(String[] args) throws SlickException {
AppGameContainer myGame = new AppGameContainer(
new Cardrive("Car Drive"));
myGame.setDisplayMode(600, 800, false);
myGame.start();
}
}
Sobald man das erste Auto trifft, ist Game Over. Soweit so gut.
Dann hören die Autos auf zu spawnen(auch gut), dann läuft man absichtlich in das letzte Auto(auch noch okay), dabei crasht das Spiel mit einer Exception(nicht gut).
Hier die detaillierte Ausgabe:
Code:
Sun Jan 11 14:11:15 CET 2015 ERROR:null java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at Cardrive.update(Cardrive.java:64) at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:663) at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411) at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321) at Cardrive.main(Cardrive.java:129) Sun Jan 11 14:11:15 CET 2015 ERROR:Game.update() failure - check the game code. org.newdawn.slick.SlickException: Game.update() failure - check the game code. at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:669) at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411) at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321) at Cardrive.main(Cardrive.java:129)