Game Creating Help Again

Started by
21 comments, last by Denisb 14 years, 8 months ago
Hi! Don't know if you remember me but I posted here for like 3 - 4 days. Okey i finished that game i asked for help. but now i want to do one even more advanced. So i coded a whole new game. It works fine and all. But the problem i have not learned is how to prevent the game to exit. Like. I pick '1' and then you 'eat'. After he have eaten the games comes up with ' What do you want to do now? ' message. But not only for 'eat'. Except it should be for the whole 'pick' statements. ( bad english so can't really explain C++ language in english :) ) PS: Feel free to correct me on something Okey here is my code. Also feel free to try the game to see waht i mean. Also if you are going to use the game code or something that i came up with. ( Dont know if there is something but IF there is ) Please give me creds :)

#include <iostream>
#include <windows.h>
#include <string>
#include <ctime>
using namespace std;

int main()
{
   int userInput = 0;
   int HP = 100;
   int Stamina = 100;
   int Gold = 1000;
   int pick = 0;

   cout << "Welcome to the first test of my RPG Game.\n";
   cout << "More features are coming, be tuned.\n\n\n\n";


   cout << "Stats:\n";
   cout << "HP: " << HP << endl;
   cout << "Stamina: " << Stamina << endl;
   cout << "Gold: " << Gold << endl;
   cout << "\n\n";

   //First QUESTION TO ASK THE PLAYER 
   cout << "Welcome to my store young travler!\n";
   cout << "What do you want to do?\n";
   cout << "1. Eat ( Gains 20% HP otherwise if you have 100% HP nothing happends. )\n";
   cout << "2. Sleep ( Gains 20% Stamina otherwise if you have 100% Stamina nothing happends. )\n\n";
   cin >> pick;

   if(pick == 1)
   {
	   if(HP < 100)
	   {
		   cout << "You just ate a big sandwhich which made your little tubby grow and you got 20% HP\n";
		   HP += 20;
		   cout << "HP:" << HP << endl;
		   system("pause");
	   }

	   if(HP >= 100)
	   {
		   cout << "You are full! You can't stand food anymore.\n";
	   cout << "HP:" << HP << endl;
	   system("pause");
	   }
   }

   if(pick == 2)
   {
	   if(Stamina < 100)
	   {
		   cout << "You have just sleept 1 hour and got 20% back of your Stamina.\n";
		   Stamina += 20;
		   cout << "Stamina:" << Stamina << endl;
		   system("pause");
	   }

	   if(Stamina >= 100)
	   {
		   cout << "You are fully awake and don't need any sleep\n";
		   cout << "Stamina:" << Stamina << endl;
		   system("pause");
	   }
   }

}




Hope you understand, and thanks Denis
Advertisement
Quote:Original post by Denisb
Hi! Don't know if you remember me but I posted here for like 3 - 4 days.

Okey i finished that game i asked for help. but now i want to do one even more advanced.

So i coded a whole new game. It works fine and all. But the problem i have not learned is how to prevent the game to exit.

Like.

I pick '1' and then you 'eat'. After he have eaten the games comes up with ' What do you want to do now? ' message. But not only for 'eat'. Except it should be for the whole 'pick' statements. ( bad english so can't really explain C++ language in english :) )

PS: Feel free to correct me on something

Okey here is my code.

Also feel free to try the game to see waht i mean. Also if you are going to use the game code or something that i came up with. ( Dont know if there is something but IF there is ) Please give me creds :)

(code here)

Hope you understand, and thanks
Denis


This is where you would use a while loop. Do a little research into those (searching the forums would be a great headstart) and come back if you need help setting up.
Quote:Original post by dragonhawk360
Quote:Original post by Denisb
Hi! Don't know if you remember me but I posted here for like 3 - 4 days.

Okey i finished that game i asked for help. but now i want to do one even more advanced.

So i coded a whole new game. It works fine and all. But the problem i have not learned is how to prevent the game to exit.

Like.

I pick '1' and then you 'eat'. After he have eaten the games comes up with ' What do you want to do now? ' message. But not only for 'eat'. Except it should be for the whole 'pick' statements. ( bad english so can't really explain C++ language in english :) )

PS: Feel free to correct me on something

Okey here is my code.

Also feel free to try the game to see waht i mean. Also if you are going to use the game code or something that i came up with. ( Dont know if there is something but IF there is ) Please give me creds :)

(code here)

Hope you understand, and thanks
Denis


This is where you would use a while loop. Do a little research into those (searching the forums would be a great headstart) and come back if you need help setting up.


Okey thanks I will try :)

I will post here once i fix it or need help with it .)
oh im so dumb. lol. My friend showed me how to do it and he just told me do this.

while(true)
{

PROGRAM CODE HERE

}

So yea I'm stupid hehe. Well beginners luck lol. No body can learn on some days


BUT! Now the game resets all the code... My gold goes from 2000 to 1000 And my stamina from 30% to 100%... I dont want taht. any way to fix it?
Quote:Original post by Denisb
oh im so dumb. lol. My friend showed me how to do it and he just told me do this.

while(true)
{

PROGRAM CODE HERE

}

So yea I'm stupid hehe. Well beginners luck lol. No body can learn on some days


BUT! Now the game resets all the code... My gold goes from 2000 to 1000 And my stamina from 30% to 100%... I dont want taht. any way to fix it?


You don't want ALL the code in the while(true) loop. Here's an example (not real code.)

int main(){     start variables     while (!quit) //quit is of type bool     {          Ask for input          use a switch to handle input          if said quit then "quit = true"     }}


Basically, you initialize variables, then ask for input. You could use a switch statement to handle what they input, and if they said 'q' for example, you set the variable quit to true, which will exit the loop.
ah!

Thanks.

Now i go nerd some more c++ book and try to finish my project!

Thanks dude!
Here is some actual code incase dragonhawk's example wasn't enough.

#include <iostream>using namespace std;int main(){   int userInput = 0;   int hp = 100;   int stamina = 100;   bool exit = 0;   cout << "Welcome to the first test of my RPG Game.\n";   cout << "More features are coming, stay tuned.\n\n\n\n";   cout << "Stats:\n";   cout << " hp: " << hp << "%" << endl;   cout << " stamina: " << stamina << "%" << endl;   cout << "\n\n";   do   {   cout << endl	    << "Welcome to my inn young traveler!\n" 	    << "What do you want to do?\n"	    << "1. Eat (Gains 20% hp if you're at full hp it does nothing) \n"            << "2. Sleep (Gains 20% stamina if you're at full stamina it does nothing) \n"		<< "3. Hit the shop keeper. \n"		<< "4. Exit \n";   cin >> userInput;   switch(userInput)   {   case 1:	   	   if(hp >= 100)	   {		   cout << endl << endl			    << "You're not hungry.\n"		        << "hp:" << hp << endl;	   } 	   else if(hp < 100)	   {		   cout << endl << endl			   << "You ate a big steak sandwich!\n";		   hp += 20;		   cout << "hp:" << hp << endl;	   }	   break;   case 2:	   	   if(stamina >= 100)	   {		   cout << endl << endl			    << "You don't need to sleep.\n"		        << "stamina:" << stamina << "%" << endl;	   } 	   else if(hp < 100)	   {		   cout << endl << endl 			    << "You hade a good rest!";		   stamina += 20;		   cout << "stamina:" << stamina << "%" << endl;	   }	   break;   case 3:	   cout << " You you think about  hitting the inn keeper, but suddenly get attacked \n"		   << " by a rabid grue! \n"		   << " Lost 25 hp! \n";		   hp -= 25;	   if( hp <= 0)	   {		   cout << endl			    << " YOU ARE DEAD! \n";		   cin.get();		   exit = 1;	   }	   break;   case 4:	   exit = 1;	   break;   }   }while( exit != 1);      return 0;}


I tried to keep it as simple as possible.

Edit:
Looks like the source tag messed with my spacing some.
Hehe thanks but i allready fixed it. But how do you get
to work?

PS: Im starting with 'Fight' Option now.
sorry for bumping it but i need help lol. This is better instead for making a new thread.

Anyways. I do not use switch and case thing. But i finished the game ( going to add more features ) but when i want to fight it dont let me.

I can start the game and fight. I can sleep and fight after wards and i can eat and fight afterwards. But when i go and mine Ores i can not fight.

Please try it and help me find the error :(
#include <iostream>#include <windows.h>#include <string>#include <ctime>using namespace std;int RandInt(int a,int b){   return a + rand() % (b - a + 1);}int main(){   //Seed rand() with current time   srand(unsigned(time(NULL)));   //x will be between 2 and 10, inclusive   int n = RandInt(60,160);   int x = RandInt(5,12);   int c = RandInt(15, 23);   int h = RandInt(50,60);//   int userInput = 0;   int HP = 100;   int Stamina = 100;   int Gold = 1000;   int pick = 0;   int Man = 60;   cout << "\n\n";   cout << "Welcome to the V1.2 of my RPG Game.\n";   cout << "More features are coming, be tuned.\n\n\n\n";while(true){   cout << "Stats:\n";   cout << "HP: " << HP << endl;   cout << "Stamina: " << Stamina << endl;   cout << "Gold: " << Gold << endl;   cout << "\n\n";   //First QUESTION TO ASK THE PLAYER    cout << "Welcome to my store young travler!\n";   cout << "What do you want to do?\n";   cout << "1. Eat ( Gains 20% HP otherwise if you have 100% HP nothing happends. )\n";   cout << "2. Sleep ( Gains 20% Stamina otherwise if you have 100% Stamina nothing happends. )\n";   cout << "3. Mine some ores. ( Takes 40% of your stamina. )\n";   cout << "4. Fight in the arena!\n";   cin >> pick;if(pick != 1 && pick != 2 && pick != 3 && pick != 4){	cout << "Ahh! Wrong, please type in one of those numbers you want!" << endl;	system("pause");}   if(pick == 1)   {	   if(HP < 100)	   {		   cout << "You just ate a big sandwhich which made your little tubby grow and you got 20% HP\n";		   HP += 20;		   if(HP > 100)		   {			   HP = 100;		   }		   cout << "HP:" << HP << endl;		   system("pause");	   }	   if(HP >= 100)	   {		   cout << "You are full! You can't stand food anymore.\n";	   cout << "HP:" << HP << endl;	   system("pause");	   }   }   if(pick == 2)   {	   if(Stamina < 99)	   {		   cout << "You have just sleept 1 hour and got 20% back of your Stamina.\n";		   Stamina += 20;		   if(Stamina >=100)		   {			   Stamina = 100;		   }		   cout << "Stamina:" << Stamina << endl;		   system("pause");	   }	   if(Stamina >= 100)	   {		   cout << "\nYou are fully awake and don't need any sleep.\n";		   cout << "So go and charm somebody or kill ^^\n";		   //cout << "Stamina:" << Stamina << endl;		   system("pause");	   }   }   if(pick == 3)   {	   if(Stamina >=50)	   {		   cout << "You mined some ores and sold them to the store.\n";		   cout << "Stamina Lost: 40%\n";		   cout << "HP Lost: " << x << endl; HP -= x;		   Stamina -= 40;		   cout << "Stamina:" << Stamina << endl;		   Gold += n;		   cout << "Total Gold: " << Gold << endl;		   system("pause");	   }	   if(Stamina < 40)	   {		   cout << "\nYou dont have enough of stamina to do anything. Execpt Eat and Sleep!\n";		   cout << "You better sleep! Otherwise you'll die!\n";		   //cout << "Stamina:" << Stamina << endl;		   system("pause");	   }   }	if(pick == 4)	{		if(Stamina >= 100 && HP >= 100)		{			cout << "Welcome to the arena fellow adventurer!" << endl;			cout << "1. Fight" << endl;			cin >> pick;			if(pick == 1)			{				system("CLS");				cout << "Let the fight begin!\n" << endl;				cout << "You are fighting a man!" << endl;				cout << "Man HP:" << h << endl;				cout << "Your HP:" << HP << endl;				system("pause");				cout << "You attack the man!" << endl; Man -= c;								cout << "You get hit by the man!" << endl; HP -= x;								cout << "Man HP:" << Man << endl;				cout << "Your HP:" << HP << endl;				if(Man <= 0)				{					cout << "You won!" << endl;					system("PAUSE");				}								if(HP <= 0)				{					cout << "You lost!" << endl;					cout << "But he did not finish you off!" << endl;					cout << "So you will start with 100hp!" << endl; HP += 100;					cout << "Your HP:" << HP << endl;					system("PAUSE");				}								system("pause");			}		}					if(Stamina < 100 && HP < 100)		{			cout << "Refill your Stamina and HP to max please!" << endl;			system("PAUSE");		}	}	system("CLS"); }	}
Quote:Original post by Denisb
sorry for bumping it but i need help lol. This is better instead for making a new thread.

Anyways. I do not use switch and case thing. But i finished the game ( going to add more features ) but when i want to fight it dont let me.

I can start the game and fight. I can sleep and fight after wards and i can eat and fight afterwards. But when i go and mine Ores i can not fight.

Please try it and help me find the error :(
*** Source Snippet Removed ***


I think the problem is that when you mine ore your stamina is reduced. Then, at option 4, you check for (Stamina < 100 && HP < 100). Since the HP is supposedly at 100, but the Stamina is not, that condition won't be satisfied and option 4 will do nothing.

You want to check for (Stamina < 100 || HP < 100) instead if you want both to be maxed out to allow fights.

This topic is closed to new replies.

Advertisement