Jump to content

Recommended Posts

I've been working on a game in php and have gotten a lot of help here. My question is How do I get a script to execute 1 line and the clear the page then execute the next line using a while loop?? Here is an example of where Im at now:

 

<br>
<br>
<form action="fight_script3.php" method="post"
<input type="hidden" name="fight">
<input type="submit" value="Fight The Monster">
</form>
<?php
$player_hp = 30;
$mob_hp = 30;

// Variables to include in while loop
$fight = $_POST['fight'];
while($player_hp > 0 && $mob_hp > 0){

// Determine who goes first
$first = mt_rand(1,100);

// Weapons
$player_weapon = mt_rand(1,4);
$mob_weapon = mt_rand(1,4);

// start fight
if($first <= 60){
$mob_hp = $mob_hp - $player_weapon;
echo "You attacked with your Weapon causing $player_weapon damage!";
echo "<br>";
echo "The monster has $mob_hp Hitpoints left";
echo "<hr color=\"0000ff\">";
echo "<br>";
}
if($mob_hp <= 0){
echo "You have Killed the The Monster with Your Mighty Weapon!! Yeehaa!!";
echo "<br>";
}
if($first > 60){
$player_hp = $player_hp - $mob_weapon;
echo "The Monster attacked you and caused $mob_weapon damage!";
echo "<br>";
echo "You have $player_hp Hitpoints left";
echo "<br>";
echo "<hr color=\"#ff0000\">";
echo "<br>";
}

if($player_hp <= 0){
echo "You have been killed by the miserable Monster! Eegads!";
echo "<br>";
}
}
?>

 

If I can figure this out I would really be able to continue on the game as this is the only thing atm thats holding the developement back. Ideally I would like the combat to porceed every time the submit button is pushed but only do 1 line the when you click it again another line replaces the precious and so on till the fight is over??

 

Thanks,

 

Link to comment
https://forums.phpfreaks.com/topic/120846-where-do-i-go-from-here/
Share on other sites

You can use a similar concept:

 

<?php
session_start();
if(!isset($_SESSION['player_hp'])){
    $player_hp = 30;
} else{
    $player_hp = $_SESSION['player_hp'];
}

if($player_hp > 0 and $mob_hp > 0){
    //the hack and slash code
    $_SESSION['player_hp'] = $player_hp; //set the new hp in the session
} else{
   if($player_hp == 0){
         echo 'You were killed';
   } elseif($mob_hp == 0){
         echo 'You killed the monster';
   }
}
?>

 

Don't know if I got the right logic of the game, but the idea is to use a session to store the player hp and based on that session you specify which killed who. Give it a try and if you don't make it work, feel free to ask again.

Yeah that's generally what I want to do is use the players hitpoints in the fight. The problem is I jus want it to do something like this:

 

"You attacked first causing 5 points of damage! The Mob now has 23 hitpoints left!"

pause for a few seconds

clear the screen

"The Mob attacked you causing 3 points of damage! You have 22 hitpoints left!"

pause for a few seconds

clear the screen

so on and so on till whichever dies!

 

Also from there display other things but until I can get this to work I'm not gonna try and do that yet??

My friend suggested a function and here is the code for that but I have no idea how to use it??

 

<?php
// Call function
function cdCombat()
{

if ($players_init < $enemys_init)
    {
        echo "$player Attacks 1st!<br>";
        echo "You swing your $player_weapon striking the $mob for $players_damage points of damage!";
        if ($players_damage >= $mob_hp) 
            echo "The $mob is dead!";
    }
    else
    {
        $players_hp = $players_hp - $enemy_damage;
        echo "The $mob attacks you with $mob_weapon striking for $enemy_damage Points of damage!";
    }
    
    if ($players_hp <= $enemy_damage)
        {
        echo "the $mob has killed you!";
        }
    }
?>

 

I have no Idea how to use functions but would like to see if this will help??

 

Thanks,

You can use ajax eventually (as suggested by neil.johnson). The ajax will call a php script (that function for example) which shows the hp and points of damage. If you want to trigger one attack only (each submit button press triggers and attack) then ajax would be a aesthetic (and performance maybe) option, otherwise ajax would be the only way. At least it's how I see it. An ajax periodical updater (with a specified delay) will run the backend php script and show the results. You still would need sessions though to retain hp. Hope this helps out.

you could but probably best not to. Is use the sleep(); function in between the attacks.

 

<?php
// attack
sleep(5); // pause for 5 seconds
// attack again
sleep(3); // pause 3 seconds
// battle results

 

however as mentioned above, you would be better off using AJAX for the attacking

you could but probably best not to. Is use the sleep(); function in between the attacks.

 

<?php
// attack
sleep(5); // pause for 5 seconds
// attack again
sleep(3); // pause 3 seconds
// battle results

 

however as mentioned above, you would be better off using AJAX for the attacking

 

That wouldn't clear the screen...it would only make the page hang for a long time.

Thanks guys and gals for putting your time and effort into helping me I have some code maybe wrong but this is the desired effect but not the desired result it just does it 1 attack attack at a time and doesn't progress till there is a winner!!

 

<?php
// My First Function (crimson diamond fight)!
$fight = $_POST['fight'];
if (!isset($_POST['fight'])){
echo "Waiting for the start of the fight!";
}
else {

// The Names
$player = "The Gladiator";
$mob = "Purple People Eater";

//set inititive (who attacks 1st)
//	$players_init = mt_rand(1, 10);
//	$enemys_init = mt_rand(1, 10);

// Function here
function cdFight(){

// Who Goes first
 $first = mt_rand(1,100);
 if($first < 60){
	 // Player goes first
	$mob_hp = 30;
	$player_weapon = "Nun-Chucks";
	$player_damage = mt_rand(1,4);
	$mob_hp = $mob_hp - $player_damage;
	echo "<hr color=\"0000ff\">";
	echo "You attacked with your $player_weapon causing $player_damage damage!";
	echo "<br>";
	echo "The monster has $mob_hp Hitpoints left";
	echo "<br>";
	echo "<hr color=\"0000ff\">";
}

     if($first > 60){
     // monster goes first
    $player_hp = 30;
    $mob_weapon = "Rubberband";
    $enemy_damage = mt_rand(1,4);
	$player_hp = $player_hp - $enemy_damage;
	echo "<hr color=\"#ff0000\">";
	echo "The Monster attacked you with his $mob_weapon and caused $enemy_damage damage!";
	echo "<br>";
	echo "You have $player_hp Hitpoints left";
	echo "<br>";
	echo "<hr color=\"#ff0000\">";
}
}
cdFight();
}
?>
<b>The Fight</b><br>
<hr color="#00ff00">
<form action="cdFight.php" method="post"
<input type="hidden" name="fight">
<input type="submit" value="Fight The Monster">
</form>

 

I will see what else I can come up with I'm wracking my brain any other ideas will be welcome?!?!

 

If you want it to progress, use the suggestions you got here. You are going in a dead end with that script. Have a while loop to display all the attacks, or use an ajax periodical updater to display each one alone. Have a look at the Prototype javascript framework for some easy to use ajax libraries.

I'm seeing that now I am going to have to use something else but I have no Idea which way to go cause I can barely do php and it is kind of cryptic to me about the ajax which is ?? I have no idea how to use it even?? Thanks again! I will check into the ajax thing!!

 

 

Actually that's why I suggested using a framework as your life will become a lot easier. I've found prototype to be really cool and i'm suggesting it to everybody now :). I'll write a modified example from the prototype "Introduction to Ajax", so you'll get the idea of how easy it is:

 

the html markup

<h2>Attacks</h2>
<div id="attacks">no attack done</div>

 

the ajax code

var req = new Ajax.Updater('attacks', 'attacks_execute.php?hp=30', { method: 'get' });

 

the script

<?php
$hp = $_GET['hp'] - 5;
echo $hp;
?>

 

It will simply call the php script passing "hp" via get and update the "attacks" div with the code returned by the script. That's all.

 

While the ajax periodical update would work like:

new Ajax.PeriodicalUpdater('attacks', 'attacks_exec.php',
  {
    method: 'get',
    insertion: Insertion.Top,
    frequency: 1,
    decay: 2
  });

 

which will send the ajax request every 1 second (frequency: 1). Hope this clears out things a bit.

Ok so where do I find it whats its name do i just put it in like an include folder or im not sure what to do with it?? I have peoblems understanding website documentations like mysql and such thats why I come here to real folks that can talk. You could im me if you would It would speed things up??

 

Thanks,

It's very simple, there's no need for reading a lot on it. You can find the library (or framework) here. Download it and put it in a folder in your website, ie: /js/prototype.js. In the < head > of your php page call it as you would do with any javascript file:

 

<script type="text/javascript" src="js/prototype.js"></script>

 

It's that simple to include all the classes and methods. Done that you can start writing javascript (and ajax) in a whole new way. Read the "Tips and Tutorials" on the prototype site and you'll get started immediately, i'm sure.

 

Quick note: You can write javascript and send/recieve ajax requests without using any frameworks. The purpose of these frameworks it provide an easy and intuitive approach to javascript, by assuring also cross browser support. Those are used in many high end websites, from high end companies, so that should be a strong point to choose a very well supported framework.

Ok all that is done. I just need to know where to go to learn how to use it decently fast lol huh why is it we always have to everything right now I have no patience. I had been doing a lot of copying and pasting but now I'm starting to write code but I want it yesterday lol enough rambling. I need something easy and understandable to common people who aren't like a big company or such. I'm not very techy but I do have some knowledge anyways I'm gonna stop here I have a lot to do for now.

Ok I have read quite a bit but what I'm not understanding is how to piece it all together. I have the Javascript included in my header file but the header is just part of my index controller so how do i include the code in my php and controller??

 

Thanks, I'll mess with it to see what I can do!

Took a look at some of that and then tried on one of my fight scripts. It started fine and was updating every 3 seconds but now output from the fight. Then I tried to edit it and messed it up , then got distracted by my wife and forgot which file it was lol I had so many of them. Never worked after that lol. Somtimes I can intuitively handle problems but its not working in this case. So I have a lot to learn.

  • 2 weeks later...

Ok I'm back had some technical difficulties and had to practically redo the whole game. I am now back to the fight script and ajax, it works kind of it updates the page but, the script just keeps running over and over again and it doesn't just do 1 attack at a time as the script executes too quickly I think.

here are the 2 files I'm using:

 

Number 1: attacks_exec.php

<?php
$player_hp = 30;
$mob_hp = 30;

// Variables to include in while loop
$fight = $_POST['fight'];
while($player_hp > 0 && $mob_hp > 0){

// Determine who goes first
$first = mt_rand(1,100);

// Weapons
$player_weapon = mt_rand(1,4);
$mob_weapon = mt_rand(1,4);

// start fight
if($first <= 60){
$mob_hp = $mob_hp - $player_weapon;
echo "You attacked with your Weapon causing $player_weapon damage!";
echo "<br>";
echo "The monster has $mob_hp Hitpoints left";
echo "<hr color=\"0000ff\">";
echo "<br>";
}
if($mob_hp <= 0){
echo "You have Killed the The Monster with Your Mighty Weapon!! Yeehaa!!";
echo "<br>";
exit();
}
if($first > 60){
$player_hp = $player_hp - $mob_weapon;
echo "The Monster attacked you and caused $mob_weapon damage!";
echo "<br>";
echo "You have $player_hp Hitpoints left";
echo "<br>";
echo "<hr color=\"#ff0000\">";
echo "<br>";
}

if($player_hp <= 0){
echo "You have been killed by the miserable Monster! Eegads!";
echo "<br>";
exit();
}
}
?>

 

Number 2: fighttest.php

<html>
<head>
<title>Fight Script Testing - Ajax</title>
<script type="text/javascript" src="includes/prototype.js"></script>
</head>
<body>
<h2>The Fight Test</h2>
<br>
<br>
<form action="attacks_exec.php" method="post"
<input type="hidden" name="fight">
<input type="submit" value="Fight The Monster">
</form>
<div id="attacks">
<script type="text/javascript">
new Ajax.PeriodicalUpdater('attacks', 'attacks_exec.php',
  {
    method: 'get',
    insertion: Insertion.Top,
    frequency: 1,
    decay: 2
  });
</script>
</div>
</body>
</html>

 

Is this the correct way to do it?? Any pointers? The website you pointed me to is a little vague and is more than I can understand atm. Any reply will be appreciated!!

 

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.