Jump to content

Recommended Posts

hi, i have a small game script i made (i know it has a ton of globals sorry)but i cant get the player or the dragons hit points to go down they keep going back to 100 each time the page loads. is there a way i can get the script to "remember" their hit points? any help greatly appreciated. here is the code.

 

<?php

//The objects

define('PLAYER_NAME','Derek');//constant unchanging name of player
global $playerHp;
global $dragonHp;

$playerHp=100;   // starting HP for dragon and player.
$dragonHp=100;

global $playerDmg;
global $dragonDmg;
global $dragonHit;
global $playerHit;
global $dragonSlain;
global $playerSlain;
global $dragonMiss;
global $playerRoll;
global $dragonRoll;
global $playerMiss;
global $dragonMiss;
global $gameOver;

$playerDmg=20;
$dragonDmg=20;//default damage for player and dragon


//echo '<pre>'. print_r($_POST, 1).'</pre>';

if(!empty($_POST['playerAttack']))//if player attack button pressed do the following
{           $warrior= "<img src='images/warrior_t.jpg'/><br /><br />";
         $playerRoll=rand(1,20); //roll for the player's turn  
		 $echoPlayerRoll= "<br /> player roll ".$playerRoll."<br />";
		 if($playerRoll >= 11)
			 {  
			 	$dragonHit="<br/> You hit the dragon for ". $playerDmg." hit points!<br />";//hit or miss based on player roll.
			    $dragonHp=$dragonHp-$playerDmg;//subtract the damage the player did to dragon
			    $echodragonHp="<br /> dragon hp:".$dragonHp."<br />";
			 }  
		    

			 if($playerRoll<=10) // if low roll, you missed
			 {
			 	$playerMiss= "<br /> Sorry , you missed the dragon!<br />";
			 }
			 if($dragonHp<=0)
		     {   echo "<img src='images/torch1_t.jpg'<br /><br />";
		    	$dragonSlain="Congratulations ".PLAYER_NAME.". You have slain the dragon and won the game!";
		    	$gameOver='true';
		     }
              
		 	$playerRoll=0;//reset playerRoll
}

if(!empty($_POST['dragonAttack']))
{
             $dragon= "<img src='images/dragon_t.jpg'/><br /><br />";
	 	 $dragonRoll=rand(1, 20); //roll for the player's turn
	  	 $echodragonRoll= "<br /> dragon roll ".$dragonRoll."<br />";
		 if($dragonRoll  >= 11)//if high roll, hit
		 {
	 	    
		 	 $playerHit="<br/> You have been hit by the dragon for ". $playerDmg." hit points!<br />";//hit or miss based on player roll.
		     $playerHp=$playerHp-$dragonDmg;//subtract the damage the player did to dragon
                 $echoPlayerHp="<br /> Player hp:".$playerHp."<br />";     
		 }

	     if($dragonRoll <=10)
	     {
	       $dragonMiss="Wow the dragon missed you!<br />";

	     }
		if($playerHp<=0)
	     {  
	     	echo "<img src='images/skull_t.jpg'/><br /><br />";
	    	$playerSlain="<br />Sorry ".PLAYER_NAME.". You have been slain the dragon and lost the game!<br />";
	    	$gameOver='true';
	     }

 		 $dragonRoll=0;//reset dragon roll

} 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<title>Dragon</title>
</head>
<body>

<p><h2>Derek's Dragon game</h2></p>
<br />
<br />
<?php if(!empty($echoPlayerRoll))
{
echo $warrior;
echo "<table>";
echo "<tr><td>".$echoPlayerRoll."</td></tr>";
}
if(!empty($echodragonRoll))
{
echo $dragon;
echo "<tr><td>".$echodragonRoll. "</tr></td>";
}

if(!empty($echoPlayerHp))
{
echo"<tr><td>".$echoPlayerHp."</tr></td>";
}

if(!empty($echodragonHp))
{
echo"<tr><td>".$echodragonHp."</tr></td>";
}
if(!empty($playerSlain))
{
echo"<tr><td>".$playerSlain."</tr></td>";
}
if(!empty($dragonSlain))
{
echo "<tr><td>".$dragonSlain."</tr></td>";
}
if($gameOver=='true')
{
echo "<tr><td> Now the game is over </tr></td>";
}
?>
<?php echo "<tr><td>" ?>
<?php echo $dragonHit;?> 
<?php echo $dragonSlain;?>
<?php echo $playerHit;?>  <!-- echos out logic messages -->
<?php echo $playerSlain;?>
<?php echo $playerMiss;?>
<?php echo $dragonMiss;?>
<?php echo "</tr></td>"?>
<?php echo "<tr><td>"?>
<form name="dereksform" method="post" action="">
<input "type="submit" name="playerAttack" value="Player attacks">
<br />
<input "type="submit" name="dragonAttack" value="dragon attacks"> 
<?php echo "</tr></td></table>"?>
</form>
<!-- encompasses form, doesnt display form upon gameOver. -->
</body>
</html>

Also, the global keyword only has meaning inside of a function definition and even then it should not be used. I'm guessing that the code you posted is not inside of a function, so you wasted some time typing all those lines with the global keyword and they should be removed since they don't do anything.

i tried to use session variables to hold the dragon and player hit points, but they never go below 80. it looks like they get reset to 100 each time the page loads. any more help greatly appreciated. thanks. here is the code now.

 

<?php

//The objects


//store session data
  session_start(); 
$_SESSION["playerHp"] = 100;

$_SESSION["dragonHp"] = 100;

define('PLAYER_NAME','Derek');//constant unchanging name of player
  

global $playerDmg;
global $dragonDmg;
global $dragonHit;
global $playerHit;
global $dragonSlain;
global $playerSlain;
global $dragonMiss;
global $playerRoll;
global $dragonRoll;
global $playerMiss;
global $dragonMiss;
global $gameOver;

$playerDmg=20;
$dragonDmg=20;//default damage for player and dragon


//echo '<pre>'. print_r($_POST, 1).'</pre>';

if(!empty($_POST['playerAttack']))//if player attack button pressed do the following
{           $warrior= "<img src='images/warrior_t.jpg'/><br /><br />";
         $playerRoll=rand(1,20); //roll for the player's turn  
		 $echoPlayerRoll= "<br /> player roll ".$playerRoll."<br />";
		 if($playerRoll >= 11)
			 {  
			 	$dragonHit="<br/> You hit the dragon for ". $playerDmg." hit points!<br />";//hit or miss based on player roll.
			    $_SESSION["dragonHp"]=$_SESSION["dragonHp"]-$playerDmg;//subtract the damage the player did to dragon
			    $echodragonHp="<br /> dragon hp:".$_SESSION["dragonHp"]."<br />";
			 }  
		    

			 if($playerRoll<=10) // if low roll, you missed
			 {
			 	$playerMiss= "<br /> Sorry , you missed the dragon!<br />";
			 }
			 if($_SESSION["dragonHp"]<=0)
		     {  
		     	session_destroy();
		    
		     	echo "<img src='images/torch1_t.jpg'<br /><br />";
		    	$dragonSlain="Congratulations ".PLAYER_NAME.". You have slain the dragon and won the game!";
		    	
		    	$gameOver='true';
		    	
		    	
		     }
              
		 	$playerRoll=0;//reset playerRoll
}

if(!empty($_POST['dragonAttack']))
{
             $dragon= "<img src='images/dragon_t.jpg'/><br /><br />";
	 	 $dragonRoll=rand(1, 20); //roll for the player's turn
	  	 $echodragonRoll= "<br /> dragon roll ".$dragonRoll."<br />";
		 if($dragonRoll  >= 11)//if high roll, hit
		 {
	 	    
		 	 $playerHit="<br/> You have been hit by the dragon for ". $playerDmg." hit points!<br />";//hit or miss based on player roll.
		    $_SESSION["playerHp"]=$_SESSION["playerHp"]-$dragonDmg;//subtract the damage the player did to dragon
                 $echoPlayerHp="<br /> Player hp:".$_SESSION["playerHp"]."<br />";     
		 }

	     if($dragonRoll <=10)
	     {
	       $dragonMiss="Wow the dragon missed you!<br />";

	     }
		if($_SESSION["playerHp"]<=0)
	     {  
	     	session_destroy();
	      
	     	
	     	echo "<img src='images/skull_t.jpg'/><br /><br />";
	    	$playerSlain="<br />Sorry ".PLAYER_NAME.". You have been slain the dragon and lost the game!<br />";
	    	$gameOver='true';
	    	 
	    	
	     }

 		 $dragonRoll=0;//reset dragon roll

} 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<title>Dragon</title>
</head>
<body>

<p><h2>Derek's Dragon game</h2></p>
<br />
<br />
<?php if(!empty($echoPlayerRoll))
{
echo $warrior;
echo "<table>";
echo "<tr><td>".$echoPlayerRoll."</td></tr>";
}
if(!empty($echodragonRoll))
{
echo $dragon;
echo "<tr><td>".$echodragonRoll. "</tr></td>";
}

if(!empty($echoPlayerHp))
{
echo"<tr><td>".$echoPlayerHp."</tr></td>";
}

if(!empty($echodragonHp))
{
echo"<tr><td>".$echodragonHp."</tr></td>";
}
if(!empty($playerSlain))
{
echo"<tr><td>".$playerSlain."</tr></td>";
}
if(!empty($dragonSlain))
{
echo "<tr><td>".$dragonSlain."</tr></td>";
}
if($gameOver=='true')
{
echo "<tr><td> Now the game is over </tr></td>";
}
?>
<?php echo "<tr><td>" ?>
<?php echo $dragonHit;?> 
<?php echo $dragonSlain;?>
<?php echo $playerHit;?>  <!-- echos out logic messages -->
<?php echo $playerSlain;?>
<?php echo $playerMiss;?>
<?php echo $dragonMiss;?>
<?php echo "</tr></td>"?>
<?php echo "<tr><td>"?>
<form name="dereksform" method="post" action="">
<input "type="submit" name="playerAttack" value="Player attacks">
<br />
<input "type="submit" name="dragonAttack" value="dragon attacks"> 
<?php echo "</tr></td></table>"?>
</form>
<!-- encompasses form, doesnt display form upon gameOver. -->
</body>
</html>

it looks like they get reset to 100 each time the page loads.

 

Yes, because you have two lines of code that unconditionally assign 100 every time the page is requested -

$_SESSION["playerHp"] = 100;

$_SESSION["dragonHp"] = 100;

 

You would need to add conditional logic so that they are only initialized once -

 

if(some_condition_in_your_script_when_to_initialize_the_variables){
    $_SESSION["playerHp"] = 100;
    $_SESSION["dragonHp"] = 100;
}

awesome thanks! i cant figure out where in my script to do the conditional. because each time it runs, its always going to make it 100 hitpoints. any more help greatly appreciated. please . sorry im a newb. thanks. derek

holy cow your logic is awesome! HAHAHA! IT WORKED! thank you so much for your time and help!!! this is great. my newbie game really works. hahah. awesome. here is what made it work thanks to you.

 

if(empty($_SESSION["playerHp"])&& empty($_SESSION["playerHp"]))
{
$_SESSION["playerHp"] = 100;

    $_SESSION["dragonHp"] = 100;

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.