Jump to content

Recommended Posts

Hello,

I am a fairly new at php and i was trying to make a real simple battle script and for some reason I am having a problem. The attached file is the output of the script. The problem i am having is that as you can see, in the second till last battle line, the monster is killed, yet in the last battle line, he attacks me. You can also see this script live @ www.homickshouse.com/kryppienationtest/testbattle.php  .    I would like the battle to end after someone get's to zero, i'd like the number to stop at zero as well, instead of going negative, but most important, a dead person/monster cannot attack. I am open to suggestions for better programming as well because i'm just going by what i can read online and from a few cheap books i bought. If you can help me out please send me an email: kryppienation@gmail.com  --> please use the subject "battle script" if possible.

 

 

Here is the code i used to make the output. if anyone knows how to make this work completely, please let me know.

 

<?php


//master variables
$turn = 0;
$count = 0;

// monster variables go here
$monster = 'Hell Leasher';
$monsterstr = 5;
$monsterdef = 20;
$monsterhp = 300;
$monsterlow = 35;
$monsterhigh = 45;



//player variables go here

$player = 'Kryppie';
$playerstr = 5;
$playerdef = 50;
$playerhp = 300;

$playerlow = 43;
$playerhigh = 70;


// Let's do some scripting
//Now, we need to let each fighter attack until zero health remains.


while ( $playerhp >= 1 and $monsterhp >= 1 and $count == 0 ){

$monsterhit = rand($monsterlow, $monsterhigh);
$playerhit = rand($playerlow, $playerhigh);

//the battle begins

if ( $playerhp <= 0 or $monsterhp <=0 ){
$count++;
} else {

//this starts the fight.
$monsterhp = $monsterhp - $playerhit;
$playerhp = $playerhp - $monsterhit;
//the fight is over. we will display info below about each round

echo '<font color="blue"> ' . $player . ' hit\'s ' . $monster . ' for ' . $playerhit . ' points of damage ' . $monster . ' now has ' . $monsterhp . ' health left.</font>';
echo '<br>';
echo '<font color="red"> ' . $monster . ' hit\'s ' . $player . ' for ' . $monsterhit . ' points of damage ' . $player . ' now has <b>' . $playerhp . '</b> health left.</font>';
echo '<br>';
}


}


// now we will sort out the info we need. and declare official  results.
if ($monsterhp >= 1)
{
echo '<p>';
echo '<b>You have been defeated by the mighty ' . $monster . '</b>';

} else {
echo '<p>';
echo '<b>You have been victorous and defeated the mighty ' . $monster . '</b>';

}


?>

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/129916-solved-script-help/
Share on other sites

You always output both lines regardless of the hp levels.

 

    } else {

        //this starts the fight.
        $monsterhp = $monsterhp - $playerhit;
        $playerhp = $playerhp - $monsterhit;
        //the fight is over. we will display info below about each round

        if ($playerhp>0) {
            echo '<font color="blue"> ' . $player . ' hit\'s ' . $monster . ' for ' . $playerhit . ' points of damage ' . $monster . ' now has ' . $monsterhp . ' health left.</font>';
            echo '<br>';
            
        }
        
        if ($monsterhp > 0) {
            echo '<font color="red"> ' . $monster . ' hit\'s ' . $player . ' for ' . $monsterhit . ' points of damage ' . $player . ' now has <b>' . $playerhp . '</b> health left.</font>';
            echo '<br>';
        }
    }

Link to comment
https://forums.phpfreaks.com/topic/129916-solved-script-help/#findComment-673517
Share on other sites

That helped in the case that the user was able to kill the monster. I change the variables so the monster win's and the output is messed up, notice, the user is not showing their last attack. Any suggestions? Also, live result's are here: www.homickshouse.com/kryppienationtest/testbattle.php

 

 

<?php


//master variables
$turn = 0;
$count = 0;

// monster variables go here
$monster = 'Hell Leasher';
$monsterstr = 50;
$monsterdef = 20;
$monsterhp = 300;
$monsterlow = 60;
$monsterhigh = 100;



//player variables go here

$player = 'Kryppie';
$playerstr = 5;
$playerdef = 50;
$playerhp = 300;
$playerlow = 43;
$playerhigh = 70;


// Let's do some scripting
//Now, we need to let each fighter attack until zero health remains.


while ( $playerhp >= 1 and $monsterhp >= 1 and $count == 0 ){

$monsterhit = rand($monsterlow, $monsterhigh);
$playerhit = rand($playerlow, $playerhigh);

//the battle begins

if ( $playerhp <= 0 or $monsterhp <=0 ){
$count++;
} else {

//this starts the fight.
$monsterhp = $monsterhp - $playerhit;
$playerhp = $playerhp - $monsterhit;
//the fight is over. we will display info below about each round

        if ($playerhp>0) {
            echo '<font color="blue"> ' . $player . ' hit\'s ' . $monster . ' for ' . $playerhit . ' points of damage ' . $monster . ' now has ' . $monsterhp . ' health left.</font>';
            echo '<br>';
            
        }
        
        if ($monsterhp > 0) {
            echo '<font color="red"> ' . $monster . ' hit\'s ' . $player . ' for ' . $monsterhit . ' points of damage ' . $player . ' now has <b>' . $playerhp . '</b> health left.</font>';
            echo '<br>';
        }
    }
}





// now we will sort out the info we need. and declare official  results.
if ($monsterhp >= 1)
{
echo '<p>';
echo '<b>You have been defeated by the mighty ' . $monster . '</b>';

} else {
echo '<p>';
echo '<b>You have been victorous and defeated the mighty ' . $monster . '</b>';

}



















?>

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/129916-solved-script-help/#findComment-673523
Share on other sites

well I believe I have got you closer. Try this out..

<?php
//master variables
$turn = 0;
$count = 0;

// monster variables go here
$monster = 'Hell Leasher';
$monsterstr = 50;
$monsterdef = 20;
$monsterhp = 300;
$monsterlow = 60;
$monsterhigh = 100;



//player variables go here

$player = 'Kryppie';
$playerstr = 5;
$playerdef = 50;
$playerhp = 300;
$playerlow = 43;
$playerhigh = 70;


// Let's do some scripting
//Now, we need to let each fighter attack until zero health remains.


while ( $playerhp >= 1 and $monsterhp >= 1 and $count == 0 ){

$monsterhit = rand($monsterlow, $monsterhigh);
$playerhit = rand($playerlow, $playerhigh);

if($monsterhit > $playerhp) {
$playerhp = 0;

            echo '<font color="red"> ' . $monster . ' hit\'s ' . $player . ' for ' . $monsterhit . ' points of damage ' . $player . ' is Finished off with the last devastating blow!</font>';
            echo '<br>';

}
if($playerhit > $monsterhp) {
$monsterhp = 0;

            echo '<font color="blue"> ' . $player . ' hit\'s ' . $monster . ' for ' . $playerhit . ' points of damage ' . $monster . ' Finished off with one last executing move by ' . $player . '</font>';
            echo '<br>';

}

//the battle begins

if ( $playerhp <= 0 or $monsterhp <=0 ){
$count++;
} else {

//this starts the fight.
$monsterhp = $monsterhp - $playerhit;
$playerhp = $playerhp - $monsterhit;
//the fight is over. we will display info below about each round

        if ($playerhp > 0) {
            echo '<font color="blue"> ' . $player . ' hit\'s ' . $monster . ' for ' . $playerhit . ' points of damage ' . $monster . ' now has ' . $monsterhp . ' health left.</font>';
            echo '<br>';
            
        }
        
        if ($monsterhp > 0) {
            echo '<font color="red"> ' . $monster . ' hit\'s ' . $player . ' for ' . $monsterhit . ' points of damage ' . $player . ' now has <b>' . $playerhp . '</b> health left.</font>';
            echo '<br>';
        }
    }
}

// now we will sort out the info we need. and declare official  results.
if ($monsterhp >= 1)
{
echo '<p>';
echo '<b>You have been defeated by the mighty ' . $monster . '</b>';

} else {
echo '<p>';
echo '<b>You have been victorous and defeated the mighty ' . $monster . '</b>';

}

?>

 

It appears to be skipping the last attack for the user, making the monster win everytime.

Link to comment
https://forums.phpfreaks.com/topic/129916-solved-script-help/#findComment-674161
Share on other sites

updated: i changed it around on the live site and it still isn't showing the users last attack. ; ; to see the results check here www.homickshouse.com/kryppienationtest/testbattle.php

 

Thanks again for your time everyone. Hope we can get this working soon :)

 

 

 


<?php
//master variables
$turn = 0;
$count = 0;

// monster variables go here
$monster = 'Hell Leasher';
$monsterstr = 50;
$monsterdef = 20;
$monsterhp = 300;
$monsterlow = 60;
$monsterhigh = 100;



//player variables go here

$player = 'Kryppie';
$playerstr = 5;
$playerdef = 50;
$playerhp = 300;
$playerlow = 43;
$playerhigh = 70;


// Let's do some scripting
//Now, we need to let each fighter attack until zero health remains.


while ( $playerhp >= 1 and $monsterhp >= 1 and $count == 0 ){

$monsterhit = rand($monsterlow, $monsterhigh);
$playerhit = rand($playerlow, $playerhigh);

if($monsterhit > $playerhp) {
$playerhp = 0;

            echo '<font color="red"> ' . $monster . ' hit\'s ' . $player . ' for ' . $monsterhit . ' points of damage ' . $player . ' is Finished off with the last devastating blow!</font>';
            echo '<br>';
         
}
if($playerhit > $monsterhp) {
$monsterhp = 0;

            echo '<font color="blue"> ' . $player . ' hit\'s ' . $monster . ' for ' . $playerhit . ' points of damage ' . $monster . ' Finished off with one last executing move by ' . $player . '</font>';
            echo '<br>';
         
}

//the battle begins

if ( $playerhp <= 0 or $monsterhp <=0 ){
$count++;
} else {

//this starts the fight.
$monsterhp = $monsterhp - $playerhit;
$playerhp = $playerhp - $monsterhit;
//the fight is over. we will display info below about each round

        if ($playerhp > 0) {
            echo '<font color="blue"> ' . $player . ' hit\'s ' . $monster . ' for ' . $playerhit . ' points of damage ' . $monster . ' now has ' . $monsterhp . ' health left.</font>';
            echo '<br>';
            
        }
        
        if ($monsterhp > 0) {
            echo '<font color="red"> ' . $monster . ' hit\'s ' . $player . ' for ' . $monsterhit . ' points of damage ' . $player . ' now has <b>' . $playerhp . '</b> health left.</font>';
            echo '<br>';
        }
    }
}

// now we will sort out the info we need. and declare official  results.
if ($monsterhp >= 1)
{
echo '<p>';
echo '<b>You have been defeated by the mighty ' . $monster . '</b>';

} else {
echo '<p>';
echo '<b>You have been victorous and defeated the mighty ' . $monster . '</b>';

}

?>

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/129916-solved-script-help/#findComment-674167
Share on other sites

I added a new variable and made that the loop variable, then handeled the attacked of each in if statements.

 

 

i also added min and max so it's differant. I should be able to add this to my game now.

 

if ya wanna see the final test it's in same location

 

www.homickshouse.com/kryppienationtest/testbattle.php

 

 

Thanks again ^^

 

 

<?php

//master variables
$death = 0;


// monster variables go here
$monster = 'Hell Leasher';
$monsterhp = 300;
$monsterstr = 10;




$monstercalculatedatk = ($monsterstr * .4) + $monsterstr;
echo 'monster attack base = ' . $monstercalculatedatk . ' <br>';


$monsterlow = $monstercalculatedatk - ($monstercalculatedatk * .1);
$monsterhigh = $monstercalculatedatk + ($monstercalculatedatk * .1);
echo 'monster attack low = ' . $monsterlow . ' <br>';
echo 'monster attack high = ' . $monsterhigh . ' <p>';

//player variables go here

$player = 'Kryppie';
$playerhp = 300;
$playerstr = 10;

$playercalculatedatk = ($playerstr * .4) + $playerstr;
echo 'player attack base = ' . $playercalculatedatk . ' <br>';


$playerlow = $playercalculatedatk - ($playercalculatedatk * .1);
$playerhigh = $playercalculatedatk + ($playercalculatedatk * .1);
echo 'player attack low = ' . $playerlow . ' <br>';
echo 'player attack high = ' . $playerhigh . ' <p>';

//Battle Begin


while ($death == 0){


//calculate the damage for attack
$monsterhit = rand($monsterlow, $monsterhigh);
$playerhit = rand($playerlow, $playerhigh);

// calculate monster attack
if ($playerhp >=1 and $monsterhp >=1){

$playerhp = $playerhp - $monsterhit;
echo 'Monster hits you for ' . $monsterhit . '. You know have ' . $playerhp . ' Health. <br>';

} else {

$death++;

}

//calculate your attack
if ($monsterhp >=1 and $playerhp >=1){

$monsterhp = $monsterhp - $playerhit;
echo 'player hits monster for ' . $playerhit . '. Monster know has ' . $monsterhp . ' Health. <br>';

} else {

$death++;

}












}





//The battle is over, Now we will give the Official Results




if($playerhp >=1){

echo '<p>';
echo 'Congratulation\'s ' . $player . ' you have defeated the mighty ' . $monster . '';

}else{


echo '<p>';
echo '' . $player . ' you have been defeated by the mighty ' . $monster . '. You must train harder.';

}

?>

Link to comment
https://forums.phpfreaks.com/topic/129916-solved-script-help/#findComment-674196
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.