The Source Code of Shooting game:
/* SHOOTING (GAME) */
/* B.E(INFORMATION TECHNOLOGY) IIIrd YR */
/* FOR SUGGESTIONS contact me at [email protected] */
// INCLUDE LIBRARIES//
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
// GLOBAL VARIABLES //
int m = 0,n = 60,g1 = 10,bit=1,g2 = 15, a = 410 , b = 405, c ,d, score = 0;
char s[4];int x=0;
// PROGRAMMER DEFINED FUNCTIONS //
int getkey();
void playgame();
void shoot();
void control(int);
void drawobject();
void end();
void drawblock(int,int,int,int,int);
void main() // MAIN FUNCTION //
{
int gd = DETECT,gm,i=0;
initgraph(&gd,&gm,"c:\\tc\\bgi");
while(i!=3000)
{
putpixel(random(getmaxx()),random(getmaxy()),random(16));
i++;
}
drawblock(0,460,640,480,BROWN);
setcolor(LIGHTGREEN);
outtextxy(50,467,"SHOOTING - DESIGNED BY PARTHA PRATIM SANYAL.");
itoa(score,s,10);
setcolor(BLUE);
outtextxy(530,467,"SCORE");
outtextxy(600,467,s);
playgame();
}
void drawobject() // DRAWS THE OBJECT TO BE HIT //
{
drawblock(m,40,n,50,BLACK);
m+=10;
n+=10;
drawblock(m,40,n,50,RED);
delay(10);
if(m==640)
{
m = 0; n = 60;
}
}
void control(int key) // KEYBOARD CONTROL //
{
switch(key)
{
case 72 : if(bit==1)
{
c = g1; d = g2;a = 410 ; b = 405 ;
sound(500);
delay(50);
nosound();
shoot();
}
break;
case 75 : if(g1>9)
{
drawblock(g1,410,g2,455,BLACK);
g1 = g1-10;
g2 = g2-10;
drawblock(g1,410,g2,455,YELLOW);
}
break;
case 77 : if(g2<630)
{
drawblock(g1,410,g2,455,BLACK);
g1 = g1+10;
g2 = g2+10;
drawblock(g1,410,g2,455,YELLOW);
}
break;
}
}
void playgame() // THE GAME //
{
while(1)
{
if((a!=-10)&&(a<410))
{
bit = 0;
shoot();
if(a==0)
bit = 1;
}
drawblock(g1,410,g2,455,YELLOW);
while(!kbhit())
drawobject();
int key = getkey();
if(key==1)
end();
control(key);
}
}
void shoot() // TO SHOW THE BULLET FIRED OUT OF THE GUN //
{
while(!kbhit())
{
drawobject();
drawblock(c,a,d,b,BLACK);
a-=10;
b-=10;
drawblock(c,a,d,b,GREEN);
if((c>=m-5)&&(d<=n+5)&&(a==50))
{
score+=50;
setfillstyle(SOLID_FILL,BROWN); //ERASING OLD SCORE
bar(600,465,640,475);
itoa(score,s,10);
setcolor(RED);
outtextxy(600,467,s);
sound(1000);
delay(50);
nosound();
drawblock(m,40,n,50,BLACK);
drawblock(c,a,d,b,BLACK);
m =0; n = 60;
if(bit==1)
{
a = 410 ; b =405;
}
playgame();
}
if(a<0)
bit = 1;
if(b <0)
return;
}
}
void drawblock(int x1,int y1,int x2 ,int y2,int col)
{
setcolor(col); // DRAWS ANY BLOCK OF FIXED SIZE AND COLOR //
setfillstyle(SOLID_FILL,col);
rectangle(x1,y1,x2,y2);
floodfill((x1+x2)/2,(y1+y2)/2,col);
}
int getkey() // FUNCTION WHICH RETURNS THE SCAN CODE OF THE KEY HIT//
{
union REGS i,o;
while(!kbhit())
;
i.h.ah = 0;
int86(22,&i,&o);
return(o.h.ah);
}
void end()
{
clearviewport();
drawblock(0,0,640,480,RED);
settextstyle(4,0,6);
setcolor(YELLOW);
outtextxy(160,200,"GAME OVER");
setcolor(BLUE);
settextstyle(2,0,6);
itoa(score,s,10);
outtextxy(240,270,"YOUR SCORE :");
outtextxy(380,270,s);
for(int i=0;i<80;i++)
{
sound(20*i);
delay(100);
nosound();
}
getch();
closegraph();
}
// SOME CRUCIAL COMMENTS ABOUT THE PROGRAM //
/* 1) m , n are the x coordinates of the object to be hit.
2) a,b are the y coordinates of the bullet.
3) g1,g2 are the x coordinates of the gun.
4) Initially the bit variable is set to 1.
after the gun shoots the bullet it is set to 0,
unless it reaches the top where it is again set to 1.
5) One can fire only one bullet at a time and has to wait
till the bullet reaches the top , for firing the next bullet. */
![]()