#include<iostream>
#include<SDL/SDL.h>
#include<SDL/SDL_ttf.h>
#include<string>
using namespace std;
int frame=0;
bool cap=true;
Timer fps;
const int SCREEN_WIDTH=640;
Const int SCREEN_HEIGHT=480;
Const int SCREEN_BPP=32;
Const int FRAMES_PER_SECOND=20;
int Player_One_Points,Cpu_Points,Player_Two_Points=0;
bool quit=false;
int game_state=o;
intrandomNumber=rand();
intrandomVelocity(randomNumber%6)+1;
class Ball
{
public:
int x,y;
int xVel,yVel;
void Ball();
void Ball_Track();
void Show_Ball();
void Ball_Track_2_Player();
void Ball_Wall_Collision();
};
Ball::Ball() : randomNumber(rand()), randomVelocity((randomNumber%6)+1)
{
x=310;
y=230;
xVel=3;
yVel=0;
};
Ball::Ball_Track()
{
if(x+20 > SCREEN_WIDTH)
{
Player_One_Points+=1;
x=310;
y=230;
SDL_Delay(2000);
if(Player_One_Points==1)
{
SDL_FreeSurface(Message0);
apply_surface(30,10,Message1,screen);
}
else if(Player_One_Points==2)
{
SDL_FreeSurface(Message1);
apply_surface(30,10,Message2,screen);
}
else if(Player_One_Points==3)
{
SDL_FreeSurface(Message2);
apply_surface(30,10,Message3,screen);
}
else if(Player_One_Points==4)
{
SDL_FreeSurface(Message3);
apply_surface(30,10,Message4,screen);
}
else if(Player_One_Points==5)
{
SDL_FreeSurface(Message4);
apply_surface(30,10,Message5,screen);
}
else if(Player_One_Points==6)
{
SDL_FreeSurface(Message5);
apply_surface(30,10,Message6,screen);
}
else if(Player_One_Points==7)
{
SDL_FreeSurface(Message6);
apply_surface(30,10,Message7,screen);
}
else if(Player_One_Points==8)
{
SDL_FreeSurface(Message7);
apply_surface(30,10,Message8,screen);
}
else if(Player_One_Points==9)
{
SDL_FreeSurface(Message8);
apply_surface(30,10,Message9,screen);
}
else if(Player_One_Points==10)
{
SDL_FreeSurface(Message9);
apply_surface(30,10,Message10,screen);
apply_surface(310,205,MessageCongratsP1,screen);
SDL_Delay(4000);
SDL_FreeSurface(Message10);
SDL_FreeSurface(MessageCongratsP1);
SDL_FreeSurface(Ball);
SDL_Freesurface(Player_One_Bat);
SDL_FreeSurface(Cpu_Bat);
game_state==0;
}
}
else(x<0)
{
Cpu_Points+=1;
x=310;
y=230;
SDL_Delay(2000);
if(Cpu_Points==1)
{
SDL_FreeSurface(Message0);
apply_surface(30,10,Message1,screen);
}
else if(Cpu_Points==2)
{
SDL_FreeSurface(Message1);
apply_surface(30,10,Message2,screen);
}
else if(Cpu_Points==3)
{
SDL_FreeSurface(Message2);
apply_surface(30,10,Message3,screen);
}
else if(Cpu_Points==4)
{
SDL_FreeSurface(Message3);
apply_surface(30,10,Message4,screen);
}
else if(Cpu_Points==5)
{
SDL_FreeSurface(Message4);
apply_surface(30,10,Message5,screen);
}
else if(Cpu_Points==6)
{
SDL_FreeSurface(Message5);
apply_surface(30,10,Message6,screen);
}
else if(Cpu_Points==7)
{
SDL_FreeSurface(Message6);
apply_surface(30,10,Message7,screen);
}
else if(Cpu_Points==8)
{
SDL_FreeSurface(Message7);
apply_surface(30,10,Message8,screen);
}
else if(Cpu_Points==9)
{
SDL_FreeSurface(Message8);
apply_surface(30,10,Message9,screen);
}
else if(Cpu_Points==10)
{
SDL_FreeSurface(Message9);
apply_surface(30,10,Message10,screen);
apply_surface(310,205,MessageCongratsCpu,screen);
SDL_Delay(4000);
SDL_FreeSurface(Message10);
SDL_FreeSurface(MessageCongratsCpu);
SDL_FreeSurface(Ball);
SDL_Freesurface(Player_One_Bat);
SDL_FreeSurface(Cpu_Bat);
game_state==0;
}
}
};
Ball::Show_Ball()
{
apply_surface(x,y,Ball,screen);
};
Ball::Ball_Track_2_Player()
{
if(x+20 > SCREEN_WIDTH)
{
Player_One_Points+=1;
x=310;
y=230;
SDL_Delay(2000);
if(Player_One_Points==1)
{
SDL_FreeSurface(Message0);
apply_surface(30,10,Message1,screen);
}
else if(Player_One_Points==2)
{
SDL_FreeSurface(Message1);
apply_surface(30,10,Message2,screen);
}
else if(Player_One_Points==3)
{
SDL_FreeSurface(Message2);
apply_surface(30,10,Message3,screen);
}
else if(Player_One_Points==4)
{
SDL_FreeSurface(Message3);
apply_surface(30,10,Message4,screen);
}
else if(Player_One_Points==5)
{
SDL_FreeSurface(Message4);
apply_surface(30,10,Message5,screen);
}
else if(Player_One_Points==6)
{
SDL_FreeSurface(Message5);
apply_surface(30,10,Message6,screen);
}
else if(Player_One_Points==7)
{
SDL_FreeSurface(Message6);
apply_surface(30,10,Message7,screen);
}
else if(Player_One_Points==8)
{
SDL_FreeSurface(Message7);
apply_surface(30,10,Message8,screen);
}
else if(Player_One_Points==9)
{
SDL_FreeSurface(Message8);
apply_surface(30,10,Message9,screen);
}
else if(Player_One_Points==10)
{
SDL_FreeSurface(Message9);
apply_surface(30,10,Message10,screen);
apply_surface(310,205,MessageCongratsP1,screen);
SDL_Delay(4000);
SDL_FreeSurface(Message10);
SDL_FreeSurface(MessageCongratsP1);
SDL_FreeSurface(Ball);
SDL_Freesurface(Player_One_Bat);
SDL_FreeSurface(Player_Two_Bat);
game_state==0;
}
}
else(x<0)
{
Player_Two_Points+=1;
x=310;
y=230;
SDL_Delay(2000);
if(Player_Two_Points==1)
{
SDL_FreeSurface(Message0);
apply_surface(30,10,Message1,screen);
}
else if(Player_Two_Points==2)
{
SDL_FreeSurface(Message1);
apply_surface(30,10,Message2,screen);
}
else if(Player_Two_Points==3)
{
SDL_FreeSurface(Message2);
apply_surface(30,10,Message3,screen);
}
else if(Player_Two_Points==4)
{
SDL_FreeSurface(Message3);
apply_surface(30,10,Message4,screen);
}
else if(Player_Two_Points==5)
{
SDL_FreeSurface(Message4);
apply_surface(30,10,Message5,screen);
}
else if(Player_Two_Points==6)
{
SDL_FreeSurface(Message5);
apply_surface(30,10,Message6,screen);
}
else if(Player_Two_Points==7)
{
SDL_FreeSurface(Message6);
apply_surface(30,10,Message7,screen);
}
else if(Player_Two_Points==8)
{
SDL_FreeSurface(Message7);
apply_surface(30,10,Message8,screen);
}
else if(Player_Two_Points==9)
{
SDL_FreeSurface(Message8);
apply_surface(30,10,Message9,screen);
}
else if(Player_Two_Points==10)
{
SDL_FreeSurface(Message9);
apply_surface(30,10,Message10,screen);
apply_surface(310,205,MessageCongratsP2,screen);
SDL_Delay(4000);
SDL_FreeSurface(Message10);
SDL_FreeSurface(MessageCongratsP2);
SDL_FreeSurface(Ball);
SDL_Freesurface(Player_One_Bat);
SDL_FreeSurface(Player_Two_Bat);
game_state==0;
}
}
};
Ball::Ball_Wall_Collision()
{
if(y==0)&&(xVel==3)
{
yVel+=randomVelocity;
}
else if(y==0)&&(xVel<0)
{
yVel+=randomVelocity;
}
else if(y+=20==SCREEN_HEIGHT)&&(xVel==3)
{
yVel-=randomVelocity;
}
else(y+=20==SCREEN_HEIGHT)&&(xVel<0)
{
yVel-=randomVelocity;
}
};
class One_Player_Button
{
public:
SDL_Rect box;
One_Player_Button(intx,int,y,intw,inth);
void handle_events();
void show();
};
One_Player_Button::One_Player_Button(intx,inty,intw,inth)
{
box.x=x;
box.y=y;
box.w=w;
box.h=h;
};
One_Player_Button::handle_events()
{
intx=320,y=240;
if(event.type==SDL_MOUSEMOTION)
{
x=event.motion.x;
y=event.motion.y;
if(event.type==SDL_MOUSEBUTTONDOWN)
{
if(event.button.button==SDL_BUTTON_LEFT)
{
x=event.button.x;
y=event.button.y;
if((x>box.x)&&(x<box.x+box.w)&&(y>box.y)&&(y<box.y+box.h))
{
SDL_FreeSurface(MessagePong);
SDL_FreeSurface(MessageOnePlayer);
SDL_FreeSurface(MessageTwoPlayer);
game_state==1;
}
}
}
}
};
One_Player_Button::Show()
{
apply_surface(box.x,box.y,MessageOnePlayer,screen);
};
class Two_Player_Button
{
public:
SDL_Rect box;
Two_Player_Button(intx,int,y,intw,inth);
void handle_events();
void show();
};
Two_Player_Button::Two_Player_Button(intx,inty,intw,inth)
{
box.x=x;
box.y=y;
box.w=w;
box.h=h;
};
Two_Player_Button::handle_events()
{
intx=320,y=240;
if(event.type==SDL_MOUSEMOTION)
{
x=event.motion.x;
y=event.motion.y;
if(event.type==SDL_MOUSEBUTTONDOWN)
{
if(event.button.button==SDL_BUTTON_LEFT)
{
x=event.button.x;
y=event.button.y;
if((x>box.x)&&(x<box.x+box.w)&&(y>box.y)&&(y<box.y+box.h))
{
SDL_FreeSurface(MessagePong);
SDL_FreeSurface(MessageOnePlayer);
SDL_FreeSurface(MessageTwoPlayer);
game_state==2;
}
}
}
}
};
Two_Player_Button::Show()
{
apply_surface(box.x,box.y,MessageTwoPlayer,screen);
};
class Player_One_Bat
{
public:
int x,y;
const float yVel;
int xVel;
void Player_One_Bat();
void Show();
void Player_One_Bat_Collision();
void Player_One_Bat_Handle_Input();
};
Player_One_Bat::Player_One_Bat() : randomNumber(rand()), randomVelocity((randomNumber%6)+1)
{
x=10;
y=215;
xVel=0;
yVel=2.5;
};
Player_One_Bat::Show()
{
apply_surface(x,y,Player_One_Bat,screen);
};
Player_One_Bat::Player_One_Bat_Collision()
{
if(Ball.x==x+=10)
{
Ball.yVel+=||-= randomVelocity;
if(Ball.xVel<0)
{
Bal.xVel=3;
}
}
else if(Ball.x==x+=10)&&(SDLK_W)
{
Ball.yVel-= randomVelocity;
if(Ball.xVel<0)
{
Ball.xVel=3;
}
}
else(Ball.x==x+=10)&&(SDLK_S)
{
Ball.yVel+=randomVelocity;
if(Ball.xVel<0)
{
Ball.xVel=3;
}
}
};
Player_One_Bat::Player_One_Bat_Handle_Input()
{
if(event.type==SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_W:y-=yVel;break;
case SDLK_S:y+=yVel;break;
}
}
};
class Cpu_Bat
{
public:
int x,y;
const float yVel;
int xVel;
void Cpu_Bat();
void Show();
void Cpu_Bat_Collision();
void Cpu_Ai();
};
Cpu_Bat::Cpu_Bat() : randomNumber(rand()), randomVelocity((randomNumber%6)+1)
{
x=620;
y=215;
xVel=0;
yVel=2.5;
};
Cpu_Bat::Show()
{
apply_surface(x,y,Cpu_Bat,screen);
};
Cpu_Bat::Cpu_Bat_Collision()
{
if(Ball.x==x+=10)
{
Ball.yVel+=||-= randomVelocity;
if(Ball.xVel==3)
{
Bal.xVel-=6;
}
}
};
Cpu_Bat::Cpu_Bat_Ai()
{
if(y<Ball.y)
{
y+=Ball_Speed;
}
else(y>Ball.y)
{
y-=Ball_Speed;
}
};
class Player_Two_Bat
{
public:
int x,y;
const float yVel;
int xVel;
void Player_Two_Bat();
void Show();
void Player_Two_Bat_Collision();
void Player_Two_Bat_Handle_Input();
};
Player_Two_Bat::Player_Two_Bat() : randomNumber(rand()), randomVelocity((randomNumber%6)+1)
{
x=620;
y=215;
xVel=0;
yVel=2.5;
};
Player_Two_Bat::Show()
{
apply_surface(x,y,Player_Two_Bat,screen);
};
Player_Two_Bat::Player_Two_Bat_Collision()
{
if(Ball.x==x)
{
Ball.yVel+=||-= randomVelocity;
if(Ball.xVel==3)
{
Bal.xVel-=6;
}
}
else if(Ball.x==x)&&(SDLK_UP)
{
Ball.yVel-= randomVelocity;
if(Ball.xVel==3)
{
Ball.xVel-=6;
}
}
else(Ball.x==x)&&(SDLK_DOWN)
{
Ball.yVel+=randomVelocity;
if(Ball.xVel==3)
{
Ball.xVel-=6;
}
}
};
Player_Two_Bat::Player_Two_Bat_Handle_Input()
{
if(event.type==SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:y-=yVel;break;
case SDLK_DOWN:y+=yVel;break;
}
}
};
apply_surface(intx,inty,SDL_Surface*source,SDL_Surface*destination)
{
SDL_Rect offset;
offset.x=x;
offset.y=y;
SDL_BlitSurface(source,NULL,destination,&offset);
};
SDL_Surface*load_image(string filename)
{
SDL_Surface*loadedImage=NULL;
SDL_Surface*optimizedImage=NULL;
loadedImage=SDL_LoadBMP(filename.c_str());
if(loadedImage!=NULL)
{
optimizedImage=SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
};
int main (int argc, char*args [])
{
if(SDL_Init(SDL_INIT_EVERYTHING)==-1)
{
return 1;
}
SDL_Surface *screen=NULL;
SDL_Surface *background=NULL;
SDL_Surface *Player_One_Bat=NULL;
SDL_Surface *Cpu_Bat=NULL;
SDL_Surface *Player_Two_Bat=NULL;
SDL_Surface *MessagePong=NULL;
SDL_Surface *MessageOnePlayer=NULL;
SDL_Surface *messageTwoPlayer=NULL;
SDL_Surface *Message0=NULL;
SDL_Surface *Message1=NULL;
SDL_Surface *Message2=NULL;
SDL_Surface *Message3=NULL;
SDL_Surface *Message4=NULL;
SDL_Surface *Message5=NULL;
SDL_Surface *Message6=NULL;
SDL_Surface *Message7=NULL;
SDL_Surface *Message8=NULL;
SDL_Surface *Message9=NULL;
SDL_Surface *Message10=NULL;
SDL_Surface *MessageCongratsP1=NULL;
SDL_Surface *MessageCongratsCpu=NULL;
SDL_Surface *MessageCongratsP2=NULL;
SDL_Surface *Ball=NULL;
TTF_Font*font=NULL:
TTF_Font*font2=NULL;
SDL_Color textColor={255,255,255};
screen=SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
if(screen==NULL)
{
return 1;
}
if(TTF_Init()==-1)
{
return false;
}
SDL_WM_SetCaption("Ping Pong",NULL);
background=load_image("background.bmp");
Player_One_Bat=load_image("paddle.bmp");
Player_Two_Bat=load_image("paddle.bmp");
Cpu_Bat=load_image("paddle.bmp");
Ball=load_image("ball.bmp");
if(background==NULL)
{
return false;
}
if(Player_One_Bat==NULL)
{
return false;
}
if(Player_Two_Bat==NULL)
{
return false;
}
if(Cpu_Bat==NULL)
{
return false;
}
if(Ball==NULL)
{
return false;
}
font=TTF_OpenFont("EthnocentricRg-Regular",36);
font2=TTF_OpenFont("EthnocentricRg-Regular",28);
if(font==NULL)
{
return false;
}
if(font2==NULL)
{
return false;
}
MessagePong=TTF_RenderText_Solid(font,"Ping Pong",textColor);
MessageOnePlayer=TTF_RenderText_Solid(font2,"One Player",textColor);
MessageTwoPlayer=TTF_RenderText_Solid(font2,"Two Player",textColor);
Message0=TTF_RenderText_Solid(font2,"0",textColor);
Message1=TTF_RenderText_Solid(font2,"1",textColor);
Message2=TTF_RenderText_Solid(font2,"2",textColor);
Message3=TTF_RenderText_Solid(font2,"3",textColor);
Message4=TTF_RenderText_Solid(font2,"4",textColor);
Message5=TTF_RenderText_Solid(font2,"5",textColor);
Message6=TTF_RenderText_Solid(font2,"6",textColor);
Message7=TTF_RenderText_Solid(font2,"7",textColor);
Message8=TTF_RenderText_Solid(font2,"8",textColor);
Message9=TTF_RenderText_Solid(font2,"9",textColor);
Message10=TTF_RenderText_Solid(font2,"10",textColor);
MessageCongratsP1=TTF_RenderText_Solid(font2,"Congratulations Player One",textColor);
MessageCongratsCpu=TTF_RenderText_Solid(font2,"Congratulations Cpu",textColor);
MessageCongratsP2=TTF_RenderText_Solid(font2,"Congratulations Player Two",textColor);
if(MessagePong==NULL)
{
return 1;
}
if(MessageOnePlayer==NULL)
{
return 1;
}
if(MessageTwoPlayer==NULL)
{
return 1;
}
if(Message0==NULL)
{
return 1;
}
if(Message1==NULL)
{
return 1;
}
if(Message2==NULL)
{
return 1;
}
if(Message3==NULL)
{
return 1;
}
if(Message4==NULL)
{
return 1;
}
if(Message5==NULL)
{
return 1;
}
if(Message6==NULL)
{
return 1;
}
if(Message7==NULL)
{
return 1;
}
if(Message8==NULL)
{
return 1;
}
if(Message9==NULL)
{
return 1;
}
if(Message10==NULL)
{
return 1;
}
if(MessageCongratsP1==NULL)
{
return 1;
}
if(MessageCongratsCpu==NULL)
{
return 1;
}
if(MessageCongratsP2==NULL)
{
return 1;
}
SDL_Event event;
while(quit=false)
{
fps.start();
if(game_state==0)
{
apply_surface(0,0,background,screen);
apply_surface(310,10,MessagePong,screen);
Player_One_Button.Show();
Player_Two_Button.Show();
}
else if(game_state==1)
{
Ball.Ball_Track();
Ball.Ball_Wall_Collision();
Cpu_Bat.Cpu_Bat_Ai();
Cpu_Bat.Cpu_Bat_Collision();
Player_One_Bat.Player_One_Bat_Collision();
Ball.Show_Ball();
Player_One_Bat.Show();
Cpu_Bat.Show();
apply-surface(30,10,Message0,screen);
apply-surface(600,10,Message0,screen);
}
else if(game_state==2)
{
Ball.Ball_Two_Player_Track();
Ball.Ball_Wall_Collision();
Player_One_Bat.Player_One_Bat_Collision();
Player_Two_Bat.Player_Two_Bat_Collision();
Ball.Show_Ball();
Player_One_Bat.Show();
Player_Two_Bat.Show();
apply-surface(30,10,Message0,screen);
apply-surface(600,10,Message0,screen);
}
while(SDL_PollEvent(&event))
{
if(game_state==0)
{
One_Player_Button.handle_events();
Two_Player_Button.handle_events();
if(event.type==SDL_QUIT)
{
quit=true;
}
}
else if(game_state==1)
{
Player_One_Bat.Player_One_Bat_Handle_Input();
if(event.type==SDL_QUIT)
{
quit=true;
}
}
else(game_state==2)
{
Player_One_Bat.Player_One_Bat_Handle_Input();
Player_Two_Bat.Player_Two_Bat_Handle_Input();
if(event.type==SDL_QUIT)
{
quit=true;
}
}
}
if(SDL_Flip(screen)==-1)
{
return 1;
}
frame++;
if((cap==true)&&(fps.get_ticks()<1000/FRAMES_PER_SECOND))
{
SDL_Delay((1000/FRAMES_PER_SECOND)-fps.get_ticks());
}
}
SDL_FreeSurface(background);
return 0;
}
What is the problem, it won't compile?