-
getch
I am in the process of writing an old invaders game of sorts. When I ran into a problem, I was using
Code:
key = getch ( );
switch ( key )
{
case '4':
//go left
break;
case '6':
//go right
break;
case '8':
//go up
break;
case '2':
//go down
break;
}
when I realized then when I reach the getch statement, my programs halts. And waits for user input, that is a very serious problem. How can I get around this.
p:s: the enemy ships and their bullets only move when I move.
-
You need an event checking method. I only know how to do this using SDL_PollEvent, which requires (surprise) SDL. A friend of mine once made a console based space invaders game. So it is possible. I don't know how, but I would be interested to find out.
-
in visual basic there is a keypress command, does C have a similar command that doesnt halt the program?
-
aside from learning another language, any other ideas?
-
The function for checking the "is there anything from the keyboard" is system and compiler dependant, but give "kbhit()" a try - it may work, depending on what environment you are programming in. It won't work under Linux, that's for sure.
--
Mats
-
Try learning the O/S you're writing this for or a library that covers this stuff.
-
>p:s: the enemy ships and their bullets only move when I move.
One idea is to use kbhit() to check whether a key has been pressed.
Code:
if (kbhit())
{
key = getch ( );
switch ( key )
{
case '4':
//go left
break;
case '6':
//go right
break;
case '8':
//go up
break;
case '2':
//go down
break;
}
}
/* Other code */
-
Yup and linux doesn't have kbhit() so u could use the ncurses library (or graphics library of your choice) to detect for a keystroke... It's in any good game library, just a matter of finding it,
-
kbhit is a step in the right direction. But now if I hit the left key, my ship keeps moving left until I till it to go somewhere else.
-
in other words its acting like a toggle switch without the off command.
-
Post some more code, in particular the whole loop containing moving commands, kbhit() and getch() calls.
-
swoopy has posted the code already.
-
So you have no code to move any object outside of the block guarded by if ( kbhit() ) ?
-
The actual code is over 3000 characters , I am just working on the movement system as of now. And right now when I hit the left movement key, it keeps on moving until I change the direction.
*edit*