Jump to content

Player vs. Computer PHP game help


gcusson

Recommended Posts

hey guys im sure this is going to seem super easy for you but i am new at this and i am really struggling. Basically i am trying to make a simple game where the player clicks a radio button and presses submit and then it deducts points from the computer. Then the computer goes and it goes on like that until someone reaches 0. Right now however i am having 2 problems.

 

#1- When i choose an attack and i press submit it doesnt update the score, it will take it down the right amount of points but then once i attack again it doesnt update it from that score it just changes to the total number minus the attack points. I hope that makes sense but basically what i am trying to say is that the points dont update.

 

#2- the computer never attacks me. I dont know how to trigger it so it knows to attack me

 

i guess i will copy and paste the coding, sorry if it is a little long

 

<body>

<?php

extract($_POST);

$hp_computer = "200";

$hp_player = "200";

//the above coding is the computer and player hit points

if($player=="snarl")

  $player_attack_value=$hp_computer="2";

if($player=="ravage")

  $player_attack_value=$hp_computer="18";

if($player=="blast")

    $player_attack_value=$hp_computer="30";

//the above coding is the player attacks and player attack values

 

$computer_value=array("growl","bite","chew");

$computer=$computer_value[rand(0,sizeof($computer_values)-1)];

if($computer_value=="growl")

$computer_attack_value=$hp_player="1";

if($computer_value=="bite")

$computer_attack_value=$hp_player="15";

if($computer_value=="chew")

$computer_attack_value=$hp_player="25";

//the above coding is the computer attacks and the computer attack values

 

$hp_computer=$hp_computer-$player_attack_value;

$hp_player=$hp_player-$computer_attack_value;

//print_r($hp_computer);

//print_r($hp_player);

//the above coding is the effect of the attacks on the player and the computer

 

if($hp_computer<="0")

  echo "YOU WIN!";

if($hp_player<="0")

  echo "YOU LOSE! :(";

//the above coding is what happens if the player or the computer reach 0 hit points

 

      echo $hp_player;

  echo $hp_computer;

 

 

 

?>

 

 

<form action="default.php" method="post">

<p>

  <input name="player" type="radio" value="snarl" />

  Snarl</p>

<p>

  <input name="player" type="radio" value="ravage" />

  Ravage</p>

<p>

  <input name="player" type="radio" value="blast" />

 

        Blast      </p>

<p>

  <input type="submit" name="button" id="button" value="Submit" />

  <label></label>

</p>

</form>

 

</body>

</html>

 

thanks guys i appreciate your help

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.