| | |
acceleration and brakes in a car game
![]() |
•
•
Join Date: Jul 2004
Posts: 7
Reputation:
Solved Threads: 0
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);
}
}
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);
}
}
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 50
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
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
•
•
Join Date: Jul 2004
Posts: 7
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2004
Posts: 17
Reputation:
Solved Threads: 0
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).
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).
•
•
Join Date: Jul 2004
Posts: 7
Reputation:
Solved Threads: 0
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?
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?
•
•
•
•
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?
Alex Cavnar, aka alc6379
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.
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.
•
•
•
•
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.
It's not wrong not knowing, but it's wrong not wanting to know.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Newbie need help on HW
- Next Thread: in need of some hw help
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






