Jump to content

Hello World 2!!


Kryllster

Recommended Posts

I am working on a turn based game and I am having some difficulty making the HP counter stop counting at the specified number here is some of the code;

 

<?php
session_start();
$player_hp = $_SESSION['player_hp'];
$turns = $_SESSION['turns'];
$turns = $turns - 1;
?>

 

and this is how I was trying to stop it.

<?php
if($total_hp < 30){
$player = $player_hp + 5;
echo "You Still Have More recovery to do!";
}
else {
echo "You are fully recovered!";
}
$_SESSION['turns'] = $turns;
$_SESSION['player_hp'] = $player_hp;
?>

I know this is simple but I'm having a heck of a time getting the HP to stop counting at 30.

Also here is the code which I use to refresh the page to count it out.

<form action="safe-spot.php" method="post">
<input type="hidden" name="sub">
<input type="submit" value="Click Here">

 

 

Thanks in advance,

Link to comment
https://forums.phpfreaks.com/topic/224556-hello-world-2/
Share on other sites

Can you go into a little more detail please? Is your problem that it is sometimes going above 30? If so, is it going above 34?

 

What are the $total_hp and $player variables supposed to represent? I would hazard a guess that this is the problem. This should work better, and will make the upper limit for HP hard:

 

<?php
session_start();
$_SESSION['turns']--;


if($_SESSION['player_hp'] < 30){

$_SESSION['player_hp'] += 5;



echo "You Still Have More recovery to do!";
}
else {


if($_SESSION['player_hp'] > 30) $_SESSION['player_hp'] = 30;
echo "You are fully recovered!";
}

?>

 

 

 

I've cut out the middle man here, as well. It now stores directly into the session.

Link to comment
https://forums.phpfreaks.com/topic/224556-hello-world-2/#findComment-1159981
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.