如何将事件侦听器与“询问"结合起来参加活动? [英] How to combine event listeners with "asking" for an event?

查看:30
本文介绍了如何将事件侦听器与“询问"结合起来参加活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个终端编写了一个简单的小迷宫游戏,它反复要求用户做某事(例如你想去哪个方向?[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 类,并将我的游戏作为终端游戏作为 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.

问题在于 navigate 方法询问用户一个问题,然后等待"响应,Utils 类通过使用 Scanner 读取最新的输入行.但是,如果我在 GUI 中为新的 N/E/S/W 按钮使用事件侦听器,则无论 navigate 方法是否要求一个事件,它们都会触发事件.--> 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 编写新的 navigate 方法?

Is there any way I can combine this or do I need to write a new navigate method for the GUI?

(老实说,我也不完全确定我的 GUI 类是否应该实例化一个 game 类,在这种情况下,navigate 的逻辑可能会在无论如何是一个 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 通过不断执行已发布到 EventQueue.例如,当用户按下标记为 button 时>E,代码被发布到调用您的 ActionEvent 实现的队列中,以处理您的游戏对 move Eastern 命令的解释.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆