Sign in to follow this  
Fragala

text game

Recommended Posts

can some 1 possibly post there code for a text game they have i wanna write 1 my self but i need to look at 1 first to see how to get started or does any 1 know any tutorials on how to make ur own text based game also im writng it in c++ im uses devcpp and im on windows

Share this post


Link to post
Share on other sites
Hello,

Here is my first game I've ever made. The idea is as old as computer games. I wrote it to practice my C skills (very poor those days :).

The code isn't commented but it fairly simple and descriptive.

Anyway, I hope you'll find it useful.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>


void MainScreen();
void Medic();
void Shop();
void Training();
void PrintEnemyStats();
void Unavailavle();
void CheckArena();
void QuitProgram();
void NextDay();
void GenerateDay();
void ReturnToMain();
void Training();
void Fight();
void Pause();
void EnemyDeath();
void PlayerDeath();
void Sure();

char RandEnemyNames();

int MakeYourChoice();
int GenerateEnemyTouhg();
int GenerateEnemyAverage();
int GenerateEnemyPoor();
int GenerateRewards();



int day,str,dex,hp,hpm,as,ds,rstr,rdex,rhpm,ras,rds,gold,sword,armour,shield,bet,choice,turns,kills;
int estr,edex,ehp,eas,eds,esword,earmour,treward,areward,preward;



char *npc;
char *npc1,*npc2,*npc3;
char enemynames1[4][20]={"Kraway Urlich ","Nieugiety Konrad ","Tloczony Zawisza ","Odporny Lancelot"};
char enemynames2[4][20]={"Don Chichot ","Krzywy Ryszard ","Warszawski Cwaniak","Stanowczy Prusak"};
char enemynames3[4][20]={"Wygolony Matolek","Niesmialy Pacholek","Oborowy Anemik ","Zasmarkany Burak"};
char *penemynames1;
char *penemynames2;
char *penemynames3;

main ()
{
unsigned int random,seed;
seed=(unsigned int)time(NULL);
srand(seed);
GenerateDay();


day=1;
kills=0;
str=50+rand()%16;
dex=50+rand()%16;
hpm=50+rand()%16;
hp=hpm;
as=25+rand()%6;
ds=25+rand()%11;
gold=20000+(rand()%6*100);
sword=0;
armour=0;
shield=0;

MainScreen();

}



void MainScreen()
{
choice=1;

while (choice)
{
system("cls");
printf("Day %d starts\n",day);
printf("Your statistics are:\n");
printf("Strength = %d\n",str);
printf("Dexterity = %d\n",dex);
printf("Health = %d of %d\n",hp,hpm);
printf("Attack skill = %d\n",as);
printf("Defence skill = %d\n",ds);
printf("Your money = %d gold pieces\n\n",gold);
printf("Your inventory:\n");
printf("Sword strength = %d\n",sword);
printf("Armour strength = %d\n",armour);
printf("Shield strength = %d\n\n\n",shield);
printf("1) Check the arena\n");
printf("2) Go shopping\n");
printf("3) Visit medic\n");
printf("4) Training\n");
printf("5) Sleep\n");
printf("0) Quit\n");

MakeYourChoice();

switch (choice)
{
case '1': CheckArena();
case '2': Shop();
case '3': Medic();
case '4': Training();
case '5': NextDay();
GenerateDay();
MainScreen();
case '0': choice=0;
continue;
}
}
Sure();
}


char RandEnemyNames()
{
npc1=enemynames1[0+rand()%4];
npc2=enemynames2[0+rand()%4];
npc3=enemynames3[0+rand()%4];
return *npc1,*npc2,*npc3;
}



void CheckArena()
{
system("cls");
printf("Fight:\n\n");
printf("\n1) Toughest: \t%s \tbets: %d",npc1,treward);
printf("\n2) Average : \t%s \tbets: %d",npc2,areward);
printf("\n3) Poor : \t%s \tbets: %d",npc3,preward);
printf("\n\n\n0) Return to main menu");

MakeYourChoice();

switch (choice)
{

case '1':
{
if (gold<treward)
CheckArena();
if (sword==0 || armour ==0)
{
system("cls");
printf("Get yourself some armour and weapon first!\n");
sleep(3000);
MainScreen();
}
else
{
npc=npc1;
GenerateEnemyTough();
bet=treward;
Fight();
}
}
case '2':
{
if (gold<areward)
CheckArena();
if (sword==0 || armour ==0)
{
system("cls");
printf("Get yourself some armour and weapon first!\n");
sleep(3000);
MainScreen();
}
else
{
npc=npc2;
GenerateEnemyAverage();
bet=areward;
Fight();
}
}
case '3':
{
if (gold<preward)
CheckArena();
if (sword==0 || armour ==0)
{
system("cls");
printf("Get yourself some armour and weapon first!\n");
sleep(3000);
MainScreen();
}
else
{
npc=npc3;
GenerateEnemyPoor();
bet=preward;
Fight();
}
}
case '0': MainScreen();
}
}

void Unavailable()
{
system("cls");
printf("Sorry, this option is not available... yet ;-)\n\n\n");
}


int MakeYourChoice()
{
printf("\n\n\nYour choice: ");
return (scanf("%s", &choice));
}


int GenerateEnemyPoor()
{
estr=40+rand()%10;
edex=40+rand()%10;
ehp=40+rand()%10;
eas=25+rand()%5;
eds=25+rand()%5;
esword=3;
earmour=3;
return(estr,edex,ehp,eas,eds,esword,earmour);
}



int GenerateEnemyAverage()
{
estr=60+rand()%15;
edex=60+rand()%15;
ehp=60+rand()%15;
eas=40+rand()%15;
eds=40+rand()%15;
esword=5;
earmour=6;
return(estr,edex,ehp,eas,eds,esword,earmour);
}


int GenerateEnemyTough()
{
estr=85+rand()%6;
edex=85+rand()%6;
ehp=85+rand()%6;
eas=80+rand()%11;
eds=80+rand()%11;
esword=6+rand()%4;
earmour=6+rand()%4;
return(estr,edex,ehp,eas,eds,esword,earmour);
}

int GenerateRewards()
{
treward=1500+100*(rand()%21);
areward=700+50*(rand()%9);
preward=200+10*(rand()%11);
return (treward,areward,preward);
}

void PrintEnemyStats()
{
system("cls");
printf("Enemy statistics:\n");
printf("\nStrength: %d",estr);
printf("\nDexterity: %d",edex);
printf("\nHealth: %d",ehp);
printf("\nAttack skill: %d",eas);
printf("\nDefence skill: %d",eds);
printf("\nSword: %d",esword);
printf("\nArmour: %d\n",earmour);
exit(0);
}

void Shop()
{
system("cls");
printf("Your money: %d\n",gold);
printf("Welcome to the weapon store!\n");
printf("\nWould you like to:\n\n");
if (sword<10)
printf("1) Buy a better sword (%d) for: %d \n",sword+1,100*(sword+1)*(sword+1));
else
printf("1) I'm afraid you cannot get anything better than your sword\n");
if (armour<10)
printf("2) Buy a better armour (%d) for %d \n",armour+1,90*(armour+1)*(armour+1));
else
printf("2) I'm afraid you cannot get anything better than your armour\n");
printf("\n3) Degrade your weapon for: %d \n",80*(sword)*(sword));
printf("4) Degrade your aromur for: %d \n",70*(armour)*(armour));
printf("\n5) Buy a shield for 1000 \n");
printf("\n\n0) Go touch yourself you cheaty sucker!!!\n");

MakeYourChoice();

switch (choice)
{
case '1':
{
if ((gold<100*(sword+1)*(sword+1)) || (sword>=10))
Shop();
else
{
gold=gold-(100*(sword+1)*(sword+1));
sword=sword+1;
Shop();
}
}
case '2':
{
if ((gold<100*(armour+1)*(armour+1)) || (armour>=10))
Shop();
else
{
gold=gold-(90*(armour+1)*(armour+1));
armour=armour+1;
Shop();
}
}
case '3':
{
if (sword>0)
{
gold=gold+(80*(sword)*(sword));
sword=sword-1;
Shop();
}
else
Shop();
}
case '4':
{
if (armour>0)
{
gold=gold+(70*(armour)*(armour));
armour=armour-1;
Shop();
}
else
Shop();
}
case '5':
{
if (gold>=1000 && shield==0)
{
shield=100;
gold=gold-1000;
Shop();
}
else
Shop();
}

case '0': MainScreen();
}
}



void Medic()
{
system("cls");
printf("Welcome to the medic house!\n");
if (hp==hpm)
{
printf("\nI don't think to be anything to patch up!\n");
MainScreen();
}
else
printf("\nYou are hurt! Your health points are: %d of %d max",hp,hpm);
printf("\nI can heal some of you health points for 100 by tomorrow");
printf("\nKeep in mind that you'll be unable to do anything else today!\n");
printf("\n1) Yes - I badly need heeling");
printf("\n0) You're taking too much time - I'm out of here!");

MakeYourChoice();

switch (choice)
{
case '1':
{
if (gold<100)
{
printf("\nSorry, it appears you cannot afford healing!");
MainScreen();
}
else
{
gold=gold-100;
if (hp+10>hpm)
hp=hpm;
else
{
hp=hp+10;
NextDay();
GenerateDay();
MainScreen();
}
}
case '0': MainScreen();
}
}
}


void QuitProgram()
{
system("cls");
printf("Thank's for playing!\n");
printf("\nBuild date: 04-04-2004\n");
printf("\n\ne-mail me: piotr_borowiec@o2.pl\n\n\n\n");
exit(0);
}

void NextDay()
{
system("cls");
day++;
if (hp<hpm)
hp=hp+rand()%4;
if (rstr<100 && rdex<100 && rhpm<100 && ras<100 && rds<100)
printf("No changes\n");
else
{
printf("Stats change:\n\n");
if (rstr>=100 && str<100)
{
printf("Strength +1\n");
str++;
rstr=rstr-100;
sleep(1000);
}
if (rdex>=100 && dex<100)
{
printf("Dexterity +1\n");
dex++;
rdex=rdex-100;
sleep(1000);
}
if (rhpm>=100 && hpm<100)
{
printf("Maximum health points +1\n");
hpm++;
rhpm=rhpm-100;
sleep(1000);
}
if (ras>=100 && as<100)
{
printf("Attack skill +1\n");
as++;
ras=ras-100;
sleep(1000);
}
if (rds>=100 && ds<100)
{
printf("Defence skill +1\n");
ds++;
rds=rds-100;
sleep(1000);
}
}
printf("RSTR = %d\n",rstr);
printf("RDEX = %d\n",rdex);
printf("RHPM = %d\n",rhpm);
printf("RAS = %d\n",ras);
printf("RDS = %d\n",rds);
sleep(5000);



}

void GenerateDay()
{

RandEnemyNames();
GenerateRewards();

}


void Training()
{
system("cls");
printf("So... looking to get tougher, huh?\n");
printf("This is what you can train up to 85 points here:\n");
printf("\n");
printf("1) Strength +1 [presently: %d]\n",str);
printf("2) Dexterity +1 [presently: %d]\n",dex);
printf("3) Max Health +1 [presently: %d]\n",hpm);
printf("4) Attack skill +1 [presently: %d]\n",as);
printf("5) Defence skill +1 [presently: %d]\n",ds);
printf("\n0) Main menu\n");

MakeYourChoice();

switch (choice)
{
case '1':
{
if (str>=85 || gold<100)
{
system("cls");
printf("Sorry can't train more of that\n");
sleep(3000);
Training();
}
else
{
gold=gold-100;
rstr=rstr+100;
GenerateDay();
NextDay();
MainScreen();
}
}

case '2':
{
if (dex>=85 || gold<100)
{
system("cls");
printf("Sorry can't train more of that\n");
sleep(3000);
Training();
}
else
{
gold=gold-100;
rdex=rdex+100;
GenerateDay();
NextDay();
MainScreen();
}
}

case '3':
{
if (hpm>=85 || gold<100)
{
system("cls");
printf("Sorry can't train more of that\n");
sleep(3000);
Training();
}
else
{
gold=gold-100;
rhpm=rhpm+100;
hp=hp+1;
GenerateDay();
NextDay();
MainScreen();
}
}

case '4':
{
if (as>=85 || gold<100)
{
system("cls");
printf("Sorry can't train more of that\n");
sleep(3000);
Training();
}
else
{
gold=gold-100;
ras=ras+100;
GenerateDay();
NextDay();
MainScreen();
}
}

case '5':
{
if (ds>=85 || gold<100)
{
system("cls");
printf("Sorry can't train more of that\n");
sleep(3000);
Training();
}
else
{
gold=gold-100;
rds=rds+100;
GenerateDay();
NextDay();
MainScreen();
}
}
case '0': MainScreen();

}
}


void Fight()
{
int chance,damage,done,ehpm,cont;
cont=1;
done=1;
ehpm=ehp;
turns=0;

system("cls");
printf("OFF THEY WENT !!!\n");
sleep(1000);
while (done)
{
printf("[%d]Player attacks... ",hp);
if ((1+rand()%100) > (50*as)/eds)
{
printf("and misses.\n");
ras++;
rdex++;
rstr++;
}
else
{
damage=((str/10)+rand()%(as/10)+rand()%sword)-((rand()%(eds/10))+earmour);
if (damage<=0)
{
printf("but armour protects %s\n",npc);
rstr++;
ras++;
}
else
{
ehp=ehp-damage;
printf("and does %d of damage\n",damage);
if (eas<as)
ras++;
else
ras=ras+(eas-as);
if (estr<str)
rstr++;
else
rstr=rstr+(estr-str);
if (ehp<=0)
EnemyDeath();
}
}
printf("[%d]%s attacks... ",ehp,npc);
if ((1+rand()%100) > (50*eas)/ds)
{
printf("and misses.\n");
if (eas<ds)
rds++;
else
rds=rds+(eas-ds);
rdex++;
}
else
{
damage=((estr/10)+rand()%(eas/10)+rand()%esword)-((rand()%(ds/10))+armour);
if (damage<=0)
{
printf("but armour protects the player\n");
rstr++;
}
else
{
if (shield>0 && (1+rand()%100)>(50*edex/dex))
{
printf("You protect yourself with your shield!\n");
shield=shield-damage;
if (eds<ds)
rds++;
else
rds=rds+(eds-ds);
rstr++;
if (shield-damage<=0)
{
printf("Bad luck! You shield broke!\n");
shield=0;
}
}
else
{
hp=hp-damage;
rhpm++;
printf("and does %d of damage\n",damage);
if (hp<=0)
PlayerDeath();
}
}
}
if (ehp<(ehpm/5) && hp>(hpm/4))
{
printf("\n\n\nI give up!\n");
gold=gold+bet;
printf("take the money: %d coins\n",bet);
done=0;
}
if (hp<(hpm/5) && (cont==1))
{
printf("\n\nOops! Looks like you're badly hurt\n");
printf("Would you like to give up?\n");
printf("\n1) Charge!\n");
printf("0) I'll give up this time\n");

MakeYourChoice();

switch (choice)
{
case '1':
{
cont=0;
break;
}
case '0':
{
printf("\n\nWise, it's better to be alive coward rather than dead hero\n");
printf("You loose %d coins",bet);
gold=gold-bet;
done=0;
}
}
}
turns++;
sleep(1000);
printf("\n");
}
printf("\n\n\nTotal turns: %d\n",turns);
printf("Remaining health points:\n");
printf("Player: %d\n",hp);
printf("%s: %d\n",npc,ehp);
rstr=rstr+(turns/2);
rhpm=rhpm+(turns/2);
sleep(3000);
GenerateDay();
NextDay();
MainScreen();
}

void EnemyDeath()
{
system("cls");
printf("You've killed %s !\n",npc);
printf("\nShame on you - theis was supposed to honourable fight.\n");
printf("Oh well, money counts\n");
printf("You won: %d coins\n",bet);
gold=gold+bet;
sleep(5000);
GenerateDay();
NextDay();
MainScreen();
}


void PlayerDeath()
{
sleep(2000);
system("cls");
printf("You were killed by %s !\n",npc);
printf("\nBetter luck next time\n");
printf("\n\n[`] [`] [`]\n");
sleep(5000);
QuitProgram();
}

void Sure()
{
system("cls");
printf("Are you sure you want to quit?\n");
printf("\n1)No, no! Let me kick more bottocks!\n");
printf("0)I'm done, this games is for suckers!\n");

MakeYourChoice();

switch(choice)
{
case '1': MainScreen();
case '0': QuitProgram();
}
}



You can compile and run it on UNIX/Linux/Windows. Any compiler should do. If you have gcc just:

gcc -o game.exe file.c

Under UNIX/Linux OS you'd probably need to chnge the system commands (cls->clear) and sleep option (it's in seconds under linux while in ms under Windows as far as I can remember)

Share this post


Link to post
Share on other sites
The thing about text games, is that on a simple level they're pretty easy to write, and so there are as many ways to code them as there are programmers.

And the best thing about that, is it allows you to experiment.

How good are you at C/C++? For a text game, you actually don't really need to know any C++ at all, C will do everything for you.

Going by my experience, just take what you know and have fun experimenting around. For your first time, there's nothing difficult that you'll need to ask too much about. Start off with a pre made character that can move around depending on the player's input, then add increased functionality - fights, different characters to play as, picking up gold or treasure, eventually buying stuff... Or whatever...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://www.gamedev.net/community/forums/viewreply.asp?ID=2676403

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this