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);
}
}