Separate User-Input and Graphical-Preview of a Game In Java

Viewed 24

I made a simple "snake" game in java. As an exercise I want to control the user-input and the graphical-preview of the game separately using interfaces.
I have created 2 interfaces for that: InputSystem and GraphicalSystem. So my main-loop of the game looks something like that:

while (snake.isAlive()) {
    byte direction = inputSystem.getDirection(snake); // get input from user
    snake.moveDirection(direction);  // move in the game

    graphicalSystem.update();  // preview current game state

The problem I've encountered is the implementation of the InputSystem interface. More precisely I try to make it a keyboard-input kind of thing. (while my GraphicalSystem is a GUI that I have already implemented).

The only way I found to receive a keyboard input was using some kind of GUI. But my GraphicalSystem is already using GUI. I also don't want to add the keyListener into the GraphicalSystem, because it will miss the whole idea of separating inputs and graphics. Do you have any suggestions for what should I do? I would love any idea.
Thanks in advance :)

0 Answers
Related