...

View Full Version : managing hit points in a simple game



matthewst
07-05-2007, 10:13 PM
The problem I have is when I click "hit_player_2" it first takes away a point from player one. If I continue to click "hit_player_2" it will then take away points from player 2. "Hit_player_1" does th same thing. I also noticed that it takes away a point from the last player to lose one when I manually refresh.



<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test_game") or die(mysql_error());
//error_reporting(E_ALL);
error_reporting(0);
?>
<?php
$query = "SELECT player_name, hit_points FROM users";
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$player_name = $row['player_name'];
$hit_points = $row['hit_points'];
if($hit_points <= "0")
{
echo "$player_name is dead&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
else
{
echo "Player: $player_name&nbsp";
echo "Hit points: $hit_points&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
}
?>
<title>test game</title>
</head>
<body>
<br><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="FormName"><input name="hit_player_2" type="submit" value="hit_player_2">
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="hit_player_1" type="submit" value="hit_player_1"></form>
<?php
if(isset($_POST['hit_player_2'])){
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'";
mysql_query($sql)or die(mysql_error());
}
if(isset($_POST['hit_player_1'])){
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'";
mysql_query($sql)or die(mysql_error());
}
?>
</body>
</html>

mlseim
07-05-2007, 10:42 PM
first of all, you did set a value in your form:
value="hit_player_2"

doing that, you are always setting the value, so
if(isset($_POST['hit_player_2'])) is always true.

same for player 1.

===================

I think you should ditch the <form> and use links instead.
<a href="yourscript.php?p=1">hit player one</a>
<a href="yourscript.php?p=2">hit player two</a>

(Maybe create some nice graphic buttons for those "hit" links)

Read in your GET variables from the URL ....

if($_GET['p'] == 2){
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'";
mysql_query($sql)or die(mysql_error());
}
if($_GET['p'] == 1){
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'";
mysql_query($sql)or die(mysql_error());
}

==================

Finally,
Using sessions, you could then allow only one hit per page visit.
Once "hit", the buttons would disappear. No refresh would help.
They would have to exit and return to "re-hit" the buttons.

matthewst
07-07-2007, 01:34 AM
You ROCK!!!!

game.php

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test_game") or die(mysql_error());
//error_reporting(E_ALL);
error_reporting(0);
?>
<?php
$query = "SELECT player_name, hit_points FROM users";
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$player_name = $row['player_name'];
$hit_points = $row['hit_points'];
if($hit_points <= "0")
{
echo "$player_name is dead&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
else
{
echo "Player: $player_name&nbsp";
echo "Hit points: $hit_points&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
}
?>
<title>test game</title>
</head>
<body>
<br>
<a href="atk.php?p=2">hit player two</a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<a href="atk.php?p=1">hit player one</a>
</body>
</html>

atk.php

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test_game") or die(mysql_error());
//error_reporting(E_ALL);
error_reporting(0);
?>
<?php
if($_GET['p'] == 2){
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'";
mysql_query($sql)or die(mysql_error());
}
if($_GET['p'] == 1){
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'";
mysql_query($sql)or die(mysql_error());
}

echo "<script type='text/javascript'>
<!--
window.location = 'test_game.php'
//-->
</script>";
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum