Krieg -> RE: How do you make a game? (6/24/2007 15:40:11)
|
Well, whatever: I've done what I've done. I had some free time today so I worked on it. There are some bugs that I've listed on top, but it more or less shows the complexity of a simple text RPG. To use the code, you'll have to download Visual C++ Express Edition 2005 and install it. After that, create a new project (CLR Empty Project). On the left hand side where you see folders, right click "Source Files" and select Add>New Item. Here, select "C++ File" (.cpp extension). Copy the below code and paste it into your code window. After that, right click your source file ([filename].cpp) and select "Properties". Under C/C++: General Optimization Preprocessor Code Generation Language Precompiled Headers Output Files Browse Information Advanced Note: Failure to follow any of the above can cause unwanted results. To prevent the code from stretching the window too much, I've split some of the longer lines of code. //a simple, incomplete text game
//bugs: does not include switches, missing random function, nonstandard structure, can fall off the map
#using <mscorlib.dll>
using namespace System;
//function prototypes
void help();
void status();
void move(double);
void world(double &);
void battle(double);
//monsters
void cat(String ^&, int &, int &, int &, int &, int &, int &);
void dog(String ^&, int &, int &, int &, int &, int &, int &);
void bear(String ^&, int &, int &, int &, int &, int &, int &);
//Default Player Stats (Global Variable Declarations)
int pLevel = 1;
int pSpeed = 4;
int pHealth = 20;
int pDamage = 0;
int pDefense = 2;
int pOffense = 5;
int pExperience = 0;
int rExperience = 5;
void levelUp();
int main()
{
String ^name = ""; //declares a string variable called "name"
double mov = 1.0; //declares a floating/double variable called "mov"
Console::WriteLine("Welcome to TEXT ADVENTURES!"); //displays a message line
Console::WriteLine(); //skips a line
Console::Write("Name: "); //prompts user for name
name = Console::ReadLine(); //puts user input into the name variable
Console::WriteLine();
Console::WriteLine("Welcome, {0}. You are in front of your house on a peninsula at the south west end of a 3x3 island.", name);
move(mov); //calls the move function and sends the mov variable by value
return 0;
}
void move(double mov)
{
while (mov != 10000.0) //while loop
{
String ^action = "";
Console::WriteLine();
Console::Write("What would you like to do? ");
action = Console::ReadLine();
if (action->CompareTo("help") == 0) help(); //if statement that compares a string
if (action->CompareTo("status") == 0) status();
if (action->CompareTo("n") == 0)
{
mov = mov + 1;
world(mov);
}
if (action->CompareTo("s") == 0)
{
mov = mov - 1;
world(mov);
}
if (action->CompareTo("e") == 0)
{
mov = mov + .1;
world(mov);
}
if (action->CompareTo("w") == 0)
{
mov = mov - .1;
world(mov);
}
}
Console::WriteLine("GAME OVER");
}
void help()
{
Console::WriteLine("To move, type n (North), s (South), e (East), or w (West)");
}
void status()
{
Console::WriteLine();
Console::WriteLine("Level: {0}", Convert::ToString(pLevel));
Console::WriteLine("Health: {0}/{1}", Convert::ToString(pHealth - pDamage), Convert::ToString(pHealth));
Console::WriteLine("Offense: {0}", Convert::ToString(pOffense));
Console::WriteLine("Defense: {0}", Convert::ToString(pDefense));
Console::WriteLine("Speed: {0}", Convert::ToString(pSpeed));
Console::WriteLine("Experience: {0}", Convert::ToString(pExperience));
}
void world(double §ion)
{
double monster = 0.0;
//valid movements
if (section == 1.0) Console::WriteLine("You're now in front of your house.");
if (section == 1.1)
{
monster = 1.0;
battle(monster);
}
if (section == 1.2) Console::WriteLine("You have arrived to the south eastern most end of the island.");
if (section == 2.0) Console::WriteLine("You can see the wester ocean from here.");
if (section == 2.1) Console::WriteLine("You can hear the sound of rushing water surround you.");
if (section == 2.2)
{
Console::WriteLine("You are on the east coast.");
monster = 2.0;
battle(monster);
}
if (section == 3.0)
{
Console::WriteLine("You have arrived to the north western most end of the island.");
monster = 3.0;
battle(monster);
}
if (section == 3.1) Console::WriteLine("You are on the north coast.");
if (section == 3.2) Console::WriteLine("You have arrived to the north eastern most end of the island.");
//invalid movements
if (section < 0)
{
Console::WriteLine("Sorry, but you cannot go any further at this time.");
section = section + 1;
}
if (section == 0.0)
{
Console::WriteLine("Sorry, but you cannot enter your house at this time.");
section = section + 1;
}
if (section == 0.1 || section == 0.2)
{
Console::WriteLine("Sorry, but you cannot swim in the southern ocean at this time.");
section = section + 1;
}
if (section == 4 || section == 4.1 || section == 4.2)
{
Console::WriteLine("Sorry, but that's as far up north you're allowed to go at this time.");
section = section - 1;
}
if (section == .9 || section == 1.9 || section == 2.9)
{
Console::WriteLine("Sorry, but that's as far west as you can go at this time.");
section = section + .1;
}
if (section == 1.3 || section == 2.3 || section == 3.3)
{
Console::WriteLine("Sorry, but that's as far east as you can go at this time.");
section = section - .1;
}
}
void battle(double monster)
{
String ^action = "";
//monster local variables
String ^mName = "";
int mLevel = 0;
int mSpeed = 0;
int mHealth = 0;
int mDefense = 0;
int mOffense = 0;
int mExperience = 0;
//obtain monster stats
if (monster == 1.0) cat(mName, mLevel, mSpeed, mHealth, mDefense, mOffense, mExperience);
if (monster == 2.0) dog(mName, mLevel, mSpeed, mHealth, mDefense, mOffense, mExperience);
if (monster == 3.0) bear(mName, mLevel, mSpeed, mHealth, mDefense, mOffense, mExperience);
//battle
Console::WriteLine();
Console::WriteLine("You have been attacked by a {0}!", mName);
while(mHealth > 0)
{
if ((pHealth - pDamage) >= 0)
{
Console::Write("What would you like to do (a/r)? ");
action = Console::ReadLine();
Console::WriteLine();
if (action->CompareTo("a") == 0)
{
if (pSpeed >= mSpeed)
{
mHealth = mHealth - pOffense + mDefense; //monster health calculation
Console::WriteLine("You do {0} damage to {1}. It has {2} health left.", Convert::ToString(pOffense - mDefense), mName,
Convert::ToString(mHealth));
if (mHealth <= 0)
{
Console::WriteLine("Congratulations! You have defeated a {0}! You have gained {1} experience points.", mName,
Convert::ToString(mExperience));
pExperience = pExperience + mExperience; //update player experience
levelUp();
}
else
{
pDamage = pDamage + mOffense - pDefense; //player health calculation
Console::WriteLine("It does {0} damage to you. You have {1} health left.", Convert::ToString(mOffense - pDefense),
Convert::ToString(pHealth - pDamage));
}
}
else
{
pDamage = pDamage + mOffense - pDefense; //player health calculation
Console::WriteLine("It does {0} damage to you. You have {1} health left.", Convert::ToString(mOffense - pDefense),
Convert::ToString(pHealth - pDamage));
mHealth = mHealth - pOffense + mDefense; //monster health calculation
Console::WriteLine("You do {0} damage to {1}. It has {2} health left.", Convert::ToString(pOffense - mDefense), mName,
Convert::ToString(mHealth));
if (mHealth <= 0)
{
Console::WriteLine("Congratulations! You have defeated a {0}! You have gained {1} experience points.", mName,
Convert::ToString(mExperience));
pExperience = pExperience + mExperience; //update player experience
levelUp();
}
}
}
else
{
Console::WriteLine("You have successfully run away.");
mHealth = 0;
}
}
else
{
Console::WriteLine("You couldn't take the pain! You've blacked out...");
mHealth = 0;
pDamage = pDamage / 2;
Console::WriteLine();
Console::WriteLine("You wake up. The {0} seems to have left. You feel slightly better.", mName);
}
}
}
void levelUp()
{
if (pExperience >= rExperience)
{
Console::WriteLine();
Console::WriteLine("Congratulations! You have just leveled up!");
rExperience = rExperience * 2;
pLevel = pLevel + 1;
pHealth = pHealth * 2;
pDamage = 0;
pOffense = pOffense * 2;
pDefense = pDefense * 2;
status();
}
}
void cat(String ^&name, int &level, int &speed, int &health, int &defense, int &offense, int &experience)
{
name = "Cat";
level = 2;
speed = 5;
health = 9;
defense = 2;
offense = 5;
experience = 3;
}
void dog(String ^&name, int &level, int &speed, int &health, int &defense, int &offense, int &experience)
{
name = "Dog";
level = 2;
speed = 4;
health = 8;
defense = 3;
offense = 5;
experience = 3;
}
void bear(String ^&name, int &level, int &speed, int &health, int &defense, int &offense, int &experience)
{
name = "Bear";
level = 4;
speed = 2;
health = 15;
defense = 6;
offense = 8;
experience = 10;
} Hope this helps! If you have any questions, feel free to ask. I've tried to include some comments so that you could follow along. Edit: Fixed links.
|
|
|
|