Kensaii Posted May 20, 2010 Share Posted May 20, 2010 Sadly, my teacher is unreachable at the moment.. Dx And I need a little help with a game I'm making. I'm having troubles understanding how to start_session(); for my battles and how to add a new part of my script to an old one. I'm also terrible at putting code in order, so sorry if it looks too bad. ^^; So, what I'm looking for to happen is a person would select an AI to fight, then a random number of 1 or 2 will be picked. If number 2 is picked I want the fight to start (my old battle script takes place), but if not I want to echo "You looked and could not find a target, blah blah blah."; . Not sure how I would make the if else statement on this one. =\ Then lastly, I'd like to know where to place my new script inside the old one. Totally lost on that one. Dx New Part of my Code: <?php session_start(); ?> <?php if(isset($_POST['ai')) { // Check for player choice $rand_num = mt_rand(1, 2); $ai[0]= "Angry Human"; $ai[1]= "Monster"; $ai[2]= "Fallen Angel"; $ai[3]= "Waffle"; if($rand_num == 2) { $choice = $_POST['ai']; // some code to make sure the choice is okay $_SESSION['monster'] = $choice; } echo "You begin to check your equpment and items as you get ready to head out to Nubilous Wood. As you take the five minute walk to the border of the forest, you start to ponder on what you might be hunting today."; ?> <html> <form> <input type="radio" name="ai" value=$ai[0] /> Angry Human <br /> <input type="radio" name="ai" value=$ai[1] /> Monster <br /> <input type="radio" name="ai" value=$ai[2] /> Fallen Angel <br /> <input type="radio" name="ai" value=$ai[3] /> Waffle <br /> <input type="submit" value="Hunt" /> </form> </html> My old Battle Script: <?php session_start(); ?> <?php $header = " <html> <head> <title>Terra Reverie -Browser MMORPG-</title> <link href='style/default_style/style.css' rel='stylesheet' type='text/css'> </head> <body> <div id='header'> </div> <div id='bodyone'> </div> <div id='bodytwo'> </div> <div id='bodythree'> </div> <div id='navone'> </div> <div id='navtwo'> <div id='contentnav'> This is nav content, I guess? <p>This is moar nav content, I guess?</p> </div> </div> <div id='navthree'> </div> <div id='footer'> Rawr! >=3 </div> </body> </html>"; echo $header; ?> <?php // If the user hasn't started, set the player and monster hp variables in $_SESSION if(!isset($_SESSION['player_hp'])) { $_SESSION['player_hp'] = 100; } if(!isset($_SESSION['ai_hp'])) { $_SESSION['ai_hp'] = 100; } // Get the player and monster HP and put them in variables $player_hp = $_SESSION['player_hp']; $ai_hp = $_SESSION['ai_hp']; if(isset($_POST['attack'])) { $attack = $_POST['attack']; if($attack == "kiss") { $attack_strength = 10; $attack_text = " Poke your lips out, and plant a wet one! "; } elseif($attack == "slap") { $attack_strength = 17; $attack_text = " Oh em gee! Bish Slap~ =o "; } mt_srand((double)microtime() * 1000000); if(isset($attack_strength)) { $player_damage = $attack_strength * mt_rand(2, 5); $ai_hp = $ai_hp - $player_damage; } // Subtract monster damage from player health $monster_damage = 9 * mt_rand(2, 5); $player_hp = $player_hp - $monster_damage; // Update $_SESSION with hp $_SESSION['player_hp'] = $player_hp; $_SESSION['ai_hp'] = $ai_hp; } // Display player and monster health echo "<table border='1'>"; echo "<tr>"; echo "<td width='50%'>Your health: " . $player_hp . "</td>"; echo "<td width='50%'>Monster's health: " . $ai_hp . "</td>"; echo "</tr>"; // If player attacked if(isset($player_damage)) { // Display damages echo "<tr><td colspan='2'>"; echo $attack_text . $player_damage . " damage was dealt to the enemy! <br />"; echo "Monster attacks and deals " . $monster_damage . " damage."; echo "</td></tr>"; } // If somebody won, display winner. Otherwise display attack button echo "<tr><td colspan='2'>"; if($player_hp <= 0 && $ai_hp > 0) { echo "Monster wins... ^o.o^ You suck! =o<br />"; echo "<a href='index.php'>Fight Again</a>"; session_destroy(); } elseif($ai_hp <= 0 && $player_hp > 0) { echo "You win!<br />"; echo "<a href='index.php'>Fight Again</a>"; session_destroy(); } elseif($player_hp <= 0 && $ai_hp <= 0) { echo "D-KO!<br />"; echo "<a href='index.html'>Fight Again</a>"; session_destroy(); } else { echo "<form action='index.php' method='post'>"; echo "<input type='radio' name='attack' value='kiss' /> Kiss<br />"; echo "<input type='radio' name='attack' value='slap' /> Slap<br />"; echo "<input type='submit' value='Attack' />"; echo "</form>"; } echo "</tr></td>"; echo "</table>"; ?> <?php echo $menus; echo $footer; ?> Link to comment https://forums.phpfreaks.com/topic/202452-starting-sessions-order-and-modifying-script/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.