how can i make a valor to a variable like:
how can i make this raise stregnth last only for a couple of minutes?Code:int x;
x = 0;
...
...
...
...
if (raisestrenght = "yes"){
x = x + 1;
Printable View
how can i make a valor to a variable like:
how can i make this raise stregnth last only for a couple of minutes?Code:int x;
x = 0;
...
...
...
...
if (raisestrenght = "yes"){
x = x + 1;
You could do it like this:
store the time when stength was raised
then if your program has a main loop you can check periodically to see if time has elapsed.Code:#include <time.h>
...
time_t StrengthRaised=time(NULL);
This will lower x in about 2 minutes.Code:if ((StrengthRaised+120)>=time(NULL))
x-=1;
thank youuu
damn i could not make this work here is the code that im trying to make it last for a determined time:
Code:dice = diceRoller(1,8);
cout << "\n You feel stronger\n";
cout << " Your strenght raised for "<<dice<<" points!\n\n";
StatStr = StatStr + dice;
PlyMagic = PlyMagic - 6;
}
here's an example
Code:#include <stdio.h>
#include <time.h>
time_t LastIncrease=0;
int IncreaseStrength(int s)
{
s+=10;
LastIncrease=time(NULL);
printf("Strength Increased to %d\n", s);
return s;
}
int LowerStrength(int s)
{
s-=10;
LastIncrease=0;
printf("Strength lowered to %d\n", s);
return s;
}
int main()
{
int Strength=10;
Strength=IncreaseStrength(Strength);
for (;;)
{
//do other things here
//do time check
if ((LastIncrease+5/*seconds*/)<=time(NULL))
{
Strength=LowerStrength(Strength);
break; //exit example
}
};
return 0;
}
That's a C example. :)
You can also use clock(), which is very similar to time(), except it returns 0 the first time it is called.
i surrender why isn that working? this is a function of a magic, that is the magic bless, the strenght is properly raised but i cant make it last for a determined time :/ i simply dont back to the normal status
Code:void Bless()
{
clock_t lastIncrease = 0;
lastIncrease = clock();
system("cls");
TopBorder();
StatBorder();
if (PlyMagic <6)
{
cout << "/n Nao ha magica suficiente!\n\n"<<endl;
}
else
{
dice = diceRoller(1,8);
cout << "\n Voce se sente mais forte!\n";
cout << " Sua forca aumentou por "<<dice<<" pontos!\n\n";
StatStr = StatStr + dice;
if ((lastIncrease + 50) <= clock()){
StatStr = StatStr - dice;
}
PlyMagic = PlyMagic - 6;
}
return;
}
It's pretty hard to play the game from the function and it's pretty hard to go back to main without the nessassary information. You're gonna have to return the dice roll, the time at which the spell was cast, the time it wears off and the magic loss.
As you get better at programming, you'll find things like this work much better in classes.
i already know a bit of classes, but how i could do that function as a class?
You don't do the function as a class, you do the function in a class. From looking at your level, right now, I'd say continue the way you're going and don't try classes, yet. It's best to get a good understanding of functions and other procedural programming before you try to get object-oriented. With the classes, though. What you would do is end up writing perhaps a class for spells, a class for players, a class for enemy creatures, a class for items. Things of that nature. You would probably go into inheritence with types of spells as well, fire, ice, lighting, etc. This way you can set base standard for all the spells like who they damage the most and what kind of player classes can use them.
As I said, though, stick with just plain 'ol functions for now. You're on the right track with what you have, you just need to make the connection between your main program and your functions.
ok ty ;)