0
I’m playing a little game where you simulate turn-based role-playing. I created two variables for the life of the player and the enemy, and then created two variables for their actions (if you fall into a certain number, you will do such an action).
The problem is that I want a defense action, which would reduce the attack received by half, but I don’t know how to put it. I tried to put a variable that would change in each different if, but this would cause the attack of one of them not to be updated, causing the character to receive the last number that was registered in the variable.
$player =100;
$enemy= 100;
while ($enemy > 0)
{
$ataque = rand(1, 4)
echo '
1 - ataque<br>
2- defesa <br>
3- tomar poção<br>
4- bola de fogo<br>
<input type="text" name="action">
<input type="submit" value="Enviar">" ';
if(isset($_GET["action"]))
{
$action= $_GET["action"];
if($action== 1)
{
$player = $player - 15;
echo "<br>você deu um ataque no seu inimigo. causando 15 de dano";
echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
}
if($action== 2)
{
$dano = $golpe/2;
$player = $player - $dano;
echo "<br>você se defendeu do ataque do inimigo, recebendo metade do dano";
echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
}
if($ataque == 1)
{
$golpe = 15;
echo "<br>ele te deu um ataque, causando 15 de dano.";
echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
}
if($ataque == 2)
{
$enemy = $enemy - 5;
echo "<br>Ele se defendeu, recebendo só metade do dano";
echo "<br>Você: ".$player."<br>Inimigo: ".$enemy;
}
I was aware of the mistakes, this because I was adding more things, as a healing option and a stronger attack, so it gave conflict with the option to block. But the code you wrote is excellent, thank you for your help.
– riki481