java - 蛇类游戏中的 IndexOutOfBoundsException

标签 java

我应该让蛇形矩形沿着屏幕的一侧移动。然而它保持在原位,当我运行它时,我还收到 IndexOutOfBoundsException 错误。我相信这与我的 Render.java 文件中的 for 循环有关。这是代码:

LewisProject.java

package lewisproject;

import java.awt.Point;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;

public class LewisProject implements ActionListener {

    public Toolkit toolkit;

    public JFrame jframe;
    public Render render;
    public static LewisProject lewisproject;

    public Timer timer = new Timer(20, this);

    public ArrayList<Point> catParts = new ArrayList<>();

    //head is the position of the first cat and the mouse is what you need to get in order to get more cats. 
    public Point head, mouse;

    public Random random;

    public boolean over = false;

    public Dimension dim;
    public int ticks = 0, direction = DOWN, score, tailLength;

    public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;

    public LewisProject() {

        //Grabs the screen size
        dim = Toolkit.getDefaultToolkit().getScreenSize();
        toolkit = Toolkit.getDefaultToolkit();
        jframe = new JFrame("Cat Assassin");
        jframe.setVisible(true);
        //Sets Size of screen
        jframe.setSize(800, 800);
        //Centers the JFrame to the middle of your screen
        jframe.setLocation(dim.width / 2 - jframe.getWidth() / 2, dim.height / 2 - jframe.getHeight() / 2);
        jframe.add(render = new Render());
        //Closes the JFrame when you hit X.
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        head = new Point(0, 0);
        //Puts a mouse in a random area of the screen.
        mouse = new Point(dim.width / SCALE, dim.height / SCALE);
        random = new Random();
        timer.start();

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        //re renders square 
        render.repaint();
        ticks++;
        //These if statements are self explanatory. They add the point through the x and y axis depending on what direction the cats are going. 
        if (ticks % 10 == 0 && head != null && over != true) {
            catParts.add(new Point(head.x, head.y));
            if (direction == UP) {
                if (head.y - 1 > 0) {
                    head = new Point(head.x, head.y - 1);
                } else {
                    over = true;
                }
            }
        }
        if (direction == DOWN) {
            if (head.y + 1 < dim.height / SCALE) {
                head = new Point(head.x, head.y + 1);
            }
        } else {
            over = true;
        }

        if (direction == LEFT) {
            if (head.x + 1 > 0) {
                head = new Point(head.x - 1, head.y);
            } else {
                over = true;
            }
        }

        if (direction == RIGHT) {
            if (head.x + 1 < dim.width / SCALE) {

                head = new Point(head.x + 1, head.y);
            } else {
                over = true;
            }
        }
        catParts.remove(0);
        head = catParts.get(catParts.size() - 1);
        if (mouse != null) {
            if (head.x == mouse.x && head.y == mouse.y) {
                score++;
                tailLength++;
                mouse.setLocation(dim.width / SCALE, dim.height / SCALE);
            }
        }
    }


    public static void main(String[] args) {
        lewisproject = new LewisProject();
    }
}

Render.java

package lewisproject;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JPanel;

public class Render extends JPanel{
   //It's more of a light gray but whatever.
    public static Color white = new Color(15132390);

//used @Override to get the error to be quiet while I work.   
    @Override
    protected void paintComponent(Graphics g){
        //Calls the super class (JPanel) and paints graphics.
        super.paintComponent(g);
        //Sets the Color of Rectangle to black and sets dimensions
        g.setColor(white);
        g.fillRect(0, 0, 800, 800);
        LewisProject lewisproject = LewisProject.lewisproject;
        g.setColor(Color.BLUE);
        for (Point point : lewisproject.catParts){

            g.fillRect(point.x * LewisProject.SCALE, point.y * LewisProject.SCALE, LewisProject.SCALE, LewisProject.SCALE);

        }
            g.fillRect(lewisproject.head.x * LewisProject.SCALE, lewisproject.head.y * LewisProject.SCALE, LewisProject.SCALE, LewisProject.SCALE);

    }
}

错误

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:638)
    at java.util.ArrayList.remove(ArrayList.java:477)
    at lewisproject.LewisProject.actionPerformed(LewisProject.java:101)
    at javax.swing.Timer.fireActionPerformed(Timer.java:313)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我相信我收到此错误是因为 Render.java 中的 for 循环未正确结束。有没有人有好的解决办法?

最佳答案

在尝试删除或获取项目之前,您需要检查 catParts 集合的大小:

if (!catParts.isEmpty()) {
    catParts.remove(0);
}
if (!catParts.isEmpty()) {
    head = catParts.get(catParts.size() - 1);
    if (mouse != null) {
        if (head.x == mouse.x && head.y == mouse.y) {
            score++;
            tailLength++;
            mouse.setLocation(dim.width / SCALE, dim.height / SCALE);
        }
    }
}

关于java - 蛇类游戏中的 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27307799/

相关文章:

java - 如何配置 smarteist/Android-Image-Slider?

java - Libgdx 中的 setCenter 在 nightlies 包中不可用,并且 setOrigin 无法正常工作?

java - 当框架小于表格时,GridBagLayout 导致 JScrollPane 消失

java - 安排任务

java - 如何将二进制字符串转换为 int 数组?

java - 主 Activity 回收器 View 上未设置行布局

java - "set"是Java的关键字吗?

java - 每次打开时都会调用抽屉导航菜单方法?

java - 在子类上使用 Mockito spy 时调用抽象类中的可变长度参数方法抛出异常

java - 将列表写入 .csv 文件