help with pong game code

I am trying to write a simple pong game and have been following the lazy foo tutorial. I cannot get collision detection on the second paddle and I cannot get the paddle to move. please help, I am a beginner and really dont know much about programming

here is my code

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>


const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The frame rate
const int FRAMES_PER_SECOND = 20;

//The attributes of the square
const int SQUARE_WIDTH = 20;
const int SQUARE_HEIGHT = 20;
const int WALL_HEIGHT = 20;

//The surfaces
SDL_Surface *square = NULL;
SDL_Surface *screen = NULL;

//The event structure
SDL_Event event;

//The wall
SDL_Rect wall;
SDL_Rect wall2;
//The square
class Square
{
private:

SDL_Rect box;


int xVel, yVel;

public:

Square();


void handle_input();


void move();


void show();
};


class Wall
{
private:

SDL_Rect box;


int xVel, yVel;

public:

Wall();

void handle_input2();

void move2();
};




SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;

//The optimized surface 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 surface
optimizedImage = SDL_DisplayFormat( loadedImage );

//Free the old surface
SDL_FreeSurface( loadedImage );

//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}

//Return the optimized surface
return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = x;
offset.y = y;

//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}

bool check_collision( SDL_Rect A, SDL_Rect B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;

//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;

//Calculate the sides of rect B
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;

//If any of the sides from A are outside of B
if( bottomA <= topB )
{
return false;
}

if( topA >= bottomB )
{
return false;
}

if( rightA <= leftB )
{
return false;
}

if( leftA >= rightB )
{
return false;
}

//If none of the sides from A are outside B
return true;
}

bool check_collision2( SDL_Rect B, SDL_Rect C )
{

int leftB, leftC;
int rightB, rightC;
int topB, topC;
int bottomB, bottomC;


leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;


leftC = C.x;
rightC = C.x + C.w;
topC = C.y;
bottomC = C.y + C.h;




if( bottomB <= topC )
{
return false;
}

if( topB >= bottomC )
{
return false;
}

if( rightB <= leftC )
{
return false;
}

if( leftB >= rightC )
{
return false;
}


return true;
}

bool init()
{

if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}


screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );


if( screen == NULL )
{
return false;
}
return true;
}

bool load_files()
{

square = load_image( "square.bmp" );


if( square == NULL )
{
return false;
}


return true;
}

void clean_up()
{

SDL_FreeSurface( square );


SDL_Quit();
}

Square::Square()
{

box.x = 0;
box.y = 0;



box.w = SQUARE_WIDTH;
box.h = SQUARE_HEIGHT;




xVel = 0;
yVel = 0;
}

Wall::Wall()
{
wall.x = 0;
wall.y = 0;

wall.h = WALL_HEIGHT;


xVel = 0;
yVel = 0;
}
void Square::handle_input()
{

if( event.type == SDL_KEYDOWN )
{

switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;
}
}

else if( event.type == SDL_KEYUP )
{

switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += SQUARE_HEIGHT / 2; break;
case SDLK_DOWN: yVel -= SQUARE_HEIGHT / 2; break;
case SDLK_LEFT: xVel += SQUARE_WIDTH / 2; break;
case SDLK_RIGHT: xVel -= SQUARE_WIDTH / 2; break;
}
}
}

void Wall::handle_input2()
{

if( event.type == SDL_KEYDOWN )
{

switch( event.key.keysym.sym )
{
case SDLK_w: yVel -= WALL_HEIGHT / 2; break;
case SDLK_z: yVel += WALL_HEIGHT / 2; break;

}
}

else if( event.type == SDL_KEYUP )
{

switch( event.key.keysym.sym )
{
case SDLK_w: yVel += WALL_HEIGHT / 2; break;
case SDLK_z: yVel -= WALL_HEIGHT / 2; break;

}
}
}
void Square::move()
{

box.x += xVel;


if( ( box.x < 0 ) || ( box.x + SQUARE_WIDTH > SCREEN_WIDTH ) || ( check_collision( box, wall ) ) )
{
//Move back
box.x -= xVel;
}

//Move the square up or down
box.y += yVel;


if( ( box.y < 0 ) || ( box.y + SQUARE_HEIGHT > SCREEN_HEIGHT ) || ( check_collision( box, wall ) ) )
{
//Move back
box.y -= yVel;
}

}

void Wall::move2()
{

//Move the square up or down
wall.y += yVel;

//If the square went too far up or down or has collided with the wall
if( ( wall.y < 0 ) || ( wall.y + WALL_HEIGHT > SCREEN_HEIGHT ) || ( check_collision2( box, wall ) ) )
{
//Move back
wall.y -= yVel;
}

}


void Square::show()
{
//Show the square
apply_surface( box.x, box.y, square, screen );
}

void Wall::show2()
{
//Show the square
apply_surface( box.x, box.y, square, screen );
}




int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;

//The square
Square mySquare;

//The square
Wall myWall;


//The frame rate regulator
Timer fps;

//Initialize
if( init() == false )
{
return 1;
}

//Load the files
if( load_files() == false )
{
return 1;
}

//Set the wall
wall.x = 600;
wall.y = 200;
wall.w = 20;
wall.h = 100;

//Set the wall
wall2.x = 20;
wall2.y = 200;
wall2.w = 20;
wall2.h = 100;



//While the user hasn't quit
while( quit == false )
{


while( SDL_PollEvent( &event ) )
{

mySquare.handle_input();


if( event.type == SDL_QUIT )
{

quit = true;
}
}


mySquare.move();


myWall.move2();


SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

//Show the wall
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 0x77, 0x77, 0x77 ) );


SDL_FillRect( screen, &wall2, SDL_MapRGB( screen->format, 0x77, 0x77, 0x77 ) );



mySquare.show();


if( SDL_Flip( screen ) == -1 )
{
return 1;
}

//Clean up
clean_up();

return 0;
}
had to take out some time code in order to get this to fit, so you might see some things wrong with that
thanks in advance :)
Topic archived. No new replies allowed.