Jump to content

[SOLVED] What can I post?


Cheeseweasel

Recommended Posts

Okay, here's what I want as I don't fully understand what you're suggesting.

 

I am trying to make a quick 'RPG'.

It will work like so;

  • The player is 'man'. The so-called 'CPU' is 'dragon'.
  • Each has 'health' - man's health is a variable called $manhealth and dragon's health is a variable called $dragonhealth.
  • Each 'round', a random number (see below for more detail) is drawn for the damage which man and dragon do towards eachother.
  • Two options are available for the 'attack' which the player chooses; 'SWORD', which can do between 5 and 10 damage, and 'BOW/ARROW', which can do between 0 and 20 damage.
  • The dragon will always do between 0 and 15 damage each round.
  • The 'attack' is choosable via a drop-down menu and 'submit' button.
  • There are two files involved; rpg2.php and action.php, the latter being the one in which the damage is actually done.

 

From these notes, could someone please draft up just a few lines of code? All I need is:

 

  • A form - a drop-down menu which has two options and POSTs to action.php (to be used in rpg2.php)
  • An echo saying the name of the attack you have used (to be used in action.php)

Thanks

-Cheeseweasel

Link to comment
Share on other sites

<form name="form1" id="form1" method ="post" action="action.php">
<select name="attack">
  <option value ="sword">SWORD</option>
  <option value ="bow">BOW/ARROW</option>
</select>
<input type="submit" name="Submit2" value="Attack">
</form>

 

on your action.php

 

<?php
$attack = $_POST['attack']; 

if($attack == "bow")
{
//do whatever you want to do
}
elseif($attack == "sword")
{
// do whatever you want to do
}
else
{
//throw an error because you got here without using other page or the post got lost
}
?>

Link to comment
Share on other sites

and if you want to pass the two variables of $manhealth and $dragonhealth use two hidden fields like this

 

<form name="form1" id="form1" method ="post" action="action.php">
<select name="attack">
  <option value ="sword">SWORD</option>
  <option value ="bow">BOW/ARROW</option>
</select>
<input name="dragon" type="hidden" id="dragon" value="<?php echo $dragonhealth; ?>">
<input name="man" type="hidden" id="man" value="<?php echo $manhealth; ?>">
<input type="submit" name="Submit2" value="Attack">
</form>

 

and extract that information on your action.php by

<?php
$attack = $_POST['attack']; 
$dragonhealth = $_POST['dragon'];
$manhealth = $_POST['man'];

if($attack == "bow")
{
echo "Its a Bow and Arrow attack";
}
elseif($attack == "sword")
{
echo "Its a Sword attack";
}
else
{
echo "You got here without using the correct page you cheat";//try just typing the action.php without using the form
}
?>

Link to comment
Share on other sites

THE WHOLE THING:

 

<form action="action.php" method="POST">
  <div align="center">
    <p>Choose your attack from the drop-down menu.<br>
      <select name="weapon" id="weapon">
        <option value="sword">Sword (5-10 Damage)</option>
        <option value="bow">Bow + Arrow (0-20 Damage)</option>
      </select>
      <br>
      <input name="submit" type="submit" value="Play Round">
    </p>
  </div>
  </form>

  <?PHP
$manhealth = 100;
$dragonhealth = 100;

echo "Your Health = $manhealth";
echo "Dragon's Health = $dragonhealth";

$attack = $_POST['attack']; 

if($attack == "bow")
{
echo "$attack" //do whatever you want to do
};
elseif($attack == "sword")
{
// do whatever you want to do
}
else
{
//throw an error because you got here without using other page or the post got lost
}
?>
</center>

Link to comment
Share on other sites

if($attack == "bow")

{

echo "$attack" //do whatever you want to do

};

elseif($attack == "sword")

{

// do whatever you want to do

}

else

{

//throw an error because you got here without using other page or the post got lost

}

 

the semi colon after the curly brace should be after

 echo "$attack"

not after the closing brace so it should look like this

 

if($attack == "bow")
{
echo "$attack" ;//do whatever you want to do
}
elseif($attack == "sword")
{
// do whatever you want to do
}
else
{
//throw an error because you got here without using other page or the post got lost
}

Link to comment
Share on other sites

Just place all your code in one file and use the same page for the form and the processing. Like so:

 

<?php

function showAttackForm($player_hp, $cpu_hp, $weapons){
echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">
	<table width="200" border="1">
	<tr>
	<td>Player HP </td>
	<td>Dragon HP </td>
	</tr>
	<tr>
	<td><font color="green">'.$player_hp.'</font></td>
	<td><font color="red">'.$cpu_hp.'</font></td>
	</tr>
	</table>
	<p>Choose your weapon:<br>
	<select name="playerweapon_choose">';

for($i=0;$i < count($weapons);$i++){
	echo '<option value="'.$i.'">'.$weapons[$i]['name'].' ('.$weapons[$i]['min'].'-'.$weapons[$i]['max'].' dmg)</option>';
}

echo '
	</select>
	</p>
	<p>
	<input type="hidden" name="playerhealth" value="'.$player_hp.'">
	<input type="hidden" name="cpuhealth" value="'.$cpu_hp.'">
	</p>
	<p>
	<input type="submit" name="submit" value="Attack!">
	</p>
	</form>';
}

// Set the default hitpoints for player and cpu
$default_playerhealth = 50;
$default_cpuhealth = 50;

// Set damage variables for cpu
$cpudamage = array();
$cpudamage['name'] = "Fire Breath";
$cpudamage['min'] = 0;
$cpudamage['max'] = 15;

// Set damage varaibles for player
$playerweapons = array();
$playerweapons[0]['name'] = "Bow & Arrow";
$playerweapons[0]['min'] = 0;
$playerweapons[0]['max'] = 20;
$playerweapons[1]['name'] = "Sword";
$playerweapons[1]['min'] = 5;
$playerweapons[1]['max'] = 10;

if($_POST['submit']){
// Get the health levels for player and cpu
$playerhealth = $_POST['playerhealth'];
$cpuhealth = $_POST['cpuhealth'];

// Get the player damage and weapon name for the round
$playerweapon_round = $playerweapons[$_POST['playerweapon_choose']]['name'];
$playerdamage_round = rand($playerweapons[$_POST['playerweapon_choose']]['min'],$playerweapons[$_POST['playerweapon_choose']]['max']);

// Calculate the cpu damage for the round
$cpudamage_round = rand($cpudamage['min'], $cpudamage['max']);

// Apply the damage to the player and cpu
$playerhealth = $playerhealth - $cpudamage_round;
$cpuhealth = $cpuhealth - $playerdamage_round;

echo '<div align="left"><font color="green">You attacked with the '.$playerweapon_round.' and did '.$playerdamage_round.' damage.</font></div>
      <div align="left"><font color="red">The dragon attacked you with '.$cpu_damage['name'].' and did '.$cpudamage_round.' damage.</font></div>';

// Check if player or cpu reached 0 hitpoints
if($playerhealth <= 0 || $cpuhealth <= 0){
	// Check who died
	if($playerhealth <= 0){
		echo '<div align="left"><strong><font color="red">YOU HAVE DIED!</font></strong></div>';
	} else {
		echo '<div align="left"><strong><font color="green">YOU HAVE SLAIN THE DRAGON!</font></strong></div>';
	}

	echo '<div align="left"><a href="'.$_SERVER['PHP_SELF'].'">Start a new battle!</a></div>';		
} else {      
	// Show the attack form for another round
	showAttackForm($playerhealth, $cpuhealth, $playerweapons);
}
} else {
// Just show the attack form
showAttackForm($default_playerhealth, $default_cpuhealth, $playerweapons);
}
?>

 

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.