hi all,
below is a part of a car game am developing.can any one tell me how to accelerate my car while pressing a key(in this case,its'a') n also how to decelarate it? also tell me how to mov up or down while still pressing the 'a' key.i have to release the 'a' key n then do it in this program.
thnx.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void rect(int a, int b,int c,int cr);
void tria(int a, int b,int c,int cr);
void erase(int a,int b,int i);
void main()
{
 clrscr();
 char ch;
 int x=-50,y=240;
 int i,gd=DETECT,gm;
 initgraph(&gd,&gm,"\\tc\\bgi");
 for(i=1;i<=1000;i++)
 {
  ch=getch();
  if(ch=='a')
  {
   erase(x,y,i-1);
   rect(x,y,i*5,14);
   tria(x,y,i*5,15);
   delay(1000/i);
  }
  else if(ch=='z')
  {
   erase(x,y,i-1);
   y+=2;
   rect(x,y,i*5,14);
   tria(x,y,i*5,15);
  }
  else if(ch=='q')
  {
   erase(x,y,i-1);
   y-=2;
   rect(x,y,i*5,14);
   tria(x,y,i*5,15);
  }
 }
 sound(500);
 delay(500);
 nosound();
 getch();
 closegraph();
}
void erase(int x,int y,int j)
{
 rect(x,y,j*5,0);
 tria(x,y,j*5,0);
}
void rect(int a,int b,int c, int cr)
{
  setcolor(cr);
  setlinestyle(SOLID_LINE,0,1);
  setfillstyle(SOLID_FILL,cr);
  int div=(a+50+c)/620;
  if(div<1)
   rectangle(a-10+c,b-10,c+a+50,b+10);
  else
   rectangle(a-10+c-620*div,b-10,a+50+c-620*div,b+10);
  floodfill(a+c,b,BLACK);
}
void tria(int a,int b,int c,int cr)
{
  int div=(a+50+c)/620,tri[6];
  if(div<1)
  {
    int tri[]={a-50+c,b-15,
       a-10+c,b,
       a-50+c,b+15};
    setcolor(BLACK);
   setfillstyle(SOLID_FILL,cr);
   fillpoly(3,tri);
  }
  else
  {
   int tri[]={a-50+c-620*div,b-15,
      a-10+c-620*div,b,
      a-50+c-620*div,b+15};
   setcolor(BLACK);
   setfillstyle(SOLID_FILL,cr);
   fillpoly(3,tri);
  }
}

Recommended Answers

All 8 Replies

Hello,

I would think you would want to have the user hit the a key multiple times, instead of having them hold down the key. If they just hold down the key, you are relying on a keyboard repeat rate, and that will lead to different responses on different systems.

In your game code, you are going to need to put a little loop in there to check if there is keyboard input before calculating where the car is next.

Enjoy,

Christian

hi,
kc0arf:thanx for the info...but i dont want the user to hit the 'a' key multiple times.i jus want him to hold it down n at the same time move it up or down usin the 'q' n 'z' keys.what happens in my code is that once the 'q' or 'z' key is pressed, the car fails to move forward. i wanna correct this..n also, i dont understand the loop u mentioned..how am i gonna use it?i wud be greateful if u cud give me the details..
thanx

Are you asking for how to check for the keys, or a formula for determing the rate of accelleration?

If you're looking for a formula, that can be as simple as decreasing the speed by X% for each loop (where X also increases with each iteration of the loop), or it can be VERY complex (using real physics).

hi,
cypher:thanx for showin interest.i think i didnt make my point clear.all i want to do is to make my car move faster or slower according to the input.its jus like any other car game..u keep pressing a key n the car moves faster. n the moment u release the key,it slows down.n u cud also move ur car left or rite while still acceleratin it..jus like in roadrash or similar games..this,am not able to do in my program..can u help?

hi,
cypher:thanx for showin interest.i think i didnt make my point clear.all i want to do is to make my car move faster or slower according to the input.its jus like any other car game..u keep pressing a key n the car moves faster. n the moment u release the key,it slows down.n u cud also move ur car left or rite while still acceleratin it..jus like in roadrash or similar games..this,am not able to do in my program..can u help?

I'm not trying to knock you or anything, but cutting back on your abbreviations and minding your punctuation would really help when asking a question like this. I don't know of any particular method of helping you, but I'd hate to know that someone here who could help you skips over this thread because they can't really understand what you're typing.

If you are using Windows, you want to check out:

GetKeyState(); // checks state of a single key
GetKeyboardState(); // checks state of all 256 keys
GetAsynchKeyState(); // checks real-time CURRENT state of a single key.

So, you'd call one of those to get the key state(s) you care about, and sample the current system time to see how long the key has been held down, and do your accelleration calcs from that.

If you are using Windows, you want to check out:

GetKeyState(); // checks state of a single key
GetKeyboardState(); // checks state of all 256 keys
GetAsynchKeyState(); // checks real-time CURRENT state of a single key.

So, you'd call one of those to get the key state(s) you care about, and sample the current system time to see how long the key has been held down, and do your accelleration calcs from that.

Note that your going to have to include windows.h to do that; buy the way why not learn windows and make your game windows based because I hate to tell you man but dos is dead.

hi,
cypher:thanks for your suggestions. i will try to better my way of posting my queries.
chainsaw and balckdeath: thanks for the info. i will try learning windows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.