3

我遇到了一个(至少对我来说)非常奇怪的情况。
我正在尝试重写蛇,并且移动进展顺利,只有蛇在吃自己,虽然我正在删除 1 段,并添加 1 (x + 1 - 1 = x?) 但蛇消失了,而 Arraylist 保持相同的尺寸(打印它)。

计算蛇位置的函数(我认为这是导致它的原因):

private void move() {
    System.out.println(position.size());
    Point toAdd = position.get(position.size() - 1);
    position.remove(0);
    if(dir == 1)
        toAdd.y -= 5;
    else if(dir == 2) 
        toAdd.x -= 5;
    else if(dir == 3)
        toAdd.x += 5;
    else if(dir == 4)
        toAdd.y += 5;

    if(toAdd.x < 0) toAdd.x = 150;
    else if(toAdd.x > 150) toAdd.x = 0;

    if(toAdd.y < 0) toAdd.y = 150;
    else if(toAdd.y > 150) toAdd.y = 0;
    position.add(toAdd);
}

但是,为了使它成为一个简短的、独立的、正确的例子:

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.*;
import java.util.ArrayList;

class Snake extends JPanel implements Runnable {
    int x,y;
    boolean horizontal;
    ArrayList<Point> position = new ArrayList<Point>();
    byte dir = 3;

    public Snake(JFrame parent) {
        for(int i = 0; i < 5; i++)
            position.add(new Point(i*5 + 10, 10));

        parent.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                byte change = dir;
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_DOWN:
                        change = 4;
                        break;
                    case KeyEvent.VK_UP:
                        change = 1;
                        break;
                    case KeyEvent.VK_LEFT:
                        change = 2;
                        break;
                    case KeyEvent.VK_RIGHT:
                        change = 3;
                        break;
                }

                changeDirection(change);
            }
        });
        System.out.println("starting thread");
        new Thread(this).start();
    }

    private void changeDirection(byte change) {
        if(change != dir && (change - 2 != dir || change + 2 != dir)) {
            dir = change;
        }
    }

    public void run() {
        for(int i = 0; i != -1; i++){
            try {
                move();
                this.repaint();
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {
                System.out.println("INTERRUPTED");
            }
        }
    }

    private void move() {
        System.out.println(position.size());
        Point toAdd = position.get(position.size() - 1);
        position.remove(0);
        if(dir == 1)
            toAdd.y -= 5;
        else if(dir == 2) 
            toAdd.x -= 5;
        else if(dir == 3)
            toAdd.x += 5;
        else if(dir == 4)
            toAdd.y += 5;

        if(toAdd.x < 0) toAdd.x = 150;
        else if(toAdd.x > 150) toAdd.x = 0;

        if(toAdd.y < 0) toAdd.y = 150;
        else if(toAdd.y > 150) toAdd.y = 0;
        position.add(toAdd);
    }

    public void paintComponent(Graphics g) {
        g.clearRect(0,0,150,150);
        for(Point p : position)
            g.drawRect(p.x, p.y, 5, 5);
    }

    public static void main(String[] args) {
        Snake snake;
        JFrame f = new JFrame("Snake");
        f.setSize(150, 150);
        f.add((snake = new Snake(f)));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        f.setVisible(true);
    }
}

运动是我尝试实施的第一件事,我还没有接触到碰撞检测/食物,这不是这里的问题。

我的问题是:

为什么蛇在缩小而数组列表没有缩小,我该如何修复它,即阻止蛇除了前段之外消失?

4

1 回答 1

6

好的。这很简单!

您正在更改列表中已经存在的点,position最后您将相同的点添加到此列表中。您最终会得到越来越多具有相同位置的相同(通过引用)元素。

您应该创建新实例Point并将其添加到列表中。

Point lastPoint = position.get(position.size() - 1);
    Point toAdd = new Point(lastPoint.x, lastPoint.y);
于 2012-07-27T19:41:22.340 回答