Help with Pong clone

Started by
4 comments, last by AspiringDev 9 years, 9 months ago

Hello I'm close to finishing my Pong clone and I would like some input . First I would like to clarify and it's in my plans to clean the code and make it more readable so excuse me if you found that it's too messy and a bit monotonous as I haven't implemented the speed change to the ball after it hits the paddle. Well to the main point now. I have implemented a PLAY ( start game button before the match starts) and a PLAY AGAIN button after a match has ended. The thing is that despite the fact that the PLAY button behaves(or it seems at least) as expected , the PLAY AGAIN button doesn't. In addition after the player clicks on it and he is returned to a supposedly fresh new match...it appears that the score of the winning player of the last match is set to 1 and the collision sound of the ball is played with no real reason some times. My main problems are these but any general tips and input will be vastly appreciated ! I hope someone can help me so thanks in advance !

I am providing a link to a zip file with the project file(source code and assets) from visual studio unfortunately as I couldn't figure out how to deploy a game properly.

Advertisement

I thought it would be good to add the source code here so you can take a glance at it !


#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include <stdlib.h>
#include <SDL_TTF.h>
#include <sstream>
#include "SDL_mixer.h"

//Screen attributes
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
const int SCREEN_BPP = 32;

//The dimensions of objects
const int PADDLE_WIDTH = 16;
const int PADDLE_HEIGHT = 100;
const int BALL_WIDTH = 18;
const int BALL_HEIGHT =18;
const int SPEED = 1000;

//The surfaces
SDL_Surface *rightPaddle = NULL;
SDL_Surface *leftPaddle = NULL;
SDL_Surface *ball = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *button = NULL;
SDL_Surface *button2 = NULL;

SDL_Surface *score1 = NULL;
SDL_Surface *score2 = NULL;
SDL_Surface *winningMessage = NULL;

//The event structure
SDL_Event event;

//The font in use
TTF_Font *font = NULL;
SDL_Color textColor = { 255, 250, 250 };

//The collision sound
Mix_Chunk *collisionSound = NULL;

class Ball
{
    private:        
        float xVelocity,yVelocity;
        float x,y;

    public:        
        Uint32 Player1Score ;
        Uint32 Player2Score ;
        Ball();        
        void move(float xLeftPaddle, float yLeftPaddle,float xRightPaddle, float yRightPaddle, Uint32 deltaticks);
        void show();        
};

Ball::Ball()
{
    x = SCREEN_WIDTH/2;
    y = SCREEN_HEIGHT/2;
    xVelocity = 300;
    yVelocity = 150;
    Player1Score = 0;
    Player2Score = 0;
}

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 );
}

void Ball::move(float xLeftPaddle, float yLeftPaddle,float xRightPaddle, float yRightPaddle, Uint32 deltaTicks)
{        
    y +=  yVelocity * ( deltaTicks / 1000.f );
    x +=  xVelocity * ( deltaTicks / 1000.f );    
            
    if( y + BALL_HEIGHT > SCREEN_HEIGHT )//Bottom wall
    {
        Mix_PlayChannel(-1,collisionSound, 0);
        x = x-1;
        y = y-1;
        yVelocity = -yVelocity;
        
    }
    else if( y < 0 )//Top wall
    {
        Mix_PlayChannel(-1,collisionSound, 0);
        x = x+1;
        y = y+1;
        yVelocity = -yVelocity ;        
    }    
    else if(x + BALL_WIDTH >= xRightPaddle && y <= yRightPaddle + PADDLE_HEIGHT && y >=yRightPaddle && x < xRightPaddle + PADDLE_WIDTH  )//Right Paddle collision
    {

        x = x-1;
        y = y-1;        
        xVelocity = -xVelocity;

        Mix_PlayChannel(-1,collisionSound, 0);
        
        if( y + BALL_WIDTH < yRightPaddle + (PADDLE_HEIGHT)/2 && yVelocity > 0)
        {
            yVelocity = -yVelocity;
        }

        if( y  + BALL_WIDTH >= yRightPaddle + (PADDLE_HEIGHT)/2 && yVelocity < 0)
        {            
            yVelocity = -yVelocity;        
        }
        
    }
    else if( x <= xLeftPaddle + PADDLE_WIDTH && y <= yLeftPaddle + PADDLE_HEIGHT && y >= yLeftPaddle && x > xLeftPaddle )//Left Paddle collision
    {        
        x = x + 1;
        y = y + 1;    

        Mix_PlayChannel(-1,collisionSound, 0);

        xVelocity = -xVelocity;    
        
        if( y < yLeftPaddle + (PADDLE_HEIGHT)/2 && yVelocity > 0)
        {
            yVelocity = -yVelocity;
        }

        if( y >= yLeftPaddle + (PADDLE_HEIGHT)/2 && yVelocity < 0)
        {            
            yVelocity = -yVelocity;        
        }
    }    
    else if(x < 0 )//Reset position if out of bounds
    {
        Player2Score++;
        
        x = SCREEN_WIDTH/2;
        y = SCREEN_HEIGHT/2;
    }
    else if(x > SCREEN_WIDTH)
    {
        Player1Score++;
        
        x = SCREEN_WIDTH/2;
        y = SCREEN_HEIGHT/2;
    }    
}

void Ball::show()
{
    apply_surface( (int)x, (int)y, ball, screen );
}

class RightPaddle
{
    private:        
        
        float yVelocity;
    public:
        float x,y;
        RightPaddle();
        void handle_right_input(Uint32 deltaTicks);
        void move(Uint32 deltaTicks);
        void show();
};

RightPaddle::RightPaddle()
{
    x = 900;
    y = SCREEN_HEIGHT;
    yVelocity = 0;
}

void RightPaddle::handle_right_input(Uint32 deltaTicks)
{
    
    if(event.type == SDL_KEYDOWN)
    {
        switch(event.key.keysym.sym)
        {
            case SDLK_UP: yVelocity -= SPEED  ; break;
            case SDLK_DOWN: yVelocity += SPEED ; break;
        }
    }
    else if(event.type == SDL_KEYUP)
    {
        switch(event.key.keysym.sym)
        {
            case SDLK_UP: yVelocity += SPEED; break;
            case SDLK_DOWN: yVelocity -= SPEED ;break;
        }
    }
}

void RightPaddle::move(Uint32 deltaTicks)
{
    y += yVelocity  * ( deltaTicks / 1000.f ) ;

    if( y  < 0 )
    {
        y =  0;
        
    }
    else if( y + PADDLE_HEIGHT > SCREEN_HEIGHT)
    {
        y = SCREEN_HEIGHT - PADDLE_HEIGHT;
    }
}

void RightPaddle::show()
{
    apply_surface( (int)x, (int)y, rightPaddle, screen );
}

class LeftPaddle
{
    private:        
        
        float yVelocity;
    public:
        float x,y;
        LeftPaddle();
        void handle_left_input(Uint32 deltaTicks);
        void move(Uint32 deltaTicks);
        void show();
};

LeftPaddle::LeftPaddle()
{
    x = 100;
    y = 0;
    yVelocity = 0;
}

void LeftPaddle::handle_left_input(Uint32 deltaTicks)
{
    if(event.type == SDL_KEYDOWN)
    {
        switch(event.key.keysym.sym)
        {
            case SDLK_w: yVelocity -= SPEED  ; break;
            case SDLK_s: yVelocity += SPEED  ; break;
        }
    }
    else if(event.type == SDL_KEYUP)
    {
        switch(event.key.keysym.sym)
        {
            case SDLK_w: yVelocity += SPEED ; break;
            case SDLK_s: yVelocity -= SPEED ; break;
        }
    }
}

void LeftPaddle::move(Uint32 deltaTicks)
{
    y += yVelocity * ( deltaTicks / 1000.f );

    if( y  < 0 )
    {
        y =  0;
    }
    else if( y + PADDLE_HEIGHT > SCREEN_HEIGHT)
    {
        y = SCREEN_HEIGHT - PADDLE_HEIGHT;
    }
}

void LeftPaddle::show()
{
    apply_surface( (int)x, (int)y, leftPaddle, screen );
}

class Button
{
    private:
        SDL_Rect box;
        

    public:
        Button(int x,int y,int w, int h);

        void handle_events(bool *win,Uint32 *score1,Uint32 *score2,bool *menuState,bool *gameState,Uint32 *firstTick);
        void handle_events1(bool *gameState,bool *menuState);

        void show();
        void show1();
};

Button::Button(int x, int y, int w, int h)
{
    box.x = x;
    box.y = y;
    box.w = w;
    box.h = h;    
}

void Button::handle_events(bool *win,Uint32 *score1,Uint32 *score2,bool *menuState,bool *gameState,Uint32 *firstTick)
{
    int x = 0 ;
    int y = 0 ;

    while( SDL_PollEvent(&event))
    {
    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 ) )
            {
                //Reset the values of the match (win and scores) and return to the game state
                *win = false;
                *score1 = 0;
                *score2 = 0;
                *menuState = false;
                *gameState = true;
                *firstTick =0;
                
            }
        }
    }
    }
}

void Button::handle_events1(bool *gameState,bool *menuState)
{
    int x = 0 ;
    int y = 0 ;

    while( SDL_PollEvent(&event))
    {
    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 ) )
            {
                //Do after button is pressed
                *gameState = true;
                *menuState = false;                
            }
        }
    }
    }
}

void Button::show()
{
    apply_surface(box.x, box.y, button, screen);
}

void Button::show1()
{
    apply_surface(box.x, box.y, button2, screen);
}

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //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 false;
    }

    //Initialize SDL_ttf
    if(TTF_Init() == -1)
    {
        return false;
    }

    //Initialize SDL_mixer
    if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Git Gud Engine", NULL );

    //If everything initialized fine
    return true;
}

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;
}

bool load_files()
{
    //Load the dot image
    leftPaddle = load_image( "Paddle.bmp" );
    rightPaddle = load_image( "Paddle.bmp" );
    ball = load_image("ball.bmp");
    font = TTF_OpenFont("agentorange.ttf",30);
    collisionSound = Mix_LoadWAV("bounce.wav");
    button = load_image("playAgain.bmp");
    button2 = load_image("play.bmp");
       
    if( leftPaddle == NULL )
    {
        return false;
    }

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

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

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

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

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

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

    //If everything loaded fine
    return true;
}

void clean_up()
{
    //Free the surface
    SDL_FreeSurface( leftPaddle );
    SDL_FreeSurface( rightPaddle );
    SDL_FreeSurface( ball );
    SDL_FreeSurface(score1);
    SDL_FreeSurface(score2);
    Mix_FreeChunk(collisionSound);
    SDL_FreeSurface(winningMessage);
    SDL_FreeSurface(button);
    SDL_FreeSurface(button2);

    TTF_CloseFont(font);

    TTF_Quit();

    //Quit SDL
    SDL_Quit();
}

class Timer
{
    private:
        int startTicks;
        int pausedTicks;
        bool paused;
        bool started;
    public:
        Timer();
        void start();
        void stop();
        void pause();
        void unpause();
        int get_ticks();
        bool is_started();
        bool is_paused();
};

Timer::Timer()
{
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}

void Timer::start()
{
    started = true;
    paused = false;
    startTicks = SDL_GetTicks();
}

void Timer::stop()
{
    if( (started == true) && (paused == false) )
    {
        paused = true;
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    if(paused == true)
    {
        paused = false;
        startTicks = SDL_GetTicks() - pausedTicks;
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the timer was paused
            return pausedTicks;
        }
        else
        {
            //Return the current time minus the start time
            return SDL_GetTicks() - startTicks;
        }
    }

    //If the timer isn't running
    return 0;
}

bool Timer::is_started()
{
    return started;
}

bool Timer::is_paused()
{
    return paused;
}

int main( int argc, char* args[] )
{
    bool quit = false;
    bool win = false;
    bool menuState = true;
    bool gameState = false;
    bool isMatchStarted = false;
    Uint32 firstTick = 0;
    LeftPaddle player1;
    RightPaddle player2;
    Timer delta;    
    Ball ball;
    Button playButton(SCREEN_WIDTH/2-120, SCREEN_HEIGHT/2 + 200, 280 , 56);
    Button playStart(SCREEN_WIDTH/2-65 , SCREEN_HEIGHT/2, 130, 54);
    std::stringstream Score1 ;
    std::stringstream Score2 ;

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

    if(load_files() == false)
    {
        return 1;
    }
    
    delta.start();    

    while(quit == false )
    {

        if(menuState == true)
        {            
                if(win == false)//Play button
                {

                    playStart.show1();    
                    playStart.handle_events1(&gameState,&menuState);                

                }                
                else if(win == true)//Play again button
                {                                
                    playButton.show();                    
                    playButton.handle_events(&win,&ball.Player1Score,&ball.Player2Score,&menuState,&gameState,&firstTick);                    
                }

                if(event.type == SDL_QUIT)
                {
                    quit = true;
                }
        }
            
        if(gameState == true )
        {
            firstTick = SDL_GetTicks();

            while( SDL_PollEvent(&event))
            {                
                player1.handle_left_input(delta.get_ticks());
                player2.handle_right_input(delta.get_ticks());

                if(event.type == SDL_QUIT)
                {
                    quit = true;
                }
            }

            player1.move(delta.get_ticks());
            player2.move(delta.get_ticks());

            if(firstTick >= 3000)
            {
               ball.move(player1.x,player1.y,player2.x,player2.y, delta.get_ticks());
            }            

            delta.start();        

            SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
        
            if(ball.Player1Score == 0)
            {
                score1 = TTF_RenderText_Solid( font, "0", textColor );
            }
            else if( ball.Player1Score == 1)
            {
                SDL_FreeSurface(score1);
                score1 = TTF_RenderText_Solid( font, "1", textColor );
            }
            else if( ball.Player1Score == 2)
            {
                SDL_FreeSurface(score1);
                score1 = TTF_RenderText_Solid( font, "2", textColor );
            }
            else if( ball.Player1Score == 3)
            {
                SDL_FreeSurface(score1);
                score1 = TTF_RenderText_Solid( font, "3", textColor );
            }

            if(ball.Player2Score == 0)
            {
                score2 = TTF_RenderText_Solid( font, "0", textColor );
            }
            else if( ball.Player2Score == 1)
            {
                SDL_FreeSurface(score2);
                score2 = TTF_RenderText_Solid( font, "1", textColor );
            }
            else if( ball.Player2Score == 2)
            {
                SDL_FreeSurface(score2);
                score2 = TTF_RenderText_Solid( font, "2", textColor );
            }
            else if( ball.Player2Score == 3)
            {
                SDL_FreeSurface(score2);
                score2 = TTF_RenderText_Solid( font, "3", textColor );
            }

            if(ball.Player1Score == 3)
            {
                win = true;        
                winningMessage = TTF_RenderText_Solid( font, "Player 1 wins !", textColor );
                
            }
            else if(ball.Player2Score == 3)
            {
                win = true;                    
                winningMessage = TTF_RenderText_Solid( font, "Player 2 wins !", textColor );                
            }

            apply_surface(SCREEN_WIDTH/2 - 100, 10, score1, screen );
            apply_surface(SCREEN_WIDTH/2 + 100, 10, score2, screen );
            

            if( win == false)
            {
                player1.show();
                player2.show();        
                ball.show();
            }            
            else if(win == true)
            {
                apply_surface(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2, winningMessage, screen );
                gameState = false;
                menuState = true;//Return to the menu if a match is won            
            }
        }        

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

    clean_up();

    return 0;
}
 










I bet your problem stems from how not resetting the Timer, so after a game ends, and before the 1st Ball::Move() is called, the deltaTime will be huge (the time the player waited to click play again).

You need to reset the time in the Timer class when you restart the game (you reset firstTick, but you don't reset the Timer).

There are also some simple things you can do to really make the code look better.

First, you don't need separate LeftPaddle and RightPaddle classes. Just have a Paddle Class, and in the constructor, pass in the locations of the paddles, and what keys control them.

Also, there looks to be a bug in the Ball::Move function when you check for collisions (See my comment in the code below):


    else if(x + BALL_WIDTH >= xRightPaddle && y <= yRightPaddle + PADDLE_HEIGHT && y >=yRightPaddle && x < xRightPaddle + PADDLE_WIDTH  )//Right Paddle collision
.
.
.
   // **HERE** Look at how you checked the collision against the left paddle, and how you check it against the right paddle.  HINT, where's BALL_WIDTH below?
    else if( x <= xLeftPaddle + PADDLE_WIDTH && y <= yLeftPaddle + PADDLE_HEIGHT && y >= yLeftPaddle && x > xLeftPaddle )//Left Paddle collision

Lastly, you should't have all the logic in Ball::Move. You should Move it in Ball::Move(), then separately check for collisions against a paddle, and reverse the speed. Also, separately check for the lose condition outside of Ball::Move.

Good luck and have fun.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


You need to reset the time in the Timer class when you restart the game (you reset firstTick, but you don't reset the Timer).

Thanks man for the heads up ! Your suggestion about reseting the timer really solved my problems. Now as far as your tip about collision detection , I'm not sure if I'm getting you right but the ball's width isn't involved in the collision detection between the LEFT paddle and the ball since the ball's offsets(x and y ) are enough to determine if it intersects with the left paddle's boundaries , compared to the collision detection with the RIGHT paddle since the ball's width is necessary to determine the right side of the ball that intersects the right paddle's boundaries. Please excuse me if I'm wrong and share your thoughts about this. Also now that this bug with the timer is fixed should I consider my pong game complete ? After I clean the code and make it more readable(split the source into multiple files etc) of course. I can't thank you enough my friend, I was really desperate that no one would reply since I saw all the views my post got but no one had replied yet.

Untitled.jpg

Your drawing is correct, but, what happens if the bottom-right side of the ball hits the top left of the left paddle? Right now it would pass through it as if it didn't intersect anything.

Obviously, the ball would still be lost either way, but seeing the ball pass through the paddle might look odd. Keeping it as you have it is probably fine for this pong clone though.

Good luck and have fun.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


Your drawing is correct, but, what happens if the bottom-right side of the ball hits the top left of the left paddle? Right now it would pass through it as if it didn't intersect anything.

Fair enough, I will look into it.

Good luck and have fun too !

This topic is closed to new replies.

Advertisement