The Source Code of Pingpong game..
/* PINGPONG (GAME) */
/* B.E(INFORMATION TECHNOLOGY) IIIrd YR */
/* FOR SUGGESTIONS contact me at [email protected] */
// INCLUDE LIBRARIES//
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
// GLOBAL VARIABLES //
int x1 = 180 ,x2 = 250,x = 51 ,y = 51,xdir=1,ydir=1,score = 0,speed;
char s[4];
// FUNCTION PROTOTYPES //
int getkey();
void playgame(),moveplatform(int),drawplatform(int),over();
int pingpong() // SHOWS THE BALL ON THE SCREEN //
{
int i;
while(!kbhit())
{
setcolor(GREEN);
setfillstyle(SOLID_FILL,RED);
circle(x,y,30);
floodfill(x,y,GREEN);
delay(120-speed);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(x,y,30);
floodfill(x,y,BLACK);
if((x>=getmaxx()-51)||(x<51))
{
xdir *= -1;
sound(500);
delay(100);
nosound();
}
if(y<51)
{
ydir*=-1;
sound(500);
delay(100);
nosound();
}
if((x>=(x1-15))&&(x<=(x2+15))&&(y>=getmaxy()-68)) //MAIN PROGRAM STATEMENT//
{
ydir*=-1;
sound(1000);
delay(100);
nosound();
score = score +10;
setcolor(BLACK); //ERASING OLD SCORE
bar(490,465,520,475);
}
itoa(score,s,10);
setcolor(LIGHTRED);
outtextxy(420,467," SCORE : ");
outtextxy(500,467,s);
if((y>=getmaxy()-51)) // WHEN THE BALL TOUCHES GROUND : EXIT //
{
for(i=0;i<80;i++)
{
sound(20*i);
delay(100);
nosound();
}
over();
}
x = x+xdir*10;
y = y+ydir*10;
}
return(1);
}
void main() // THE MAIN FUNCTION //
{
int gd = DETECT,gm;
char a[4];
initgraph(&gd,&gm,"c:\\tc\\bgi");
clearviewport();
setcolor(LIGHTRED);
outtextxy(20,20,"WHAT SPEED WOULD YOU LIKE :(0 TO 100)? PLEASE ENTER");
gotoxy(60,2);
scanf("%d",&speed);
clearviewport();
setcolor(GREEN);
rectangle(10,10,getmaxx()-15,getmaxy()-15);
drawplatform(YELLOW);
setcolor(BROWN);
outtextxy(50,467,"PINGPONG - DESIGNED BY PARTHA PRATIM SANYAL.");
getkey();
playgame();
}
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 moveplatform(int key) // MOVES THE PLATFORM //
{
switch(key)
{
case 77 : if(x2<getmaxx()-26)
{
drawplatform(BLACK);
x1 = x1+10;
x2 = x2+10;
drawplatform(YELLOW);
}
break;
case 75 : if(x1>20)
{
drawplatform(BLACK);
x1 = x1-10;
x2 = x2-10;
drawplatform(YELLOW);
}
break;
default :break;
}
}
void drawplatform(int col) // DRAWS THE PLATFORM //
{
setcolor(col);
setfillstyle(SOLID_FILL,col);
rectangle(x1,450,x2,460);
floodfill((x1+x2)/2,455,col);
}
void playgame() // THE GAME //
{
int key,i;
while(1)
{
i=pingpong();
if(i!=0)
{
key = getkey();
if (key ==1) // TO EXIT FROM THE GAME AT USER'S DISCRETION //
over();
moveplatform(key);
}
}
}
void over()
{
drawplatform(BLACK);
settextstyle(4,0,6);
setcolor(MAGENTA);
outtextxy(160,200,"GAME OVER");
setcolor(LIGHTGREEN);
settextstyle(2,0,6);
outtextxy(240,270,"YOUR SCORE :");
outtextxy(390,270,s);
getkey();
closegraph();
restorecrtmode();
}
![]()