Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[C++]Create A Text Based Game

Status
Not open for further replies.
Joined
Aug 4, 2010
Messages
572
Reaction score
177
I'm going to be teaching / giving examples on how to create a simple to advanced C++ text based game for anyone who is interested..

Requirements:
Any Compiler
A Computer

Knowledge Required:
OOP
Classes
Basic Algorithm

OK lets get started.

Step 1:
Download a compiler and create a new basic C++ Project, then write in or copy and paste the following below
Code:
#include <iostream>
using namespace std;

int main(){



}

Above is the basic structure for a C++ Program now were going to add a few headers needed for later...


Code:
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstring>

Now The program should look like:
Code:
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstring>

using namespace std;

int main(){
    
    
    
    
    cin.get(); //Added to keep the program paused, system("pause") is acceptable.
    }


The basic structure of a video game if your thinking simple like me right now would be having a monster and having it assigned with basic stats..such as Attack,Defense,Mana or so.

That's exactly what were going to do next.

Code:
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstring>

using namespace std;

class Player{
    
    /* All integers for our player meaning YOU */
    char name[MAX];
    
    int attack;
    int defense;
    int health;
    
    void setHealth;
    void setDefense
    
    
    
    };
    
class Monster{
    
    /* All integers and data for Monster Meaning BOT */
    
    char name[MAX2];
    
    int botattack;
    int botdefense;
    int bothealth;
    
    void setBHealth;
    void setBDefense
    
    
    
    };

int main(){
    
    
    Player Human;
    Monster Bot;
    
    
    
    
    cin.get();
    
}

Step 2:
Now that we've gotten our full base completed we can start to assign data for the player and bot..

NOTES:
[MAX] && [MAX2] Are referring to us defining the max characters allowed for the players name along with the maximum characters allowed for the bots name... which will most likely be random.

NEXT
Code:
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstring>

/* Defining our values */

#define MAX 256
#define MAX2 20


using namespace std;

class Player{
    
    /* All integers for our player meaning YOU */
    char name[MAX];
    char setName[MAX];
    
    int attack;
    int defense;
    int health;
    
    void setHealth(int cHealth);
    void setDefense(int dDefense);
    
    
    
    };
    
void Player::setHealth(int cHealth){
    
    Player::health = cHealth; //Setting the integer health to equal to our new integer cHealth
    
    
    }
    
void Player::setDefense(int dHealth){
    
    Player::defense = dDefense; //Setting the integer defense to equal to our new integer cDefense
    
    } 
    
void Player::name(char setName[MAX]){
    
    strcpy(Player::name,setName); //Copying the player name into our new varible...
    
    }
    
class Monster{
    
    /* All integers and data for Monster Meaning BOT */
     
    int botattack;
    int botdefense;
    int bothealth;
    
    void setBHealth(int BHealth);
    void setBDefense(int BDefense);
    
    
    
    };

//Setting bots health  = our varible BHealth    
void Monster::setBHealth(int BHealth){
    
    Monster::bothealth = BHealth;
    
    }
    
//Setting bots defense  = our varible BDefense     
void Monster::setBDefense(int BDefense){
    
    Monster::botdefense = BDefense;
    
    }

int main(){
    
    
    Player Human;
    Monster Bot;
    
    
    
    
    cin.get();
    
}

I've left comments stating all that I had done.

Next we focus on main
Code:
int main(){
    
    
    Player Human;
    Monster Bot;
    
    
    
    
    cin.get();
    
}

I Didn't really add anything now if we look below at the next code youll see the finished
version..

Code:
int main(){
    
    
    Player Human;
    Monster Bot;
    
    /* Configuring our HUMAN.. UNPROTECTED */
    
    Human.name = "Wizcoder";
    Human.setHealth(100); //We've given the Human(Player) a maximum of 100 Hitpoints
    Human.setDefense(50); //We've given the Human(Player) a maximum of 50 Defense Points
    
    /* Configuring our BOT. UNPROTECTED */
    Bot.setBHealth(50); //Vice Versa
    bot.setBDefense(50); //Vice Versa to Human
    
    
    
       
    cin.get();
    
}

If you haven't noticed already from the start I made everything within our classes Public. Meaning anyone can edit your stats just as we did below and this is called poor programming or poor practice in which you should never do when making a real video game.

So guess what we are going to use public to access our private.

Example:
Public -> Private -> Public

Really strange Right? We'll this is the correct way to go.
For our stats here is a READ only version in which you can set to the final product yourself.

Code:
class Player{

public:
      void PlayerStats(setHealth){

health = setHealth;

}

string Php(){

return health;

}

private:
string defense;
string health;

int main(){

Player public
public.PlayerStats("VALUE HERE."); //

cout << public.PlayerStats();

}

};

FINAL PRODUCT
Code:
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstring>

/* Defining our values */





using namespace std;

class Player{
    public:
    /* All integers for our player meaning YOU */
  
    
    int attack;
    int defense;
    int health;
    
    void setHealth(int cHealth);
    void setDefense(int dDefense);
    
    
    
    };
    
void Player::setHealth(int cHealth){
    
    Player::health = cHealth; //Setting the integer health to equal to our new integer cHealth
    
    
    }
    
void Player::setDefense(int dDefense){
    
    Player::defense = dDefense; //Setting the integer defense to equal to our new integer cDefense
    
    } 
    

class Monster{
    
    /* All integers and data for Monster Meaning BOT */
     public:
    int botattack;
    int botdefense;
    int bothealth;
    
    void setBHealth(int BHealth);
    void setBDefense(int BDefense);
    
    
    
    };

//Setting bots health  = our varible BHealth    
void Monster::setBHealth(int BHealth){
    
    Monster::bothealth = BHealth;
    
    }
    
//Setting bots defense  = our varible BDefense     
void Monster::setBDefense(int BDefense){
    
    Monster::botdefense = BDefense;
    
    }
int main(){
    
    
    Player Human;
    Monster Bot;
    
    /* Configuring our HUMAN.. UNPROTECTED */
    
    
    Human.setHealth(100); //We've given the Human(Player) a maximum of 100 Hitpoints
    Human.setDefense(50); //We've given the Human(Player) a maximum of 50 Defense Points
    
    /* Configuring our BOT. UNPROTECTED */
    Bot.setBHealth(50); //Vice Versa
    Bot.setBDefense(50); //Vice Versa to Human
    
    
    
       
    cin.get();
    
}


In the demonstration above we were able to access a private using a public so it's not editable by randoms.

I don't really know if I helped anyone from this tutorial so if there is positive replys that'll let me know to go and make a part two meaning the battle system and File I/O etc..
 
Last edited:
Newbie Spellweaver
Joined
Feb 23, 2010
Messages
32
Reaction score
3
sorry for pushing but i read your thread and in my opinion you should call the thread s.th. like "how to create a class and edit members in c++" cause thats the only thing youre explaining. No game logic or outputs in any kind of form (like console outputs) so its just a thread which explains how to create a class, add members and methods to it and create an instance...

sry m8, but i think its really useless :/
 
Status
Not open for further replies.
Back
Top