Jump to content

Help with PHP Sessions...


Far Cry

Recommended Posts

I there. I am making a small game, either you or the computer win depending on who's life hit 0 first I am using sessions to hold the health values, however I need a little bit of help. How do I make it actually go down after each move until one hits 0? Here is my script and thanks in advance...

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Fighting Game</title>
</head>
<body>
<form action="fighting_game.php" method="post">
<select name='move_choice' id='move_choice'>
<option value='punch'>Punch</option>
<option value='kick'>Kick</option>
</select>
<input type='submit' name='submitbtn' value='Continue' id='submitbtn'>
</form>
<div id='matchdiv'>
<?php
$_SESSION['ai_health'] = 100;
$_SESSION['player_health'] = 100;

$moves_ai = array("Computer punches you in the face!","Computer kicks you in the gut!");
$moves_player = array("You punch Computer in the face!","You kick computer in the gut!");
$move_damage = array(2,10);
$move_dam_multiplier_player = array(rand(1,5),rand(5,10));
$move_dam_multiplier_ai = array(rand(1,5),rand(5,10));
if($_POST['submitbtn']){
  $choice = $_POST['move_choice'];
    }

   if($choice == "punch"){
   $total_dam_ai=($move_damage[0]*$move_dam_multiplier_ai[0]);
   $total_dam_player=($move_damage[0]*$move_dam_multiplier_player[0]);
   
   echo"$moves_player[1]"." Causing ".$total_dam_player." damage!<br>";
       echo"Computers current health is ". ($_SESSION['ai_health']-$move_damage[0]*$move_dam_multiplier_player[0]);
   echo"<br>$moves_ai[1]"." Causing ".$total_dam_ai." damage!<br>";
       echo"Your current health is ". ($_SESSION['player_health']-$move_damage[0]*$move_dam_multiplier_ai[0]);

  
     }

	 elseif($choice == "kick"){
   $total_dam_ai=($move_damage[1]*$move_dam_multiplier_ai[1]);
   $total_dam_player=($move_damage[1]*$move_dam_multiplier_player[1]);
   
   echo"$moves_player[1]"." Causing ".$total_dam_player." damage!<br>";
       echo"Computers current health is ". ($_SESSION['ai_health']-$move_damage[1]*$move_dam_multiplier_player[1]);
   echo"<br>$moves_ai[1]"." Causing ".$total_dam_ai." damage!<br>";
       echo"Your current health is ". $new_player_health=($_SESSION['player_health']-$move_damage[1]*$move_dam_multiplier_ai[1]);
   

		 }
   if($_SESSION['ai_health']<=0 && $_SESSION['player_health']>=0){
	   echo"<br>Computer falls to the ground! He is knocked out! You win!";
	   }
	   
   if($_SESSION['player_health']<=0 && $_SESSION['ai_health'] >= 0){
	   echo"<br>You fall to the ground! You are knocked out! You lose!";
	   }
   if($_SESSION['player_health']<=0 && $_SESSION['ai_health'] <=0){
	   echo"<br>You both fall to the ground! You are both knocked out! It's a draw!";
	   }

?>
</div>
</body>
</html>

 

Link to comment
https://forums.phpfreaks.com/topic/222579-help-with-php-sessions/
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.