Jump to content

Archived

This topic is now archived and is closed to further replies.

supanoob

Lopping

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.