Ping Pong game code for project in c++
Code:
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<windows.h>
using
namespace std;
void
z(){Sleep(300);}
enum
edir{STOP=0,LEFT=1,UPLEFT=2,DOWNLEFT=3,RIGHT=4,UPRIGHT=5,DOWNRIGHT=6};
struct
cball
{
int x,y;
int originalX,originalY;
edir direction;
cball(int posX,int posY)
{
originalX=posX;
originalY=posY;
x=posX; y=posY;
direction=STOP;
}
void Reset()
{
x=originalX; y=originalY;
direction=STOP;
}
void changeDirection(edir d)
{
direction=d;
}
void randomDirection()
{
direction=(edir)((rand()%6)+1);
}
getX() {return x; }
getY() {return y; }
edir getDirection() {return direction; }
void Move()
{
switch(direction)
{
case STOP:
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UPLEFT:
x--;y--;
break;
case DOWNLEFT:
x--;y++;
break;
case UPRIGHT:
x++;y--;
break;
case DOWNRIGHT:
x++;y++;
break;
default:
break;
}
}
friend
ostream&operator<<(ostream & o,cball c)
{
o << "Ball {"
<< c.x << "," <<c.y <<"}{" <<
c.direction << "}";
return o;
}
};
struct
cpaddle
{
int x,y;
int originalX,originalY;
cpaddle()
{
x=y=0;
}
cpaddle(int posX,int posY)
{
originalX = posX;
originalY = posY;
x= posX;
y= posY;
}
void Reset() { x =originalX; y=originalY;}
int getX() { return x;}
int getY() { return y;}
moveup() { y--;}
int movedown() { y++;}
friend ostream & operator <<(ostream
& o,cpaddle c)
{
o << "paddle {"
<< c.x << "," <<c.y << "}";
return o;
}
};
struct cgamemanger
{
int
width,height;
int
score1 , score2;
char
up1, down1, up2, down2;
bool
quit;
cball
*ball;
cpaddle
*player1;
cpaddle
*player2;
cgamemanger(int w,int h)
{
srand(time(NULL));
quit
= false;
up1=
'w'; up2='i';
down1='s';
down2='k';
score1=score2=0;
width=w;
height=h;
ball=
new cball(w/2,h/2);
player1=
new cpaddle(1,h/2 -3);
player2=
new cpaddle(w-2,h/2 -3);
}
cgamemanger()
{
delete
ball , player1 , player2;
}
void scoreup(cpaddle *player)
{
if(player==player1)
score1++;
else
if(player==player2)
score2++;
ball->Reset();
player1->Reset();
player2->Reset();
}
void draw()
{
system("cls");
for(int
i=0; i<width+2; i++)
cout
<<"\xB2";
cout
<<endl;
for(int
i=0; i<height; i++)
{
for(int
j=0; j<width; j++)
{
int
ballx = ball->getX();
int
bally = ball->getY();
int
player1x = player1->getX();
int
player2x = player2->getX();
int
player1Y = player1->getY();
int
player2Y = player2->getY();
if(j==0)
cout
<<"\xB2";
if
(ballx == j && bally == i)
cout
<<"0"; //ball
else
if(player1x ==j && player1Y==i)
cout
<<"\xDB"; //player1
else
if(player2x ==j && player2Y==i)
cout <<"\xDB"; //player2
else
if(player1x ==j && player1Y +1==i)
cout
<<"\xDB"; //player1
else
if(player1x ==j && player1Y +2==i)
cout
<<"\xDB"; //player1
else
if(player1x ==j && player1Y +3==i)
cout
<<"\xDB"; //player1
else
if(player2x ==j && player2Y +1==i)
cout
<<"\xDB"; //player1
else
if(player2x ==j && player2Y +2==i)
cout
<<"\xDB"; //player1
else
if(player2x ==j && player2Y +3==i)
cout
<<"\xDB"; //player1
else
cout
<<" ";
if(j==width-1)
cout
<<"\xB2";
}
cout
<<endl;
}
for(int
i=0; i<width+2; i++)
cout
<<"\xB2";
cout
<<endl;
cout <<"Score
1 = " <<score1 <<endl <<" Score 2 =
"<<score2<<endl;
}
void input()
{
ball->Move();
int
ballx = ball->getX();
int
bally = ball->getY();
int
player1x = player1->getX();
int
player2x = player2->getX();
int
player1Y = player1->getY();
int
player2Y = player2->getY();
if(_kbhit())
{
char
current =_getch();
if(current==up1)
if(player1Y
>0)
player1->moveup();
if(current==up2)
if(player2Y
>0)
player2->moveup();
if(current==down1)
if(player1Y
+ 4 < height)
player1->movedown();
if(current==down2)
if(player2Y
+ 4 < height)
player2->movedown();
if(ball->getDirection()==STOP)
ball->randomDirection();
if(current
=='q')
quit
= true;
}
}
void logic()
{
int
ballx = ball->getX();
int
bally = ball->getY();
int
player1x = player1->getX();
int
player2x = player2->getX();
int
player1Y = player1->getY();
int
player2Y = player2->getY();
//left
paddle
for(int
i=0; i<4; i++)
if(ballx==player1x+1)
if(bally==player1Y
+ i)
ball->changeDirection((edir)((rand()%3)+4));
//right
paddle
for(int
i=0; i<4; i++)
if(ballx==player2x-1)
if(bally==player2Y
+ i)
ball->changeDirection((edir)((rand()%3)+1));
//bottom
wall
if(bally==height-1)
ball->changeDirection(ball->getDirection()==
DOWNRIGHT ? UPRIGHT : UPLEFT);
//top
wall
if(bally==0)
ball->changeDirection(ball->getDirection()==
UPRIGHT ? DOWNRIGHT : DOWNLEFT);
//right
wall
if(ballx==width-1)
scoreup(player1);
//LEFT
wall
if(ballx==0)
scoreup(player2);
}
void Run()
{
while(!quit)
{
draw();
input();
logic();
z();
}
}
};
int
main()
{
cball c(0,0);
cout <<c <<endl;
c.randomDirection();
cout << c <<endl;
c.Move();
cout << c <<endl;
c.randomDirection();
c.Move();
cout << c <<endl;
c.randomDirection();
c.Move();
cout << c <<endl;
cpaddle p1(0,0);
cpaddle p2(10,0);
cout << p1 <<endl;
cout << p2 <<endl;
p1.moveup();
p2.movedown();
cout << p1 <<endl;
cout << p2 <<endl;
cgamemanger cball(40, 20);
cball.draw();
cball.Run();
getch();
return 0;
}
Comments
Post a Comment