Thread: Help With using classes for games.

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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