Hi guys,

I'm quite new to C++, I have just recently gone through 3/4 of SAMS teach yourself C++ in 21 days and I am hoping to start programming my first object-oriented game.

I have created a few games in the past (you can check them out on my
site www.urix.us), all however procedural; and I would like to focus
more on the OO side of programming from now on.

What (or who) I need is a guide though, because I understand everything
about OO programming, I just dont really understand yet how it can be implemented in a game concept. If anyone has an example of their own game it would be very helpful for me if you could send it over so I can check out the source code and get into the game programmers mind.

Please help, thanks in advance :cheesy:

Recommended Answers

All 11 Replies

I would strongly recommend you to try to simulate the programs you have written during your C days in C++. That would help you in gaining an OO perspective and make it to approach every problem in the OO way. Jumping head on in complex projects would only cause total chaos.

But still if you must have it, try these sites:

http://dmoz.org/Computers/Open_Source/Software/Games/
http://osswin.sourceforge.net/games.html

Best of luck.

I just dont really understand yet how it can be implemented in a game concept.

I'm gonna play the part of a naysayer if ya don't mind. ;) You say you understand how OO works and all the concepts and such, right? If that's really how it is then not seeing a good fit right away means that OO might not be a good idea. For you, I think you're trying to apply organizational patterns with OO to a project that doesn't need it.

There's more to objects than just the full blown OO that you've prolly been learning. An object-based method is more common than an object-oriented method, if that makes any sense at all. :confused: When you write object-based stuff, it means that you're using objects for the data hiding and encapsulation benefits, but you don't really need inheritance and polymorphism for the organization benefits that only large projects really need.

When you write object-oriented stuff, the main win comes from being able to use inheritance hierarchies to organize the concepts in a big project. That's a full OO approach, and you don't really need it unless your project is too big to get a big picture view without getting a headache. :D

My (psychotic as my colleagues like to say) guess is that you're trying to go object-oriented and it's confusing you because you don't need that level of abstraction. If you drop down a level to object-based design, you'll prolly have more luck breaking your game concepts up into objects. :)

I'm gonna play the part of a naysayer if ya don't mind. ;) You say you understand how OO works and all the concepts and such, right? If that's really how it is then not seeing a good fit right away means that OO might not be a good idea. For you, I think you're trying to apply organizational patterns with OO to a project that doesn't need it.

There's more to objects than just the full blown OO that you've prolly been learning. An object-based method is more common than an object-oriented method, if that makes any sense at all. :confused: When you write object-based stuff, it means that you're using objects for the data hiding and encapsulation benefits, but you don't really need inheritance and polymorphism for the organization benefits that only large projects really need.

When you write object-oriented stuff, the main win comes from being able to use inheritance hierarchies to organize the concepts in a big project. That's a full OO approach, and you don't really need it unless your project is too big to get a big picture view without getting a headache. :D

My (psychotic as my colleagues like to say) guess is that you're trying to go object-oriented and it's confusing you because you don't need that level of abstraction. If you drop down a level to object-based design, you'll prolly have more luck breaking your game concepts up into objects. :)

Thats the very thing i was trying to convey to him, but looks like he has made off with the links i provided in my previous post and is busy studying them. Well maybe he would learn better the hard way ;)

What (or who) I need is a guide though.

If anyone has an example of their own game it would be very helpful for me if you could send it over so I can check out the source code and get into the game programmers mind.
Please help, thanks in advance :cheesy:

I dont know how good the book is...I'm sure you can read reviews on Amazon.com but maybe this is a good start?

Beginning Game Programming: $23.09
Product Details
Paperback: 624 pages
Publisher: Sams; Bk&CD-Rom edition (July 19, 2004)
Language: English
ISBN: 0672326590

Book description from publisher's homepage:

If you are hooked on video games and have a basic knowledge of C++ and visual programming, you will be hooked on Beginning Game Programming. Clear, practical lessons based on C++ programming are the basis of this book's lessons. By focusing on the Windows API to construct games, you will learn game theory in double-buffered graphics, sprite animation, digitized sound effects and music. A fully functional game engine provided on CD, along with tools, code and graphics, will give you the ability to create your own games in the future. Learn the art and science of game programming with help from Beginning Game Programming.

Here is another informational link:
http://www.toymaker.info/Games/html/beginners.html

Ye, sorry I'm not able to answer right away, I'm in Cyprus see n still got a bit of a crappy connection so I'm not always online. I'll def be here and answer everyday, probably even twice!! hehe :cheesy:

But up to now, I think I get the picture of what you guys are trying to tell me. I have made a 3D tic-tac-toe chess game a while back for my Final Year Project at uni, using C++ and OpenGL; but as I mentioned before, it was procedural and becuase of that lowered my grade to 80% :sad:

What this game is, just to get u into it, is a 4x4 playing board, each player having 4 pawns: Horse, Bishop, Castle and Queen. Each can move on the board using their corresponding chess moves, and the object of the game is to align them all together in a row either vertically, horizontally or diagonaly. Tic-Tac-Toe chess geddit?!..:cool:

I am only going to focus on the C++ programming part, i don't care about the graphics right now. I am sure this game can be done using classes and basically this is what I need help with.

After carefully consideration i have come up with the following classes:
1. Player (player1 and player2 who will be able to move the pawns around the board and change color-eventually in the graphics version)
2. Pawn (the Horse, Bishop, Castle and Queen pawns used in the game - will be derived from class Pawn)
3. Board (the 4x4 playing board that will consist of a 2-dimensional array of type int - int playingBoard[4][4])
4. Location (I'm not sure of this one, but this is the location or block that the pawn will move to on the board?...)
...and hopefully a logfile with statistics of the game but i'll leave that for when i finish with this.

You see, I'm sort of on the right track, I'm just not sure. Any comments how I can clear my mind?

oh by the way, thanks for the links ~s.o.s~ but a bit too advanced just for now, I'll have to check them out a bit later on. ToySoldier, the link was great! nice n helpful stuff there.
And Inanna, i totally get what you're trying to tell me. I understand I dont need all the levels of abstraction of a huge game, its just nice to classify and seperate objects into classes; I just dont know how to present them and how they will interact with each other :(

3. Board (the 4x4 playing board that will consist of a 2-dimensional array of type int - int playingBoard[4][4])
4. Location (I'm not sure of this one, but this is the location or block that the pawn will move to on the board?...)

Why not make "Square" as a class since your board willl have many squares.

Each square can have:

1. Type of piece.
2. The player whose piece it is.
3. Position value (x, y)

Well this is one way of looking at things, you can have your own go at it.

Cool, i'll c wot i can do and i'll get back to you guys hopefully in the next few days with some source code to check out.. Thanks for ur help so far!!!

ok guys, here's my code upto now. It's probably not much because it hasn't got much functionality in it. I would just like your opinions and help as to how to get on with it. So please, if it's good, say it's good. If it's crap, say it is with as much help as possible... Thankssss :-)

#include <iostream>

using namespace std;

enum PieceType { NoPiece, Queen, Bishop, Horse, Castle };
enum PlayerNum { NoPlayer, Player1, Player2 };

// Square is each individual block the playing board will consist of. Each block 
// is considered to have 3 member data: type of piece that occupies that block, 
// the player whose piece that is, and the position it holds.
class Square
{
    public:
        Square(){}
        ~Square(){}
        PieceType GetItsPieceType() const    { return itsPieceType; }
        void SetItsPieceType(PieceType type) { itsPieceType = type; }
        PlayerNum GetItsPlayerNum() const    { return itsPlayerNum; }
        void SetItsPlayerNum(PlayerNum num)  { itsPlayerNum = num;  }
        int GetItsX() const { return itsX; }
        void SetItsX(int x) { itsX = x;    }
        int GetItsY() const { return itsY; }
        void SetItsY(int y) { itsX = y;    }
    private:
        PieceType itsPieceType;
        PlayerNum itsPlayerNum;
        int itsX;
        int itsY;
};

// PlayingBoard is the board of the game which consists of a 16 block, 4x4 square
// playing field of class Square
class PlayingBoard
{
    public:
        PlayingBoard(); // initialize the blcok values in the board constructor
        ~PlayingBoard(){}
        void Draw(); // draws the playing board
    private:
        Square itsBoard[4][4];
};

PlayingBoard::PlayingBoard()
{
    // initialize square's x and y positions
    for (int i=0; i<4; i++)
    {
        for (int j=0; j<4; j++)
        {
            itsBoard[i][j].SetItsX(i);
            itsBoard[i][j].SetItsY(j);
        }
    }

    // initialize pawn positions for player 1
    itsBoard[0][0].SetItsPieceType(Queen);
    itsBoard[0][0].SetItsPlayerNum(Player1);
    itsBoard[2][0].SetItsPieceType(Bishop);
    itsBoard[2][0].SetItsPlayerNum(Player1);
    itsBoard[1][3].SetItsPieceType(Horse);
    itsBoard[1][3].SetItsPlayerNum(Player1);
    itsBoard[3][3].SetItsPieceType(Castle);
    itsBoard[3][3].SetItsPlayerNum(Player1);

    // initialize pawn positions for player 2
    itsBoard[0][3].SetItsPieceType(Queen);
    itsBoard[0][3].SetItsPlayerNum(Player2);
    itsBoard[2][3].SetItsPieceType(Bishop);
    itsBoard[2][3].SetItsPlayerNum(Player2);
    itsBoard[1][0].SetItsPieceType(Horse);
    itsBoard[1][0].SetItsPlayerNum(Player2);
    itsBoard[3][0].SetItsPieceType(Castle);
    itsBoard[3][0].SetItsPlayerNum(Player2);

    // initialize other pawn positions to NoPlayer (empty)
    itsBoard[0][1].SetItsPieceType(NoPiece);
    itsBoard[0][1].SetItsPlayerNum(NoPlayer);
    itsBoard[1][1].SetItsPieceType(NoPiece);
    itsBoard[1][1].SetItsPlayerNum(NoPlayer);
    itsBoard[2][1].SetItsPieceType(NoPiece);
    itsBoard[2][1].SetItsPlayerNum(NoPlayer);
    itsBoard[3][1].SetItsPieceType(NoPiece);
    itsBoard[3][1].SetItsPlayerNum(NoPlayer);
    itsBoard[0][2].SetItsPieceType(NoPiece);
    itsBoard[0][2].SetItsPlayerNum(NoPlayer);
    itsBoard[1][2].SetItsPieceType(NoPiece);
    itsBoard[1][2].SetItsPlayerNum(NoPlayer);
    itsBoard[2][2].SetItsPieceType(NoPiece);
    itsBoard[2][2].SetItsPlayerNum(NoPlayer);
    itsBoard[3][2].SetItsPieceType(NoPiece);
    itsBoard[3][2].SetItsPlayerNum(NoPlayer);
}

void PlayingBoard::Draw()
{
    cout << endl;
    cout << "    0   1   2   3 \n";
    cout << "  ----------------\n";
    for (int i=0; i<4; i++)
    {
        cout << i << "| ";
        for (int j=0; j<4; j++)
        {
            switch (itsBoard[i][j].GetItsPlayerNum())
            {
                case Player1 :  cout << "p1"; break;
                case Player2 :  cout << "p2"; break;
                case NoPlayer : cout << "__"; break; // no player in square
            }

            switch (itsBoard[i][j].GetItsPieceType())
            {
                case Queen:    cout << "Q "; break;
                case Bishop:   cout << "B "; break;
                case Horse:    cout << "H "; break;
                case Castle:   cout << "C "; break;
                case NoPiece : cout << "_ "; break; // no piece in square
            }            
        }    
        cout << endl;
    }
    cout << endl;
}

// Pawn is an abstract class that will be used to create specific pawns such as 
// Queen, Bishop, Horse and Castle.
class Pawn
{
    public:
        Pawn(){}
        ~Pawn(){}
        int GetItsCurrentX() const { return itsCurrentX; }
        void SetItsCurrentX(int x) { itsCurrentX = x; }
        int GetItsCurrentY() const { return itsCurrentY; }
        void SetItsCurrentY(int y) { itsCurrentY = y; }
        // Move() is a function that will enable the user to move a certain pawn 
        // to a specific location on the board. All tests to whether it is a valid
        // move will be held in the driver program. Therefore, it can be taken for
        // granted that Move() will always be valid and will be used without any
        // further checks.
/*
   what will move() have as parameters? a square location? the whole board?
   a new X location, a new Y location, player who is moving? pawn type that
   is to be moved? 
*/
    private:
        int itsCurrentX;
        int itsCurrentY;
};

// Queen is a class derived from the Pawn class. It inherits all its data and 
// functions and also includes its own if needed.
class QueenPawn : public Pawn
{
    public:
        QueenPawn(){}
        ~QueenPawn(){}
};

// Bishop is a class derived from the Pawn class. It inherits all its data and 
// functions and also includes its own if needed.
class BishopPawn : public Pawn
{
    public:
        BishopPawn(){}
        ~BishopPawn(){}
};

// Horse is a class derived from the Pawn class. It inherits all its data and 
// functions and also includes its own if needed.
class HorsePawn : public Pawn
{
    public:
        HorsePawn(){}
        ~HorsePawn(){}
};

// Castle is a class derived from the Pawn class. It inherits all its data and 
// functions and also includes its own if needed.
class CastlePawn : public Pawn
{
    public:
        CastlePawn(){}
        ~CastlePawn(){}
};

// Player is a class which represents the 2 players in the game. Each player has 
// one of each available pawn. This is represented by a has-a relationship, 
// including an object instantiation for each pawn in the player class.
class Player
{
    public:
        Player(){}
        ~Player(){}
        // the user must be able to move a pawn. This is, basically, all he/she
        // can do in the game. This Move() function should trigger the pawn Move()
        // in the Pawn class. The player will have a seperate move function for
        // all pawns (MoveQueen, MoveBishop, etc) so that the specific value for
        // each pawn can be sent up to the Move() function in Pawn class as a
        // parameter.
        // ie MoveQueen() { Pawn::Move(..., PawnType Queen, int newX, int newY); }
    private:
        QueenPawn  Queen;
        BishopPawn Bishop;
        HorsePawn  Horse;
        CastlePawn Castle;
};

// main program
int main()
{
    PlayingBoard GameBoard; // initialize the playing board
    Player Player1; // initialize player 1
    Player Player2; // initialize player 2
    GameBoard.Draw(); // draw the current playing board, expected result:
    /////////////////////////
    //     0   1   2   3   //
    //   ----------------  //
    // 0| p1Q ___ ___ p2Q  //
    // 1| p2H ___ ___ p1H  //
    // 2| p1B ___ ___ p2B  //
    // 3| p2C ___ ___ p1C  //
    //                     //
    /////////////////////////
    return 0;
}

hi

hru all i am new to c , c++ programing and i am intrested in programing plz help me???????????

This tutorial shows how to create an object oriented game in C++ using SFML. It covers C++ in a fair bit of detail and acts as a bit of a primer, but most importantly it shows how to model a game in an object oriented style. It's not finished yet and is already up to 7 parts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.