Sign in to follow this  
bknouse1344

Pong/tictactoe source?

Recommended Posts

Im starting to write games with graphics (pong,tic tac toe...) and I was wondering if there is any place where I could find the source to these games. Like good code...someone who knows what they're doing so I can read and learn. I'm tired of following outta the books, and I can't find this code on google. Any ideas?

Share this post


Link to post
Share on other sites
Well thats the thing...I'm new to programming and would rather see a professional code first because I would rather spend the next few days knowing what I'm trying to accomplish through each function class or whatnot.

Share this post


Link to post
Share on other sites
No, you need to learn yourself. Copying out of books or from the internet will do you no good. Sure, you might have good code, but who cares, it's not yours. Surely you've heard the expression "you only learn by your mistakes." Well, this applies to programming.

Share this post


Link to post
Share on other sites
I said

"Well thats the thing...I'm new to programming and would rather see a professional code first because I would rather spend the next few days knowing what I'm trying to accomplish through each function class or whatnot."

I did not say I would copy it out of the source, I was just looking for something to understand...quite obviously nothing was taken out of copying from a web browser and pasting it to my compiler. If I wanted to show off to friends I would be asking for the code to Doom 3.

...I'm guessing that is out of the question??

Share this post


Link to post
Share on other sites
I know you said you didn't like to get it from books but I have been finding Beginning C++ Game Programming very helpful. I have combined the Tic-Tac-Toe program I learned there with Allegro to make a graphical version of it. I am now trying to add the ability to play against my roomate over the LAN. The book is very well written and only about $30

Share this post


Link to post
Share on other sites
Alright...my friend was actually offering up to lemme read that so ill take a look. My main problem with books were, in the past, they gave me a load of information and keywords and stuff, but failed to bring it all together to sortof explain to you how they make the game.

Share this post


Link to post
Share on other sites
Alright, I'll help you get started.

First off, choose an API for graphics. Popular choices are SDL and ClanLib. Both are cross-platform. Technically, ClanLib is the superior choice, as it relies on OpenGL whereas SDL uses DirectDraw. However, ClanLib is more targeted towards experienced programmers, as it makes heavy use of OOP design patterns and the like. So make your choice wisely.

Next, learn to use that API by reading the documenation on their respective website.

Here's the classes you will need for a simple Pong clone:

Ball - This should be obvious, it's represents the ball. Members would probably include:
Position - where the ball is in screen coordinates. This would best be stored as a vector, but you could easily get away with a pair of floats, one for each coordinate.
Velocity - how fast the ball is moving and the direction it's moving. This too would best be stored as a vector, but again a pair of floats would do.
Image - the image representing the ball.

The member functions should be something like these:
bool Update(float TimeStep) - simply updates the balls position based on velocity and timestep. Timestep is simply the number of milliseconds a single frame lasts. By setting position equal to velocity times timestep, you are effectively integrating the balls position with respect to time. This gives smooth, stable motion. Should return a boolean indicating success or failure.
bool Render - draws the balls image to the screen at the balls current position. This would use your chosen graphics API to get the job done. Should return a boolean, just like Update(), indicating success or failure.

The constructor and destructor should handle loading the image data and deleting image data, respectively.

Paddle - this is the base class for all paddles. You will derive PlayerPaddle and CpuPaddle from this class. If you don't understand what I'm talking about, then maybe first look at this article and maybe this one. This class should contain exactly the same things as Ball, except the destructor and Update() method should be virtual, so that derived classes can implement them.

PlayerPaddle - the player's paddle. Derived from Paddle. Loads appropriate image data for the player. Also overrides the Update() method of Paddle to include input handling. This would require use of your chosen graphics API (as they also include support for input and sound).

CpuPaddle - the computer controlled paddle. Derived from Paddle. Very similar to PlayerPaddle. However, this class should have a pointer to the Ball, for AI purposes. The Update() method of Paddle would of course have to overridden to do AI.

GameController - controls the game. This could be a singleton, or all methods and members could be declared as static. Either way is fine. Should contain instances of the Ball, PlayerPaddle, and CpuPaddle classes. Should have methods Update() and Render(). Update() would call the respective Update() function for the ball, and the paddles. Render() would do the same, except with the Render() functions. Update() would also be responsible for collision detection and response. This class would also keep track of score, game states, etc.

You would need to make a loop in the main() function, each iteration would call the Update() and Render() methods of GameController.

Alright, that is the rough framework for a very simple Pong type game. I leave the implementation totally up to you. If you have any trouble along the way (and you most likely will) come back and I will gladly help you more.

Just so you know, there are many better and more elaborate ways of doing these things. I just showed you the simplest most straightforward method. After writing your game, I suggest you rewrite it and try and make it as efficient as possible, i.e. get rid of redundant code, that sort of thing. It can be quite the challenge for a beginner, and is pretty fun to mess around with.

EDIT: BTW, you actually can get approximately half the code for Doom 3 by way of the Doom 3 SDK. Just in case you didn't know.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Aside from SDL and ClanLib there is the ever popular Allegro. Check out my tutorial site which has both Tic-tac-toe and Pong sources.

http://agdn.pyrosoftware.net/main/index.php

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