From 50c194c3337545bd47bd32739934c957db3b40c4 Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 21:51:02 +0100 Subject: [PATCH] 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 {