为什么执行此代码时没有出现蛇?



在我将 Rectangle2D 对象放入 ArrayList 之前,程序运行良好,只是每当我改变方向时,蛇都会旋转而不是弯曲。现在蛇甚至没有出现,我的控制台给了我错误,如疯狂程序员提供的那样:

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.get(ArrayList.java:414)
    at javaapplication968.JavaApplication968$Display.actionPerformed(JavaApplication968.java:110) 

我知道该问题与 ArrayList 有关,但我该如何解决它?

这是我的班级:

public class Display extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(1, this);
double xCoord = 50;
double yCoord = 50;
double xvel = 0;
double yvel = .01;
double ranx, xtemp;
double rany, ytemp;
int eaten = 0;
ArrayList<Rectangle2D> rects = new ArrayList<Rectangle2D>();
ArrayList<Double> xloc = new ArrayList<Double>();
ArrayList<Double> yloc = new ArrayList<Double>();
public Display(int xsize, int ysize) {
    t.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    xtemp = xsize;
    ranx = (xtemp - 15) * Math.random();
    ytemp = ysize;
    rany = (ytemp - 15) * Math.random();
    formSnake();
}
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    // Adds snake
    g2.setColor(Color.YELLOW);
    for (int i = 0; i < rects.size(); i++) {
        g2.fill(rects.get(i));
    }
    // Adds fruit at random location
    g2.setColor(Color.BLUE);
    g2.fill(new Ellipse2D.Double(ranx, rany, 15, 15));
    setBackground(Color.BLACK);
}
@Override
public void actionPerformed(ActionEvent e) {
    repaint();
    // Replaces each rectangle location with one before it
    for (int i = rects.size() - 1; i > 0; i--) {
        xloc.set(i, xloc.get(i - 1));
        yloc.set(i, yloc.get(i - 1));
    }
    // Sets new head location
    xloc.set(0, xloc.get(0) + xvel);
    yloc.set(0, yloc.get(0) + yvel);
    getEaten();
}
// Adds initial 3 2DRectangles to snake at start of game
public void formSnake() {
    for (int i = 0; i < rects.size(); i++) {
        xloc.add(xCoord);
        yloc.add(yCoord - 16 * i);
        rects.add(rects.size(),
                new Rectangle2D.Double(xloc.get(i), yloc.get(i), 15, 15));
    }
}
// Adds 1 2DRectangle to the front of the snake every time it eats
public void extend() {
    // Vertical
    if (xvel == 0) {
        if (yvel <= 0) { // Up
            xloc.add(xloc.get(xloc.size() - 1));
            yloc.add(yloc.get(yloc.size() - 1) - 16);
            rects.add(new Rectangle2D.Double(xloc.get(xloc.size() - 1),
                    yloc.get(yloc.size() - 1), 15, 15));
        } else
            // Down
            xloc.add(xloc.get(xloc.size() - 1));
        yloc.add(yloc.get(yloc.size() - 1) + 16);
        rects.add(new Rectangle2D.Double(xloc.get(xloc.size() - 1), yloc
                .get(yloc.size() - 1), 15, 15));
    }
    // Horizontal
    else if (yvel == 0) {
        if (xvel <= 0) { // Left
            xloc.add(xloc.get(xloc.size() - 1) - 16);
            yloc.add(yloc.get(yloc.size() - 1));
            rects.add(new Rectangle2D.Double(xloc.get(xloc.size() - 1),
                    yloc.get(yloc.size() - 1), 15, 15));
        } else
            // Right
            xloc.add(xloc.get(xloc.size() - 1) + 16);
        yloc.add(yloc.get(yloc.size() - 1));
        rects.add(new Rectangle2D.Double(xloc.get(xloc.size() - 1), yloc
                .get(yloc.size() - 1), 15, 15));
    }
}
public void getEaten() {
    if (Math.abs(xCoord - ranx) < 15 && Math.abs(yCoord - rany) < 15) {
        rany = ytemp * Math.random();
        ranx = xtemp * Math.random();
        eaten++;
        extend();
    }
}
//Directions
public void up() {
    yvel = -0.5;
    xvel = 0;
}
public void down() {
    yvel = 0.5;
    xvel = 0;
}
public void left() {
    xvel = -0.5;
    yvel = 0;
}
public void right() {
    xvel = 0.5;
    yvel = 0;
}
//Just for testing purposes
public void stop() {
    xvel = 0;
    yvel = 0;
}
//Direction implementation
@Override
public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();
    if (code == KeyEvent.VK_UP || code == KeyEvent.VK_NUMPAD8) {
        up();
    }
    if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_NUMPAD2) {
        down();
    }
    if (code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_NUMPAD6) {
        right();
    }
    if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_NUMPAD4) {
        left();
    }
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
    }
}

你似乎得到了一个

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.get(ArrayList.java:414)
    at javaapplication968.JavaApplication968$Display.actionPerformed(JavaApplication968.java:110)

xloc.set(0, xloc.get(0) + xvel);,因为没有元素xloc List

更改您的formSnake方法以完全按照它所说的去做......// Adds initial 3 2DRectangles to snake at start of game而不是使用 rects.size() ,这将在调用时0...

    // Adds initial 3 2DRectangles to snake at start of game
    public void formSnake() {
        for (int i = 0; i < 3; i++) {
            xloc.add(xCoord);
            yloc.add(yCoord - 16 * i);
            rects.add(rects.size(),
                            new Rectangle2D.Double(xloc.get(i), yloc.get(i), 15, 15));
        }
    }

相关内容

  • 没有找到相关文章

最新更新