Jump to content


Photo

Lopping


  • Please log in to reply
2 replies to this topic

#1 supanoob

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:

<?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>";


?>

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

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'

Cheers.
Pat
rtfm :)

#3 obsidian

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) {
    $player->attack();
    if ($dragon->health > 0) $dragon->attack();
    else continue;
  } else {
    $dragon->attack();
    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.

<?php
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