Help for my little game....

This is a discussion on Help for my little game.... within the C++ Programming forums, part of the General Programming Boards category; Hi everybody, now i am creating a little RPG game(It is still FAR from over). But i have a little ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    Help for my little game....

    Hi everybody,
    now i am creating a little RPG game(It is still FAR from over).

    But i have a little problem with the display....
    it display text all at once instead of word by word(like in real RPG...
    Can C++ do that??(Display word by word)

    Also i would like to know how to display some text and then "clear" the hold screen and then display some other text again....just like "cls" in DOS to clear the whole screen....

    I hope you understand what i mean, (sorry for my bad english...).

    Here is my Program, there are no error, but as i say, you can see that by default, it displays the whole text at once and never clear the screen:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef char string[25];
    typedef string string2[5];
    string2 inventory;
    
    class Digimon
    {
    private:
    	string name;
    	int health;
    	int stamina;
    	int atthp;
    public:
    	Digimon(string="nothing", int=0, int=0, int=0);
    	void SetValue(string, int, int, int);
    	friend ostream& operator<<(ostream& out, Digimon& digi);
        friend void Play(Digimon&, Digimon&);
        void AttLost(int);
    	void StaLost(int);
         friend void EvilVillage(Digimon&);
         friend void DigmonHouse(Digimon&);
         
    };
    
    
    Digimon::Digimon(string n, int hp, int sp, int ahp)
    {
    	strcpy(name,n);
    	health=hp;
    	stamina=sp;
    	atthp=ahp;
    }
    
    void Digimon::SetValue(string n, int hp, int sp, int ahp)
    {
    	strcpy(name,n);
    	health=hp;
    	stamina=sp;
    	atthp=ahp;
    }
    
    ostream& operator<<(ostream& out, Digimon& digi)
    {
    	out<<"Your Digimon STATS"<<endl;
    	out<<"Your Digimon : "<<digi.name<<endl;
    	out<<"Your HP : "<<digi.health<<endl;
    	out<<"Your SP : "<<digi.stamina<<endl;
    	return out;
    }
    
    void DisplayInventory();					//Normal Function Prototype
    void Play(Digimon& enem, Digimon& you)
    {
    	int res;
    	cout<<"Choose an option"<<endl;
    	cout<<"1.Attack Opponent"<<endl;
    	cout<<"2.Not Moving"<<endl;
    	cout<<"3.Defend"<<endl;
    	cout<<"4.Inventory"<<endl;
    	cin>>res;
    	while ((enem.health>0)&&(you.health>0))
        {
    		
    		if (res==1)
    		{
    			cout<<you.name<<" ATTACK!!!"<<endl;
    			if (enem.stamina >= you.stamina)
    			{
    				cout<<"attack fail"<<endl;
    				you.StaLost(2);
    			}
    			else
    			{
    				enem.AttLost(you.atthp);
    				you.StaLost(2);
    				cout<<"Enemy lose "<<you.atthp<<" HP"<<endl;
    				cout<<enem;
    			}
    		}
    		else if (res==2)
    		{
    			cout<<"Your digimon is not moving"<<endl;
    			cout<<"Suddenly...";
    			cout<<enem.name<<" WOOF!!"<<endl;
    			if (you.stamina >= enem.stamina)
    			{
    				cout<<"He fail!";
    			}
    			else
    			{
    				you.AttLost(enem.atthp);
    				enem.StaLost(2);
    				cout<<"You lose "<<enem.atthp<<" HP";
    				cout<<you;
    			}
    		}
    		else if (res==3)
    		{
    			cout<<"I defend"<<endl;
    			if (enem.stamina >= you.stamina)
    			{
    				cout<<"But you fail!";
    				cout<<"He attacks"<<enem.atthp<<" of your life points"<<endl;
    				you.AttLost(enem.atthp);
    				enem.StaLost(2);
    			}
    			else
    			{
    				cout<<"I defend"<<endl;
    				cout<<"Defend Successuful!!"<<endl;
    				enem.StaLost(2);
    			}
    		}
    		else 
    		{
    			int option;
    			cout<<"Select an Option"<<endl<<endl;
    			DisplayInventory();
    			cin>>option;
    			if (option == 1)
    			{
    				cout<<"Attack With Magic Rod!!"<<endl;
    				if ((you.stamina >= enem.stamina)&&(you.stamina >5))
    				{
    					enem.AttLost(5);
    					cout<<"He lost 5 hp point";
    				}
    				else if (you.stamina >= enem.stamina)
    				{
    					cout<<"Not enough Stamina"<<endl;
    				}
    				else if (you.stamina > 5)
    					cout<<"You missed"<<endl;
    				else
    					cout<<"Not enought Stamina"<<endl;
    			}
    			else
    				cout<<"Will be available soon"<<endl;
    		}
    		cout<<"Choose an option"<<endl;
    		cout<<"1.Attack Opponent"<<endl;
    		cout<<"2.Not Moving"<<endl;
    		cout<<"3.Defend"<<endl;
    		cout<<"4.Inventory"<<endl;
    		cin>>res;
        }
        if (enem.health<=0)
        {
           cout<<"YOU WIN!!"<<endl;
        }
        else
            cout<<"YOU LOST!!"<<endl;
    }
     
    
    void Digimon::AttLost(int lostHP)
    {
         health=health-lostHP;
    }
    
    void Digimon::StaLost(int lostSP)
    {
    	stamina=stamina-lostSP;
    }
    
    void ChooseCharacter(int&);
    void DefaultInventory();
    void InputInventory(string invent);         //Function prototype
    
    int main()
    {
    	int ans;
    	DefaultInventory();
    	Digimon obj;
    	ChooseCharacter(ans);
    	if (ans==1)
    	{
    		obj.SetValue("Guilmon", 25, 10, 4);
    		cout<<obj;
    	}
    	else if (ans==2)
    	{
    		obj.SetValue("Renamon", 20, 20, 2);
    		cout<<obj;
    	}
    	else
    	{
    		obj.SetValue("Terriermon", 22, 15,3);
    		cout<<obj;
    	}
        int dest;
        cout<<"Choose your destination"<<endl;
        cout<<"1. Evil Village"<<endl;
        cout<<"2. Darkness Forest"<<endl;
        cout<<"3. Rubymon's Dungeon"<<endl;
        cout<<"4. The Shadow Realm"<<endl;
        cin>>dest;
        if (dest==1)
        {
         EvilVillage(obj);
        }
         
         
    	/*cout<<"You found a magic rod!!"<<endl;
    	InputInventory("MAGICROD");
    	cout<<"You found a magic stone"<<endl;
    	InputInventory("STONE");
        cout<<"\nYou encounter a crazy stupid yet powerful digiMONSTER!!"<<endl;
        cout<<"IT is RUBYMON!!!"<<endl;
    	Digimon enemy1("Rubymon", 12, 22, 2);
        Play(enemy1, obj);                      
        cout<<obj;
        cout<<enemy1;*/
    	return 0;
    }
    
    void ChooseCharacter(int& ans)
    {
    	cout<<"Welcome to The Digital World"<<endl;
    	cout<<"Please choose your favorite digimon to travel with you!"<<endl;
    	cout<<"1.Guilmon"<<endl;
    	cout<<"2.Renamon"<<endl;
    	cout<<"3.Terriermon"<<endl;
    	cin>>ans;
    }
    
    void DefaultInventory()
    {
    	for (int k=0;k<5;k++)
    		strcpy(inventory[k], "??");
    }
    void InputInventory(string invent)
    {
    	static int i=0;
    	strcpy(inventory[i], invent);
    	i++;
    }
    
    void DisplayInventory()
    {
    	for (int j=0;j<5;j++)
    		cout<<j+1<<". "<<inventory[j]<<endl;
    }
    
    void EvilVillage(Digimon& obj)         //Friend Function
    {
         int dec1;
         cout<<"You are now walking in Evil Village....there are no people but old and abandoned house...";
         cout<<"\nUnfortunately, all the door are lock except the big house in front of you, which has no front door";
         cout<<"Enter in or not?"<<endl<<endl;
         cout<<"1.Yes ENTER IN NOW!!";
         cout<<"\n2.No I am afraid"<<endl;
         cin>>dec1;
         if (dec1==1)
         {
            DigmonHouse(obj);
         }
            
    }
    
    void DigmonHouse(Digimon& obj)             //Friend Function
    {
         cout<<"When you enter into the house, you see an empty table and a big television"<<endl;
         cout<<"It seem that nobody are here...SUDDENTLY"<<endl;
         cout<<"The TV turns on automatically...and you see a Digimon";
         cout<<"He says: YOU ARE THE CHOSEN ONE...OUR VILLAGE IS DESTROY BY THE EVIL DIGIMON 'RUBY'";
         cout<<"WHO EAT ALL MY PEOPLE AND DESTROY OUR VILLAGE POLUTE OUR WORLD AND DESTROY OTHERS!!";
         cout<<"He says: My name is DIGMON, i am currently using my magic power to communicate with you through this TV..";
         cout<<" DIGMON: I am now in a safe place(somewhere in the digital World), but you gotta help to save the digital world";
         cout<<" DIGMON: The Evil Ruby will DESTROY the whole Digital World, so you gotta fight him and save the digital world..";
         cout<<"....beep....beep"<<endl<<endl;
         cout<<"Digmon dissappear in TV and the TV turns off...."<<endl;
         cout<<"I say: What can we do now?"<<endl;
         cout<<obj.name<<" says: "<<"well, i think he says that we are the chosen one, so we must destroy RUBY and SAVE THE DIGITAL WORLD!!"<<endl;
         cout<<"I say: Well, what are we waiting for?....LET'S GO!!"<<endl;
    }
    Thanks

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You're gonna have to write your own function to output text character-by-character or word-by-word.

    For clearing the screen, why don't you people read the FAQ?
    Last edited by XSquared; 05-26-2003 at 08:28 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    sorry X too funny Click your link.... its not a 404 but its something just as funny for when you tell someone to read the fAq :P

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Heh. Oops.

    Fixed the link.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    ok thanks, but also, i would like to know how to display a text and then let the user press any key(Without display the message Press any key to continue), then it clear the text and display the following text....

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Again, read the FAQ. Just don't output the 'Press a key to continue' message.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    yeah you dont have to post your entire program either..takes up room

    you can try something like


    void printmessage(const char *msg){

    while(*msg){
    cout<<*msg;
    Sleep(/*time in miliseconds between each letter*/);
    *msg++;
    }
    }

    for Sleep() you need windows.h. but something like that

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    thanks "the wookie",
    but i don't know how to get your funtion work...:

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    void printmessage(const char *);
    
    int main(void)
    
    {
    cout << "HI everybody, i have a cow"<< endl;
    cout << "Whoo Hoo, DONUT"<< endl;
    return 0;
    }
    
    void printmessage(const char *msg)
    {
    	while(*msg)
    	{
    	cout<<*msg;
    	Sleep(1000);
    	*msg++;
    	}
    }
    Thanks

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You can easily write your own sleep function.

    Code:
    #include <ctime>
    
    void sleep( long millis ) 
    {
      long t = clock( );
    
      #ifdef _MSC_VER
          millis *= CLOCKS_PER_SEC / 1000;
      #else
          millis *= CLOCKS_PER_SECOND / 1000;
      #endif
      
      while( clock( ) < t + millis )
        t = t;
      
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    With the Wookie's example you have to use printmessage( "whatever" ); instead of cout.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    Thanks XSquared,
    but i don't understand your function...(too difficult for me)

    however i start get "the wookie" 's program work, please take a look.

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    void printmessage(const char *);
    
    int main(void)
    
    {
    printmessage("HI everybody, i have a cow");
    cout<<endl;
    printmessage( "Whoo Hoo, DONUT");
    return 0;
    }
    
    void printmessage(const char *msg)
    {
    	while(*msg)
    	{
    	cout<<*msg;
    	Sleep(100);
    	*msg++;
    	}
    }
    The problem with this is, it display the whole line, and around 5 seconds later, it display the other line.

    But what i want is display each word every a period of time and not the whole line....

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    In that case, you're gonna have to scan through and print from space to space.

    Code:
    void printmessage(const char *msg)
    {
    	while(*msg)
    	{
    		while( *msg && *msg != ' ' )
    		{
    			cout<<*msg;
    			*msg++;
    		}
    		cout<<flush;
    		
    		Sleep(100);
    		while( *msg && *msg == ' ' )
    		{
    			cout<<*msg;
    			*msg++;
    		}
    		
    	}
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    Smile

    Thank Everyone, it works now

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    if some of you have time, could you please explain me line by line how the "printmessage" function work?
    Especially this line:
    cout<<flush;

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 11:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21