Skip to content

Instantly share code, notes, and snippets.

@pathikrit
Created June 24, 2013 09:38
Show Gist options
  • Save pathikrit/5848927 to your computer and use it in GitHub Desktop.
Save pathikrit/5848927 to your computer and use it in GitHub Desktop.
First "big" real code I ever wrote - for my 11th grade project at Sardar Patel Vidyalaya in 2003. It is a clone of Breakout written in Turbo C++ compiled using the Borland compiler. It ran (fast) on an old Pentium with 512kb of RAM. I have come a long way since then...
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
#include <ctype.h>
class player
{private: int color,x,y,width,height,psila;
public : player(int,int,int,int,int,int);
void init_size(int,int,int);
void paint_player(int,int);
void unpaint_player(int,int);
void init_psila();
void paint_psila();
int unpaint_psila();
int player_size();
};
player::player(int _color=13,int _x=300,int _y=454,int _width=72,int _height=10,int _psila=3)
{color=_color;
x=_x;
y=_y;
width=_width;
height=_height;
psila=_psila;
}
void player::paint_player(int _x,int _y)
{x=_x;
y=_y;
setfillstyle(2,color);
bar(x,y,x+width,y+height);
}
void player::unpaint_player(int _x,int _y)
{x=_x;
y=_y;
setfillstyle(2,BLACK);
bar(x,y,x+width,y+height);
}
void player::init_psila()
{psila=3;
setfillstyle(2,13);
bar(600,0,565,10);
bar(560,0,525,10);
}
void player::paint_psila()
{setfillstyle(2,13);
psila++;
if(psila==7) bar(400,0,365,10);
if(psila==6) bar(440,0,405,10);
if(psila==5) bar(480,0,445,10);
if(psila==4) bar(520,0,485,10);
if(psila==3) bar(560,0,525,10);
if(psila==2) bar(600,0,565,10);
}
int player::unpaint_psila()
{setfillstyle(2,BLACK);
psila--;
if(psila==6) bar(400,0,365,10);
if(psila==5) bar(440,0,405,10);
if(psila==4) bar(480,0,445,10);
if(psila==3) bar(520,0,485,10);
if(psila==2) bar(560,0,525,10);
if(psila==1) bar(600,0,565,10);
return psila;
}
void player::init_size(int _width,int _height,int _color)
{width=_width;
height=_height;
color=_color;
}
int player::player_size() { return width; }
class ball
{private: int color,radius,x,y;
public : ball(int,int,int,int);
void init_size(int _radius,int _color);
void paint_ball(int,int);
void unpaint_ball(int,int);
};
ball::ball(int _color=15,int _radius=4,int _x=335,int _y=450)
{color=_color;
radius=_radius;
x=_x;
y=_y;
}
void ball::paint_ball(int _x,int _y)
{x=_x;
y=_y;
setfillstyle(1,color);
fillellipse(x,y,radius,radius);
}
void ball::unpaint_ball(int _x,int _y)
{x=_x;
y=_y;
setfillstyle(1,BLACK);
fillellipse(x,y,radius,radius);
}
void ball::init_size(int _radius,int _color)
{color=_color;
radius=_radius;
}
struct HIGH_SCORE { char name[20],score[20]; };
class brick
{private: int x,y,enable,shape,destroy,height,width,bg;
public : void init(int,int,int,int,int,int,int);
void hide();
void init_destroy();
void undo_destroy();
void paint();
int is_in(int,int);
};
void brick::init(int _x,int _y,int w,int h,int _bg,int _shape=0,int _destroy=0) //initialize
{x=_x;
y=_y;
width=w;
height=h;
bg=_bg;
enable=1;
shape=_shape;
destroy=_destroy;
}
void brick::paint()
{if(shape) setfillstyle(10,bg);
else setfillstyle(1,bg);
bar(x, y, x+width-1, y+height-1);
setcolor(WHITE);
line(x, y, x+width-1, y);
line(x, y, x, y+height-1);
line(x, y+height-1, x+width-1, y+height-1);
line(x+width-1, y+height-1, x+width-1, y);
if(shape)
{setfillstyle(1,RED);
fillellipse(x+width/2,y+height/2,10,10);
setcolor(BLACK);
circle(x+width/2-5,y+height/2-3,1);
circle(x+width/2+5,y+height/2-3,1);
circle(x+width/2,y+height/2+1,1);
pieslice(x+width/2,y+height/2+5,180,360,3);
}
setcolor(BLACK);
}
int brick::is_in(int x1, int y1)
{if(x1>=x-5&&x1<=x+width+5&&y1<=y+height+5&&y1>=y-5&&destroy&&enable) return 5;
if(x1>=x-5&&x1<=x+width+5&&y1<=y+height+5&&y1>=y+height&&enable) return 1;
else if(x1>=x-5&&x1<=x+width+5&&y1<=y+5&&y1>=y&&enable) return 2;
else if(x1<=x+width+5&&x1>=x+width&&y1>=y-5&&y1<=y+height+5&&enable) return 3;
else if(x1>=x-5&&x1<=x&&y1>=y-5&&y1<=y+height+5&&enable) return 4;
return 0;
}
void brick::hide()
{setfillstyle(1,BLACK);
bar(x, y, x+width-1, y+height-1);
setcolor(BLACK);
line(x, y, x+width-1, y);
line(x, y, x, y+height-1);
line(x, y+height-1, x+width-1, y+height-1);
line(x+width-1, y+height-1, x+width-1, y);
setfillstyle(1,WHITE);
enable=0;
}
void brick::init_destroy() { destroy=1; }
void brick::undo_destroy() { destroy=0; }
brick array1[10][14];
player p;
ball b;
int x=300,y=460,x1=335,y1=460,angle,up_down,destroy,color_exchange=1,i1random_number,
number_of_bricks,sound_on_off=1,irandom_number,jrandom_number,j1random_number;
long result;
int psila();
char level[]={"1"};
char flag;
void graphic_init();
void start_position();
void start_screen();
void paint_bricks1();
void paint_bricks2();
void paint_bricks3();
void paint_bricks4();
void paint_bricks5();
void del_check();
void play();
void ball_hit();
void another_play();
void end_level();
void make_sound(int);
void bonus_check();
void bonus_type();
void game_is_over();
void high_score();
void main()
{graphic_init();
time_t t;
srand((unsigned)time(&t));
start_screen();
if((flag=getch())==27)
{closegraph();
exit(1);
}
cleardevice();
p.init_psila();
paint_bricks1();
start_position();
}
void graphic_init()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\DX-BALL\\Graphic-files");
}
void start_position()
{int s1=0;
p.unpaint_player(x,y);
for(int i=0;i<10;i++)
for(int j=0;j<14;j++)
array1[i][j].undo_destroy();//undestroy bricks
p.init_size(72,10,13);
b.init_size(4,15);
setcolor(10);
settextstyle(0,0,2);
outtextxy(30,0,"SCORE:");
setcolor(color_exchange);
setfillstyle(2,color_exchange);
outtextxy(240,0,"LEVEL");
outtextxy(330,0,level);
bar(0,0,20,500);//left wall
bar(639,0,619,500);//right wall
setcolor(BLACK);
while(1)
{while(!kbhit())
{if(x==24||x==618-p.player_size()-2)
{s1=0;
x= (x==24)? 28:618-p.player_size()-6;
}
x+=s1;
x1=x+p.player_size()/2;
if(s1)//not freeze
{p.paint_player(x,y);
delay(6);
p.unpaint_player(x,y);
b.paint_ball(x1,y1);
delay(4);
b.unpaint_ball(x1,y1);
}
else//freeze
{p.paint_player(x,y);
b.paint_ball(x1,y1);
}
}//end of while (!kbhit())//
p.unpaint_player(x,y);
b.unpaint_ball(x1,y1);
flag=getch();
if(flag==27)//esc//
{closegraph();
exit(0);
}
if(flag==' ') s1=0;
if(flag=='o'||flag=='O') sound_on_off*=-1;
if((flag=='s'||flag=='S')&&(x>=x1-p.player_size()&&x<=x1)) play();
if(flag==0) flag=getch();
if(flag==77||flag==75)
{if(flag==77) s1=4;//move right//
if(flag==75) s1=-4;//move left//
}
}//end of while//
}
void start_screen()
{char place1[]={"1"};
destroy=0; //0 bricks destroyed//
result=0; //result is 0//
color_exchange=1;
(*level)='1'; //level 1//
setcolor(13);
setfillstyle(2,0);
bar(0,0,650,500);
settextstyle(0,0,8);
setcolor(10);
outtextxy(110,20,"DX-BALL");
settextstyle(4,0,6);
setcolor(12);
outtextxy(60,90,"By Pathikrit Bhowmick");
setcolor(10);
settextstyle(2,0,7);
outtextxy(10,200,"Space : Stop");
outtextxy(10,230,"S : Shoot");
outtextxy(10,260,"O : Sound (On/Off)");
outtextxy(10,290,"P : Pause");
outtextxy(10,320,"-> : Right");
outtextxy(10,350,"<- : Left");
outtextxy(10,380,"Esc : Exit");
setcolor(12);
outtextxy(10,450,"Press Any Key To Start The Game.....");
setfillstyle(1,0);
setcolor(2);
bar3d(320,180,620,440,8,1);
setcolor(12);
settextstyle(1,0,3); //print high score//
outtextxy(410,185,"High Scores :-");
setcolor(2);
outtextxy(390,200,"-------------");
HIGH_SCORE ezer1;
int y=220;
FILE *fin=fopen("high_score.txt","r");
while(fscanf(fin,"%s %s\n",ezer1.name,ezer1.score)!=EOF)
{setcolor(4);
if(y==220) setcolor(12);
outtextxy(350,y,place1);
setcolor(2);
if(y==220) setcolor(10);
outtextxy(380,y,ezer1.name);
setcolor(4);
if(y==220) setcolor(12);
outtextxy(530,y,ezer1.score); //end print high score//
y+=23;
(*place1)++;
}
fclose(fin);
}
void play()
{int s=0;
while(1)
{while(!kbhit())
{if(x==24||x==618-p.player_size()-2)
{s=0;
x= (x==24)? 28:618-p.player_size()-6;
}
x+=s;
if(s)
{p.paint_player(x,y);
delay(6);
p.unpaint_player(x,y);
}
else
{p.paint_player(x,y);
delay(6);
}
del_check();
if(y1==30||y1==460) ball_hit();//ball hits the player or the ceiling
if(x1>=26&&x1<=30||x1<=613&&x1>=609)//ball hits the walls
{if(sound_on_off==1) make_sound(30);
angle*=-1;
}
b.paint_ball(x1,y1);
delay(4);
b.unpaint_ball(x1,y1);
y1+=up_down;
x1+=angle;
}//end of while(!kbhit())//
p.unpaint_player(x,y);
flag=getch();
if(flag==27)//exit//
{closegraph();
exit(0);
}
if(flag==' ')s=0;//stop//
if(flag=='p'||flag=='P')flag=getch();//pause//
if(flag=='o'||flag=='O')sound_on_off*=-1;
if(flag==0) flag=getch();
if(flag==77) s= 4;//move right
if(flag==75) s=-4;//move left
}//end of nonstop loop//
}
void paint_bricks1()
{irandom_number=rand()%4;
jrandom_number=rand()%13;
i1random_number=rand()%4;
j1random_number=rand()%13;
int i,j,x=22,y=120,width=44,height=25,color=1,shap=1;
for(i=0;i<4;i++,y+=26,x=22)
for(j=0;j<13;j++,x+=46)
{if(color==15) color=1;
if(i==irandom_number&&j==jrandom_number)
{array1[irandom_number][jrandom_number].init(x,y,width,height,15,shap);
array1[irandom_number][jrandom_number].paint();
}
else if(i==i1random_number&&j==j1random_number)
{array1[i1random_number][j1random_number].init(x,y,width,height,15,shap);
array1[i1random_number][j1random_number].paint();
}
else
{array1[i][j].init(x,y,width,height,color++);
array1[i][j].paint();
}
}
number_of_bricks=i*j;
}
void paint_bricks2()
{irandom_number=rand()%7;
jrandom_number=rand()%11;
i1random_number=rand()%7;
j1random_number=rand()%11;
int i,j,x=26,y=80,width=40,height=25,color=1,step=0,direct=1,shap=2;
for(i=0;i<7;i++,y+=27,step=step+42*direct,x=26+step)
for(j=0;j<11;j++,x+=42)
{if(i==3) direct=-1;
if(color==15) color=1;
if(i==irandom_number&&j==jrandom_number)
{array1[irandom_number][jrandom_number].init(x,y,width,height,15,shap);
array1[irandom_number][jrandom_number].paint();
}
else if(i==i1random_number&&j==j1random_number)
{array1[i1random_number][j1random_number].init(x,y,width,height,15,shap);
array1[i1random_number][j1random_number].paint();
}
else
{array1[i][j].init(x,y,width,height,color++);
array1[i][j].paint();
}
}
number_of_bricks=i*j;
}
void paint_bricks3()
{irandom_number=rand()%2;
jrandom_number=rand()%14;
i1random_number=rand()%2;
j1random_number=rand()%14;
int i,j,x=27,y=30,width=40,height=25,color=3,shap=5;
for(i=0;i<2;i++,y+=250,x=27)
for(j=0;j<14;j++,x+=42)
{if(i==irandom_number&&j==jrandom_number)
{array1[irandom_number][jrandom_number].init(x,y,width,height,15,shap);
array1[irandom_number][jrandom_number].paint();
}
else if(i==i1random_number&&j==j1random_number)
{array1[i1random_number][j1random_number].init(x,y,width,height,15,shap);
array1[i1random_number][j1random_number].paint();
}
else
{array1[i][j].init(x,y,width,height,color);
array1[i][j].paint();
}
}
for(color++,width=30,height=15,y=100,x=95;i<10;i++,color++,x=95,y+=17)
for(j=0;j<14;j++,x+=32)
{array1[i][j].init(x,y,width,height,color);
array1[i][j].paint();
}
number_of_bricks=j+(i-1)*j;
}
void paint_bricks5()
{irandom_number=rand()%5;
jrandom_number=rand()%14;
i1random_number=rand()%5;
j1random_number=rand()%14;
int i,j,x=26,y=90,width=40,height=25,color=1,shap=3;
for(i=0;i<5;i++,y+=60,x=26,color++)
for(j=0;j<14;j++,x+=42)
{if(i==irandom_number&&j==jrandom_number)
{array1[irandom_number][jrandom_number].init(x,y,width,height,15,shap);
array1[irandom_number][jrandom_number].paint();
}
else if(i==i1random_number&&j==j1random_number)
{array1[i1random_number][j1random_number].init(x,y,width,height,15,shap);
array1[i1random_number][j1random_number].paint();
}
else
{array1[i][j].init(x,y,width,height,color);
array1[i][j].paint();
}
}
number_of_bricks=i*j;
}
void paint_bricks4()
{irandom_number=rand()%10;
jrandom_number=rand()%7;
i1random_number=rand()%10;
j1random_number=rand()%7;
int i,j,x=27,y=70,width=40,height=25,color=4,shap=4;
for(i=0;i<10;i++,y+=27,x=27,color++)
for(j=0;j<7;j++,x+=91)
{if(i==irandom_number&&j==jrandom_number)
{array1[irandom_number][jrandom_number].init(x,y,width,height,15,shap);
array1[irandom_number][jrandom_number].paint();
}
else if(i==i1random_number&&j==j1random_number)
{array1[i1random_number][j1random_number].init(x,y,width,height,15,shap);
array1[i1random_number][j1random_number].paint();
}
else
{array1[i][j].init(x,y,width,height,color);
array1[i][j].paint();
}
}
number_of_bricks=i*j;
}
void del_check()
{int check;
static char show_result[]={0};
for(int i=0;i<10;i++)
for(int j=0;j<14;j++)
{check=array1[i][j].is_in(x1,y1);
if(check)
{if(check==1) up_down=2;
if(check==2) up_down=-2;
if(check==3||check==4) angle*=-1;
array1[i][j].hide();
if(i==irandom_number&&j==jrandom_number||i==i1random_number&&j==j1random_number) bonus_type();//bonus//
outtextxy(135,0,show_result);
result+=125;
bonus_check();
ltoa(result,show_result,10);
setcolor(12);
outtextxy(135,0,show_result);
setcolor(BLACK);
if(sound_on_off==1)
{sound(500);
delay(20);
nosound();
}
if(++destroy==number_of_bricks) end_level(); //all bricks have been destroyd//
}
}
}
int psila()
{int game_over;
game_over=p.unpaint_psila();
if(game_over)
{if(sound_on_off==1) make_sound(800);
start_position();
}
else game_is_over();
return 0;
}
void ball_hit()
{up_down=(y1==460)?(-2):(2);
if(y1==460)
{if(x>=x1-p.player_size()&&x<=x1)//ball hit player//
{if(sound_on_off==1)
{sound(900);
delay(20);
nosound();
}
if(x1==x+p.player_size()/2) angle=1;//middle hit
else if(x+p.player_size()/2+1<=x1&&x+p.player_size()/2+15>=x1) angle= 2;
else if(x+p.player_size()/2+15+1<=x1&&x+p.player_size()/2+25>=x1) angle= 3;
else if(x+p.player_size()/2+25+1<=x1&&x+p.player_size()/2+35>=x1) angle= 5;
else if(x+p.player_size()/2-10<=x1&&x+p.player_size()/2-1>=x1) angle=-2;
else if(x+p.player_size()/2-25<=x1&&x+p.player_size()/2-16>=x1) angle=-3;
else if(x<=x1&&x+p.player_size()/2-26>=x1) angle=-5;
}
else up_down=psila();//ball mised player//
}
else if(sound_on_off==1) make_sound(30);//ball mised player//
}
void end_level()
{for(int i=0;i<10;i++)
for(int j=0;j<14;j++)
array1[i][j].hide();//delete bricks
p.unpaint_player(x,y);
x=300,y=460;//player//
x1=335,y1=460;//ball//
setcolor(BLACK);
outtextxy(330,0,level);
outtextxy(240,0,"LEVEL");
(*level)++;
color_exchange++;
if(sound_on_off==1) make_sound(3000);
destroy=0;
delay(1000);
if(*level=='2') paint_bricks2();
if(*level=='3') paint_bricks3();
if(*level=='4') paint_bricks4();
if(*level=='5') paint_bricks5();
if(*level=='5') paint_bricks4();
start_position();
}
void make_sound(int s)
{for(;s>0;s=s-30)
{sound(s);
delay(30);
nosound();
} //end of for//
} //end of make_sound//
void bonus_check()
{if(result%10000==0)//bonus every 10,000 points//
{if(sound_on_off==1) make_sound(300);
p.paint_psila();
}
}
void bonus_type()
{int bonus_number=rand()%6;
if(sound_on_off==1)
{make_sound(400);
make_sound(500);
make_sound(600);
make_sound(700);
}
switch(bonus_number)
{case 0: p.paint_psila(); break; //bonus chance
case 1: if(*level=='5') bonus_type();
else end_level();
break; //skip level//
case 2: p.init_size(100,15,10);break; //player is big//
case 3: p.init_size(140,20,14);break; //player is very big//
case 4: p.init_size(180,25,9);break; //player is very very big//
case 5: for(int i=0;i<10;i++) //bricks destroyer is on//
for(int j=0;j<14;j++)
array1[i][j].init_destroy();
b.init_size(5,4);
}
}
void game_is_over()
{char show_result1[]={0};
cleardevice();
setcolor(10);
settextstyle(0,0,3);
outtextxy(180,250,"Game over!!!");
setcolor(10);
outtextxy(100,120,"Your Score:");
setcolor(12);
ltoa(result,show_result1,10);
outtextxy(420,120,show_result1);
delay(5000);
high_score();
start_screen();
delay(2000);
if((flag=getch())==27)
{closegraph();
exit(0);
}
cleardevice();
for(int i=0;i<10;i++)
for(int j=0;j<14;j++)
array1[i][j].hide();//delete bricks
p.init_psila();
paint_bricks1();
x=300,y=460;//player//
x1=335,y1=460;//ball//
start_position();
}
void high_score()
{char name1[20]={' '},*p=name1;
int i,j;
FILE *fin;
HIGH_SCORE ezer,array[9]={0};
fin=fopen("high_score.txt","r");
for(i=0;fscanf(fin,"%s %s\n",array[i].name,array[i].score)!=EOF;i++);
if(result<=atol(array[8].score))
{fclose(fin);
return;
}
ltoa(result,array[8].score,10);
cleardevice();
bar(0,0,650,500);
setcolor(10);
settextstyle(0,0,3);
outtextxy(80,20,"New High Score !!!");
setcolor(12);
outtextxy(100,90,array[8].score);
setcolor(10);
outtextxy(50,150,"Enter Your Name :");
setfillstyle(1,BLACK);
bar3d(280,250,520,300,4,4);
settextstyle(7,0,4);
setcolor(12);
for(i=0;i<10;)
{*p=getch();
if(islower(*p))
{name1[i]=*p;
name1[i+1]=NULL;
outtextxy(300,250,name1);
p++;
i++;
}
if((*p==13||*p==27||i==9)&&i>0)
{*p=NULL;
break;
}
}
strcpy(array[8].name,name1);
fclose(fin);
for(i=0;i<9;i++)
for(j=0;j<8;j++)
if(atol(array[j].score)<atol(array[j+1].score))
{ezer=array[j];
array[j]=array[j+1];
array[j+1]=ezer;
}
fin=fopen("high_score.txt","w");
for(i=0;i<9;i++) fprintf(fin,"%s %s\n",array[i].name,array[i].score);
fclose(fin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment