Sign in to follow this  
nooblet

[SDL] Pong Collision Error

Recommended Posts

Hey everyone, I'm attempting a Pong clone using the SDL library and C++, who hasn't?! :P. Anyways, I'm having an error with my code, tell me if you can see the error, sorry if it's sloppy for you.
// HEADERS
#include <SDL/SDL.h>

// SET BOOLEANS && EVENT
SDL_Event event;

bool GAMEOVER = false;

// SET SURFACES
SDL_Surface *screen = NULL;
SDL_Surface *bg = NULL;
SDL_Surface *paddle = NULL;
SDL_Surface *ball = NULL;

// SET VARIABLES
int p1_x = 0;
int p1_y = 150;
int p1_w = 27;
int p1_h = 117;
int p1_sx = 50;
int p1_sy = 50;
int p2_x = 613;
int p2_y = 150;
int p2_w = 27;
int p2_h = 117;
int p2_sx = 5;
int p2_sy = 5;

int ball_x = 320;
int ball_y = 240;
int ball_w = 22;
int ball_h = 22;
int ball_sx = 1;
int ball_sy = 1;

int screen_width = 640;
int screen_height = 480;
int screen_bpp = 32;

void draw(int x, int y, SDL_Surface *src, SDL_Surface *dst)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(src, NULL, dst, &offset);
}

int main(int argc, char* argv[])
{
	// START SDL
	SDL_Init(SDL_INIT_EVERYTHING);
	
	// LOAD SURFACES
	screen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_SWSURFACE);
	bg = SDL_LoadBMP("bg.bmp");
	paddle = SDL_LoadBMP("paddle.bmp");
	ball = SDL_LoadBMP("ball.bmp");
	// SET SCREEN
	SDL_WM_SetCaption("My Pong Clone", NULL);	
	SDL_ShowCursor(SDL_DISABLE);

	while(GAMEOVER == false) // MAIN GAME LOOP
	{
		// DRAW IMAGES
		draw(0, 0, bg, screen);
		draw(p1_x, p1_y, paddle, screen);
		draw(p2_x, p2_y, paddle, screen);
		draw(ball_x, ball_y, ball, screen);

		// SET SPEEDS
		ball_x += ball_sx;
		ball_y += ball_sy;
		// HANDLE MOVEMENT
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
			{
				GAMEOVER = true;
			}
			else if(event.type == SDL_KEYDOWN)
			{
				if(event.key.keysym.sym == SDLK_UP)
				{
					p1_y -= p1_sy;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					p1_y += p1_sy;
				}
			}
			else if(event.type == SDL_KEYUP)
			{
				if(event.key.keysym.sym == SDLK_UP)
				{
					p1_y += p1_sy - 46;
				}
				else if(event.key.keysym.sym == SDLK_DOWN)
				{
					p1_y -= p1_sy - 46;
				}
			}
		}

		// CHECK COLLISION
		if(p1_y < 0)
		{
			p1_y += p1_sy;
		}
		else if(p1_y + p1_h >= screen_height)
		{
			p1_y -= p1_sy;
		}
		else if(p2_y < 0)
		{
			p2_y += p2_sy;
		}
		else if(p2_y + p2_h >= screen_height)
		{
			p2_y -= p2_sy;
		}
		else if(ball_x <= p1_x + p1_w)
		{
			ball_sx *= -1;
		}
		else if(ball_x + ball_h >= p2_x)
		{
			ball_sx *= -1;
		}
		else if(ball_y + ball_h >= p1_y)
		{
			ball_sy *= -1;
		}
		else if(ball_y * ball_h >= p2_y)
		{
			ball_sy *= -1;
		}
		else if(ball_y <= p1_y + p1_h)
		{
			ball_sy *= -1;
		}
		else if(ball_y <= p2_y + p2_h)
		{
			ball_sy *= -1;
		}
		
		// REFRESH SCREEN
		SDL_Flip(screen);
	} 	// END LOOP
	// CLEAN UP
	SDL_FreeSurface(bg);
	SDL_FreeSurface(paddle);
	SDL_FreeSurface(ball);

	SDL_Quit();
	
	return 0;
}

Share this post


Link to post
Share on other sites
Well I looked at it a little bit more, and found out the problem, but I don't know the solution.

What's happening, is the ball keeps going back to the other side, even if the paddle is moving up.

It's drawing a huge line for the X coordinate, and the ball is hitting the x coordinate and bouncing back because of...


else if(ball_x <= p1_x + p1_w)
{
ball_sx *= -1;
}







Share this post


Link to post
Share on other sites
Instead of just using position variables, use an SDL_Rect to represent the paddles and ball. Then write a generic rect-collision function, that you can use in almost every game you write from now to 3D. Then, at least section off your screen bounds-checking with comments like "player 1 bounds checking", "ball bounds-checking", etc. Use slighly longer, more descriptive variable names.

Actually, this would be a good place to wrap the players and ball in a class. Just look at all the variables you prefix with p1 or p2, and put those in a class Player. then you can just go Player p1, p2. put their bounds checking in a member function. Same with the ball. Later, try making them both inherit from a base class Sprite. As it is, you may as well be programming in C.

Make sure you check every SDL error return.

On the whole, other than that, not bad!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this