Game Snake: how to stop the game when the snake eats itself

I make a snake game, and it works very well, except that part of the snake is itself.

Here is my code:

Obstacle class

class Obstacle {
    int x, y;

    public Obstacle(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public void draw(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(x, y, 10, 10);
    }
}

Food grade

class Food {
    int x, y;

    public Food(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public void draw(Graphics g) {
        g.setColor(Color.red);
        g.fillRect(x, y, 10, 10);
    }
}

Body class

class Body {
    int x, y;

    public Body(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void draw(Graphics g) {
        g.setColor(Color.green);
        g.fillRect(x, y, 10, 10);
        g.setColor(Color.black);
        g.drawRect(x, y, 10, 10);
    }
}

Screen class

class Screen extends JPanel implements ActionListener, KeyListener {
    int WIDTH = 800, HEIGHT = 800;
    public static int time = 50;
    Timer t;
    int x = 400, y = 400;
    boolean right = false, left = false, up = false, down = false;
    public static int grade = 0;

    LinkedList<Food> foods;
    LinkedList<Body> snake;
    LinkedList<Obstacle> block;

    Body b;
    Food f;
    Obstacle k;

    Random rand = new Random();
    int size = 3;

    public Screen() {

        init();
    }


    public void init() {

        block = new LinkedList<Obstacle>();
        foods = new LinkedList<Food>();
        snake = new LinkedList<Body>();

        level();
        t = new Timer(time, this);
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public void tracking() {

        if (x < 0 || x > 800 || y < 0 || y > 800) {
            hit();
        }
    }

    public void level() {
        Object[] options = {"Level 1", "Level 2", "Level 3"};

        int option = JOptionPane.showOptionDialog(null, "Please Select a Level", "Level Option",
        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);

        switch (option) {
        case JOptionPane.YES_OPTION: 
            System.out.println("Before: " + time);
            time = 50;
            System.out.println("After: " + time);
            break;
        case JOptionPane.NO_OPTION: 
            System.out.println("Before: " + time);
            time = 30;
            System.out.println("After: " + time);
            break;
        case JOptionPane.CANCEL_OPTION: 
            System.out.println("Before: " + time);
            time = 10;
            System.out.println("After: " + time);
            break;
        } 
    }

    public void hit() {
        t.stop();
        if (JOptionPane.showConfirmDialog(null, "Snake: You hit the wall! \nRestart the Game?", "OUCH!!!", 
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
            x = 400;
            y = 400;
            while (snake.size() != 3) {
                snake.remove();
                size = 3;
            }
            init();

        } else {
            System.exit(0);
        }
    }

    public void move() {
        if (right) x += 10;
        if (left) x -= 10;
        if (up) y -= 10;
        if (down) y += 10;
    }

    public void snake() {
        if (snake.size() == 0) {
            b = new Body(x, y);
            snake.add(b);
        }
        move();
        b = new Body(x, y);
        snake.add(b);

        if (snake.size() > size) {
            snake.remove(0);
        }
    }

    public void food() {
        if (foods.size() == 0) {
            int ok = 0;
            int rx = 0;
            int ry = 0;
            while (ok != 1) {
                rx = (int) (rand.nextInt(700) + 1);
                ry = (int) (rand.nextInt(700) + 1);

                if ((rx % 10) == 0 && (ry % 10) == 0) {
                    ok = 1;
                } 
            }
            f = new Food(rx, ry);
            foods.add(f);
        }

        if (snake.get(snake.size() - 1).x == foods.get(0).x && snake.get(snake.size() - 1).y == foods.get(0).y) {
            foods.remove();
            size++;
            grade += 100;
        }
    }

    public void block() {
        if (block.size() == 0) {
            int ok = 0;
            int rx = 0;
            int ry = 0;
            while (ok != 15) {
                rx = (int) (rand.nextInt(750) + 1);
                ry = (int) (rand.nextInt(750) + 1);

                if ((rx % 10) == 0 && (ry % 10) == 0) {
                    k = new Obstacle(rx, ry);
                    block.add(k);
                    int temp = 10;

                    int r = (int) (rand.nextInt(2) + 1);

                    for (int i = 0; i < 5; i++) {

                        if (r == 1) {
                            k = new Obstacle(rx + temp , ry);
                        } else if (r == 2) {
                            k = new Obstacle(rx , ry + temp);
                        }
                        block.add(k);

                        if ((k.x == foods.get(0).x && k.y == foods.get(0).y) || 
                            (k.x == 400 && k.y == 400) || 
                            ((k.x >= 350 && k.x <= 450) && (k.y >= 350 && k.y <= 450)))  {
                            block.remove(k);
                        }

                        temp += 10;
                    }
                    ok++;
                } 
            }
        }

        for (int i = 0; i < block.size(); i++) {
            if (snake.get(snake.size() - 1).x == block.get(i).x && snake.get(snake.size() - 1).y == block.get(i).y) {
                hit();
            }
        }
    }

    public void update() {
        snake();
        food();
        block();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.BLACK);
        for (int i = 1; i < WIDTH / 10; i++) {
            g.drawLine(i * 10, 0, i * 10, HEIGHT);
        }
        for (int i = 1; i < HEIGHT / 10; i++) {
            g.drawLine(0, i * 10, WIDTH, i * 10);
        }

        for (int i = 0; i < snake.size(); i++) {
            snake.get(i).draw(g);
        }

        for (int i = 0; i < foods.size(); i++) {
            foods.get(i).draw(g);
        }

        for (int i = 0; i < block.size(); i++) {
            block.get(i).draw(g);
        } 
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        update();
        tracking();
        repaint(); 
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_RIGHT && left == false) { 
            up = false;
            down = false;
            right = true;

        }
        if (key == KeyEvent.VK_LEFT && right == false) { 
            up = false;
            down = false;
            left = true;
        }
        if (key == KeyEvent.VK_UP && down == false) {
            left = false;
            right = false;
            up = true;
        }
        if (key == KeyEvent.VK_DOWN && up == false) {
            left = false;
            right = false;
            down = true;
        } 
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

and main class SnakeGame

public class SnakeGame extends JFrame {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        Screen s = new Screen();
        f.add(s);
        f.setSize(810, 810);
        f.setVisible(true);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

So, in the beginning I decided to make a comparison when the head of a snake eats its tails, but as my snake moves; tails x and y follow the head x and y, so the comparison is always true.

I also think about when the snake changes its direction and then does some code to track the xy coordinates and xy tail coordinates, but I have no idea how to step

Are there other ways to make the game stop when the snake eats itself?

+4
source share
1 answer

move();, x y, , "". , LinkedList<Body> snake; .

move(); "" , , .

- :

        for (int i = 0; i < snake.size(); i++) {

        if ((x == snake.get(i).x) && y == snake.get(i).y) {
            hit();
        }
    }

hit();, "head x y x y, (, move(); ).

Edit:

:

boolean started = false; Screen class:

snake(), :

public void snake() {

if (snake.size() == 0) {
    b = new Body(x, y);
    snake.add(b);

}
move();

if (started) {

    for (int i = 0; i < snake.size(); i++) {

        if ((x == snake.get(i).x) && y == snake.get(i).y) {
            hit();
        }
    }

}
b = new Body(x, y);
snake.add(b);

if (snake.size() > size) {
        snake.remove(0);
}

}

started = true; food() ( , , !):

public void food() {
    if (foods.size() == 0) {
        int ok = 0;
        int rx = 0;
        int ry = 0;
        while (ok != 1) {
            rx = (int) (rand.nextInt(700) + 1);
            ry = (int) (rand.nextInt(700) + 1);

            if ((rx % 10) == 0 && (ry % 10) == 0) {
                ok = 1;
            }
        }
        f = new Food(rx, ry);
        foods.add(f);
    }

    if (snake.get(snake.size() - 1).x == foods.get(0).x
            && snake.get(snake.size() - 1).y == foods.get(0).y) {
        foods.remove();
        size++;
        grade += 100;
        started = true;
    }
}

!

The snake itself strikes

+1

Source: https://habr.com/ru/post/1598995/


All Articles