Code:
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface *background = NULL;
SDL_Surface *pong = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *paddle = NULL;
// pong cords
int speed_x = 2;
int speed_y = 2;
int pong_x = 140;
int pong_y = 200;
//use paddle cordinates
int paddle_y = 200, aipaddle_y = 50;
const int paddle_x = 620;
const int aipaddle_x = 20;
int random[10] = { 0, 50, 100, 150, 200, 250, 300, 400, 410, 125};
int x = 0;
// collision (fixes glitch of ball staying on paddle)
bool hit = true, loss = false, ai_top = true, ai_bottom = true;
//The event structure
SDL_Event event;
Uint8 *keystate;
bool start()
{
bool game = true;
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return false;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return false;
}
// quit button
SDL_Surface* quit_temp = IMG_Load("quit.png");
if (!quit_temp)
{
printf("Unable to load image: %s\n", SDL_GetError());
return false;
}
SDL_Surface* quit = SDL_DisplayFormat( quit_temp);
SDL_Surface* quit_rollover_temp = IMG_Load("quit_rollover.png");
if (!quit_rollover_temp)
{
printf("Unable to load image: %s\n", SDL_GetError());
return false;
}
SDL_Surface* quit_rollover = SDL_DisplayFormat( quit_rollover_temp );
SDL_Surface* start_temp = IMG_Load("start.png");
if (!start_temp)
{
printf("Unable to load image: %s\n", SDL_GetError());
return false;
}
SDL_Surface* start = SDL_DisplayFormat( start_temp );
SDL_Surface* start_rollover_temp = IMG_Load("start_rollover.png");
if (!quit_rollover_temp)
{
printf("Unable to load image: %s\n", SDL_GetError());
return false;
}
SDL_Surface* start_rollover = SDL_DisplayFormat( start_rollover_temp );
// free loaded image
SDL_FreeSurface(quit_temp);
SDL_FreeSurface(start_temp);
SDL_FreeSurface(quit_rollover_temp);
SDL_FreeSurface(start_rollover_temp);
// center the bitmap on screen
SDL_Rect quitrect;
quitrect.x = (screen->w - quit->w) / 2;
quitrect.y = (screen->h - quit->h);
SDL_Rect startrect;
startrect.x = (screen->w - start->w) / 2;
startrect.y = (screen->h - start->h) / 2;
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
game = false;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
game = false;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 230, 230, 230));
// draw image
SDL_BlitSurface(quit, 0, screen, &quitrect);
SDL_BlitSurface(start, 0, screen, &startrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(quit);
SDL_FreeSurface(start);
SDL_FreeSurface(quit_rollover);
SDL_FreeSurface(start_rollover);
// all is well ;)
printf("Exited cleanly\n");
return game;
}
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
//If the image was optimized just fine
if( optimizedImage != NULL )
{
//Map the color key
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 255, 255 );
//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
}
//Return the optimized image
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Temporary rectangle to hold the offsets
SDL_Rect offset;
//Get the offsets
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return 1;
}
//Set the window caption
SDL_WM_SetCaption( "Foo says \"Hello!\"", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the background image
background = load_image( "gamebg.bmp" );
//If the background didn't load
if( background == NULL )
{
return false;
}
//Load the pong figure
pong = load_image( "pong.png" );
//If the stick figure didn't load
if( pong == NULL )
{
return false;
}
//Load the paddle figure
paddle = load_image( "paddle.png" );
//If the stick figure didn't load
if( paddle == NULL )
{
return false;
}
return true;
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface( pong );
SDL_FreeSurface(paddle);
//Quit SDL
SDL_Quit();
}
int main( int argc, char* args[] )
{
if(start())
{
//Quit flag
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//While the user hasn't quit
while( quit == false )
{
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
} // end if statement
} // end event loop
/****************************************************************
/ if someone loses, reset game with delay and random pong_x
/*******************************************************************/
if(loss)
{
pong_x = 100;
pong_y = random[x];
paddle_y = 200;
aipaddle_y = 50;
loss = false;
speed_x = 2;
speed_y = 2;
hit = true;
SDL_Delay( 1000 );
}
srand((unsigned)time(0));
x = (rand()%10)+1;
/************************************************
/
/ check if ball is touching screen
*************************************************/
if (pong_x <= 10) {loss = true;}
// image is 30 pixels wide
if (pong_x >= 610) {loss = true;}
if (pong_y <= 0) {speed_y = 2;}
// image is 30 pixels high
if (pong_y >= 450) {speed_y = -2;}
pong_x += speed_x;
pong_y += speed_y;
/**********************************************
/ check for paddle movement
/**********************************************/
keystate = SDL_GetKeyState(NULL);
if (keystate[SDLK_UP] & paddle_y >= 0)
{
paddle_y -= 2;
}
else if (keystate[SDLK_DOWN] & paddle_y <= 430)
{
paddle_y += 2;
}
/********************************************
/ Check if pong is touching paddle
/********************************************/
if( hit & pong_y >= paddle_y & pong_y <= paddle_y + 40 & pong_x + 30 >= paddle_x & pong_x + 30 <= paddle_x + 10)
{
speed_x = - speed_x;
hit = false;
}
if(hit & pong_y + 30 >= paddle_y & pong_y + 30 <= paddle_y + 40 & pong_x + 30 >= paddle_x & pong_x + 30 <= paddle_x + 10)
{
speed_x = - speed_x;
hit = false;
}
/******************************************
/ ai paddle proccess
/*****************************************/
// if paddle is on screen
if(aipaddle_y >= 0)
{
ai_top = true;
}
else {ai_top = false;}
if(aipaddle_y <= 440)
{
ai_bottom = true;
}
else {ai_bottom = false;}
// puase for to make ai look more human
if(pong_x < 360)
{
if(pong_y > aipaddle_y & ai_bottom) {aipaddle_y += 3;}
if(pong_y < aipaddle_y & ai_top) {aipaddle_y -= 3;}
}
else if(pong_x > 300 & pong_y > 460 & ai_top)
{
aipaddle_y -= 3;
}
else if(pong_x > 300 & pong_y < 200 & ai_bottom)
{
aipaddle_y += 3;
}
if( pong_y >= aipaddle_y & pong_y <= aipaddle_y + 40 & pong_x >= aipaddle_x & pong_x <= aipaddle_x + 10)
{
speed_x = - speed_x;
hit = true;
}
if(pong_y + 30 >= aipaddle_y & pong_y + 30 <= aipaddle_y + 40 & pong_x >= aipaddle_x & pong_x <= aipaddle_x + 10)
{
speed_x = - speed_x;
hit = true;
}
pong_x += speed_x;
pong_y += speed_y;
//Apply the surfaces to the screen
apply_surface( 0, 0, background, screen );
apply_surface( pong_x, pong_y, pong, screen );
apply_surface(paddle_x, paddle_y, paddle, screen);
apply_surface(aipaddle_x, aipaddle_y, paddle, screen);
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
} // end update statement
} //end while loop
//Free the surfaces and quit SDL
clean_up();
return 0;
}
}
Thanks for any help,