#include <SDL/SDL.h>
#include <iostream>
#include <conio.h>
using namespace std;
int newgame();
int end();
int win();
int computer();
int checkscore();
void score();
void DrawALL();
void Moveball();
void collision();
int player1score = 0;
int player2score = 0;
int maxscore = 10;
int ballx = 394;
int bally = 294;
int player1x = 20;
int player1y = 280;
int player2x = 764;
int player2y = 280;
int ballspeedx = 3;
int ballspeedy =1;
SDL_Event poolevent;
SDL_Event event;
SDL_Surface * screen;
SDL_Surface * score1;
SDL_Surface * score2;
SDL_Surface * back3=SDL_LoadBMP("back3.BMP");
SDL_Surface * back2=SDL_LoadBMP("back2.BMP");
SDL_Surface * back=SDL_LoadBMP("back.BMP");
SDL_Surface * player1=SDL_LoadBMP("player1.BMP");
SDL_Surface * player2=SDL_LoadBMP("player1.BMP");
SDL_Surface * ball=SDL_LoadBMP("ball.BMP");
SDL_Surface * player1win;
SDL_Surface * player2win;
void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}
-
int main( int argc, char *arcv[])
{
if( SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0 )
{
std::cout<<"ERROR INITILASING SDL!" <<std::endl<<"Reason: "<<SDL_GetError();
exit(1);
}
atexit(SDL_Quit);
-
screen=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE|SDL_HWPALETTE);
SDL_WM_SetCaption( "Pong clone", "Pong clone" );
SDL_ShowCursor( SDL_DISABLE );
if(screen == NULL)
{
std::cout<<"ERROR SETTING 800x600x32 VIDEO!"<<std::endl<<"Reason: "<<SDL_GetError();
exit(1);
}
-
while(1)
{
DrawALL();
Moveball();
collision();
score();
-
if (player1score == maxscore || player2score == maxscore)
{
win();
}
-
-
SDL_Event event;
Uint8* keys;
-
-
while( SDL_PollEvent(&event) )
{
if(event.type == SDL_QUIT) { return 0; }
-
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym==SDLK_ESCAPE) { return 0; }
}
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_w] && player1y >= 0 )
{
player1y -= 2;
}
if ( keys[SDLK_s] && player1y <= 540 )
{
player1y += 2;
}
if ( keys[SDLK_UP] && player2y >=0)
{
player2y -=2;
}
if ( keys[SDLK_DOWN] && player2y <=540 )
{
player2y +=2;
}
-
-
-
-
}
-
}
void DrawALL()
{
DrawIMG(player1win, 270, 250);
DrawIMG(player2win, 270, 250);
DrawIMG(back2, ballx-2, bally-2);
DrawIMG(back, player1x-5, player1y-5);
DrawIMG(back, player2x-5, player2y-5);
DrawIMG(player1, player1x, player1y);
DrawIMG(player2, player2x, player2y);
DrawIMG(back3, 300 ,0);
DrawIMG(back3, 450, 0);
DrawIMG(score1, 300, 0);
DrawIMG(score2, 450, 0);
if (player1score < 10 && player2score < 10)
{
DrawIMG(ball, ballx, bally);
}
SDL_Flip(screen);
}
void Moveball()
{
ballx = ballx - ballspeedx;
bally = bally - ballspeedy;
DrawALL();
}
void score()
{
if (player1score == 0)
{
score1=SDL_LoadBMP("0.BMP");
}
if (player1score == 1)
{
score1=SDL_LoadBMP("1.BMP");
}
if (player1score == 2)
{
score1=SDL_LoadBMP("2.BMP");
}
if (player1score == 3)
{
score1=SDL_LoadBMP("3.BMP");
}
if (player1score == 4)
{
score1=SDL_LoadBMP("4.BMP");
}
if (player1score == 5)
{
score1=SDL_LoadBMP("5.BMP");
}
if (player1score == 6)
{
score1=SDL_LoadBMP("6.BMP");
}
if (player1score == 7)
{
score1=SDL_LoadBMP("7.BMP");
}
if (player1score == 8)
{
score1=SDL_LoadBMP("8.BMP");
}
if (player1score == 9)
{
score1=SDL_LoadBMP("9.BMP");
}
if (player1score == 10)
{
score1=SDL_LoadBMP("10.BMP");
}
if (player2score == 0)
{
score2=SDL_LoadBMP("0.BMP");
}
if (player2score == 1)
{
score2=SDL_LoadBMP("1.BMP");
}
if (player2score == 2)
{
score2=SDL_LoadBMP("2.BMP");
}
if (player2score == 3)
{
score2=SDL_LoadBMP("3.BMP");
}
if (player2score == 4)
{
score2=SDL_LoadBMP("4.BMP");
}
if (player2score == 5)
{
score2=SDL_LoadBMP("5.BMP");
}
if (player2score == 6)
{
score2=SDL_LoadBMP("6.BMP");
}
if (player2score == 7)
{
score2=SDL_LoadBMP("7.BMP");
}
if (player2score == 8)
{
score2=SDL_LoadBMP("8.BMP");
}
if (player2score == 9)
{
score2=SDL_LoadBMP("9.BMP");
}
if (player2score == 10)
{
score1=SDL_LoadBMP("10.BMP");
}
}
int win()
{
-
SDL_Event event;
Uint8* keys;
-
while( SDL_PollEvent(&event) )
{
if(event.type == SDL_QUIT) { return 0; }
-
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym==SDLK_ESCAPE) { return 0; }
}
}
keys = SDL_GetKeyState(NULL);
-
-
if (player1score == maxscore)
{
player1win=SDL_LoadBMP("player1win.BMP");
DrawALL();
ballspeedx = 0;
ballspeedy = 0;
if ( keys[SDLK_w])
{
newgame();
}
}
if (player2score == maxscore)
{
player2win=SDL_LoadBMP("player2win.BMP");
DrawALL();
ballspeedx = 0;
ballspeedy = 0;
if ( keys[SDLK_w])
{
newgame();
}
}
}
int end()
{
-
}
int newgame()
{
player1win = NULL;
player2win = NULL;
DrawIMG(player1win, 270, 250);
DrawIMG(player2win, 270, 250);
DrawIMG(back, player1x-5, player1y-5);
DrawIMG(back, player2x-5, player2y-5);
player1score = 0;
player2score = 0;
maxscore = 10;
ballx = 394;
bally = 294;
player1x = 20;
player1y = 280;
player2x = 764;
player2y = 280;
ballspeedx = 3;
ballspeedy =1;
SDL_Delay(2000);
}
-
void collision()
{
-
if (bally < 0)
{
ballspeedy = -ballspeedy;
}
if (bally > 590)
{
ballspeedy = -ballspeedy;
}
if (ballx < -15)
{
DrawALL();
player2score = player2score+1;
ballx = 394;
bally = 294;
SDL_Delay(100);
}
if (ballx > 805)
{
DrawALL();
player1score = player1score+1;
ballx = 394;
bally = 294;
SDL_Delay(100);
}
if (ballx > player1x && ballx < player1x+16 && bally > player1y && bally < player1y +60)
{
ballspeedx = -ballspeedx;
ballspeedy = ballspeedy;
}
if (ballx > player2x-16 && ballx < player2x+32 && bally > player2y && bally < player2y+60)
{
ballspeedx = -ballspeedx;
ballspeedy = ballspeedy;
}
}
-