Author Topic: My little game engine.  (Read 1220 times)

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
My little game engine.
« on: September 07, 2005, 12:31:10 am »
Well... I was fooling around with C++, and I decided to make a game that was like the ones they had on the old hand-held systems... You know the ones... They had games like tetris, space invaders, and some driving games... Well, anyways.

I was just practicing, and I made this:
http://upl.silentwhisper.net/uplfolders/upload5/CollisionTest.zip

It\'s just a small \"game\" in which you move around the \'x\' and colide with the \'o\' characters.

Later on I decided to make an engine so that I could make the upcoming games a tad faster.

But while making it I ran into a problem... Should I keep using one class as I am now, or whould I break it up into many classes (controls, board, counters, etc.)? Which would be more C++ish in this situation?

Here is the code:

P.S. What else do you think I should add?

P.S.S. Didn\'t add too many comments, so if you have any questions just ask.


Code: [Select]

//Game Engine


#include
#include
using namespace std;




class Game {
    char board[200][200];
    int boardLength;
    int boardHight;

    int xPosition;
    int yPosition;
    int xSpawn;
    int ySpawn;
    int xMax;
    int yMax;
    int xMin;
    int yMin;

    int moveNum;

    char moveL;
    char moveR;
    char moveU;
    char moveD;
    char aux1;
    char aux2;
    char aux3;
    char aux4;

    char playerSymbol;

public:

    //Get private values
    char get_moveL(){ return moveL;}
    char get_moveR(){ return moveR;}
    char get_moveU(){ return moveU;}
    char get_moveD(){ return moveD;}
    char get_aux1(){ return aux1;}
    char get_aux2(){ return aux2;}
    char get_aux3(){ return aux3;}
    char get_aux4(){ return aux4;}
    int get_xPosition(){ return xPosition;}
    int get_yPosition(){ return yPosition;}
    int get_moveNum(){ return moveNum;}
    char get_playerSymbol(){ return playerSymbol;}


    //Set Controls;
    void set_moveL(char x){moveL = x;}
    void set_moveR(char x){moveR = x;}
    void set_moveU(char x){moveU = x;}
    void set_moveD(char x){moveD = x;}
    void set_aux1(char x){aux1 = x;}
    void set_aux2(char x){aux2 = x;}
    void set_aux3(char x){aux3 = x;}
    void set_aux4(char x){aux4 = x;}


    //Set board parameters
    void set_boardSize(int x=0, int y=0){
        boardLength = x;
        boardHight = y;
    }


    //Set how far palyer can move
    void set_boundries(int xl=boardLength, int yl=boardHight,
                        int xs=0, int ys=0){
        xMax = xl;
        yMax = yl;
        xMin = xs;
        yMin = ys;
    }



    //Set player spawn position
    void set_spawn(int x=0, int y=0){
        xSpawn = x;
        ySpawn = y;
    }


    //Set player symbol
    void set_playerSymbol(char p){playerSymbol = p}
    void set_playerSymbol(int p){playerSymbol = p}


    //Set number of moves.
    void set_moveNum(int num){moveNum = num;}


    /*Spawns barriers.
    void spawnBarriers(){
        int x;
        int y;

        for(int i=0; i<10; i++){
            x = (rand()%9);
            y = (rand()%9);
            board[y][x] = \'o\';
        }
    }
    */

    //Create a \"clean\" board.
    void makeBoard(){
        for(int i=0; i            for(int x=0; x                board[i][x] = \' \';
            }
        }
    }


    //Shows the board.
    void showBoard(){
        for(int i=0; i
        for(int i=boardHight; i>(-1); i--){
            cout << \"\\n|\";
            for(int x=0; x            cout << board[i][x];
            }
            cout << \"|\";
        }

        cout << \"\\n\";

        for(int i=0; i    }


    //Resets Position.
    void resetPosition(){
        xPosition = xSpawn;
        yPosition = ySpawn;
    }


    //Fills a sqare;
    void fillPosition(){
        board[yPosition][xPosition] = playerSymbol;
    }


    /*Checks for barriers
    bool detectBarrier(char d){
        if(d==moveL){
            if(board[yPosition][xPosition-1]==\'o\') return true;
        }
        else if(d==moveR){
            if(board[yPosition][xPosition+1]==\'o\') return true;
        }
        else if(d==moveU){
            if(board[yPosition+1][xPosition]==\'o\') return true;
        }
        else if(d==moveD){
            if(board[yPosition-1][xPosition]==\'o\') return true;
        }
        else return false;
    }
    */


    //Moves a character in a direction.
    void movePosition(char direction){
        if(direction == moveL){
            if(xPosition == xMin){
                return;
            }
            board[yPosition][xPosition] = \' \';
            xPosition--;
            moveNum++;
        }

        else if(direction == moveR){
            if(xPosition == xMax){
                return;
            }
            board[yPosition][xPosition] = \' \';
            xPosition++;
            moveNum++;
        }

        else if(direction == moveU){
            if(yPosition == yMax){
                return;
            }
            board[yPosition][xPosition] = \' \';
            yPosition++;
            moveNum++;
        }

        else if(direction == moveD){
            if(yPosition == yMin){
                return;
            }
            board[yPosition][xPosition] = \' \';
            yPosition--;
            moveNum++;
        }

        else cout << \"INVALID ARGUMENT\";

    }


    //TO BE CONTINUED.
};


void clear(){
    system(\"cls\");
}



int main()
{
}
« Last Edit: September 07, 2005, 12:32:00 am by RussianVodka »



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

Robinmagus

  • Hydlaa Notable
  • *
  • Posts: 883
  • Pixies!!
    • View Profile
(No subject)
« Reply #1 on: September 07, 2005, 12:49:53 am »
/me sues russianvodka for too long a post =P though nice \"game\"
Talamir - DeT, Dark Empire, etc, etc, etc.

Cha0s

  • Veteran
  • *
  • Posts: 1860
    • View Profile
(No subject)
« Reply #2 on: September 07, 2005, 03:52:32 pm »
OOP... That means split it up using logical divisions. If anything has more than one or two properties, make it its own class, or a struct (if it needs no functions). You should definitely have a Board class at the least. For the rest, are you using multiple variables to represent one thing? If so, it needs to be in a struct or a class.
Cha0s
Mac OS X Forum Moderator
In-Game Roleplay Forum Moderator
Please search and skim existing threads before posting!

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #3 on: September 07, 2005, 08:32:57 pm »
I\'ve thought about it, and I think this is how I\'m gona break up the classes:

Class Board () { // Stores information about the baord}

Class Player() {//Stores the player controls, spawn points, movement boundries, etc.}

and

Class NPC() {//Stores information about computer controled things}


The one oncern I have now is how would the classes interact, namely how would Player, and NPC conect to the Board class? One thing that comes to mind is passing functions that return private variables into other functions as arguments... Would that work?

Here is an example:


Code: [Select]


Class A(){
     char a;
public:
     char get_a(){return a;}
     void set_a(char x){a = x;}
};


Class B(){
     char b;
public:
     char get_b(){return b;}
     void set_b(char x){a=x;}
};

int main(){
     A objA;
     B objB;

     objA.set_a(\'x\');
     objB.set_b(objA.get_a());

     cout << objB.get_b();

     return 0;
}



When compiled and run, will the program write \"x\" on the screen, or will I get an error?
« Last Edit: September 07, 2005, 08:34:19 pm by RussianVodka »



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

ramlambmoo

  • Hydlaa Notable
  • *
  • Posts: 567
    • View Profile
(No subject)
« Reply #4 on: September 08, 2005, 04:18:08 am »
Quote
When compiled and run, will the program write \"x\" on the screen, or will I get an error?


Well, you could have just compiled it and ran it yourself, But yes, you will get an \'x\' displayed to the screen.

As for the actual game code, It looks alright, it gave a few errors on my compiler but nothing too worrying.  As had been said, you should probablly split the class \"game\" up into different classes, so yeah.  Not much else to add apart from keep up the good coding :)
« Last Edit: September 08, 2005, 04:25:02 am by ramlambmoo »

Cha0s

  • Veteran
  • *
  • Posts: 1860
    • View Profile
(No subject)
« Reply #5 on: September 08, 2005, 01:20:38 pm »
Oh, and split that baby up into header files! Good cpp practice. :)
Cha0s
Mac OS X Forum Moderator
In-Game Roleplay Forum Moderator
Please search and skim existing threads before posting!

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #6 on: September 08, 2005, 02:52:34 pm »
Ok, then a few more questions...

1. How would I make header files out of my code?
2. When I #include the headers in my code will I have to also #include the headers that are in my headers? I know that if I #include\"code.cpp\" then I would have to do that.



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

Cha0s

  • Veteran
  • *
  • Posts: 1860
    • View Profile
(No subject)
« Reply #7 on: September 09, 2005, 03:37:00 am »
The headers contain all the function declarations (and comments explaining them). They also include class/struct declarations/definitions and global variable declarations. The header-files (usually) have the same name as the cpp files and include everything the cpp files need (they also have the using directive, assuming it\'s global, i.e. using namespace std;). The cpp files are JUST implementation (all the functions). They need no includes except for the the header file with the function declarations.

These are just general rules and there are often exceptions (for example: when working with templates, everything must remain in one file).
Cha0s
Mac OS X Forum Moderator
In-Game Roleplay Forum Moderator
Please search and skim existing threads before posting!