acceleration and brakes in a car game

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 7
Reputation: 3maddy3 is an unknown quantity at this point 
Solved Threads: 0
3maddy3 3maddy3 is offline Offline
Newbie Poster

acceleration and brakes in a car game

 
2
  #1
Jul 6th, 2004
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);
}
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: acceleration and brakes in a car game

 
0
  #2
Jul 7th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 7
Reputation: 3maddy3 is an unknown quantity at this point 
Solved Threads: 0
3maddy3 3maddy3 is offline Offline
Newbie Poster

Re: acceleration and brakes in a car game

 
0
  #3
Jul 7th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 17
Reputation: cypher is an unknown quantity at this point 
Solved Threads: 0
cypher cypher is offline Offline
Newbie Poster

Re: acceleration and brakes in a car game

 
1
  #4
Jul 8th, 2004
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).
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 7
Reputation: 3maddy3 is an unknown quantity at this point 
Solved Threads: 0
3maddy3 3maddy3 is offline Offline
Newbie Poster

Re: acceleration and brakes in a car game

 
0
  #5
Jul 8th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: acceleration and brakes in a car game

 
0
  #6
Jul 8th, 2004
Originally Posted by 3maddy3
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.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: acceleration and brakes in a car game

 
0
  #7
Jul 8th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 4
Reputation: BlackDeath is an unknown quantity at this point 
Solved Threads: 0
BlackDeath's Avatar
BlackDeath BlackDeath is offline Offline
Newbie Poster

Re: acceleration and brakes in a car game

 
0
  #8
Jul 8th, 2004
Originally Posted by Chainsaw
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.
It's not wrong not knowing, but it's wrong not wanting to know.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 7
Reputation: 3maddy3 is an unknown quantity at this point 
Solved Threads: 0
3maddy3 3maddy3 is offline Offline
Newbie Poster

Re: acceleration and brakes in a car game

 
0
  #9
Jul 13th, 2004
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC