meerkat2040
asked on
Console Based Game - Reading the keyboard without pausing
I'm taking an intro to c++ class. My assignment is to create a console based game.
I would like a section of the code to check to see what the last key pressed was (arrow keys), and scroll the screen in that direction continuously until another direction is pressed. I'm doing a maze that keeps moving all the time, where you have to avoid hitting the walls but can never stand still.
getch() doesn't work for me because it pauses the movement of the game while waiting for input. I would prefer just to scan the keyboard for the last key pressed before another key is entered.
I would like a section of the code to check to see what the last key pressed was (arrow keys), and scroll the screen in that direction continuously until another direction is pressed. I'm doing a maze that keeps moving all the time, where you have to avoid hitting the walls but can never stand still.
getch() doesn't work for me because it pauses the movement of the game while waiting for input. I would prefer just to scan the keyboard for the last key pressed before another key is entered.
ASKER
Hmm. I'm using Microsoft Visual C++ 2005.
I'm not familiar with interrupts in VS.
... so it is better to wait for some expert with better idea, but if you don't find other solution I would recommend you to take a look at this link I've just found
www.cs.cityu.edu.hk/~lwang/lec5a5.ppt
They even have a sample game with keyboard interrupts.
www.cs.cityu.edu.hk/~lwang/lec5a5.ppt
They even have a sample game with keyboard interrupts.
ASKER
Thank you. That was helpful.
If your compiler supports its use try _bios_keybrd().
http://www.comp.nus.edu.sg/~malin/materials/C_lib/libc_60.html
This certainly works on the Microsoft native DOS C++ compilers (last one I used was VC1.52C) but I doubt very much it'll work with newer compilers!
Basically, there is no portable way to do this -- you'll need to find a native system call to use and if you want to build x-platform you will have to do some conditional compilation.
-Rx.
http://www.comp.nus.edu.sg/~malin/materials/C_lib/libc_60.html
This certainly works on the Microsoft native DOS C++ compilers (last one I used was VC1.52C) but I doubt very much it'll work with newer compilers!
Basically, there is no portable way to do this -- you'll need to find a native system call to use and if you want to build x-platform you will have to do some conditional compilation.
-Rx.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you. This is exactly what I needed.
It's not trivial especially with newer IDEs.
If you are using Turbo C++ (16b) this can be accomplished without much trouble.
This is an old Borland IDE and since you have to write only a console application maybe you can use it instead of newer IDEs.
Of course you will be unable to compile that program anywhere else.