| | |
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 3:07 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Aug 2008
Posts: 92
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 3: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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






