Jump to content



  • Please log in to reply
2 replies to this topic

#1 supanoob

  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 22 May 2006 - 01:58 PM

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


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


//battle forumula for player

$sql2="UPDATE players SET health='$newhealth' WHERE user='$user'";

//battle formula for dragon

$sql2="UPDATE players SET health='$newhealth2' WHERE user='Dragon'";

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

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


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--]Round 1:

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

Round 2:

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.

#2 V-Man

  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationVermont

Posted 22 May 2006 - 02:01 PM

Take a look at the for() and while() loops. See the PHP manual as well as the tutorials here at PHP Freaks. You might also try googling 'PHP + For Loops'

rtfm :)

#3 obsidian

  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 22 May 2006 - 02:09 PM

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:

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

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:
while ($player->health > 0 && $dragon->health > 0)
  if ($first == 1) {
    if ($dragon->health > 0) $dragon->attack();
    else continue;
  } else {
    if ($player->health > 0) $player->attack();
    else continue;

hope this helps.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users