gpt4 book ai didi

Java程序总是崩溃

转载 作者:行者123 更新时间:2023-12-02 03:51:52 24 4
gpt4 key购买 nike

我正在用java制作一个太空射击游戏。当我运行该程序并玩一分钟时,它可以工作,但在中途,一旦子弹接触到敌人,程序就会崩溃并给出以下错误消息:

Exception in thread "Thread-1" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at main.Game.checkCollisions(Game.java:185)
at main.Game.tick(Game.java:118)
at main.Game.run(Game.java:95)
at java.lang.Thread.run(Thread.java:745)

是什么导致了这次崩溃?我该如何修复它?

这是发生错误的代码区域:

void checkCollisions() {
for(int i = 0; i < Shoot.allBullets.size(); i++){
for(int j = 0; j < Enemy.allEnemies.size(); j++){
if(new Rectangle((int)Shoot.allBullets.get(i).x, (int)Shoot.allBullets.get(i).y, 23, 48).intersects(new Rectangle((int) Enemy.allEnemies.get(j).x, (int) Enemy.allEnemies.get(j).y, 64, 64))) {
bulletIntersectsEnemy(i, j);
}
}
}
}

这是其余的代码:

游戏.java

package main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable, KeyListener {

//declare values
private static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Space Shooter";

private boolean running = false;
private Thread thread;

private Player player;

private BufferedImage playerImage;
private BufferedImage bulletImage;
private BufferedImage enemyImage;

int playerx;
int playery;

int round = 1;

public Game() {
//
player = new Player((WIDTH/2)-32, HEIGHT-200);

//allocates all file resources
try {
playerImage = ImageIO.read(this.getClass().getResourceAsStream("/resources/player.png"));
bulletImage = ImageIO.read(this.getClass().getResourceAsStream("/resources/bullet.png"));
enemyImage = ImageIO.read(this.getClass().getResourceAsStream("/resources/enemy.png"));
} catch (IOException e) {
e.printStackTrace();
}

addKeyListener(this);
setFocusable(true);
requestFocusInWindow();
}

//starts thread
private synchronized void start() {
if (running)
return;

running = true;
thread = new Thread(this);
thread.start();
}

//stops thread
private synchronized void stop() {
if (!running)
return;

running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(1);
}

@Override
//game loop
public void run() {
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if (delta > 1) {
tick();
updates++;
delta--;
}
render();
frames++;

if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " TICKS, " + frames + " FPS");
updates = 0;
frames = 0;
}
}
stop();
}

//updates sprite locations
public void tick() {
playerx = player.getX();
playery = player.getY();
Shoot.updateBullets();
Enemy.updateEnemies();
checkCollisions();
}

//renders sprites
public void render() {
//setting up triple-buffering
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();

//////////////////////////////////

g.setColor(Color.BLACK); g.fillRect(0,0,getWidth(), getHeight());
g.drawImage(playerImage, playerx, playery, this);

if (Shoot.allBullets.size() != 0) {
for (int i = 0; i < Shoot.allBullets.size(); i++) {
int bulletx = (int) Shoot.allBullets.get(i).x;
int bullety = (int) Shoot.allBullets.get(i).y;
g.drawImage(bulletImage, bulletx + 21, bullety, this);
}
}

if (Enemy.allEnemies.size() != 0) {
for (int i = 0; i < Enemy.allEnemies.size(); i++) {
int enemyx = (int) Enemy.allEnemies.get(i).x;
int enemyy = (int) Enemy.allEnemies.get(i).y;
g.drawImage(enemyImage, enemyx, enemyy, this);
}
} else {
Enemy.createEnemies(round);
round++;
}

//////////////////////////////////

g.dispose();
bs.show();
}

@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
Shoot.addBullet(player.getX(), player.getY());
}
}

public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP) {
player.setY(playery -= 20);
} else if (key == KeyEvent.VK_DOWN) {
player.setY(playery += 20);
} else if (key == KeyEvent.VK_RIGHT) {
player.setX(playerx += 40);
} else if (key == KeyEvent.VK_LEFT) {
player.setX(playerx -= 40);
}
}

void checkCollisions() {
for(int i = 0; i < Shoot.allBullets.size(); i++){
for(int j = 0; j < Enemy.allEnemies.size(); j++){
if(new Rectangle((int)Shoot.allBullets.get(i).x, (int)Shoot.allBullets.get(i).y, 23, 48).intersects(new Rectangle((int) Enemy.allEnemies.get(j).x, (int) Enemy.allEnemies.get(j).y, 64, 64))) {
bulletIntersectsEnemy(i, j);
}
}
}
}

void bulletIntersectsEnemy(int bulletIndex, int enemyIndex) {
Shoot.allBullets.remove(bulletIndex);
Enemy.allEnemies.remove(enemyIndex);
}

/*
public void manageCollisions() {
for (int i = 0; i < Shoot.allBullets.size(); i++) {
for (int e = 0; e < Enemy.allEnemies.size(); e++) {
if (Shoot.allBullets.get(i).x > Enemy.allEnemies.get(e).y) {
if (Shoot.allBullets.get(i).x < Enemy.allEnemies.get(e).y) {
if (Shoot.allBullets.get(i).y == Enemy.allEnemies.get(e).y + 64) {
System.out.println("Collision Detected!");
}
}
}
}
}
}
*/

public void keyTyped(KeyEvent e) {}

public static void main(String[] args) {

Game game = new Game();
JFrame frame = new JFrame(TITLE);

frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(game);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);

game.start();

}

}

敌人.java

package main;

import java.util.ArrayList;

public class Enemy {

static ArrayList<Point> allEnemies = new ArrayList<Point>();

public static ArrayList<Point> createEnemies(int round) {
for (int i = 0; i < round; i++) {
Point newEnemyLocation = new Point((int) (Math.random()*Game.WIDTH), 0);
double newx = validate(newEnemyLocation.x);
newEnemyLocation.x = newx;
allEnemies.add(newEnemyLocation);
}
return allEnemies;
}

private static double validate(double x) {
for (int i = 0; i < allEnemies.size();i++) {
Point otherEnemy = allEnemies.get(i);
if (x > otherEnemy.x) {
if (x < (otherEnemy.x - 64)) {
x += 200;
}
}
}
if (x > Game.WIDTH - 64) {
x -= 64;
}
if (x < 64) {
x += 64;
}
return x;
}

public static ArrayList<Point> updateEnemies() {
if (allEnemies.size() != 0) {
for (int i = 0; i < allEnemies.size(); i++) {
Point enemyLocation = allEnemies.get(i);
if (enemyLocation.y <= Game.HEIGHT) {
allEnemies.get(i).y += 1;
} else {
allEnemies.remove(i);
}
}
}
return allEnemies;
}

}

Shoot.java

package main;

import java.util.ArrayList;

public class Shoot {

static ArrayList<Point> allBullets = new ArrayList<Point>();

public static void addBullet(int x, int y) {
allBullets.add(new Point(x, y));
}

public static ArrayList<Point> getAllBulletPosistions() {
return allBullets;
}

public static void updateBullets() {
if (allBullets.size() != 0) {
for (int i = 0; i < allBullets.size(); i++) {
allBullets.get(i).y -= 5;
}
}
}

}

Player.java

package main;

public class Player {

int x, y;

public Player(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

}

最佳答案

void checkCollisions() {
for(int i = 0; i < Shoot.allBullets.size(); i++){
for(int j = 0; j < Enemy.allEnemies.size(); j++){
if(new Rectangle((int)Shoot.allBullets.get(i).x, (int)Shoot.allBullets.get(i).y, 23, 48).intersects(new Rectangle((int) Enemy.allEnemies.get(j).x, (int) Enemy.allEnemies.get(j).y, 64, 64))) {
bulletIntersectsEnemy(i, j);
}
}
}
}

void bulletIntersectsEnemy(int bulletIndex, int enemyIndex) {
Shoot.allBullets.remove(bulletIndex);
Enemy.allEnemies.remove(enemyIndex);
}

问题在于你实际上过早地取出了子弹。假设您只发射一颗子弹,并且有三个敌人。它击中了第一个。

现在您从 Shoot.allBullets 列表中删除了子弹,但您的内部 for 循环尚未完成。它仍然会继续尝试看看是否

Shoot.allBullets.get(0) 

(i 仍然是 0)与其他敌人相交。只是 Bullet 不再存在,所以你会得到这个异常。

关于Java程序总是崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35807825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com