Simple 2D Pinball Game using C/C++

Simple 2D Pinball Game with C\C++ :  Following , is a simple 2D Pinball game(like DX-Ball,Para etc.) , created , using the BGI graphics library of Turbo C/C++ 3.0.

While running this program , make sure using 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++ Code :

 #include"stdio.h"

#include"conio.h"

#include"ctype.h"

#include"process.h"

#include"alloc.h"

#include"dos.h"

#include"stdlib.h"

#include"graphics.h"

#define NULL 0

#define yes 1

#define no 0

char mmas[25];

int bri[15][20];

int s=1;

int score2=0;

char m;

int stage=1;

char mnmn[5]={'M','A','S','U','M'};

struct record{

int rscore;

char rname[30];

};

struct record record_book[10];

void cursor_on(void)

{

 union REGS mouse;

 mouse.x.ax=0;

 int86(0X33,&mouse,&mouse);

 }

 void cursor_off(void)

{

 union REGS mouse;

 mouse.x.ax=2;

 int86(0X33,&mouse,&mouse);

 }

 int leftb_pressed(void)

{

 union REGS r;

 r.x.ax=3;

 int86(0X33,&r,&r);

 return(r.x.bx&1);

 }

 void mouse_position(int *mousex,int *mousey)

{

 union REGS mouse;

 mouse.x.ax=3;

 int86(0X33,&mouse,&mouse);

 *mousex=mouse.x.cx;

 *mousey=mouse.x.dx;

}

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,160,"Score");

outtextxy(80,160,"Name");

setcolor(11);

settextstyle(0,0,1);

for(t=0;t<10;t++)

{

sprintf(M3,"%d)",t+1);

if(t==9)

outtextxy(60,185+12*t,M3);

else

outtextxy(65,185+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,185+12*t,M1);

  else if(record_book[t].rscore>=1000)

  outtextxy(225,185+12*t,M1);

  else if(record_book[t].rscore>=10000)

  outtextxy(220,185+12*t,M1);

  else

  outtextxy(230,185+12*t,M1);



  outtextxy(90,185+12*t,M2);

  }

 }

 fclose(fp);

}

void entry(int mscore)

{

FILE *fp;

int t,k;

int i,nnn=0;

char m1[10],m2[30];

cleardevice();

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,11);

  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,1,"High Scores");

setcolor(3);

settextstyle(0,0,3);

outtextxy(82,130,"NAME");

outtextxy(362,130,"SCORE");

setcolor(10);

settextstyle(0,0,1);

  for(i=0;i<10;i++)

{

 if(record_book[i].rscore)

 {

  sprintf(m1,"%d",record_book[i].rscore);

  outtextxy(392,170+15*i,m1);

  sprintf(m2,"%s",record_book[i].rname);

  outtextxy(102,170+15*i,m2);

  }

 }

  while(!leftb_pressed())

  {

  setcolor(11);

  sprintf(m1,"%d",record_book[t].rscore);

  outtextxy(392,170+15*t,m1);

  sprintf(m2,"%s",record_book[t].rname);

  outtextxy(102,170+15*t,m2);

  delay(350);

  setcolor(0);

  sprintf(m1,"%d",record_book[t].rscore);

  outtextxy(392,170+15*t,m1);

  sprintf(m2,"%s",record_book[t].rname);

  outtextxy(102,170+15*t,m2);

  delay(350);

  }

  }

  fclose(fp);

}

drawbrick(int lx,int ly)

{

setcolor(s);

rectangle(lx,ly,lx+31,ly+9);

rectangle(lx+2,ly+2,lx+31-2,ly+9-2);

setfillstyle(1,s+1);

floodfill(lx+1,ly+1,s);

setfillstyle(s,9);

bar(lx+6,ly+4,lx+31-6,ly+9-4);

setfillstyle(11,3);

return(lx,ly);

}

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:

 for(i=0;i<15;i++)

 {

  n=random(7);

  sound(octave[n]*4);

  delay(100);

  }

 nosound();

 break;

 case 3:

 while(!leftb_pressed())

 {

  n=random(7);

  sound(octave[n]*4);

  delay(100);

  }

 nosound();

 while(leftb_pressed());

 break;

 case 4:

 for(i=4;i>=0;i--)

 {

  sound(octave[i]*4);

  delay(15);

  }

  nosound();

  break;

 case 5:

 sound(octave[6]*2);

 delay(50);

 nosound();

 }

 return(type);

 }

 bricks()

{

int i,j,lx=0,ly=0;

for(i=0;i<5;i++)

{

 for(j=0;j<20;j++)

 {

  drawbrick(lx,ly);

  lx=lx+32;

  delay(20);

  }

 lx=0;

 ly=ly+10;

 }

 return 0;

}

erasebrick(int b,int l)

{

setfillstyle(1,0);

bar(b*32,l*10,(b*32)+31,(l*10)+9);

return(b,l);

}

art()

{ void *q;

  int k,maxu,maxv,u=25,v=25,du=1,dv=1,area1,maxx=getmaxx(),maxy=getmaxy(),midx=maxx/2,midy=maxy/2;;

  setfillstyle(SOLID_FILL,RED);

  circle(50,50,25);

  floodfill(50,50,WHITE);

  area1=imagesize(u,v,75,75);

  q=malloc(area1);

  getimage(u,v,75,75,q);

  maxu=getmaxx();

  maxv=getmaxy();

  setcolor(getmaxcolor());

  setfillstyle(SOLID_FILL,getmaxcolor());

  setcolor(3);

  for(k=0;k<=10;k++)

  {

  rectangle(k,k,maxu-k,maxv-k);

  }

  setcolor(9);

  rectangle(0,0,maxu,maxv);

  setcolor(3);

  outtextxy(midx-60,midy-120,"CONVENER:");

  setcolor(12);

  settextstyle(0,HORIZ_DIR,2);

  outtextxy((maxx/2)-145,(maxy/2)-100,"S.M. SHAMIM HASAN");

  setcolor(6);

  settextstyle(0,HORIZ_DIR,1);

  outtextxy((maxx/2)-90,(maxy/2)-10,"WELLCOME TO MY GAME");

  outtextxy((maxx/2)-110,(maxy/2)+10,"PRESS ANY KEY TO CONTINUE...");

  setcolor(9);

  settextstyle(10,HORIZ_DIR,1);

  outtextxy((maxx/2)-280,(maxy/2)-80,"PATUAKHALI POLYTECHNIC INSTITUTE");

  settextstyle(0,HORIZ_DIR,1);

  setcolor(3);

  outtextxy((maxx/2)-60,(maxy/2)+50,"CREATED BY...");

  setcolor(11);

  settextstyle(0,HORIZ_DIR,2);

  outtextxy((maxx/2)-170,(maxy/2)+70,"MD. MOHIUDDIN AHMED.");

  setcolor(9);

  settextstyle(0,HORIZ_DIR,1);

  outtextxy((maxx/2)-90,(maxy/2)+100,"COMPUTER TECHNOLOGY");

  setcolor(9);

  setfillstyle(1,0);

 while(!leftb_pressed())

 {

 putimage(u,v,q,XOR_PUT);

 u=u+(du*5);

 v=v+(dv*2);

 putimage(u,v,q,XOR_PUT);

 if(u>maxu-60||u<15)

 {

 music(5);

 du=-du;

 }

 if(v>maxv-55||v<7)

 {

 music(5);

 dv=-dv;

 }

 delay(15);

 }

  while(leftb_pressed());

  free(q);

  cleardevice();

  return 0;

  }

int mainscreen()

{

int num;

char *word;

FILE *fp1,*fp2;

char ch;

void *mouse;

int mus0=0,mus1=0,mus2=0,mus3=0,mus4=0,area,maxx=getmaxx(),maxy=getmaxy(),midx=maxx/2,midy=maxy/2;

int mballx=midx-298,mbally=maxy-191;

int ff[12][40]={0,0,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0};

int i,j,lx=0,ly=30;

setcolor(14);

circle(10,10,7);

setfillstyle(1,12);

floodfill(10,10,14);

area=imagesize(3,3,17,17);

mouse=malloc(area);

if(mouse==NULL)

{

printf("Insufficient memory space.\n");

getch();

exit(1);

}

getimage(3,3,17,17,mouse);

cleardevice();

setcolor(3);

rectangle(0,0,maxx,maxy);

rectangle(2,2,maxx-2,maxy-2);

setcolor(12);

rectangle(7,4,maxx-7,20);

setcolor(11);

outtextxy(midx-105,midy-165,"Use the mouse to continue...");

setcolor(1);

for(i=0;i<12;i++)

{

 for(j=0;j<40;j++)

 {

  if(ff[i][j])

  rectangle(lx,ly,lx+17,ly+9);

  lx=lx+17;

  delay(5);

  }

 lx=0;

 ly=ly+10;

 }

 setfillstyle(3,3);

 bar(22,112,622,108);

 setcolor(3);

 settextstyle(0,HORIZ_DIR,2);

 outtextxy(midx-235,midy-30,"Win this game by scoring 7500.");

 setcolor(9);

 settextstyle(0,HORIZ_DIR,1);

 outtextxy(midx-300,midy+18,"Programmer:");

 outtextxy(midx-300,midy+40,"MD. Mohiuddin ahmed.");

 outtextxy(midx-300,midy+55,"Computer Technology.");

 outtextxy(midx-300,midy+70,"Patuakhali Polytechnic Institute.");

 outtextxy(midx-300,midy+100,"COM-105553");

 setcolor(3);

 line(0,maxy-10,maxx,maxy-10);

 setfillstyle(1,12);

 setcolor(12);

 rectangle(midx-25,maxy-7-12,midx+25,maxy-12);

 floodfill(midx,maxy-1-12,12);

 setcolor(11);

 circle(320,320,12);

 setfillstyle(1,3);

 floodfill(320,320,11);

 music(3);

 masum6:

 setviewport(midx-155,midy-168,midx+115,midy-156,1);

 clearviewport();

 setviewport(0,0,maxx,maxy,1);

 setcolor(11);

 outtextxy(midx-30,midy-165,"SELECT:");

 settextstyle(0,HORIZ_DIR,2);

 setcolor(12);

 setviewport(3,125-12,maxx-3,maxy-3,1);

 clearviewport();

 setviewport(0,0,maxx,maxy,1);

 outtextxy(midx-290,midy-40,"Select any of the following:");

 setcolor(9);

 outtextxy(midx-284,midy-20,"Play");

 outtextxy(midx-284,midy,"Help");

 outtextxy(midx-284,midy+20,"High Score");

 outtextxy(midx-284,midy+40,"Credit");

 outtextxy(midx-284,midy+60,"Exit");

 cursor_on();

 while(1)

 {

 mouse_position(&mballx,&mbally);

 if(mballx>=maxx-14)

 mballx=maxx-14;

 if(mbally>=maxy-14)

 mbally=maxy-14;

 putimage(mballx,mbally,mouse,XOR_PUT);

 if(mballx>=midx-290&&mbally>=midy-22&&mballx<=midx-213&&mbally<=midy-5)

 {

 if(mus0==0)

 {

 sound(400);

 delay(30);

 nosound();

 mus0=1;

 }

 setcolor(11);

 outtextxy(midx-284,midy-20,"Play");

 if(leftb_pressed())

 goto masum5;

 }

 else

 {

 mus0=0;

 setcolor(9);

 outtextxy(midx-284,midy-20,"Play");

 }

 if(mballx>=midx-290&&mbally>=midy-2&&mballx<=midx-213&&mbally<=midy+15)

 {

 if(mus1==0)

 {

 sound(400);

 delay(30);

 nosound();

 mus1=1;

 }

 setcolor(11);

 outtextxy(midx-284,midy,"Help");

 if(leftb_pressed())

 break;

 }

 else

 {

 mus1=0;

 setcolor(9);

 outtextxy(midx-284,midy,"Help");

 }

  if(mballx>=midx-290&&mbally>=midy+18&&mballx<=midx-123&&mbally<=midy+35)

 {

 if(mus2==0)

 {

 sound(400);

 delay(30);

 nosound();

 mus2=1;

 }

 setcolor(11);

 outtextxy(midx-284,midy+20,"High Score");

 if(leftb_pressed())

 break;

 }

 else



------------------------------------------------

//sorry for incomplete code. Please download full code from  following link…

 Download

No comments:

Post a Comment