• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AspiringDev

Help with Pong clone

5 posts in this topic

  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.

Edited by AspiringDev
0

Share this post


Link to post
Share on other sites

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










Edited by AspiringDev
0

Share this post


Link to post
Share on other sites

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.

Edited by BeerNutts
2

Share this post


Link to post
Share on other sites


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

Edited by AspiringDev
0

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites


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 !

0

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  
Followers 0