How to combine event listeners with “asking” for an event?

前端 未结 1 803
梦毁少年i
梦毁少年i 2020-11-30 15:46

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

相关标签:
1条回答
  • 2020-11-30 16:27

    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.

    0 讨论(0)
提交回复
热议问题