I'm putting this under windows programming becuase I think that's a windows header. I'm making a space invaders game in the command prompt. It's pretty cool so far, I got the shooting and the random enemies, there's not much more to do. I'm working on making a small program now (write small pieces before modifying the main source) to move a ship "/\" accross the bottom of the screen.
I've had issues with thee fact that the arrow keys have 2 characters tied to them and the regular (wasd and space keys) only have one. They don't seem to miz well as I can't check for 2 every time as it will slow the space bar presses (to shoot) or if theye are using the wasd keys.
Also, it seems to "lock up" when I press too many kney at once, is there a way to avoid this?
Possible solutions I think are flushing the input buffer (where is getch() or _getch() getting it's input?) or just using all single character keys and disabling the arrow keys.
Any ideas are appreciated. Let me know if I need to post code... Thanks,
If you are using windows.h you might as well just use a proper event loop and ReadConsoleInput(). I think Bazzy (and some others here) is just a little too fond of GetAsyncKeyState(). It isn't as friendly a function to use as people think (there are ramifications to using it).
(conio.h is a very old header invented by someone at Microsoft back in the days of DOS. But it really is out-of-date and anything you want to do with it can be done better with the Windows Console API.)
It takes a little more setup to use the Win Console API functions, but it is worth your time.
This is going to sprout some more questions now...
Using the GetAsyncKeyState() function I realize how old and outdated <conio.h> is, but why is GetAsyncKeyState() bad? What are the ramifications of using this to grab keyboard input (I'm not trying to use it to see if the key is down, like it said you can on MSDN)?
And I would eventually like to port this to mac OSX and Linux (all versions if possible, at least ubuntu), are there other good functions like this for those? (Also, I am using system("mode con cols=xx lines=xx"), is there an equivalent to resize the terminal?)
Anything else you think I should know would be great, as I always need to know more... Thanks again for helping improve my skills,
GetAsyncKeyState() gets the asyncronous key state. Unless you really want to know what keys the user are actually, at this very nano-second holding down, then the function's results may throw your code some surprises.
It basically puts everything on pause while you check the actual state of the universe instead of just being satisfied with the currently processing state. Computers these days are usually fast enough to make potential problems less noticable when developing, but someone, somewhere, will hate you for it. Better to just do it right from the start. (It is actually rather rare to want to jump the event queue like that.)