943,537 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8865
  • C++ RSS
Jul 6th, 2004
1

acceleration and brakes in a car game

Expand Post »
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);
}
}
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
3maddy3 is offline Offline
7 posts
since Jul 2004
Jul 7th, 2004
0

Re: acceleration and brakes in a car game

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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Jul 7th, 2004
0

Re: acceleration and brakes in a car game

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
Reputation Points: 12
Solved Threads: 0
Newbie Poster
3maddy3 is offline Offline
7 posts
since Jul 2004
Jul 8th, 2004
1

Re: acceleration and brakes in a car game

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).
Reputation Points: 14
Solved Threads: 0
Newbie Poster
cypher is offline Offline
17 posts
since Jul 2004
Jul 8th, 2004
0

Re: acceleration and brakes in a car game

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?
Reputation Points: 12
Solved Threads: 0
Newbie Poster
3maddy3 is offline Offline
7 posts
since Jul 2004
Jul 8th, 2004
0

Re: acceleration and brakes in a car game

Quote 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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Jul 8th, 2004
0

Re: acceleration and brakes in a car game

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.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Jul 8th, 2004
0

Re: acceleration and brakes in a car game

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BlackDeath is offline Offline
4 posts
since Jul 2004
Jul 13th, 2004
0

Re: acceleration and brakes in a car game

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.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
3maddy3 is offline Offline
7 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Newbie need help on HW
Next Thread in C++ Forum Timeline: in need of some hw help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC