Snake Java增长,移动和碰撞问题

时间:2018-03-06 00:37:44

标签: java user-interface collision move

我在蛇的游戏中工作了很长一段时间,但在运动,蛇的生长和碰撞方面遇到了很多问题。如果您可以帮助调试我的代码,我将不胜感激。我目前的问题是蛇的动作很尴尬,并且一次不能向上/向下/向右/向左移动一个空间。虽然蛇的身体应该跟随前一段,但是蛇并没有做那。此外,我的蛇的生长(一旦吃掉食物)总会导致蛇的身体出现奇怪的空隙。最后,我的蛇在与边界,本身和食物碰撞时似乎总是有问题。我仍然是Java的新手,我的风格非常糟糕。此外,这是我第一次使用GUI的实验。非常感谢你们!

这是蛇类:

    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.Timer;
    import java.util.Random;
    import java.util.ArrayList;

    public class snake extends JFrame implements ActionListener {
    public static int points = 0;
    public static int direction;
    public static int delay = 50 ;
    public static int rfood;
    public static int cfood;
    public static ArrayList<Integer> X = new ArrayList<>();
    public static ArrayList<Integer> Y = new ArrayList<>();
    public static int length;
    public static Timer timer;
    public static boolean run = false;



    snake() {
        addKeyListener(new KEY());
        setFocusable(true);
        setSize(800, 800);
        getContentPane().setBackground(Color.red);
        setVisible(true);
        startGame();
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (run == true) {
            drawFood(g);
            drawSnake(i, g);


        }
        else{
            GameOver(g);
        }
    }


    public void drawSnake(int i, Graphics s) {
        s.setColor(Color.yellow);
        for (int i = length - 1; i >= 0; i--) {
                s.fillRect(bodyX.get(i), bodyY.get(i), 20, 20);
            }

    }

    public void drawFood(Graphics g) {
        g.setColor(Color.blue);
        gA.fillRect(cf, rf, 20, 20);
    }

    public void GameOver(Graphics g) {
        g.setColor(Color.red);
        g.drawString("Game Over", 300, 300);
    }

    public static void snakePosition() {
        X.clear();
        Y.clear();
        run = true;
        length = 2;
        X.add(20);
        Y.add(20);
        X.add(30);
        Y.add(20);
    }

    public static void placeFood() {
        Random rand = new Random();
        int r = rand.nextInt(19) * 40;
        int c = rand.nextInt(19) * 40;
        while ((r == Y.get(0)) && (c == X.get(0))) {
            r = rand.nextInt(19) * 40;
            c = rand.nextInt(19) * 40;
        }
        rfood = r;
        cfood = c;
    }

    public static void collision() {
        for (int i = 1; i < length; i++) {
            if ((X.get(0) == X.get(i) && (Y.get(0) == Y.get(i)))) {
                points = 0;
                snakePosition();
            }
        }

        if ((X.get(0) > 800) || (X.get(0) < 0) || (Y.get(0) > 800) || (Y.get(0) < 0)) {
            points = 0;
            resetSnakePosition();

        }
    }

    public static void collideFood() {
        if ((X.get(0) == cfood) && (Y.get(0) == rfood)) {

            X.add(0);
            Y.add(0);
            points++;
            length++;
            placeFood();
        }
    }



    public static void move() {
        for (int i = 0; i < length - 1; i++) {
            X.set(i + 1, X.get(i));
            Y.set(i + 1, Y.get(i));
            if (direction == 0) {
                Y.set(0, Y.get(0) - 20);
            }
            if (direction == 1) {
                X.set(0, X.get(0) + 20);
            }

            if (direction == 2) {
                Y.set(0, Y.get(0) + 20);
            }
            if (direction == 3) {
                X.set(0, X.get(0) - 20);

            }


        }

    }



    public void startGame() {
        placeFood();
        timer = new Timer(delay, this);
        timer.start();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (run == true) {
            collideFood();
            move();
        }
        repaint();
    }

    public static void main(String[] args) {
        snakePosition();
        new snake();
    }

}

这是我的KEY课程:

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class KEY extends KeyAdapter {
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        int key = e.getKeyCode();
        if ((key == KeyEvent.VK_UP) || (key == KeyEvent.VK_W)) {
            if (snake.direction != 2) {
                snake.direction = 0;
            }
        }
        if ((key == KeyEvent.VK_RIGHT) || (key == KeyEvent.VK_D)) {
            if (snake.direction != 3) {
                snake.direction = 1;
            }
        }
        if ((key == KeyEvent.VK_DOWN) || (key == KeyEvent.VK_S)) {
            if (snake.direction != 0) {
                snake.direction = 2;
            }
        }
        if ((key == KeyEvent.VK_LEFT) || (key == KeyEvent.VK_A)) {
            if (snake.direction != 1) {
                snake.direction = 3;
            }
        }

    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

}

0 个答案:

没有答案