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
Share on other sites

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.