0

I'm programming on java, so I have to make a game-like program. The game should be able to "pause" at any point. Can you press a key at any point so a message is displayed saying "game is paused"? Then when you press that key again it will say "game resumed"? Thanks

LearningProcess
  • 597
  • 1
  • 4
  • 27
Kevin E.
  • 9
  • 3

2 Answers2

1

It looks like you need to be able to read input directly from the keyboard into your game? Is your game built into a GUI or in the console?

This can be done by using the KeyListener interface Java provides. https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html

I'll assume your using swing so this tutorial should be pretty useful. http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Sorry I can't go into more detail, more information about what you are trying to do in the comments would help me help you.

Edit: You mentioned you're using the command line. To get user input you can use java.util.Scanner http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

NickLamp
  • 782
  • 4
  • 10
  • Does the scanner allow you to check if there was a click registered? I thought it was just for textual input? – Brandon Laidig Nov 04 '15 at 19:44
  • The command line doesn't have events like swing does, so there is no onClick or key pressed. Nothing like that. Posted this answer before OP clarified that the game was in the command line. – NickLamp Nov 04 '15 at 19:45
  • Oh, that's my mistake. I read the question incorrectly and thought they needed to somehow register a click event from the command line – Brandon Laidig Nov 04 '15 at 19:46
  • All good. I would comment this on the original post if I had the points to but I don't. Looking through more posts, this question has been asked before and there have been some rather creative answers in this thread. http://stackoverflow.com/questions/4005574/java-key-listener-in-commandline – NickLamp Nov 04 '15 at 19:48
  • thanks for the information. The thing is that ill not ask the user, he/she just press a key at any moment and the programm will print a line "game paused". – Kevin E. Nov 04 '15 at 20:41
  • Perhaps this can help you? https://github.com/kwhat/jnativehook It was alluded to in the post I linked to. I haven't use it before but it seems that it should give you what you're looking for. – NickLamp Nov 04 '15 at 20:55
0

Make a boolean, isPaused. If space is pressed, switch isPaused from true to false or vice versa. If isPaused is true, run the game, if not, run a pause screen instead.

Ryan
  • 159
  • 4