C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Getch() Function Rate Topic: -----

#1 qUarC  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 14-January 05


Dream Kudos: 0

Share |

Getch() Function

Posted 14 January 2005 - 03:56 PM

I have written a simple game with a ball which bounces off the margins of the screen and at the bottom of the screen is my pad which is controlled by 'a' and 'd' buttons on my keyboard.:(simplified)
int c = 1;
while(1)
{
if(kbhit())
{
c = getch();
if(c == 27) exit(1);
if(c == 'a' || c == 'A') movepadleft();
if(c == 'd' || c == 'D') movepadright();
}
movetheball();
}
The thing that i don't like is: when i press a or d the pad moves for a short period of time, then stops and then moves constantly as long as i keep the button pressed.(it is the same thing just if i open notepad and press a letter: the letter i press apears, then there is a pause and then it prints that letter al long as i keep the button pressed).
This function(getch()) is not suitable for this problem? What alternatives i have to solve the problem?
Was This Post Helpful? 1


#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon

Reputation: 137
  • View blog
  • Posts: 4,622
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Getch() Function

Post icon  Posted 15 January 2005 - 08:59 PM

You can use GetASyncKeyState() API in windows.h or kbhit() which is in conio.h

But GetASyncKeyState() takes an ASCII code as a parameter and returns 1 or 0 depending on whether it was pressed or not.

kbhit() takes no parameter but returns 1 or 0 depending on whether any key was pressed or not.Here's an example which shows kbhit() functionality.

Run the program....Within 3 Seconds if you press Any Key...the ZERO will Turn into 1. And I guess u now know why...any trouble let me know.
#include <conio.h>
#include <stdio.h>
#include <windows.h>


void main()
{
 long time=GetTickCount()+3000;
 while(time>=GetTickCount())
 {
   gotoxy(13,13);
   printf("%d",kbhit());
 }
 getch();
 }


Was This Post Helpful? 0
  • +
  • -

#3 qUarC  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 14-January 05


Dream Kudos: 0

Re: Getch() Function

Posted 16 January 2005 - 12:07 PM

What function should i use if i want the pad to move constantly as long as i keep a button pressed?
Was This Post Helpful? 0
  • +
  • -

#4 Link-Jun  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-January 05


Dream Kudos: 0

Re: Getch() Function

Posted 22 January 2005 - 07:09 AM

Correct me if I'm wrong, but since the GetASyncKeyState() function returns a 1 or 0, I believe you could use the function like this:

while (GetASyncKeyState([desired key]))
{
[move pad desired direction]
}


This post has been edited by Link-Jun: 22 January 2005 - 07:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon

Reputation: 137
  • View blog
  • Posts: 4,622
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Getch() Function

Posted 26 January 2005 - 10:47 AM

GetASyncKeyState would be perfect for yur use.
The previous post is perfect for you...it even shows u how to use it in yur code....in while loops etc.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users