我正在用Java开发蛇类游戏,我遇到的问题是蛇的身体部位未正确跟随头部,这类似于此问题:
这是所有发生的地方:

public class Screen extends JPanel {

    List<SnakeBody> snakeBody;

    SnakeBody snake;

    Thread thread;

    int x = 10;
    int y = 10;
    int dx = 1;
    int dy = 0;

    Boolean up = false, right = true, down = false, left = false;

    public Screen() throws InterruptedException {

        setPreferredSize(new Dimension(500, 500));
        snakeBody = new  ArrayList<SnakeBody>();
        snake = new SnakeBody();
        snake.setX(x);
        snake.setY(y);
        snakeBody.add(snake);

        SnakeBody ss = new SnakeBody();
        snakeBody.add(ss);

        setKeyBindings();
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.drawRect(snakeBody.get(0).getX(), snakeBody.get(0).getY(), 10, 10);
        g.drawRect(snakeBody.get(1).getX(), snakeBody.get(1).getY(), 10, 10);
    }

    public void move() {

        if(up) {
            snakeBody.get(0).setX(x);
            snakeBody.get(0).setY(y);

            x += dx;
            y += dy;

            snakeBody.get(1).setX(x);
            snakeBody.get(1).setY(y-10);
        }

        if(right) {
            snakeBody.get(0).setX(x);
            snakeBody.get(0).setY(y);

            x += dx;
            y += dy;

            snakeBody.get(1).setX(x+10);
            snakeBody.get(1).setY(y);
        }

        if(down) {
            snakeBody.get(0).setX(x);
            snakeBody.get(0).setY(y);

            x += dx;
            y += dy;

            snakeBody.get(1).setX(snakeBody.get(0).getX());
            snakeBody.get(1).setY(snakeBody.get(0).getY()-10);
        }

        if(left) {
            snakeBody.get(0).setX(x);
            snakeBody.get(0).setY(y);

            x += dx;
            y += dy;

            snakeBody.get(1).setX(x-10);
            snakeBody.get(1).setY(y);
        }
    }

    private void setKeyBindings() {
        ActionMap actionMap = getActionMap();
        int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
        InputMap inputMap = getInputMap(condition );

        String vkLeft = "VK_LEFT";
        String vkRight = "VK_RIGHT";
        String vkUp = "VK_UP";
        String vkDown = "VK_DOWN";

        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), vkLeft);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), vkRight);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), vkUp);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), vkDown);

        actionMap.put(vkLeft, new KeyAction(vkLeft));
        actionMap.put(vkRight, new KeyAction(vkRight));
        actionMap.put(vkUp, new KeyAction(vkUp));
        actionMap.put(vkDown, new KeyAction(vkDown));
    }

    private class KeyAction extends AbstractAction {
        public KeyAction(String actionCommand) {
            putValue(ACTION_COMMAND_KEY, actionCommand);
        }

        @Override
        public void actionPerformed(ActionEvent actionEvt) {
        if(actionEvt.getActionCommand().equals("VK_UP")) {
            dx = 0;
            dy = -1;

            snakeBody.get(1).setX(snakeBody.get(0).getX());
            snakeBody.get(1).setY(snakeBody.get(0).getY());

            up = true;
            right = false;
            down = false;
            left = false;

        }

        if(actionEvt.getActionCommand().equals("VK_RIGHT")) {
            dx = 1;
            dy = 0;

            snakeBody.get(1).setX(snakeBody.get(0).getX());
            snakeBody.get(1).setY(snakeBody.get(0).getY());
            up = false;
            right = true;
            down = false;
            left = false;
        }

        if(actionEvt.getActionCommand().equals("VK_DOWN")) {
            dx = 0;
            dy = 1;

            snakeBody.get(1).setX(snakeBody.get(0).getX());
            snakeBody.get(1).setY(snakeBody.get(0).getY());

            up = false;
            right = false;
            down = true;
            left = false;
        }

        if(actionEvt.getActionCommand().equals("VK_LEFT")) {
            dx = -1;
            dy = 0;

            snakeBody.get(1).setX(snakeBody.get(0).getX());
            snakeBody.get(1).setY(snakeBody.get(0).getY());

            up = false;
            right = false;
            down = false;
            left = true;
        }
    }
}

最佳答案

我认为您进行蛇类游戏的方法很奇怪。我能想到的最好方法是FIFO堆栈。在每一步中,您都在前面添加了一个部分,即头部,然后从尾部移除了一部分...所有其他零件保留在原处。当蛇咬食物时,您下一步不会删除一部分。

关于java - 蛇的 body 不随脑袋,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27594840/

10-10 02:23