如何将事件侦听器与“询问”组合为事件? [英] How to combine event listeners with "asking" for an event?
问题描述
我写了一个简单的小迷宫游戏终端,反复要求用户做某事(例如你要去哪个方向?[N / E / S / W])。我有一个 navigate()
方法运行在一个循环中,消除这些问题,存储他们的答案,并根据答案做一些事情。
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
}
现在,我为我的游戏写了一个简单的GUI。我故意将终端的所有引用放在一个 ConsoleUtils
类中,该类实现了一个 Utils
接口(这有一些方法,如 askQuestion()
) - 这个想法是我可以创建一个 GuiUtils
类,让我的游戏作为终端游戏em>或作为GUI游戏。
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.
问题是导航
方法向用户询问问题,然后等待响应, Utils
类通过使用 Scanner
来读取最新的输入行。但是,如果我在GUI中使用新的N / E / S / W按钮的事件监听器,则无论导航
方法是否要求一个,都会触发事件。
- > GUI的图像
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
有什么办法可以组合这个,还是需要为GUI编写一个新的导航
方法?
Is there any way I can combine this or do I need to write a new navigate
method for the GUI?
(说实话,我也不完全确定我的GUI类是否应该实例化一个游戏
类,在这种情况下,导航
的逻辑可能会导致GUI方法,或者游戏是否应该有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.)
推荐答案
你的文本基于游戏的游戏有一个循环,反复询问收集用户输入的问题。 Swing通过持续执行已发布到 Runnable 代码块,为您提供此循环/docs/api/java/awt/EventQueue.htmlrel =nofollow noreferrer> EventQueue
。例如,当用户按下标签为按钮时> E ,代码发布到调用您的 ActionEvent
实现的队列,以处理您的游戏对 move east的解释
命令。
Your text based game has a loop that repeatedly asks questions to gather user input. Swing provides this loop for you by continually executing Runnable
blocks of code that have been posted to the EventQueue
. For example, when the user presses a button labeled E, code is posted to the queue that invokes your ActionEvent
implementation to handle your game's interpretation of the move east
command.
为了参考,一个非常简单的猜测游戏的完整示例将被检查此处。在伪代码中,相应的基于文本的游戏可能如下所示:
For reference, a complete example of a very simple guessing game is examined here. In pseudocode, the corresponding text based game might look like this:
initialize
loop
prompt "Guess what color!"
get chosenColor
if chosenColor = actualColor
say "You win!"
reset game
else
say "Keep trying."
end loop
更精细的游戏引用,其中包含原始的基于文本的来源。
A more elaborate game cited there includes the original text-based source.
这篇关于如何将事件侦听器与“询问”组合为事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!