| | |
Reading char from stdin without waiting for input
Thread Solved |
I'm seeing if it is possible to write a pong game only using standard C with maximum portability. I've worked out everything except user input. When the game is playing, If the user has no key pressed down, the paddle does not move but the ball should keep moving. I can only think of two way's of doing this. One is to get the user to hold down an extra key at all times. The other is to shut off the buffer on stdin using setvbuf(), which will hopefully make getchar() return EOF or NULL when no key is being pressed. Would these work? Does anyone have any other idea's?
Thanks.
Thanks.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
•
•
•
•
That would be cheating.I want to see what can be done with standard C alone.
http://c-faq.com/osdep/cbreak.html
Last edited by Dave Sinkula; Aug 2nd, 2009 at 2:07 pm.
“The essential notion of a socialist society is force.”
— Milton Friedman
— Milton Friedman
•
•
Join Date: Aug 2008
Posts: 93
Reputation:
Solved Threads: 2
I also had the same problem in past,
I think, if you are using Turbo C, under <dos.h>, there is a function called kbhit(), checks keyboard hit.
I think, if you are using Turbo C, under <dos.h>, there is a function called kbhit(), checks keyboard hit.
C Syntax (Toggle Plain Text)
while(1){ // Ball movement codes, if(kbhit()){ //check input ASCII and move your paddle. } //exiting loop conditions }
Last edited by deepugtm; Aug 2nd, 2009 at 2:34 pm.
Is it standard behaviour for a key just pressed to outpower a key being held down? ie. The user holds down the space bar for the whole game, and the "up" key out powers the spacebar? If not, than I guess I'll have a theird key for "stay still".
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
•
•
•
•
Is it standard behaviour for a key just pressed to outpower a key being held down? ie. The user holds down the space bar for the whole game, and the "up" key out powers the spacebar? If not, than I guess I'll have a theird key for "stay still".
The standard behavior is to do nothing in particular for any keypress, save the return key.
•
•
•
•
Only when the user is satisfied and presses the RETURN key (or equivalent) is the line made available to the calling program.
“The essential notion of a socialist society is force.”
— Milton Friedman
— Milton Friedman
Hmm... Then I guess the best I can do in standard C is to pause the game and get the user input (up, down, still) befor continuing on.
I'm too stubborn.
Maybe instead of the space bar, get the user to hold down return. Just write an input function that ignores '\n'. That way the stream continuously updates. Would this work? I'll try it at home.
I'm too stubborn.
Maybe instead of the space bar, get the user to hold down return. Just write an input function that ignores '\n'. That way the stream continuously updates. Would this work? I'll try it at home. "Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
![]() |
Similar Threads
- how how to write a C source code for a Login system? (C)
- How do I convert telephone phrases to numbers? (C++)
- Not waiting for input during loop (C++)
- My own read function (C)
- Need Help in Reading characters from a text file (C++)
- C program (not C++) in/out putting a entire sentence (C++)
- system("PAUSE") (C++)
- Help with input (C)
- reading a file into code (Java)
Other Threads in the C Forum
Views: 1475 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays bash behaviour binary binarysearch c++ calculator char character code coke command conversion convert copy createcopyoffile database decimal directory dude dynamic ebook error exec factorial fgets file fork function functions givemetehcodez grade graphics highest homework i/o initialization input insert int integer kernel lazy libcurl line linked linkedlist linux list lists loop malloc matrix memory mysql no-code no-effort operator output pass path pointer pointers power printf problem process program programming read recursion recursive recv research reverse scanf sequential sms_speak socket socketprograming spoonfeeding stdout steganography string strings strtok structures student syntax system test turbo-c turboc unix user variable whileloop windows






