Stats Not Properly Working [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Stats Not Properly Working [Resolved]

Post by Epiales »

Okay, the issue now is that when you are in a fight, you have, let's say, 2 hit points left. When you attack a creature/character, and they hit you for let's say, 5 hit points of damage, it doesn't take your 2 hitpoints down to 0, but still says your dead. So you always have those 2 hit points and can go right back and fight someone else over and over and over.

I tried adding something like this: if($newplayershp > 0){$newplayershp = 0;} , but it doesn't seem to be working.

I'm sure this is the code that I need to work with, but haven't figured anything out.

Code: Select all

if ($charactersattack > $playersdefense) {

    echo $characters . " hits! <br>";
    $charactersdamage = rand(1,6);
    $newplayershp = $playershp - $charactersdamage;
    echo "For " . $charactersdamage . " points of damage. <br>";

    if ($newplayershp < 0) {
    
    echo "The " . $user['username'] . " has been killed<br>";
    echo "<a href='index.php'>Continue</a>";
    exit;
}    
    $updateplayer = "update stats set hpoints='$newplayershp' where id='$id'";
    mysql_query($updateplayer) or die("Could not update player");
 
Also, the same with their turns as well, except the turns go into a negative number
Last edited by Epiales on Sun Sep 01, 2013 8:36 pm, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Stats Not Properly Working

Post by MikuzA »

you might want to add the update query also in if() where the player dies..

or just remove the exit;
depends on why you want to exit on that point.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stats Not Properly Working

Post by Epiales »

MikuzA wrote:you might want to add the update query also in if() where the player dies..

or just remove the exit;
depends on why you want to exit on that point.
Okay, I added another update query, but still doesn't work:

Code: Select all

$charactershp = $charactersinfo3['hpoints'];
        $charactersattack = rand(1,20) + $charactersattack;
        $playersdefense = rand(1,20) + $playersdefense;
        $charactersdamage = rand(1,6);
        $playershp = $stats['hpoints'];
        $newplayershp = $playershp - $charactersdamage;
        
        if($newplayershp > 0){$newplayershp = 0;}
        
        $updateplayer="update stats set hpoints=$newplayershp where id='$id'";
        
        exit;
}

    $updatecharacters = "update characters set hpoints='$newcharactershp' where name='$characters' limit 1";
    mysql_query($updatecharacters) or die("Could not update character"); 
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stats Not Properly Working

Post by Epiales »

Actually I forgot:

Code: Select all

mysql_query($updateplayer) or die("Could not update character");
But now it sets stats to zero after each battle.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stats Not Properly Working

Post by Epiales »

Okay, it's working now. I ended up with this code:

Code: Select all

$charactershp = $charactersinfo3['hpoints'];
		$charactersattack = rand(1,20) + $charactersattack;
		$playersdefense = rand(1,20) + $playersdefense;
		$charactersdamage = rand(1,6);
		$playershp = $stats['hpoints'];
		$newplayershp = $playershp - $charactersdamage;
		
		if($newplayershp > 0){$newplayershp = 0;}
		if($newplayershp < 0){$newplayershp = 0;}
		
		$updateplayer="update stats set hpoints=$newplayershp where id='$id'";
		mysql_query($updateplayer) or die("Could not update player");
Thank you for directing me in the way I should go. :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Stats Not Properly Working [Resolved]

Post by MikuzA »

Code: Select all

      if($newplayershp > 0){$newplayershp = 0;}
      if($newplayershp < 0){$newplayershp = 0;}
Question: Why do you have them both?
If players hp is above zero, it goes to zero doesn't really make sense since the player lives :D

try something like

Code: Select all

$newplayershp = $playershp - $charactersdamage;
      
      if($newplayershp > 0){ print "Good Lord! You manage to survive the mighty blast, now it's time to turn the tables!!"; }
      if($newplayershp < 0){$newplayershp = 0; print "Did you just hear something crack? That was your skull, can you see the light? Game over."; }
      
      $updateplayer="update stats set hpoints=$newplayershp where id='$id'";
      mysql_query($updateplayer) or die("Could not update player");

Oh, I just wanted to write some lousy sentences to keep myself assumed on a monday morning :shock:
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stats Not Properly Working [Resolved]

Post by Epiales »

MikuzA wrote:

Code: Select all

      if($newplayershp > 0){$newplayershp = 0;}
      if($newplayershp < 0){$newplayershp = 0;} 
Question: Why do you have them both?
If players hp is above zero, it goes to zero doesn't really make sense since the player lives :D

try something like

Code: Select all

$newplayershp = $playershp - $charactersdamage;
      
      if($newplayershp > 0){ print "Good Lord! You manage to survive the mighty blast, now it's time to turn the tables!!"; }
      if($newplayershp < 0){$newplayershp = 0; print "Did you just hear something crack? That was your skull, can you see the light? Game over."; }
      
      $updateplayer="update stats set hpoints=$newplayershp where id='$id'";
      mysql_query($updateplayer) or die("Could not update player");

 
Oh, I just wanted to write some lousy sentences to keep myself assumed on a monday morning :shock:
The reason I have if($newplayershp > 0){$newplayershp = 0;} is because if you have 2 hitpoints left, and you are hit for 5 hit points in damage, you don't lose those 2 hit points. It says you are dead, but you still have 2 hit points left. So you can just go right back and fight again with those 2 hit points left lol.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Stats Not Properly Working [Resolved]

Post by MikuzA »

Epiales wrote: The reason I have if($newplayershp > 0){$newplayershp = 0;} is because if you have 2 hitpoints left, and you are hit for 5 hit points in damage, you don't lose those 2 hit points. It says you are dead, but you still have 2 hit points left. So you can just go right back and fight again with those 2 hit points left lol.
But with your current code, let's say you have hitpoints at 10, you get hit with 5 dmg, it results your hp's at 0 due your IF there.
The problem why that happened was because you didn't have the mysql_update happening when you die.

But with the current setup of if-brackets it works fine.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stats Not Properly Working [Resolved]

Post by Epiales »

MikuzA wrote:
Epiales wrote: The reason I have if($newplayershp > 0){$newplayershp = 0;} is because if you have 2 hitpoints left, and you are hit for 5 hit points in damage, you don't lose those 2 hit points. It says you are dead, but you still have 2 hit points left. So you can just go right back and fight again with those 2 hit points left lol.
But with your current code, let's say you have hitpoints at 10, you get hit with 5 dmg, it results your hp's at 0 due your IF there.
The problem why that happened was because you didn't have the mysql_update happening when you die.

But with the current setup of if-brackets it works fine.
Actually it works perfect the way I have it. It will take you all the way to 1 hitpoint, and if you get hit for 4 damage, it will put you at zero and say you have lost. So it keeps it from staying positive or going negative. Again, thanks for pointing out that I needed to update in a certain spot. :P
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”