Thread: Help With using classes for games.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    29

    Help With using classes for games.

    This is my first attempt at using classes for a program, it is sort of like a "learn how to use class's in c++ test" program for me. Of all the tutorials I have read about them, I couldn't find anything useful to use them with except maybe games (which I don't know how program).

    Could anyone share with me some tips about what to change about implementation of them and how I can use them better in the future? (note: this program was only a test of how to use classes not meant to be that great or anything). Thanks in advance for your help.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    char map[50][30] = { 0 };
    
    char message[30];
    bool done = false;
    
    class Charactor
    {
    public:
        Charactor();
        ~Charactor();
        
        void moveup();
        void movedown();
        void moveleft();
        void moveright();
        
        int xpos;
        int ypos;
        
        int height;
        int width;
        
        int health;
        int speed;
    
    } player;
    
    Charactor::Charactor()
    {
    xpos = 5;
    ypos = 15;
    
    height = 1;
    width  = 2;
    
    health = 100;
    speed = 1;
    }
    
    Charactor::~Charactor()
    {
    }
    
    void Charactor::moveup()
    {
    ypos -= speed;
    }
    void Charactor::movedown()
    {
    ypos += speed;
    }
    void Charactor::moveleft()
    {
    xpos -= speed;
    }
    void Charactor::moveright()
    {
    xpos += speed;
    }
    
    class Barrier
    {
    public:
        Barrier();
        ~Barrier();
        
        int x1,x2,y1,y2;
    } room;
    
    Barrier::Barrier()
    {
    x1 = 0;
    x2 = 50;
    y1 = 0;
    y2 = 20;
    }
    
    Barrier::~Barrier()
    {
    }
    
    void checkbarrier()
    {
    if (player.ypos + player.height == room.y2)
        {
        strcpy(message, "You cannot go through walls!");
        player.ypos -= 1;
        }
    else if (player.ypos - player.height == room.y1)
        {
        strcpy(message, "You cannot go through walls!");
        player.ypos += 1;
        }
    else if (player.xpos + player.width == room.x2)
        {
        strcpy(message, "You cannot go through walls!");
        player.xpos -= 1;
        }
    else if (player.xpos - player.width == room.x1)
        {
        strcpy(message, "You cannot go through walls!");
        player.xpos += 1;
        }
    else
        strcpy(message, "You are doing fine!         ");
    }
    
    void drawmap()
    {
    for (int x = 1; x < 49; x++)
        for (int y = 1; y < 19; y++)
            {
            map[x][y] = ' ';
            }
            
    for (int x = 0; x < 50; x++)
        {
        map[x][0] = '-';
        map[x][49] = '-';
        }
    for (int y = 0; y < 20; y++)
        {
        map[0][y] = '|';
        map[49][y] = '|';
        }
    map[player.xpos][player.ypos] = '|';
    map[player.xpos][player.ypos - 1] = 'O';
    map[player.xpos + 1][player.ypos] = '-';
    map[player.xpos - 1][player.ypos] = '-';
    map[player.xpos + 1][player.ypos + 1] = '\\';
    map[player.xpos - 1][player.ypos + 1] = '/';
    }
     
    void displaymap()
    {
    
    for (int y = 0; y < 20; y++)
        {
        for (int x = 0; x < 50; x++)
            {
            cout << map[x][y];
            }
            cout <<endl;
        }     
    }
    
    void drawscreen()
    {
    char command;
    
    cout <<"--------------------------------------------------"<<endl;
    cout <<"|Commands: W - Up, S - Down, A - Left, D - Right  |"<<endl;    
    cout <<"|           Q - Quit  Press Enter To confirm.     |"<<endl;
    cout <<"|Health:"<<player.health<<"  Message: "<< message <<"|"<<endl;
    cout <<"---------------------------------------------------"<<endl;
    displaymap();
    cout <<"Command: ";
    cin >> command;
    
    if (command == 'w')
        player.moveup();
    else if (command == 's')
        player.movedown();
    else if (command == 'a')
        player.moveleft();
    else if (command == 'd')
        player.moveright();
    else if (command == 'q')
       done = true;
    
    system("cls");
    }
    
    int main()
    {
    while (!done)
        {
        drawmap();
        drawscreen();
        checkbarrier();
        }
    return 0;
    }
    Last edited by rainmanddw; 10-31-2003 at 12:02 AM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Hi, I usually find that writing small test programs is very good for learning. Here are my suggestions / tips.

    Character class :

    Hide your data! Make your position, height, width, speed etc private. Then have accessor functions ( Set / Get ) to manipulate them. There is no real problem with the way you did it except something might get changed accidently. Also, think about adding an overloaded constructor to take a position to set your character to initially instead of hard coding the default constructor with a position. A fast way to initialize variables in constructors is as follows:

    Code:
    Foo::Foo( ) : m_myVar( 0 ), m_myVar2( 10 )
    {
    }
    That would intialize a variable called m_myVar in the class Foo. Again, just a suggestion.

    Why not make the collision check for barrier a method of barrier? It could take a character class as a parameter.

    Also you can make a game class to hold all of your objects if you want to keep them organized better. That way you don't have so many global variables floating around.

    Hope this helps you a little. Feel free to ask any more questions you want.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68

    Talking My source

    hey...... this is the code for my game that i have to finish inside a week i am really disorganised......

    It has inheritance, a map class, blah blah blah, but is definately not done yet.

    I am pretty sure it compiles, but hey...

    calcmain.cpp isnt part of the game, but i was just working on it and thought you might like to see it. I spent a bit commenting it, so should *hopefully* be a bit easier to understand.

    btw, dont take this code/call it your own etc..... weve all skipped reading enough EULA's to know the standar legalese

    And so, without further ado.....
    Last edited by littleweseth; 11-01-2003 at 12:09 AM.
    Ph33r the sphericalCUBE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM