Simple 2D Racing Game with C\C++ : The following , is a simple 2D racing game , created , using the BGI graphics library of Turbo C/C++ 3.0 :
While running this program , make sure that, you used the correct path for the BGI graphics driver .For example : if Turbo C/C++ is installed in the following location "C:\TC" , then the BGI graphics path should be "C:\\TC\\BGI" :
Screenshot :
C++ Codes :
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <ctype.h>
#define r 20
#define tr 35
#define jr 25
int x=140,y=400;
int num;
FILE *fp1,*fp2;
int ccar1=2,ccar2=11,ccar3=9,ccar4=12,ctruck=15,cjeep=11,cbus=6;
long int score=0;
void *car,*car1,*road,*truck,*jeep,*bus,*car2,*car3,*car4;
char mnmn[5]={'M','A','S','U','M'};
struct record{
int rscore;
char rname[30];
};
struct record record_book[10];
void init_list(void)
{
int t;
for(t=0;t<10;t++)
record_book[t].rscore=0;
}
void rcd_info()
{
FILE *fp;
int t;
int i,k;
char M1[10],M2[30],M3[5];
if((fp=fopen("C:\\RCDBOOK.MYG","r"))==NULL)
{
fp=fopen("C:\\RCDBOOK.MYG","w");
for(i=500,t=0;t<10;i-=50,t++)
{
record_book[t].rscore=i;
for(k=0;k<5;k++)
record_book[t].rname[k]=mnmn[k];
}
fwrite(record_book,sizeof record_book,1,fp);
fclose(fp);
fp=fopen("C:\\RCDBOOK.MYG","r");
}
fread(record_book,sizeof record_book,1,fp);
setcolor(11);
settextstyle(0,0,2);
outtextxy(210,220,"Score");
outtextxy(80,220,"Name");
setcolor(11);
settextstyle(0,0,1);
for(t=0;t<10;t++)
{
sprintf(M3,"%d)",t+1);
if(t==9)
outtextxy(60,245+12*t,M3);
else
outtextxy(65,245+12*t,M3);
}
setcolor(10);
for(t=0;t<10;t++)
{
if(record_book[t].rscore)
{
sprintf(M1,"%d",record_book[t].rscore);
sprintf(M2,"%s",record_book[t].rname);
if(record_book[t].rscore<100)
outtextxy(235,245+12*t,M1);
else if(record_book[t].rscore>=1000)
outtextxy(225,245+12*t,M1);
else if(record_book[t].rscore>=10000)
outtextxy(220,245+12*t,M1);
else
outtextxy(230,245+12*t,M1);
outtextxy(90,245+12*t,M2);
}
}
fclose(fp);
}
void entry(int mscore)
{
FILE *fp;
int t,k;
int i,nnn=0;
char m1[10],m2[30];
if((fp=fopen("C:\\RCDBOOK.MYG","r"))==NULL)
{
fp=fopen("C:\\RCDBOOK.MYG","w");
for(i=500,t=0;t<10;i-=50,t++)
{
record_book[t].rscore=i;
for(k=0;k<5;k++)
record_book[t].rname[k]=mnmn[k];
}
fwrite(record_book,sizeof(record_book),1,fp);
fclose(fp);
fp=fopen("C:\\RCDBOOK.MYG","r");
}
fread(record_book,sizeof(record_book),1,fp);
for(t=0;t<10;t++)
{
if(mscore>record_book[t].rscore)
{
fclose(fp);
fp=fopen("C:\\RCDBOOK.MYG","w");
for(i=10-1;i>=t;i--)
{
record_book[i].rscore=record_book[i-1].rscore;
for(k=0;k<30;k++)
record_book[i].rname[k]=record_book[i-1].rname[k];
}
record_book[t].rscore=mscore;
nnn=1;
settextstyle(0,0,2);
outtextxy(100,100," You have a high score.");
outtextxy(100,120,"Enter your name:");
gotoxy(30,10);
scanf("%s",record_book[t].rname);
fwrite(record_book,sizeof(record_book),1,fp);
fclose(fp);
fp=fopen("C:\\RCDBOOK.MYG","r");
fread(record_book,sizeof record_book,1,fp);
break;
}
}
cleardevice();
if(nnn==1)
{
setcolor(9);
settextstyle(10,0,6);
outtextxy(60,2,"High Scores");
setcolor(3);
settextstyle(0,0,3);
outtextxy(82,140,"NAME");
outtextxy(362,140,"SCORE");
setcolor(2);
settextstyle(0,0,2);
for(i=0;i<10;i++)
{
if(record_book[i].rscore)
{
sprintf(m1,"%d",record_book[i].rscore);
outtextxy(382,180+25*i,m1);
sprintf(m2,"%s",record_book[i].rname);
outtextxy(92,180+25*i,m2);
}
}
while(!kbhit())
{
setcolor(11);
sprintf(m1,"%d",record_book[t].rscore);
outtextxy(382,180+25*t,m1);
sprintf(m2,"%s",record_book[t].rname);
outtextxy(92,180+25*t,m2);
delay(350);
setcolor(0);
sprintf(m1,"%d",record_book[t].rscore);
outtextxy(382,180+25*t,m1);
sprintf(m2,"%s",record_book[t].rname);
outtextxy(92,180+25*t,m2);
delay(350);
}
}
fclose(fp);
}
music(int type)
{
float octave[7]={130.81,146.83,164.81,174.61,196,220,246.94};
int n,i;
switch(type)
{
case 1:
for(i=0;i<7;i++)
{
sound(octave[i]*8);
delay(30);
}
nosound();
break;
case 2:
fflush(stdin);
if(getch()==0)
getch();
for(i=0;i<15;i++)
{
n=random(7);
sound(octave[n]*4);
delay(100);
}
nosound();
break;
case 3:
while(!kbhit())
{
n=random(7);
sound(octave[n]*4);
delay(100);
}
fflush(stdin);
if(getch()==0)
getch();
nosound();
break;
case 4:
for(i=4;i>=0;i--)
{
sound(octave[i]*4);
delay(15);
}
nosound();
break;
case 5:
sound(octave[6]*2);
delay(20);
nosound();
}
return(type);
}
void gameover()
{
char *word,B4[100],M1[30];
cleardevice();
setcolor(3);
settextstyle(0,HORIZ_DIR,3);
outtextxy(200,200,"GAME OVER");
music(2);
cleardevice();
sprintf(M1,"YOUR SCORE: %d",score);
outtextxy(100,100,M1);
music(3);
cleardevice();
entry(score);
}
void draw_car1(int u, int v)
{
moveto(u, v);
setcolor(ccar1);
rectangle(u,v,u+r,v+2*r);
rectangle(u+1,v+2,u+4,v+3);
rectangle(u+r-4,v+2,u+r-1,v+3);
line(u,v+12,u+r,v+12);
line(u+2,v+20,u+r-2,v+20);
line(u+2,v+30,u+r-2,v+30);
line(u,v+35,u+r,v+35);
line(u+2,v+20,u+2,v+30);
line(u+r-2,v+20,u+r-2,v+30);
line(u,v+12,u+2,v+20);
line(u+r,v+12,u+r-2,v+20);
line(u+2,v+30,u,v+35);
line(u+r-2,v+30,u+r,v+35);
line(u,v+r+5,u+2,v+r+5);
line(u+r-2,v+r+5,u+r,v+r+5);
}
void draw_jeep(int c,int d)
{
moveto(c,d);
setcolor(cjeep);
rectangle(c,d,c+jr,d+2*jr);
rectangle(c+1,d+2,c+4,d+3);
rectangle(c+jr-4,d+2,c+jr-1,d+3);
line(c,d+17,c+jr,d+17);
rectangle(c+2,d+25,c+jr-2,d+47);
line(c,d+17,c+2,d+25);
line(c+jr,d+17,c+jr-2,d+25);
line(c,d+2*jr,c+2,d+47);
line(c+jr-2,d+47,c+jr,d+2*jr);
line(c,d+35,c+2,d+35);
line(c+jr-2,d+35,c+jr,d+35);
line(c+jr/2+1,d+47,c+jr/2+1,d+2*jr);
setfillstyle(1,cjeep);
bar(c+5,d+47,c+(jr/2)-2,d+50);
}
void draw_car(int x, int y)
{
moveto(x, y);
setcolor(0);
setfillstyle(1,3);
bar(x,y,x+r,y+2*r);
rectangle(x+1,y+2,x+4,y+3);
rectangle(x+r-4,y+2,x+r-1,y+3);
line(x,y+12,x+r,y+12);
line(x+2,y+20,x+r-2,y+20);
line(x+2,y+30,x+r-2,y+30);
line(x,y+35,x+r,y+35);
line(x+2,y+20,x+2,y+30);
line(x+r-2,y+20,x+r-2,y+30);
line(x,y+12,x+2,y+20);
line(x+r,y+12,x+r-2,y+20);
line(x+2,y+30,x,y+35);
line(x+r-2,y+30,x+r,y+35);
}
void reset()
{
free(car);
free(car1);
free(car2);
free(car3);
free(car4);
free(road);
free(truck);
free(jeep);
free(bus);
}
void art()
{
setcolor(3);
settextstyle(5,0,1);
outtextxy(210,130,"Racing");
setcolor(12);
settextstyle(10,0,8);
outtextxy(70,10,"MYGAME");
draw_car(260,220);
draw_car1(340,220);
draw_jeep(297,210);
setcolor(9);
settextstyle(0,0,1);
outtextxy(235,300,"WELLCOME TO MY GAME");
outtextxy(235,320," PRESS ANY KEY...");
setcolor(8);
settextstyle(0,0,2);
outtextxy(100,380,"CREATED BY...");
setcolor(3);
settextstyle(0,0,3);
outtextxy(150,420,"MD. MOHIUDDIN AHMED");
while(!kbhit()){ }
fflush(stdin);
if(getch()==0)
getch();
}
void mainscreen()
{
union REGS iii,ooo,im,om;
char *word,B4[100];
char ch;
int maxx=getmaxx(),maxy=getmaxy(),midx=maxx/2,midy=maxy/2;
int mbally=maxy-191;
int m5=0;
int chm=1;
masum6:
setviewport(1,2,maxx,3,1);
clearviewport();
setviewport(0,0,maxx,maxy,1);
setcolor(11);
settextstyle(0,0,2);
outtextxy(250,15,"SELECT:");
settextstyle(0,HORIZ_DIR,2);
while(1)
{
if(m5==0)
{
setviewport(1,170,maxx,maxy-100,1);
clearviewport();
setviewport(0,0,maxx,maxy,1);
m5=1;
}
if(chm==1)
{
setcolor(11);
settextstyle(0,HORIZ_DIR,2);
outtextxy(midx-284,midy-20,"PLAY");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy,"HELP");
outtextxy(midx-284,midy+20,"HIGH SCORE");
outtextxy(midx-284,midy+40,"CREDIT");
outtextxy(midx-284,midy+60,"EXIT");
}
if(chm==2)
{
setcolor(11);
outtextxy(midx-284,midy,"HELP");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy-20,"PLAY");
outtextxy(midx-284,midy+20,"HIGH SCORE");
outtextxy(midx-284,midy+40,"CREDIT");
outtextxy(midx-284,midy+60,"EXIT");
}
if(chm==3)
{
setcolor(11);
outtextxy(midx-284,midy+20,"HIGH SCORE");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy,"HELP");
outtextxy(midx-284,midy-20,"PLAY");
outtextxy(midx-284,midy+40,"CREDIT");
outtextxy(midx-284,midy+60,"EXIT");
}
if(chm==4)
{
setcolor(11);
outtextxy(midx-284,midy+40,"CREDIT");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy,"HELP");
outtextxy(midx-284,midy+20,"HIGH SCORE");
outtextxy(midx-284,midy+60,"EXIT");
outtextxy(midx-284,midy-20,"PLAY");
}
if(chm==5)
{
setcolor(11);
outtextxy(midx-284,midy+60,"EXIT");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy,"HELP");
outtextxy(midx-284,midy+20,"HIGH SCORE");
outtextxy(midx-284,midy+40,"CREDIT");
outtextxy(midx-284,midy-20,"PLAY");
}
if(kbhit())
{
music(4);
iii.h.ah=0;
int86(22,&iii,&ooo);
if(ooo.h.ah==72)
{
if(mbally==maxy-191)
{
setviewport(midx-305,midy-25,midx-305+14,midy+90,1);
clearviewport();
setviewport(0,0,maxx,maxy,1);
mbally=maxy-191;
setcolor(11);
outtextxy(midx-284,midy-20,"PLAY");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy,"HELP");
outtextxy(midx-284,midy+20,"HIGH SCORE");
outtextxy(midx-284,midy+40,"CREDIT");
outtextxy(midx-284,midy+60,"EXIT");
chm=1;
}
if(mbally==maxy-171)
{
setviewport(midx-305,midy-25,midx-305+14,midy+90,1);
clearviewport();
setviewport(0,0,maxx,maxy,1);
mbally=maxy-191;
setcolor(11);
outtextxy(midx-284,midy-20,"PLAY");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy,"HELP");
outtextxy(midx-284,midy+20,"HIGH SCORE");
outtextxy(midx-284,midy+40,"CREDIT");
outtextxy(midx-284,midy+60,"EXIT");
chm=1;
}
if(mbally==maxy-151)
{
setviewport(midx-305,midy-25,midx-305+14,midy+90,1);
clearviewport();
setviewport(0,0,maxx,maxy,1);
mbally=maxy-171;
setcolor(11);
outtextxy(midx-284,midy,"HELP");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy-20,"PLAY");
outtextxy(midx-284,midy+20,"HIGH SCORE");
outtextxy(midx-284,midy+40,"CREDIT");
outtextxy(midx-284,midy+60,"EXIT");
chm=2;
}
if(mbally==maxy-131)
{
setviewport(midx-305,midy-25,midx-305+14,midy+90,1);
clearviewport();
setviewport(0,0,maxx,maxy,1);
mbally=maxy-151;
setcolor(11);
outtextxy(midx-284,midy+20,"HIGH SCORE");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy,"HELP");
outtextxy(midx-284,midy-20,"PLAY");
outtextxy(midx-284,midy+40,"CREDIT");
outtextxy(midx-284,midy+60,"EXIT");
chm=3;
}
if(mbally==maxy-111)
{
setviewport(midx-305,midy-25,midx-305+14,midy+90,1);
clearviewport();
setviewport(0,0,maxx,maxy,1);
mbally=maxy-131;
setcolor(11);
outtextxy(midx-284,midy+40,"CREDIT");
setcolor(12);
outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");
setcolor(9);
outtextxy(midx-284,midy,"HELP");
outtextxy(midx-284,midy-20,"PLAY");
outtextxy(midx-284,midy+20,"HIGH SCORE");
//Sorry for incomplete code ................please download the full cpp source file
//from my google docs ......... link :
Download
No comments:
Post a Comment