Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

graveyard filla

anyone here finished or working on a text game?

This topic is 5329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

high, im new to programming (in my first semester of CIS) anyway, in class we have only covered up to functions and loops/decisions. so i havent even started with OOP or anything, so i can only do basic stuff. but ive allready made my first text based game. its kind of crappy but i think i did a good job, considereing ive only worked on it for about 4 days. i dont know where to get web space to upload the exe, so here is the source code if anyone cares to look at it/play the game. anyway, please post your game here! im curiosue to see how others made a text based game, and also how it plays. so i can get some ideas for myself game/coding wise. thanks for anyone who posts. ps heres the source
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
//current problem : none. but want to build a x,y coordinate system

using namespace std;

bool random_bool();
//a function that randomizes the event of an event (block of code) happening. such as drop loot,

//etc.it basically just returns true or false on a 50/50 chance. could be switch to 70/30 or different.


void intro_screen (); //a simple print screen function that prints the welcome screen


void attack_prompt(int player_health,int player_stamina,int player_mana,int bullets,int enemy_health,char &attack_type);
//just a print screen of the current stats for player/enemy. also prompts for next attack

//and returns attack_type


void player_attack (int &enemy_health,char attack_type,int &player_stamina,int &player_mana,int &player_health); 
//this calculates the players attack. it sends back enemies new health(after attack),

//the players stamina, and the players mana after the attack




void enemy_attack (int &player_health); 
//this function calculates the enemies attack, and sends back the players updated health


void player_movement (int &spaces_moved);//this prints the movment keys and also

											//starts a counter called spaces_moved

											//which records how many spaces the player moved


void rand_attack (bool &battle_now); //this calculates the chances of a random

													//attack. it doesnt start untill the player

													//moves at least one space. it returns a 

													//boolean value of true if combat is started

													//and false if no combat started.


void enter_combat(int &player_health,int &player_stamina,int &player_mana,int &bullets);
//this manages all combat. it called the player_attack and enemy_attack functions, updates

//all health,stamina, and mana. it also prints out results of each hit etc.





int main()
{
	
	int spaces_moved = 0;
	bool battle_now;
	int player_health = 200;
	int player_stamina = 100;
	int player_mana = 65;
	int bullets = 0;
	srand (time (NULL)); //set the seed to the internal clock of the computer

	
	
	intro_screen(); //simply prints the intro screen to the screen

	
	
	do
	{
	
	player_movement(spaces_moved); //pop up the movement screen


	rand_attack(battle_now);//check for random attack (80% chance of attack)

	
	
		if (battle_now == true) //if the attack is true, then enter combat, if not, go back

			//to the top, ask for movement, and look for another random attack

		{
			enter_combat(player_health,player_stamina,player_mana,bullets);
		}
	
	}

	while (player_health > 0 && spaces_moved < 20); //do this all untill the player dies

													//or beats the game

	
			
	if (player_health < 0) //if the player died, say game over.

		{

			cout<<"You are dead"<<endl;

			cout<<"Game Over"<<endl;
		}
	
	else if (spaces_moved == 20) //if the player reached the end, say you win

	{
		cout<<"**************************************"<<endl;
		cout<<" YOU WIN!! You have reached the end!!!"<<endl<<endl;
		cout<<"**************************************"<<endl;

	}

	return 0;
}

void intro_screen () //simply prints the welcome screen to the player. 

{
	cout<<"********************************************************************************"<<endl;
	cout<<"*********************************WELCOME TO FTA*********************************"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<"**************************DONKEY PUNCH PRODUCTIONS INC.*************************"<<endl;
	cout<<"********************************************************************************"<<endl;
}

void player_attack (int &enemy_health,char attack_type,int &player_stamina,int &player_mana,int &bullets,int &player_health) 
//calculates players attack and sends back updated enemy health, player stamina, and player mana


{
	int attack = 0;
	int defence = 0;
	int damage = 0;
	int used_stamina = 0;
	int used_mana = 0;
	int used_bullets = 0;
	int healed_for = 0;


	attack = (rand () % 11) + 2; //generates random number for attack

									// a dice number (2-12)

	defence = (rand () % 11) + 2; //generats random number  for defence

									// a dice number

	
	if (attack > defence)  //compares attack to defence, if bigger, it is a hit and 

							//then calculates damage and updates enemy_health

						
	{
		
		if(attack_type == ''1'') //if the attack type was a sword attack, then

		{
			
			if (player_stamina < 4) //check to see if the player has enough stamina to execute 

									//the attack

			{
				cout<<"|=|=|=|=|You are exhausted|=|=|=|=|"<<endl<<endl; //if player doesnt have enough stamina,

												//will print "you are exhausted", and bail 

												//out of the function completely

			}
			else
			{
			
			cout<<"****You hit!****"<<endl<<endl; //else the player has enough stamina, print

									//you hit, then

			
			damage =  ((rand () % 11) + 2); //calculate damage (random number 1-12)

			
			used_stamina = 4; //and use 4 stamina

			}
			
			
			if (damage > 10) //if damage is more then 10, say you sliced him

			{
				cout<<"You sliced the enemy for "<<damage<<" damage!"<<endl<<endl;
			}
			else if (damage > 0) //if its less then 10 but more then 0 (ie you had enough stamina)

									//print you stabbed him

			{
				cout<<"You stabbed the enemy for "<<damage<<" damage!"<<endl<<endl;
			}
			
		}
		
		else if(attack_type == ''2'') //OR if attack type was a magic attack

		{
			
				if (player_mana >= 5) //check to see if player has enough mana

				{
					cout<<"****You hit!****"<<endl<<endl; //since the player does, say "you hit"

					damage =  ((rand () % 20) + 2); //get damage from random number 2-20

					used_mana = 5;//use 5 mana

					cout<<"The enemy was burned "<<damage<<" damage!"<<endl<<endl;
					//print the enemy was burned for "damage"

				
				}
			
			else //if player doesnt have enough mana

			{
				cout<<"|=|=|=|=|Not enough mana to cast spell|=|=|=|=|"<<endl<<endl; //say you dont have enough mana

			}
			
			
			
		}			
	
		else if (attack_type == ''3'') //OR you have a gun

		{
			damage = ((rand () % 25) + 2); //damage is a random 2-26

		
			
			if (bullets >= 1) //if you have bullets, say you hit, and use a bullet

			{
				used_bullets = 1;
				cout<<"***You hit!***"<<endl<<endl;
			}
			else if (bullets <= 0) //if you dont have bullets, say your out of ammo

			{
				used_bullets = 0;
				cout<<" YOU ARE OUT OF AMMO   "<<endl<<endl;

			}
			
			cout<<"The enemy was blasted for "<<damage<<" damage!"<<endl<<endl; //print damage

		}
		
		else if (attack_type == ''4'') //OR the player casted heal

		{
			
		
			
			if (player_mana >= 3) //if you have mana, then say success, use mana and add 1-36 health

			{
				used_mana = 3;
				healed_for = ((rand () % 35) + 2); //heals for a random 2-36

				cout<<"***The spell was a success!***"<<endl<<endl;
				cout<<"You healed yourself for "<<healed_for<<" health!"<<endl<<endl; //print damage

				
			}
			else if (player_mana < 3) //if you dont have bullets, say your out of ammo

			{
				used_mana = 0;
				cout<<" YOU ARE OUT OF MANA   "<<endl<<endl;

			}
			
			
		}
		
		
	
	}

	
	else if (defence >= attack) //if defence is bigger, you miss.

	{
		damage = 0;
		
		if (attack_type == ''1'') //if you miss and used a sword attack, do the follow:

		{
			if (player_stamina > 4) //check to see if they have enough stamina

			{

			used_stamina = 2; //if they do have enough stamina, take 2 away

			cout<<"******You missed!*****"<<endl<<endl; //print "you missed"

			}
			else if (player_stamina < 4) //else if they dont have enough stamina

			{
				cout<<"|=|=|=|=|You are exhausted|=|=|=|=|"<<endl<<endl; //print you are exhausted.

			}
		}
		
		else if (attack_type == ''2'') //if you misee and used magic,

		{
			if (player_mana >= 5) //check to see if they have enough mana

			{
			used_mana = 3; //if they do, use 3 mana and

			cout<<"|=|=|=|=|The spell fizzled|=|=|=|=|"<<endl<<endl; // say the spell fizzled

			}
			else if (player_mana < 5) //if they dont have enough mana

			{
				cout<<"|=|=|=|=|=|Not enough mana to cast spell|=|=|=|=|=|"<<endl<<endl; //say "do not have enough mana"

			}
		}
		else if (attack_type == ''3'')
		{
			if (bullets >= 1)
			{
				used_bullets = 1;
				cout<<"+++++++YOU MISEED!++++++"<<endl<<endl;
			}
			else if (bullets <= 0)
			{
				used_bullets = 0;
				cout<<" YOU ARE OUT OF AMMO   "<<endl<<endl;
			}
		}
		else if (attack_type == ''4'') //OR the player casted heal

		{
			
		
			
			if (player_mana >= 3) //if you have mana, then say success, use mana and add 1-36 health

			{
				used_mana = 2;
				cout<<"***The spell fizzled!***"<<endl<<endl;
			}
			
			else if (player_mana < 3) //if you dont have bullets, say your out of ammo

			{
				used_mana = 0;
				cout<<" YOU ARE OUT OF MANA   "<<endl<<endl;
			}
			
			
		}
	
	
	
	
	}
	enemy_health = enemy_health - damage; //update all damage done to the enemy

	player_stamina = player_stamina - used_stamina; //update stamina used

	player_mana = player_mana - used_mana; //update mana used 

	player_health = player_health + healed_for;
	bullets = bullets - used_bullets;
}

void enemy_attack (int &player_health) // calculates enemy attack "hit" or "miss" and 

									//calculates damage and updates health

								
{	
	int attack;
	int defence;
	int enemydmg;
	int attack_type;
	
	
	attack = (rand () % 11) + 2; //makes attack a random between 1-12


	defence = (rand () % 11) + 2; //makes defence a random between 1-12

	
	

	if (attack > defence) //if attack > defence, the PC hits the player

	{
		
		attack_type = rand () % 100; //generates a random number between 1-100, calling it attack_type


		if(attack_type > 65) //if the number is > 50, then do a melee attack

		{
			
			cout<<"$=$=$=You were hit!=$=$=$"<<endl<<endl; //prints you were hit

			enemydmg =  (rand () % 11) + 2; //enemy damage for this melee attack is a random

											//between 1-12

			cout<<"Enemy bashed you for "<<enemydmg<<" damage!"<<endl<<endl;
			//print the damage done

		}

		else //if the number is < 50, then do a magic attack

		{
			
			cout<<"$=$=$=$=You were hit!=$=$=$=$"<<endl<<endl; //prints you were hit

			enemydmg =  (rand () % 20) + 2; //enemy damage for this "lightning" attack

											//is a random between 2-20

			cout<<"Enemy bolted you for "<<enemydmg<<" damage!"<<endl<<endl;
			//print the damage done

			
			
		}
	}
	
	else if (defence >= attack)  //if defence > attack, the pc missed the player

	
	{
		enemydmg = 0;
		cout<<"=$=$=$=$They missed you!=$=$=$=$"<<endl<<endl; //print they missed you

	}

player_health = player_health - enemydmg;	//updated player health


}


void player_movement (int &spaces_moved)
{
	
	char direction;
	
	cout<<"Press ''W'' to move up"<<endl;
	cout<<"Press ''A'' to move left"<<endl;
	cout<<"Press ''S'' to move right"<<endl;
	cout<<"Press ''Z'' to move backwards"<<endl<<endl;
	cin>>direction;

	if (direction == ''w'')
	{
		cout<<"You moved forward."<<endl<<endl;
	}
	else if (direction == ''a'')
	{
		cout<<"You moved left."<<endl<<endl;
	}
	else if (direction == ''s'')
	{
		cout<<"You moved right."<<endl<<endl;
	}
	else
	{
		cout<<"You moved backwards."<<endl<<endl;
	}
	
	spaces_moved = spaces_moved + 1;
	cout<<"You moved a total of "<<spaces_moved<<" spaces."<<endl<<endl;	
}

void rand_attack (bool &battle_now)
{

		
		int attack_chance = (rand () % 100) + 1; //this sets up a random fight wiht 15% chance of happening



		if (attack_chance > 85)
		{
			battle_now = true;
		}
		else if (attack_chance <=85 )
		{
			battle_now = false;
		}
	

}

void enter_combat(int &player_health,int &player_stamina,int &player_mana,int &bullets)
{

			char attack_type = ''0'';
			int enemy_health = 40; 
			
			int bullets_found = 0;
			bool gun;
			
			system("cls");
			
			cout<<"**************************YOU HAVE ENTERED COMBAT!*****************************"<<endl<<endl;			
	do 
	//show enemy/player status

	//do player attack

	//do enemy attack

	//while player and enemy are both still alive

	{
			attack_prompt (player_health,player_stamina,player_mana,bullets,enemy_health,attack_type);
			
			player_attack (enemy_health,attack_type,player_stamina,player_mana,bullets,player_health);
				
			//this calculates the players attack, and updates enemyhealth/player stamina/mana

		
			enemy_attack (player_health); 
			//this calculates the enemies attack, and updates player health.

	}
		while ((player_health > 0) && (enemy_health > 0)); 
		//runs this battle untill one of the player dies.

		
		
		
		//this down here is clean up code after the enemy dies

		//it prints if i defeated them, it resets the enemies health to 40

		//and it gives a random chance for a gun to drop.

		if (enemy_health < 0)
		{
			cout<<"You have defeated the enemy"<<endl;
			
			enemy_health = 40;
		
			gun = random_bool();

			if (gun == true)
			{
				cout<<"Enemy dropped a gun"<<endl<<endl;
				bullets_found = 12;
			}

		}

		bullets = bullets_found + bullets;
		

}
	
bool random_bool()
{
	
	int random = rand() % 100;

	if (random >= 50)
	{
		return true;
	}
	else 
	{
		return false;
	}

}

void attack_prompt(int player_health,int player_stamina,int player_mana,int bullets,int enemy_health,char &attack_type)
{
		
	
			
			
			
			
	
			cout<<"You have:              "<<"                   The enemy has:     "<<endl;
			cout<<"***********************"<<"                   *********************"<<endl;
			cout<<"---"<<player_health<<"---"<<"--health------"<<"                   -------"<<enemy_health<<"------health"<<endl;
			cout<<"---"<<player_stamina<<"---"<<"---stamina-----"<<"                  *********************"<<endl;
			cout<<"---"<<player_mana<<"---"<<"---mana--------"<<endl<<endl;
			cout<<"---"<<bullets<<"---"<<"---bullets--------"<<endl<<endl;
			cout<<"********************************************************************************"<<endl<<endl;
		
			
			
			cout<<"Type the number for the attack type and press enter"<<endl<<endl;
			//prompts the user to enter what kind of attack they want to do

				if (bullets > 0) //if the player has bullets, show fire gun as an option

				{

					cout<<"[1 for sword attack,2 for magic,or 3 to fire gun]"<<endl;
					cin>>attack_type;
					
					if (attack_type == ''2'') //if the player chose magic, show the spell choice

					{
						cout<<"[1 for fireball, 2 for HEAL]"<<endl;
						cin>>attack_type;
						
						if (attack_type == ''1'') 
						{
							attack_type = ''2''; //this converts the users input from ''1'' to ''2'',

												//because attack_type of ''2'' will do a fireball

												//and before this attack_type is ''0''

						}
						else if (attack_type == ''2'')
						{
							attack_type = ''4''; //this convers the users input from ''2'' to ''4''

										      //because ''4'' is the heal spell 

						}
					}

					
					cout<<endl<<endl;
					system("cls");
				}

				else if (bullets <= 0) //if the player doesnt have bullets, dont show the gun

				{
					cout<<"[1 for sword attack,2 for magic]"<<endl;
					cin>>attack_type;
					
					if (attack_type == ''2'') //if the player chose magic, show the spell choice

					{
						cout<<"[1 for fireball, 2 for HEAL]"<<endl;
						cin>>attack_type;
						
						if (attack_type == ''1'') 
						{
							attack_type = ''2''; //this converts the users input from ''1'' to ''2'',

												//because attack_type of ''2'' will do a fireball

												//and before this attack_type is ''0''

						}
						else if (attack_type == ''2'')
						{
							attack_type = ''4''; //this convers the users input from ''2'' to ''4''

										      //because ''4'' is the heal spell 

						}

					}

									
					cout<<endl<<endl;
					system("cls");
					
				}

}
let me know what you think!!

Share this post


Link to post
Share on other sites
Advertisement
I coded a text based game that did about the same thing by the looks of it, only i based it on an old MUD i used to play, way back in the day.
It was free roaming and you could play until class ended.
It had different classes(jobs), races, randomized stats based on DND, and even an experience and level system worked into it.
The code is so horribly done tho. i used to never use comments, and almost any random variable name i thought up. I had a variable called Greg, which i just used to temporarily store their name.
I eventually added a save feature to it, so you could save your experience level and stats, but since it was saved as a .txt file, you could easily open that up and change anything you wanted.

[edited by - Wr3cktangle on January 15, 2004 10:10:31 AM]

Share this post


Link to post
Share on other sites
I wrote a text-based RPG a while ago. It''s like a cross between Final Fantasy and Mortal Kombat (I wasn''t thinking of it like that at the time, but that''s how it ended up). I used Win32 console functions for screen I/O (I also wrote a curses module for Unixy systems) and a custom-built assembly-based scripting language (overkill, and a huge pain in the ass), but that was the extent of the technical prettiness. It''s not the best code in the world, but I learned a hell of a lot from it.

Share this post


Link to post
Share on other sites
I played Creation''s Arena: Resurrection for a week after I got
it. Great for a time-killer at work.

Fun game.

Thanks Creation! ^,^

-Hyatus
"da da da"

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!