The Keyboard...my friend.
I''m making this game where something moves across the screen when you push a key.
It looks something like this:
if (KEYDOWN(VK_RIGHT))
{
//stuff here
}
I don''t want the player to be able to simply hold down the key though. I would like each complete key stroke (down and up) to correspond to one movement. Anyone know how to do this?
Easiest way to handle this is to learn DirectInput. Getting perfect input in normal Windows is next to impossible and a waste of time.
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.cjb.net/Out_Source/resist.jpg)
BetaShare - Run Your Beta Right!
Instead of checking for key states, check for changes in key states. If you can''t use some sort of buffered input (which I think DirectSound supports), just buffer the keystates each frame, and compare the current frame''s keystates to the last, taking note of the changes. That lets you see which keys have been depressed or released since the last frame.
it''s pretty easy to do it
but it''s annoying
if you such a thing to it just for the fire key
Arkon
[QSoft Systems]
but it''s annoying
if you such a thing to it just for the fire key
Arkon
[QSoft Systems]
June 14, 2001 12:34 AM
you might try
if (KEYDOWN(VK_RIGHT))
{
//stuff here
KEYDOWN(VK_RIGHT) = false
}
that will slow it down to the typematic rate of windows, or you could try what i''m useing (and by the way yes i know it''s inefficiant, and yes i know it''s slow, but it was easy to do, and it works, and thats what counts :-))
bool moving=0;
int move_dir=0;
int fine_count=0;
const int MOVE_RIGHT = 1;
//other directions go like above
if(!moving)
{
if (KEYDOWN(VK_RIGHT))
{
move_dir = MOVE_RIGHT;
moving = 1;
}
//other dirs go here
}
the somwhere you have this block
if(moving)
{
fine_count++;
switch(move_dir)
{
case MOVE_RIGHT: //right
//move code
// for my game it''s the movement amount/32 for the loop.
break;
//etc
}
if(fine_count == 32)
{
moving=0;
fine_count=0;
move_dir=0;
}
}
like i said there are probably experianced programers out there that are ready to shoot me for such garbage, but it works, and thats good enough for me.
hope that helps...
-------------------------------------------------
Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny
if (KEYDOWN(VK_RIGHT))
{
//stuff here
KEYDOWN(VK_RIGHT) = false
}
that will slow it down to the typematic rate of windows, or you could try what i''m useing (and by the way yes i know it''s inefficiant, and yes i know it''s slow, but it was easy to do, and it works, and thats what counts :-))
bool moving=0;
int move_dir=0;
int fine_count=0;
const int MOVE_RIGHT = 1;
//other directions go like above
if(!moving)
{
if (KEYDOWN(VK_RIGHT))
{
move_dir = MOVE_RIGHT;
moving = 1;
}
//other dirs go here
}
the somwhere you have this block
if(moving)
{
fine_count++;
switch(move_dir)
{
case MOVE_RIGHT: //right
//move code
// for my game it''s the movement amount/32 for the loop.
break;
//etc
}
if(fine_count == 32)
{
moving=0;
fine_count=0;
move_dir=0;
}
}
like i said there are probably experianced programers out there that are ready to shoot me for such garbage, but it works, and thats good enough for me.
hope that helps...
-------------------------------------------------
Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny
that annon was me. if i''m going to get flamed for that code i''d at laest like it to be pointed the right way
-------------------------------------------------
Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny
![](smile.gif)
-------------------------------------------------
Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
Make yourself an array 256 bytes in length. Set all values to zero. When you test a key and find that it is pressed, set the value in the array with the index of the keycode to true.
unsigned char keys[256];
void Init()
{
//other initialization stuff
memset( keys, 0, 256 );
}
int keyPressed( unsigned char key )
{
if ( KEYDOWN( key ) )
{
if (!keys[key])
{
keys[key] = true;
return true;
}
}
else
{
keys[key] = false;
}
return false;
}
void handleKeyboard()
{
if (keyPressed( VK_RIGHT ))
{
moveRight();
}
if (keyPressed( VK_LEFT ))
{
moveLeft();
}
if (keyPressed( VK_UP ))
{
makeCoffee();
}
if (keyPressed( VK_CTRL_ALT_DELETE ))
{
wayCoolSuperPlasmaGun();
}
}
etc etc
lol
Seeya
Krippy
unsigned char keys[256];
void Init()
{
//other initialization stuff
memset( keys, 0, 256 );
}
int keyPressed( unsigned char key )
{
if ( KEYDOWN( key ) )
{
if (!keys[key])
{
keys[key] = true;
return true;
}
}
else
{
keys[key] = false;
}
return false;
}
void handleKeyboard()
{
if (keyPressed( VK_RIGHT ))
{
moveRight();
}
if (keyPressed( VK_LEFT ))
{
moveLeft();
}
if (keyPressed( VK_UP ))
{
makeCoffee();
}
if (keyPressed( VK_CTRL_ALT_DELETE ))
{
wayCoolSuperPlasmaGun();
}
}
etc etc
lol
Seeya
Krippy
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement