Create a menu item which restarts the game. Also add a timer, which counts the elapsed time since the start of the game level. When the restart is pressed, the restarted game should ask the player' name  (in the GameGUI constructor) and set the score of player to 0 (player.setScore(0)), and the timer should restart again. And create a logic so that if the player loses his life (checkGame if the condition is false), then save this number together with his name into two variables. And display two buttons where one quits the game altogether (System.exit(0)) and the other restarts the game. public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;     private final ImageIcon playerIcon = new ImageIcon("data/images/player.png");    private final ImageIcon dragonIcon = new ImageIcon("data/images/dragon.png");    private final ImageIcon wallIcon = new ImageIcon("data/images/wall.png");    private final ImageIcon emptyIcon = new ImageIcon("data/images/empty.png");     public GameGUI(Labyrinth labyrinth, Player player, Dragon dragon) {        this.labyrinth = labyrinth;        this.player = player;        this.dragon = dragon;        String playerName = JOptionPane.showInputDialog("Enter your name:");        player.setName(playerName);         setFocusable(true);        addKeyListener(new KeyAdapter() {            @Override            public void keyPressed(KeyEvent e) {                char move = switch (e.getKeyCode()) {                    case KeyEvent.VK_W -> 'W';                    case KeyEvent.VK_S -> 'S';                    case KeyEvent.VK_A -> 'A';                    case KeyEvent.VK_D -> 'D';                    default -> ' ';                };                 if (move != ' ') {                    player.move(move, labyrinth);                    dragon.move(labyrinth);                    repaint();                    checkGameState();                }            }        });    }     private void checkGameState() {        if (player.getX() == 0 && player.getY() == labyrinth.getSize() - 1) {            player.setScore(player.getScore() + 1);            JOptionPane.showMessageDialog(this, "You escaped! Congratulations!");            System.exit(0);        }         if (Math.abs(player.getX() - dragon.getX()) <= 1 &&            Math.abs(player.getY() - dragon.getY()) <= 1) {            JOptionPane.showMessageDialog(this, "The dragon caught you! Game Over.");            //System.exit(0);        }    }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter4: More Object Concepts
Section: Chapter Questions
Problem 2GZ
icon
Related questions
Question

Create a menu item which restarts the game. Also add a timer, which counts the elapsed time since the start of the game level.

When the restart is pressed, the restarted game should ask the player' name  (in the GameGUI constructor) and set the score of player to 0 (player.setScore(0)), and the timer should restart again. And create a logic so that if the player loses his life (checkGame if the condition is false), then save this number together with his name into two variables. And display two buttons where one quits the game altogether (System.exit(0)) and the other restarts the game.

public class GameGUI extends JPanel {
    private final Labyrinth labyrinth;
    private final Player player;
    private final Dragon dragon;

    private final ImageIcon playerIcon = new ImageIcon("data/images/player.png");
    private final ImageIcon dragonIcon = new ImageIcon("data/images/dragon.png");
    private final ImageIcon wallIcon = new ImageIcon("data/images/wall.png");
    private final ImageIcon emptyIcon = new ImageIcon("data/images/empty.png");


    public GameGUI(Labyrinth labyrinth, Player player, Dragon dragon) {
        this.labyrinth = labyrinth;
        this.player = player;
        this.dragon = dragon;
        String playerName = JOptionPane.showInputDialog("Enter your name:");
        player.setName(playerName);

        setFocusable(true);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                char move = switch (e.getKeyCode()) {
                    case KeyEvent.VK_W -> 'W';
                    case KeyEvent.VK_S -> 'S';
                    case KeyEvent.VK_A -> 'A';
                    case KeyEvent.VK_D -> 'D';
                    default -> ' ';
                };

                if (move != ' ') {
                    player.move(move, labyrinth);
                    dragon.move(labyrinth);
                    repaint();
                    checkGameState();
                }
            }
        });
    }

    private void checkGameState() {
        if (player.getX() == 0 && player.getY() == labyrinth.getSize() - 1) {
            player.setScore(player.getScore() + 1);
            JOptionPane.showMessageDialog(this, "You escaped! Congratulations!");
            System.exit(0);
        }

        if (Math.abs(player.getX() - dragon.getX()) <= 1 &&
            Math.abs(player.getY() - dragon.getY()) <= 1) {
            JOptionPane.showMessageDialog(this, "The dragon caught you! Game Over.");
            //System.exit(0);
        }
    }

AI-Generated Solution
AI-generated content may present inaccurate or offensive content that does not represent bartleby’s views.
steps

Unlock instant AI solutions

Tap the button
to generate a solution

Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning