Sign in to follow this  
becklighter

keypress speed issues...

Recommended Posts

I'm currently working on my first game with genuine DirectX enabled graphics. My Tetris type game is coming along well, but I've encountered a problem that I can't seem to correct. Button presses appear to occur extremely fast. I use the space bar to rotate the falling shape, but when I press it, I get a whirling blur as it draws out every different rotation in succession until I let off the key. The same problem occurs when I press down, or left and right. It makes it difficult to line up the pieces correctly. Does anyone have a clue what I might be doing wrong?

Here are what I believe to be the most pertinent pieces of code:
[source]
...
#define KeyDown(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KeyUp(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
...

...
MSG msg;
while(TRUE){
DWORD start = GetTickCount();
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
if(msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

RenderFrame();

#pragma region Move Block
if(KeyDown(VK_SPACE) && !fall.Move(0, 0, 1))
;// Play beep type sound uh.. uh..
if(KeyDown(VK_RIGHT) && !fall.Move(0, 1, 0))
;// Play beep type sound uh.. uh..
if(KeyDown(VK_LEFT) && !fall.Move(0, -1, 0))
;// Play beep type sound uh.. uh..
if(KeyDown(VK_DOWN) && !fall.Move(1, 0, 0))
if(fall.SetPiece())
SendMessage(hWnd, WM_DESTROY, 0, 0);
#pragma endregion

if(KeyDown(VK_ESCAPE))
SendMessage(hWnd, WM_DESTROY, 0, 0);

while((start - GetTickCount()) < 25);
}
...
[/source]

Share this post


Link to post
Share on other sites
[quote name='beatlefan' timestamp='1313541465' post='4850094']
It's because it's executing that code hundreds, if not, thousands of times per second.

For something like this you would want to use the key down messages.
[/quote]

Thank you for your reply, but, isn't that what I'm doing? Am I missing something?

Share this post


Link to post
Share on other sites
[quote name='becklighter' timestamp='1313542464' post='4850096']
Thank you for your reply, but, isn't that what I'm doing? Am I missing something?
[/quote]

What your doing is checking if the key is down. You want to check for key down [i]events[/i], which have a built in delay. So where you have your

[code]
if (msg.message == WM_QUIT)
[/code]

you will want to add a

[code]
else if(msg.message == WM_KEYDOWN)
[/code]

and if that's true check which key is down and move your tetris pieces.

Share this post


Link to post
Share on other sites
[quote name='beatlefan' timestamp='1313543603' post='4850099']
[quote name='becklighter' timestamp='1313542464' post='4850096']
Thank you for your reply, but, isn't that what I'm doing? Am I missing something?
[/quote]

What your doing is checking if the key is down. You want to check for key down [i]events[/i], which have a built in delay. So where you have your

[code]
if (msg.message == WM_QUIT)
[/code]

you will want to add a

[code]
else if(msg.message == WM_KEYDOWN)
[/code]

and if that's true check which key is down and move your tetris pieces.
[/quote]

Thank you, that is exactly what I needed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this