/* PONG GAME v1.0 Made By Gaurang Sinha gaurang85.tripod.com Score 1 | Score 2 -----------------------|----------------------- Player1 | Player2 | o Bat2| | |Bat1 | | | | | Pos2 _______________________|_______________________ Pos1 If any one can stop the ball from framing so much during runtime, i would be highly obliged if you could email at gaurang85@hotmail.com staing how you have achieved this. */ /*--------------------INCLUDE FILES-------------------*/ #include #include #include #include #include /*--------------------CONSTANTS--------------------*/ #define ENTER 13 #define SPACE 32 #define BACK 8 #define ESC 0x1b #define UP 0x48 #define DOWN 0x50 #define LEFT 0x4b #define RIGHT 0x4d /*--------------------PROTOTYPES--------------------*/ void scores(); void ball(int,int); void bat2(int); void bat1(int); void refresh(); void move(); void play(); void ai2(); /*--------------------GLOBAL VARIABLES--------------------*/ int score1=0,score2=0,pos1,pos2,xb,yb,xb1,yb1,side=1,dir=0,X,Y,wins,ai=0; /*side=1 <-ball going to Player1*/ /*side=2 ->ball going to Player2*/ /*dir=1 - ball is moving in an upward direction*/ /*dir=0 - ball is moving in a downward direction*/ /*--------------------MAIN FUNCTION--------------------*/ int main() { int gd=DETECT,gm; char c; clrscr(); initgraph(&gd,&gm,"C:\\TC\\BGI"); //StartUp Screen settextstyle(1,0,9); setcolor(9); outtextxy(80,40,"PONG"); settextstyle(1,0,4); setcolor(12); outtextxy(350,150,"By Gaurang Sinha"); settextstyle(1,0,3); setcolor(WHITE); outtextxy(230,240,"Instructions"); settextstyle(1,0,2); outtextxy(100,260,"Player 1");outtextxy(400,260,"Player 2"); outtextxy(110,280,"UP - w");outtextxy(410,280,"UP - up arrow"); outtextxy(110,300,"DOWN - s");outtextxy(410,300,"DOWN - down arrow"); outtextxy(200,340,"To Play 1 Player Mode ... Press C"); outtextxy(240,380,"QUIT - ESC"); outtextxy(200,420,"Press any key to continue ... "); c=getch();if(c=='c'||c=='C')ai=1; cleardevice(); //Game Screen setbkcolor(BLACK); setcolor(WHITE); X=getmaxx(); Y=getmaxy(); pos1=pos2=Y/2; /*Set both the bats to the center of the screen*/ line(0,50,X,50); line(0,Y-10,X,Y-10); line(X/2,0,X/2,Y-10); scores(); xb=xb1=320;yb=yb1=240; ball(xb,yb); bat2(pos1); bat1(pos2); move(); getch(); closegraph(); return 0; } /*--------------------Other FUNCTIONS--------------------*/ void ball(int i,int j) { setcolor(BLACK); circle(xb1,yb1,10); //Clear Previous setfillstyle(1,BLACK); fillellipse(xb1,yb1,9,9); //ball position setcolor(RED); circle(i,j,10); //Draw new setfillstyle(1,RED); fillellipse(i,j,9,9); //ball position refresh(); } void bat1(int i) //Right Hand Side { setlinestyle(0,0,3); setcolor(BLACK); line(630,50,630,470); setcolor(RED); line(630,i-15,630,i+15); } void bat2(int i) //Left Hand Side { setlinestyle(0,0,3); setcolor(BLACK); line(0,50,0,470); setcolor(RED); line(0,i-15,0,i+15); } void scores() { gotoxy(70,1); printf(" "); gotoxy(70,1); printf("Score2: %d",score2); gotoxy(3,1); printf(" "); gotoxy(3,1); printf("Score1: %d",score1); } void move() { char a; while(a!=ESC) { a=0; if(ai!=0){ai2();} if(kbhit()) { a=getch(); if(ai==0) { if(a==UP&&pos1>65) { pos1=pos1-10;bat1(pos1);} if(a==DOWN&&pos1<470) { pos1=pos1+10;bat1(pos1);} } if(a=='w'&&pos2>65) {pos2=pos2-10;bat2(pos2);} if(a=='s'&&pos2<470) {pos2=pos2+10;bat2(pos2);} } if(side==1) { if(xb>=15) { if(yb<65){yb=yb+5;dir=0;} else if(yb>450){yb=yb-5;dir=1;} else { if(dir==1){yb=yb-5;xb=xb-5;} if(dir==0){yb=yb+5;xb=xb-5;} } ball(xb,yb); xb1=xb;yb1=yb; if(xb==15){if(ybpos2-15){side=2;}else{score2++;wins=2;a=ESC;}} } } if(side==2) { if(xb<=615) { if(yb<65){yb=yb+5;dir=0;} else if(yb>450){yb=yb-5;dir=1;} else { if(dir==1){yb=yb-5;xb=xb+5;} if(dir==0){yb=yb+5;xb=xb+5;} } ball(xb,yb); xb1=xb;yb1=yb; if(xb==615){if(ybpos1-15){side=1;}else{score1++;wins=1;a=ESC;}} } } } play(); } void refresh() { setcolor(WHITE); line(0,50,X,50); line(0,Y-10,X,Y-10); line(X/2,0,X/2,Y-10); scores(); } void play() { char p; cleardevice(); settextstyle(1,0,3); setcolor(RED); if(wins==1)outtextxy(280,150,"Player 1 Wins "); if(wins==2)outtextxy(280,150,"Player 2 Wins "); outtextxy(200,200,"Do you want to play again ? (Y/N) "); p=getch(); if(p=='y'||p=='Y') { setbkcolor(BLACK); pos1=pos2=Y/2; /*Set both the bats to the center of the screen*/ line(0,50,X,50); line(0,Y-10,X,Y-10); line(X/2,0,X/2,Y-10); scores(); xb=xb1=320;yb=yb1=240; ball(xb,yb); bat2(pos1); bat1(pos2); move(); } else if(p=='n'||p=='N') { exit; } } void ai2() { //pos1=yb; This looked very lame so i changed the code a wee bit !!! //bat1(pos1); if(side==2) { if(pos1yb){pos1=pos1-5;} } bat1(pos1); }