0

I'm building a game using Java for a university project. The game had to use a textual interface at first, and only later we had to add a GUI.

I have a thread running the game logic and updating the game model, and a separate thread, the one with the main function, having the JFrame instantiated on, where I then switch panels according to the game state (the controller tells the gui which frame to display with a setPage method).

Getting the user input, however, is what is driving me crazy now. With the textual interface, all I had to do to get the user input was to call a method, something like getPlayerNum, and after the user put in the number and pressed enter I would have the input back to the controller. All the game logic is implemented in a game loop, asking for input when it needs it and notifying the output to the view.

How can I emulate a similar behavior now that I'm forced to use a event-based gui? How can I block the method call until the user clicks a button, and then have the method return the correct value?

Alakanu
  • 325
  • 2
  • 11
  • 1
    You need to completely re-think your program structure so that it is not linear but rather state and event driven. Don't "block" methods but rather vary your program's response to events depending on the model's state. – Hovercraft Full Of Eels Jun 01 '13 at 16:46
  • 1
    For a more detailed answer, consider asking a more concrete question, complete with a small bit of compilable and runnable example code. – Hovercraft Full Of Eels Jun 01 '13 at 17:47
  • 1
    Thanks for your answer. Building the textual interface was suggested by the teacher as "it will lead you on the right way to build the game cycle", what he "forgot" to mention is that we were meant to separate requests to and answers from the user. It took me a while to split everything up, but now it's up and -kinda- running. – Alakanu Jun 06 '13 at 09:31

1 Answers1

1

Kudos for developing an independent game model. As a prelude to designing your GUI, start with this very simple game. Add a TextView that listens to the model and reports on game progress. This will help you see how the GUI works, and it may help you decide what additional features you may need in your model.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for the sample you posted, it's a good example I wish I saw way before than just today.. – Alakanu Jun 06 '13 at 09:35