Jump to content

Recommended Posts

Well i am trying to make a battle system using the following code:

[code]<?php

if ($health < 1)
{
echo "you do not have enough life to do this";

die();
}

//battle forumula for player

$rand=rand(1,10);
$rand=$rand;
$damage=($power2*$rand-$dex);
$truedam=($damage/2);
$newhealth=($health-$truedam);
$sql2="UPDATE players SET health='$newhealth' WHERE user='$user'";
if(mysql_query($sql2))

//battle formula for dragon

$rand2=rand(1,10);
$rand2=$rand2;
$damage2=($power*$rand2-$dex2);
$truedam2=($damage/2);
$newhealth2=($health2-$truedam2);
$sql2="UPDATE players SET health='$newhealth2' WHERE user='Dragon'";
if(mysql_query($sql2))

echo "Dragon Hit You for $damage ($newhealth)<br>";

echo "You hit dragon for $damage2 ($newhealth2) <br>";


?>[/code]

now what i would like to do is make it so it does it in rounds, so it repeats the code until newhealth is less than 1. i just dont know how to loop it if you get what i mean. i would like it to look like:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--][b]Round 1:[/b]

Dragon hit you for $damage ($newhealth)
You hit dragon for $damage2 ($newhealth2)

[b]Round 2:[/b]

Dragon hit you for $damage ($newhealth)
You hit dragon for $damage2 ($newhealth2)[/quote]

but ontop of that i would like it to randomly choose which person hits first using a a stat called Speed this would be / by 100 and * 10 to give it a percent chance.


any help would be appreciated.
Link to comment
https://forums.phpfreaks.com/topic/10182-lopping/
Share on other sites

ok, here's a psueduocode example of what you want to do... i'm not giving you any actual code (except for a loop recomendation). just fill in the needed code where the comments are, and you should be off to a good start:

[code]
while (/* Player Health > 0 AND Enemy Health > 0 */)
{
  // Figure out who hits first (keep in mind that the opponent
  // can only strike back if he still has more than 0 HP).

  // First Strike

  // If opponent's health is greater than 0, you haven't killed
  // them yet, so do Second Strike now
  
}
[/code]

that's really all there is to the logic. i'd recommend you write a class that handles all the attacking and defending logic within it so you can simply do something like this for your loop:
[code]
while ($player->health > 0 && $dragon->health > 0)
{
  if ($first == 1) {
    $player->attack();
    if ($dragon->health > 0) $dragon->attack();
    else continue;
  } else {
    $dragon->attack();
    if ($player->health > 0) $player->attack();
    else continue;
  }
}
[/code]

hope this helps.
Link to comment
https://forums.phpfreaks.com/topic/10182-lopping/#findComment-37945
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.