From 1d55539da14ee4a400b9b5d6dff406d874d41f2e Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 21:49:47 +0100 Subject: [PATCH 01/12] update --- src/main/java/Snake/Controller.java | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/java/Snake/Controller.java b/src/main/java/Snake/Controller.java index 43058fe..329f303 100644 --- a/src/main/java/Snake/Controller.java +++ b/src/main/java/Snake/Controller.java @@ -1,6 +1,36 @@ package Snake; + +import javax.swing.*; +import java.awt.event.ActionEvent; + public class Controller { + private enum GameState { Running }; + private GameView gameView; + private Snake snakeModel; + private boolean inputHandled; + private GameState gameState; + + private class MoveAction extends AbstractAction + { + + private Snake.SnakeDirection direction; + + MoveAction(Snake.SnakeDirection direction) + { + this.direction = direction; + } + @Override + public void actionPerformed(ActionEvent e) + { + if(inputHandled && gameState == GameState.Running) + { + snakeModel.setDirection(direction); + inputHandled = false; + } + } + } + } From 50c194c3337545bd47bd32739934c957db3b40c4 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 21:51:02 +0100 Subject: [PATCH 02/12] update --- src/main/java/Snake/Controller.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/Snake/Controller.java b/src/main/java/Snake/Controller.java index 329f303..6fb4a14 100644 --- a/src/main/java/Snake/Controller.java +++ b/src/main/java/Snake/Controller.java @@ -12,6 +12,19 @@ public class Controller { private boolean inputHandled; private GameState gameState; + private void initializeInputHandling() + { + final int CONDITION = JComponent.WHEN_IN_FOCUSED_WINDOW; + gameView.getInputMap(CONDITION).put(KeyStroke.getKeyStroke("W"), "move up"); + gameView.getInputMap(CONDITION).put(KeyStroke.getKeyStroke("A"), "move left"); + gameView.getInputMap(CONDITION).put(KeyStroke.getKeyStroke("S"), "move down"); + gameView.getInputMap(CONDITION).put(KeyStroke.getKeyStroke("D"), "move right"); + + gameView.getActionMap().put("move up", new MoveAction(Snake.SnakeDirection.UP)); + gameView.getActionMap().put("move left", new MoveAction(Snake.SnakeDirection.LEFT)); + gameView.getActionMap().put("move down", new MoveAction(Snake.SnakeDirection.DOWN)); + gameView.getActionMap().put("move right", new MoveAction(Snake.SnakeDirection.RIGHT)); + } private class MoveAction extends AbstractAction { From 6594ea59d624b694cc8cbbab9b772054caf7a3e3 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 21:59:56 +0100 Subject: [PATCH 03/12] update --- src/main/java/Snake/Controller.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/Snake/Controller.java b/src/main/java/Snake/Controller.java index 6fb4a14..23b6cfa 100644 --- a/src/main/java/Snake/Controller.java +++ b/src/main/java/Snake/Controller.java @@ -9,6 +9,9 @@ public class Controller { private enum GameState { Running }; private GameView gameView; private Snake snakeModel; + private SnakeView snakeView; + private Apple appleModel; + private AppleView appleView; private boolean inputHandled; private GameState gameState; @@ -25,6 +28,31 @@ 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 selectApplesPosition() + { + do + { + appleModel.selectGridPosition(gameView.getGridSize()); + } while(!isApplePositionIsValid()); + appleView.setPosition(appleModel.getPosition()); + } + private boolean isApplePositionIsValid() + { + var snakeSegments = snakeModel.getBodySegments(); + for(var segmentPosition: snakeSegments) + { + if(segmentPosition.equals(appleModel.getPosition())) + return false; + } + return true; + } + private void updateSnakeViewPosition() + { + snakeView.setPositions(snakeModel.getBodySegments()); + } + + private class MoveAction extends AbstractAction { From 963b9a27d79af82c74117bf9f72e942633a533bd Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:04:32 +0100 Subject: [PATCH 04/12] 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 From af93a2a3189904721e6b3f2d11730a2586f31041 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:11:42 +0100 Subject: [PATCH 05/12] update --- src/main/java/Snake/Controller.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/Snake/Controller.java b/src/main/java/Snake/Controller.java index 625ed14..2c5ec16 100644 --- a/src/main/java/Snake/Controller.java +++ b/src/main/java/Snake/Controller.java @@ -103,6 +103,31 @@ public class Controller { snakeView.setPositions(snakeModel.getBodySegments()); } + private void timerTick() + { + snakeModel.update(); + inputHandled = true; + checkIfAppleWasEaten(); + if(snakeModel.isSelfColliding()) + { + messageView.setText("You Lose! Click 'Start Game' to play agaiin."); + messageView.setVisibility(true); + gameState = GameState.PlayerLosed; + timer.stop(); + } + updateSnakeViewPosition(); + gameView.repaint(); + } + private void checkIfAppleWasEaten() + { + if(appleModel.getPosition().equals(snakeModel.getHeadPosition())) + { + score += 1; + scoreView.setText("Score: " + Integer.toString(score)); + snakeModel.grow(); + selectApplesPosition(); + } + } private class MoveAction extends AbstractAction { From cc83727a1da6f81307175433f7b3cb4fb4d9ca65 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:14:13 +0100 Subject: [PATCH 06/12] update --- src/main/java/Snake/Controller.java | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/main/java/Snake/Controller.java b/src/main/java/Snake/Controller.java index 2c5ec16..3ae5372 100644 --- a/src/main/java/Snake/Controller.java +++ b/src/main/java/Snake/Controller.java @@ -2,6 +2,7 @@ package Snake; import javax.swing.*; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -21,6 +22,42 @@ public class Controller { private GameState gameState; private int score; + Controller(Window window, Snake snakeModel, SnakeView snakeView, Apple appleModel, AppleView appleView) + { + gameView = window.getGameView(); + this.snakeModel = snakeModel; + this.snakeView = snakeView; + this.appleModel = appleModel; + this.appleView = appleView; + + scoreView = new TextView("Scores: 9", new Point(15, 15)); + messageView = new TextView("Click 'Start Game' to Begin . (W-up, S-down, A-left, D-Right)", + new Point(gameView.getWidth()/2, gameView.getHeight() - 15)); + + scoreView.setAnchor(TextView.AnchorType.Left); + + gameView.addView(scoreView); + gameView.addView(messageView); + + initializeInputHandling(); + addButtonActionListeners(window); + + timer = new Timer(10, new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + timerTick(); + } + }); + + gameState = GameState.Paused; + inputHandled = true; + score = 0; + + selectApplesPosition(); + updateSnakeViewPosition(); + } private void initializeInputHandling() { final int CONDITION = JComponent.WHEN_IN_FOCUSED_WINDOW; From 5797e52424dc81a416260f7ac5ae28d3df45de3b Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:15:07 +0100 Subject: [PATCH 07/12] refactoring: --- src/main/java/Snake/Controller.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Snake/Controller.java b/src/main/java/Snake/Controller.java index 3ae5372..b310d5e 100644 --- a/src/main/java/Snake/Controller.java +++ b/src/main/java/Snake/Controller.java @@ -30,7 +30,7 @@ public class Controller { this.appleModel = appleModel; this.appleView = appleView; - scoreView = new TextView("Scores: 9", new Point(15, 15)); + scoreView = new TextView("Scores: 0", new Point(15, 15)); messageView = new TextView("Click 'Start Game' to Begin . (W-up, S-down, A-left, D-Right)", new Point(gameView.getWidth()/2, gameView.getHeight() - 15)); @@ -147,7 +147,7 @@ public class Controller { checkIfAppleWasEaten(); if(snakeModel.isSelfColliding()) { - messageView.setText("You Lose! Click 'Start Game' to play agaiin."); + messageView.setText("You Lose! Click 'Start Game' to play again."); messageView.setVisibility(true); gameState = GameState.PlayerLosed; timer.stop(); From ae063b839a8aad70fc4ddc151f53bbd8b5e32a83 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:16:03 +0100 Subject: [PATCH 08/12] refactoring: --- src/main/java/Snake/Controller.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Snake/Controller.java b/src/main/java/Snake/Controller.java index b310d5e..e751b57 100644 --- a/src/main/java/Snake/Controller.java +++ b/src/main/java/Snake/Controller.java @@ -42,7 +42,7 @@ public class Controller { initializeInputHandling(); addButtonActionListeners(window); - timer = new Timer(10, new ActionListener() + timer = new Timer(100, new ActionListener() { @Override public void actionPerformed(ActionEvent e) From 282dbaeba6069a9d626f57e43cb03717f02a439a Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:17:27 +0100 Subject: [PATCH 09/12] update --- src/main/java/Snake/Game.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/Snake/Game.java diff --git a/src/main/java/Snake/Game.java b/src/main/java/Snake/Game.java new file mode 100644 index 0000000..3131df2 --- /dev/null +++ b/src/main/java/Snake/Game.java @@ -0,0 +1,5 @@ +package Snake; + +public class Game { + +} From 8b0beb2cc31a5eb050592b3445561c89bff3e792 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:22:10 +0100 Subject: [PATCH 10/12] update --- src/main/java/Snake/Game.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/Snake/Game.java b/src/main/java/Snake/Game.java index 3131df2..9de31b4 100644 --- a/src/main/java/Snake/Game.java +++ b/src/main/java/Snake/Game.java @@ -1,5 +1,24 @@ package Snake; public class Game { + public Game() { + final int VIEW_SIZE = 100; + final int GRID_SIZE = 10; + final int CELL_SIZE = VIEW_SIZE / GRID_SIZE; + + Window gameWindow = new Window(VIEW_SIZE, GRID_SIZE); + + Snake snakeModel = new Snake(GRID_SIZE); + SnakeView snakeView = new SnakeView(CELL_SIZE); + + Apple appleModel = new Apple(); + AppleView appleView = new AppleView(CELL_SIZE); + + GameView gameView = gameWindow.getGameView(); + gameView.addView(snakeView); + gameView.addView(appleView); + + new Controller(gameWindow, snakeModel, snakeView, appleModel, appleView); + } } From a92cb1c8e0b633338738b1fa05aef1436afcccc4 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:22:42 +0100 Subject: [PATCH 11/12] refactoring: --- src/main/java/Snake/Game.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Snake/Game.java b/src/main/java/Snake/Game.java index 9de31b4..bc0faf4 100644 --- a/src/main/java/Snake/Game.java +++ b/src/main/java/Snake/Game.java @@ -3,8 +3,8 @@ package Snake; public class Game { public Game() { - final int VIEW_SIZE = 100; - final int GRID_SIZE = 10; + final int VIEW_SIZE = 250; + final int GRID_SIZE = 25; final int CELL_SIZE = VIEW_SIZE / GRID_SIZE; Window gameWindow = new Window(VIEW_SIZE, GRID_SIZE); From 6d2cda9390dbd2c8857250fd9a5de30edb4bc2f2 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 22:23:01 +0100 Subject: [PATCH 12/12] refactoring: --- src/main/java/Snake/Game.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Snake/Game.java b/src/main/java/Snake/Game.java index bc0faf4..f020037 100644 --- a/src/main/java/Snake/Game.java +++ b/src/main/java/Snake/Game.java @@ -3,8 +3,8 @@ package Snake; public class Game { public Game() { - final int VIEW_SIZE = 250; - final int GRID_SIZE = 25; + final int VIEW_SIZE = 500; + final int GRID_SIZE = 50; final int CELL_SIZE = VIEW_SIZE / GRID_SIZE; Window gameWindow = new Window(VIEW_SIZE, GRID_SIZE);