I wrote a simple little maze game for a terminal which repeatedly asks the user to do something (e.g. "In which direction would you like to go? [N/E/S/W]"). I have a navigate()
method running in a loop that fires off these questions, stores their answers and does something depending on the answer.
public enum Dir (N, E, S, W);
public void navigate() {
Dir nextDir = utils.askDirection("Which way do you want to go?");
// Do stuff with answer, like changing position of user in maze
}
Now, I've written a simple GUI for my game. I deliberately put all the references to the terminal in a ConsoleUtils
class which implements a Utils
interface (this has methods like askQuestion()
) - the idea being that I could create a GuiUtils
class and have my game either as a terminal game or as a GUI game.
The problem is that the navigate
method asks the user a question and then "waits" for the response, which the Utils
class gives it by using a Scanner
to read the newest line of input. However if I use Event Listeners for the new N/E/S/W buttons in my GUI, they fire off events regardless whether the navigate
method has asked for one or not.
--> Image of GUI
Is there any way I can combine this or do I need to write a new navigate
method for the GUI?
(To be honest, I'm also not entirely sure whether my GUI class should instantiate a game
class, in which case the logic for navigate
could end up in a GUI method anyway, or whether the game should have a GUI. I haven't written any code for the event listener either yet, since I'm not sure which class should be calling which. This is probably a separate question.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…