Sign in to follow this  
Denisb

Game Creating Help Again

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 .)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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"); }
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Denisb:

This isn't so much a solution, but a piece of advice that will make debugging a little easier.

When you are testing variables for their values, use the variable on the right side of the equality test.

Don't do this:

if (pick == 1)



Do this instead:

if (1 == pick)



And, when you are testing menu options that are numerical, you should use a range test.

Don't do this:

if (pick != 1 && pick != 2 && pick != 3 && pick != 4)



Do this instead:

if (pick > 0 && pick < 5)



The reason for these changes may or may not be apparent right now, but later on in your life as a programmer, you will see that its much better to code in this manner.

Good luck, and if you need anything don't hesitate to ask. :)

Share this post


Link to post
Share on other sites
Hi,

I think filipe is right and your stamina has gone down and what you meant to do was have and "else" not an "if" at the end of your program so that your refuel message is shown if they try to fight without enough stamina / health.

Share this post


Link to post
Share on other sites
Denisb,
filipe and Tsoski are right:


if(Stamina >= 100 && HP >= 100)
{
// ...
}

if(Stamina < 100 && HP < 100)
{
// ...
}




The opposite of a >= b && c >= d is a < b || c < d, or you could use the else statement:


if ( Stamina >= 100 && HP >= 100 )
{
// ...
}
if ( Stamina < 100 || HP < 100 )
{
// ...
}

// compare to:

if ( Stamina >= 100 && HP >= 100 )
{
// ...
}
else // Stamina is lower than 100 or HP is lower than 100
{
// ...
}


Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
Denisb,
filipe and Tsoski are right:

*** Source Snippet Removed ***

The opposite of a >= b && c >= d is a < b || c < d, or you could use the else statement:

*** Source Snippet Removed ***


Hm. The second source snippet wont work!
I tried it ( to see if it really worked ) and no can do. I have 95 HP and 100 Stamina and still can fight. So no sir to you!

here is my current EDITED code with the source snippet ( Second snippet over me )


#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");
}
}
else
{
cout << "Refill your Stamina and HP to max please!" << endl;
system("PAUSE");
}

//system("pause");
}
system("CLS"); }
}




and here is my working one with a *bug* ( Fixed ).


#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");
}

/*if(Stamina < 100 || HP < 100)
{
cout << "Refill your Stamina and HP to max please!" << endl;
system("PAUSE");
*/


system("pause");
}
}
else
if(Stamina < 100 || HP < 100)
{
cout << "Refill your Stamina and HP to max please!" << endl;
system("PAUSE");
}
}

//system("pause");

system("CLS");
}
}










EDIT: How do i save?
When the player types in his Username or when he go up a level. Even when he exits the game. I want it to save the files and data. And next time he start the game he will get loaded from his last position. I want to know that :) Anybody?


[Edited by - Denisb on July 25, 2009 4:54:11 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Denisb
Hm. The second source snippet wont work!
I tried it ( to see if it really worked ) and no can do. I have 95 HP and 100 Stamina and still can fight. So no sir to you!

here is my current EDITED code with the source snippet ( Second snippet over me )

*** Source Snippet Removed ***

and here is my working one with a *bug* ( Fixed ).

*** Source Snippet Removed ***





EDIT: How do i save?
When the player types in his Username or when he go up a level. Even when he exits the game. I want it to save the files and data. And next time he start the game he will get loaded from his last position. I want to know that :) Anybody?


Denisb, I think you missed the point here. In plain english, you want Stamina AND HP to be maxed out, right? In C++ you want
(Stamina >= 100 && HP >= 100)

Look at your code. The condition for being able to fight now is
if(Stamina >= 100 || HP >= 100)

See the difference? You're asking for either one of those expressions to be true. If one of the tested variables is at 100, that code will run.

Once you correct that you can use an else statement. Your else statement will be executed if either HP or Stamina doesn't meet the conditions marked above (>= 100), which is (logically) exactly the same as writing
(Stamina < 100 || HP < 100)

Hope this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by filipe
Quote:
Original post by Denisb
Hm. The second source snippet wont work!
I tried it ( to see if it really worked ) and no can do. I have 95 HP and 100 Stamina and still can fight. So no sir to you!

here is my current EDITED code with the source snippet ( Second snippet over me )

*** Source Snippet Removed ***

and here is my working one with a *bug* ( Fixed ).

*** Source Snippet Removed ***





EDIT: How do i save?
When the player types in his Username or when he go up a level. Even when he exits the game. I want it to save the files and data. And next time he start the game he will get loaded from his last position. I want to know that :) Anybody?


Denisb, I think you missed the point here. In plain english, you want Stamina AND HP to be maxed out, right? In C++ you want
(Stamina >= 100 && HP >= 100)

Look at your code. The condition for being able to fight now is
if(Stamina >= 100 || HP >= 100)

See the difference? You're asking for either one of those expressions to be true. If one of the tested variables is at 100, that code will run.

Once you correct that you can use an else statement. Your else statement will be executed if either HP or Stamina doesn't meet the conditions marked above (>= 100), which is (logically) exactly the same as writing
(Stamina < 100 || HP < 100)

Hope this helps.



I know i found it :S

Thanks to.

But does anybody know how i can save the levels and username?

Share this post


Link to post
Share on other sites
Quote:
Original post by Denisb
I know i found it :S

Thanks to.

But does anybody know how i can save the levels and username?


You're welcome.

If you want to learn about saving and reading files, look for information on ifstream and ofstream. Those are, respectively, input and output file streams, and you'll need to know how to use them to do what you plan.

Share this post


Link to post
Share on other sites
Quote:
Original post by Denisb

EDIT: How do i save?
When the player types in his Username or when he go up a level. Even when he exits the game. I want it to save the files and data. And next time he start the game he will get loaded from his last position. I want to know that :) Anybody?

But does anybody know how i can save the levels and username?


Step 1. learn File I/O look at the docs for fopen fclose fread fwrite
Step 2. create a struct that holds your game data
Step 3. open the savegame file
Step 4. write the game data to it
Step 5. close the savegame file

There.

Share this post


Link to post
Share on other sites
Quote:
Original post by Developer_X
Quote:
Original post by Denisb

EDIT: How do i save?
When the player types in his Username or when he go up a level. Even when he exits the game. I want it to save the files and data. And next time he start the game he will get loaded from his last position. I want to know that :) Anybody?

But does anybody know how i can save the levels and username?


Step 1. learn File I/O look at the docs for fopen fclose fread fwrite
Step 2. create a struct that holds your game data
Step 3. open the savegame file
Step 4. write the game data to it
Step 5. close the savegame file

There.


Thanks all but. How do i make it load the game :O
I'm stupid hehe :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Denisb
Thanks all but. How do i make it load the game :O

This is how you could do it.
This is the struct that will be used to hold the game info for the save and the load functions:


struct status
{
string name;
int gold;
int TP; // Time played
};




Then I made a function (in my game it's a class function, so you'll have to implement it the way you want) called Game::gSave():


void Game::gSave()
{
status game; // class of type status, look up

game.name = hName;
game.gold = gGold;
game.TP = gTP;

ofstream save("game.sav", ios::out | ios::binary);

if (!save)
{
cout << "\nCan't open the file for saving!\n";
}
else
{
save.write((char *) &game, sizeof(status));

cout << "\nGame saved!\n";

save.close();
}
}




And here comes the the source for loading the game:


void Game::gLoad()
{
status game;

ifstream load("game.sav", ios::in | ios::binary);

if (!load)
{
cout << "\nCan't open the file for loading!\n";
}
else
{
load.read((char *) &game, sizeof(status));

hName = game.name;
gGold = game.gold;
gTP = game.TP;

cout << "\nThe following game stats are loaded:\n";
cout << "Name: " << hName << "\n";
cout << "Gold: " << gGold << "\n";
cout << "Time played: " << gTP << "\n";

load.close();
}
}




If you got any questions, just ask :-D

Share this post


Link to post
Share on other sites
Quote:
Original post by ssj4leon
Quote:
Original post by Denisb
Thanks all but. How do i make it load the game :O

This is how you could do it.
This is the struct that will be used to hold the game info for the save and the load functions:

*** Source Snippet Removed ***

Then I made a function (in my game it's a class function, so you'll have to implement it the way you want) called Game::gSave():

*** Source Snippet Removed ***

And here comes the the source for loading the game:

*** Source Snippet Removed ***

If you got any questions, just ask :-D


Allright thanks. Going to test it later :)

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