Java Snake Game randomly starts showGameOverDialog

Have been working on this the whole day, learning as i go. Its my first somewhat complex piece of code.

Its a simple Snake game with highscores and a somewhat working menu. been watching tutorials the whole day and putting this together.

The game has a problem with the showGameOverDialog, with seemingly randomly starts no matter what tha game currently does, even in the menu and also when collecting an apple (lol).

i know its not really anythink important, but can anyone help me out with this one?

kinda want this to be my little hobbythingy and have lots of ideas to extend it with.

Thanks in advance 🙂

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SnakeGame extends JFrame {

    private CardLayout cardLayout;
    private JPanel mainPanel;
    private GamePanel gamePanel;
    private MainMenuPanel mainMenuPanel;
    private HighScorePanel highScorePanel;
    private OptionsPanel optionsPanel;
    private boolean fullscreen = false;

    public SnakeGame() {
        setTitle("Snake Game");
        setSize(800, 800);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);

        gamePanel = new GamePanel(this);
        mainMenuPanel = new MainMenuPanel(this);
        highScorePanel = new HighScorePanel(this);
        optionsPanel = new OptionsPanel(this);

        mainPanel.add(mainMenuPanel, "MainMenu");
        mainPanel.add(gamePanel, "Game");
        mainPanel.add(highScorePanel, "HighScores");
        mainPanel.add(optionsPanel, "Options");

        add(mainPanel);
        cardLayout.show(mainPanel, "MainMenu");

        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void showCard(String card) {
        cardLayout.show(mainPanel, card);
    }

    public void toggleFullscreen() {
        dispose();
        if (fullscreen) {
            setUndecorated(false);
            setExtendedState(JFrame.NORMAL);
            setSize(800, 800);
        } else {
            setUndecorated(true);
            setExtendedState(JFrame.MAXIMIZED_BOTH);
        }
        fullscreen = !fullscreen;
        setVisible(true);
    }

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

class MainMenuPanel extends JPanel {

    public MainMenuPanel(SnakeGame frame) {
        setLayout(new GridLayout(4, 1));
        JButton startButton = new JButton("Spiel starten");
        JButton highScoreButton = new JButton("Highscores");
        JButton optionsButton = new JButton("Optionen");
        JButton exitButton = new JButton("Spiel beenden");

        startButton.addActionListener(e -> frame.showCard("Game"));
        highScoreButton.addActionListener(e -> frame.showCard("HighScores"));
        optionsButton.addActionListener(e -> frame.showCard("Options"));
        exitButton.addActionListener(e -> System.exit(0));

        add(startButton);
        add(highScoreButton);
        add(optionsButton);
        add(exitButton);
    }
}

class OptionsPanel extends JPanel {

    public OptionsPanel(SnakeGame frame) {
        setLayout(new GridLayout(2, 1));
        JButton toggleFullscreenButton = new JButton("Vollbild / Fenster");
        JButton backButton = new JButton("Zurück zum Hauptmenü");

        toggleFullscreenButton.addActionListener(e -> frame.toggleFullscreen());
        backButton.addActionListener(e -> frame.showCard("MainMenu"));

        add(toggleFullscreenButton);
        add(backButton);
    }
}

class HighScorePanel extends JPanel {

    private SnakeGame frame;
    private JTextArea highScoreArea;
    private static final String HIGHSCORE_FILE = "highscore.txt";

    public HighScorePanel(SnakeGame frame) {
        this.frame = frame;
        setLayout(new BorderLayout());

        highScoreArea = new JTextArea();
        highScoreArea.setEditable(false);

        JButton backButton = new JButton("Zurück zum Hauptmenü");
        backButton.addActionListener(e -> frame.showCard("MainMenu"));

        add(new JScrollPane(highScoreArea), BorderLayout.CENTER);
        add(backButton, BorderLayout.SOUTH);

        loadHighScores();
    }

    private void loadHighScores() {
        try (BufferedReader reader = new BufferedReader(new FileReader(HIGHSCORE_FILE))) {
            highScoreArea.read(reader, null);
        } catch (IOException e) {
            highScoreArea.setText("Keine Highscores gefunden.");
        }
    }
}

class GamePanel extends JPanel implements ActionListener {

    private static final int TILE_SIZE = 25;
    private static final int BOARD_WIDTH = 30;
    private static final int BOARD_HEIGHT = 30;
    private static final int DELAY = 140;
    private static final String HIGHSCORE_FILE = "highscore.txt";

    private final List<Point> snake = new ArrayList<>();
    private Point food;
    private char direction = 'R';
    private Timer timer;
    private int score = 0;
    private int highScore = 0;
    private SnakeGame frame;

    public GamePanel(SnakeGame frame) {
        this.frame = frame;
        setPreferredSize(new Dimension(TILE_SIZE * BOARD_WIDTH, TILE_SIZE * BOARD_HEIGHT));
        setBackground(Color.BLACK);
        setFocusable(true);
        loadHighScore();
        initGame();
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_LEFT && direction != 'R') {
                    direction = 'L';
                } else if (key == KeyEvent.VK_RIGHT && direction != 'L') {
                    direction = 'R';
                } else if (key == KeyEvent.VK_UP && direction != 'D') {
                    direction = 'U';
                } else if (key == KeyEvent.VK_DOWN && direction != 'U') {
                    direction = 'D';
                }
            }
        });
        timer = new Timer(DELAY, this);
        timer.start();
    }

    private void loadHighScore() {
        try (BufferedReader reader = new BufferedReader(new FileReader(HIGHSCORE_FILE))) {
            highScore = Integer.parseInt(reader.readLine());
        } catch (IOException | NumberFormatException e) {
            highScore = 0;
        }
    }

    private void saveHighScore() {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(HIGHSCORE_FILE))) {
            writer.write(String.valueOf(highScore));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initGame() {
        snake.clear();
        snake.add(new Point(5, 5));
        snake.add(new Point(4, 5));
        snake.add(new Point(3, 5));
        score = 0;
        placeFood();
    }

    private void placeFood() {
        int x = (int) (Math.random() * BOARD_WIDTH);
        int y = (int) (Math.random() * BOARD_HEIGHT);
        food = new Point(x, y);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawBoard(g);
        drawSnake(g);
        drawFood(g);
        drawScore(g);
    }

    private void drawBoard(Graphics g) {
        g.setColor(Color.GREEN);
        for (int x = 0; x < BOARD_WIDTH; x++) {
            for (int y = 0; y < BOARD_HEIGHT; y++) {
                g.drawRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
            }
        }
    }

    private void drawSnake(Graphics g) {
        g.setColor(Color.RED);
        for (Point point : snake) {
            g.fillRect(point.x * TILE_SIZE, point.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
        }
    }

    private void drawFood(Graphics g) {
        g.setColor(Color.YELLOW);
        g.fillRect(food.x * TILE_SIZE, food.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
    }

    private void drawScore(Graphics g) {
        g.setColor(Color.WHITE);
        g.setFont(new Font("Helvetica", Font.BOLD, 20));
        g.drawString("Score: " + score, 10, getHeight() - 10);
        g.drawString("Highscore: " + highScore, 10, getHeight() - 40);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        moveSnake();
        checkCollision();
        repaint();
    }

    private void moveSnake() {
        Point head = new Point(snake.get(0));
        switch (direction) {
            case 'L':
                head.x--;
                break;
            case 'R':
                head.x++;
                break;
            case 'U':
                head.y--;
                break;
            case 'D':
                head.y++;
                break;
        }
        snake.add(0, head);
        snake.remove(snake.size() - 1);
    }

    private void checkCollision() {
        Point head = snake.get(0);
        if (head.equals(food)) {
            snake.add(food);
            placeFood();
            score++;
            if (score > highScore) {
                highScore = score;
                saveHighScore();
            }
        }

        if (head.x < 0 || head.x >= BOARD_WIDTH || head.y < 0 || head.y >= BOARD_HEIGHT) {
            timer.stop();
            showGameOverDialog();
        }

        for (int i = 1; i < snake.size(); i++) {
            if (head.equals(snake.get(i))) {
                timer.stop();
                showGameOverDialog();
            }
        }
    }

    private void showGameOverDialog() {
        String message = String.format("Game OvernScore: %dnHighscore: %d", score, highScore);
        int option = JOptionPane.showOptionDialog(this, message, "Game Over", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[]{"Zurück zum Hauptmenü", "Spiel beenden"}, "Zurück zum Hauptmenü");
        if (option == 0) {
            frame.showCard("MainMenu");
        } else {
            System.exit(0);
        }
        initGame();
        timer.start();
    }
}

Tried to play around with the code related to the showGameOverDialog, havent been able to figure something out.

New contributor

Maurice is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật