Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
need help getting player/monster hitpoints to go down
Old 07-17-2010, 12:42 AM need help getting player/monster hitpoints to go down
Webmaster Talker

Posts: 611
Trades: 0
hi, im trying to use session variables to hold the dragon/player hit points. but they keep getting reset to 100 on each page load and only go down to 80 each time. any help greatly appreciated. (i couldnt figure out what to do with the globals. lol)

Code:
<?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>
silverglade is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-17-2010, 02:48 PM Re: need help getting player/monster hitpoints to go down
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Try only assinging the hitpoints to the session if it's not assigned.

if (!isset($_SESSION['playerHp'])) $_SESSION['playerHp'] = 100;
if (!isset($_SESSION['dragonHp'])) $_SESSION['dragonHp'] = 100;
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to need help getting player/monster hitpoints to go down
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15228 seconds with 12 queries