From 963b9a27d79af82c74117bf9f72e942633a533bd Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:04:32 +0100 Subject: [PATCH] update --- src/main/java/Snake/Controller.java | 53 ++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/java/Snake/Controller.java b/src/main/java/Snake/Controller.java index 23b6cfa..625ed14 100644 --- a/src/main/java/Snake/Controller.java +++ b/src/main/java/Snake/Controller.java @@ -3,17 +3,23 @@ package Snake; import javax.swing.*; import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; public class Controller { - private enum GameState { Running }; + private enum GameState { Running, Paused, PlayerLosed }; private GameView gameView; private Snake snakeModel; private SnakeView snakeView; private Apple appleModel; private AppleView appleView; + private TextView scoreView; + private TextView messageView; + private boolean inputHandled; + private Timer timer; private GameState gameState; + private int score; private void initializeInputHandling() { @@ -28,7 +34,52 @@ public class Controller { gameView.getActionMap().put("move down", new MoveAction(Snake.SnakeDirection.DOWN)); gameView.getActionMap().put("move right", new MoveAction(Snake.SnakeDirection.RIGHT)); } + private void addButtonActionListeners(Window window) + { + window.addStartActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + if(gameState == GameState.Paused) + { + messageView.setVisibility(false); + gameState = GameState.Running; + timer.start(); + } + else if(gameState == GameState.PlayerLosed) + { + messageView.setVisibility(false); + restartGame(); + } + } + }); + window.addPauseActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + if(gameState == GameState.Running) + { + messageView.setText("Game Paused."); + messageView.setVisibility(true); + gameView.repaint(); + gameState = GameState.Paused; + timer.stop(); + } + } + }); + } + private void restartGame() + { + snakeModel.reset(); + score = 0; + scoreView.setText("Score: " + Integer.toString(score)); + selectApplesPosition(); + gameState = GameState.Running; + timer.start(); + } private void selectApplesPosition() { do