Finished Pong Game Using SDL

First ever C++ game, please have a look...

Page 1 of 1

7 Replies - 5897 Views - Last Post: 08 April 2009 - 05:49 PM Rate Topic: -----

#1 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Finished Pong Game Using SDL

Posted 20 March 2009 - 09:57 PM

Alas, I have completed the pong game I have been working on. If you remember my old thread, I used it to learn to fix the problems I had upon making the game. I have finished it now and am quite satisfied with the final product. After all, this is my first game in C++. For any people that are interested the old forum topic can be found HERE.

I have attached the .zip file with the code, the images, the .dll files and the executable if anyone is interested in giving the game a run-through. To learn how to play, read the readme file. As I said, this is my first game and by no means do I consider myself anything but an amateur. However, feel free to use the code as a learning tool for SDL. If you have any questions about the code for this game I will gladly answer them to my ability.

I learned to use SDL from Lazy Foo's Tutorials. Keep in mind that in order to compile the source code you need to have the SDL libraries set up. There are instructions to do this on Lazy Foo's website. Otherwise, if you extract the files to another folder, you should be able to play by running the PongGame.exe file.

Much thanks to Hyper for all of the help, without which I would probably still be swearing at my computer.

This game will sometimes end randomly on its own after playing for a while... I'm not sure how to fix. If anyone can help with this it would be very very much appreciated. Otherwise, I am content with my first C++ game.

Let me know of any questions. Also, I would very much appreciate any comments/suggestions/etc. about the game. Thanks.

P.S. If you have any trouble opening the .zip attachment and would still like to see the game, I will gladly email it to you. I can also post the code on this forum if anyone would like but it is pretty long...

Attached File(s)


This post has been edited by blackzer0: 21 March 2009 - 01:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Finished Pong Game Using SDL

#2 Robotics Guy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-March 09

Re: Finished Pong Game Using SDL

Posted 21 March 2009 - 08:58 AM

Congrats!!
Was This Post Helpful? 0
  • +
  • -

#3 Robb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 58
  • Joined: 16-October 06

Re: Finished Pong Game Using SDL

Posted 21 March 2009 - 04:38 PM

Very nice blackzero0, congrats :)

I tried to play against myself to no avail :P An enjoyable game, think I might play it tomorrow too :)

Next task, computer AI :^:

Happy coding :)
Was This Post Helpful? 0
  • +
  • -

#4 bobjob  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 163
  • Joined: 29-March 08

Re: Finished Pong Game Using SDL

Posted 21 March 2009 - 09:04 PM

blackzer0, its very easy to make AI, for a Pong game

all you need to do is make the other player move to the position of the ball

so if ball y position > than paddle y position, paddle y position +1;
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Finished Pong Game Using SDL

Posted 22 March 2009 - 12:53 AM

View Postblackzer0, on 20 Mar, 2009 - 11:57 PM, said:

Alas, I have completed the pong game I have been working on. If you remember my old thread, I used it to learn to fix the problems I had upon making the game.

...

Much thanks to Hyper for all of the help, without which I would probably still be swearing at my computer.


Congratulations on the super cool Pong game! .Yes, I'm being sincere. ;) I remember that thread!
That made me smile, and you're quite welcome. :P

Not to be rude or anything but, you over-commented your code. You put comments on things that shoudn't be!
For example: NumOne += NumTwo; // NumOne's sum is added with NumTwo and stored in NumOne is a bad comment!
Other than those comments, I think the indentation's pretty fine (you kept it standardized).

Like here for example:
void Timer::start()
{
    //Start the timer
    started = true;
    
    //Unpause the timer
    paused = false;
    
    //Get the current clock time
    startTicks = SDL_GetTicks();
}


You could perhaps make it more like this:
// Starts a timer
void Timer::start() {

    started = true;
    paused = false;
    startTicks = SDL_GetTicks();
}


That, would be an improvement. I'm sure you only commented on it that much 'cause you were going to post it and didn't want it to be all cryptic to people, or perhaps you really DO comment it that much and if that's the case, you honestly shouldn't comment the code that much. Group comments above the function or on a "complex" function.

Again, very very cool Pong game. Although I think the controls are confusing. A/Z and UP/DOWN for one person? :P Caught me off guard!

About computer AI, it'd be very simple! To do. Don't be fooled by thinking that "AI" (Artificial Intelligence) is some superficial "neural-network" learning human-like "thing..." AI comes in many different forms! Such as path-finding AI, avoiding objects AI, and rule-based AI (and probably other catagorizations or grouping methods).

For a simple game like Pong, a "rule-based" AI system would work great! The pseudo code would go something like:
    If Ball is above (Computer's) Paddle
        Move Paddle up
    else if Ball is below (Computer's) Paddle
        Move Paddle down


A non-psuedo coded version would be something more like this (this isn't based on your Pong):
        void AI() {

            /* Move paddle down */
            if (Paddle.GetYPaddle() < Ball.GetYBall() && Paddle.GetYPaddle() < BOTTOM_WALL) {
                Paddle.SetYPaddle(Paddle.GetYPaddle() + 1);
            }

            /* Move paddle up */
            if (Paddle.GetYPaddle() > Ball.GetYBall() && Paddle.GetYPaddle() > TOP_WALL) {
                Paddle.SetYPaddle(Paddle.GetYPaddle() - 1);
            }

            return;
        }


Something similar to that would be very effective. You could make it seem more realistic (like the computer wasn't "Cheating") if the computer only responds (moves) AFTER the Ball is in the half-way point... or if the ball's a certain speed move regardless of where the ball's at (via center).

Cool program dude, keep up the good work! :P

EDIT: This is in no way an attempt to steal your thunder nor hijak your thread, but!
Here's a link to a tutorial I wrote earlier tonight: Pong in C++
Tell me what you think. :)

This post has been edited by Hyper: 22 March 2009 - 03:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Re: Finished Pong Game Using SDL

Posted 22 March 2009 - 01:26 PM

Thank you all for the great comments and advice. My next step will be to definately include some computer AI. As a new programmer this concept was quite intimidating. But now, after looking at all the advice and tips from all of you, it seems much simpler than I had initially anticipated. When I finish implementing the AI into the game I will post a new .zip file for all of you guys that are still interested. Thanks again for all of the positive feedback. It definately helps one become a better programmer and enjoy thier work more. :D :D


@ Hyper:
I agree with you that I went a little overboard with the comments. (OK, maybe a lot overboard :P )
I guess I figured that more comments were better than less. Looking back now though I realize that quite a few of them are unnecessary. When I post the next version, I will make sure to rid my code of many of them. Also, I will gladly look upon your new tutorial. I promise to use all of my limited knowledge to evaluate it and let you know what I think. Thanks again for all of your help. :D
Was This Post Helpful? 0
  • +
  • -

#7 mmakrzem  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 56
  • Joined: 11-January 08

Re: Finished Pong Game Using SDL

Posted 08 April 2009 - 08:07 AM

I just tried downloading the zip file twice. I can't open the zip because the file is corrupted. :(
Was This Post Helpful? 0
  • +
  • -

#8 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Re: Finished Pong Game Using SDL

Posted 08 April 2009 - 05:49 PM

View Postmmakrzem, on 8 Apr, 2009 - 07:07 AM, said:

I just tried downloading the zip file twice. I can't open the zip because the file is corrupted. :(



I have attached another .zip file to this post. It may or may not work. If not, I have it in a .rar format as well, and I will happily email it to you! Let me know if it works... :^:

Attached File(s)


This post has been edited by blackzer0: 08 April 2009 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1