0

I am currently working on a console based Tetris game in Java.

The user has to be able to move the Tetrimino (piece) left and right. This is impossible with KeyEvents because everything is done with only the console.

The only solution I was able to come up with was using a scanner and see when the user typed either "Q" for left or "D" for right. Only 1 issue, the program always waits for user input before it continues to the next line of code.

while(run) {
   ...
   String movement = sc.nextLine();
   ...
   }

Is there a way so that the program keeps running the while loop without waiting for input every loop?

Edit: The solution shown here (suggested as duplicate) only works on unix based systems.

Community
  • 1
  • 1
Gino
  • 9
  • 3
  • You should use a keyActionListener instead of Scanner. https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html – Rahul Chowdhury May 19 '17 at 14:40
  • 3
    @RahulChowdhury OP is writing a *console* program, not a Swing program. – Andreas May 19 '17 at 14:41
  • @Andreas I would suggest threads but I don't know enough about it to write a good and useful answer... – ItamarG3 May 19 '17 at 14:42
  • Here comes the need of Threads !!! – Freak May 19 '17 at 14:43
  • http://stackoverflow.com/questions/5463968/how-to-make-a-while-to-run-until-scanner-get-input – Freak May 19 '17 at 14:45
  • 1
    Thread won't help, since Java program won't see any input until user hits Enter. Console programs are always in line-mode, and only receives typed text a whole line at a time. OP wants program to receive `Q` or `D` *immediately* when pressed, not later when user presses Enter. Adding extra threads won't make any difference to that problem. – Andreas May 19 '17 at 14:45
  • You are missing the point: the other answer explains that it works with Linux / Mac - and that it is almost impossible to solve this for windows. There are certain suggestions how to do that for windows. You won't hear anything else. Thus voting to leave this closed. You got your answer. We can't help you if the thing you intend to do is close to impossible with java. – GhostCat May 20 '17 at 04:11
  • "Edit: The solution shown here (suggested as duplicate) only works on unix based systems." this question is still a duplicate, the duplicate didn't ask for Unix specifically. That likely means that there is no option for the platform that you use (that you did not specify anywhere in your question) – Erwin Bolwidt May 21 '17 at 13:54

0 Answers0