Tuesday, September 25, 2007

Basic Strategy game - buy weapons

In this tutorial I will try to explain how to create system which would allow you to buy weapons for your units.

Again theory:

  • weapon1 doubles (2x) efficiency of soldier with it - costs 1000
  • weapon2 quadruples (4x) efficiency of soldier - cost 2000
  • weapon3 gives 8x bonus to soldier - cost 4000
  • by selling weapon you gain 75% of price back
Now lets create new php file like weapons.php (don't forget to include config.php in beginning of file).

//we need 2 form parts (html) - one for buying, one for selling
<form>
Weapon1: <input size="3" maxlength="4" value="0" name="buy1"> <br>
Weapon2: <input size="3" maxlength="4" value="0" name="buy2"> <br>
Weapon3: <input size="3" maxlength="4" value="0" name="buy3"> <br>
<input name="buy" type="submit" value="Buy">
</form>

<form>
Weapon1: <input size="3" maxlength="4" value="0" name="sell1"> <br>
Weapon2: <input size="3" maxlength="4" value="0" name="sell2"> <br>
Weapon3: <input size="3" maxlength="4" value="0" name="sell3"> <br>
<input name="sell" type="submit" value="Sell">
</form>

//now the PHP part
//first if buy button is pressed
if ($_POST[buy]) {
$buy1 = strip_tags(addslashes($_POST[buy1]));
$buy2 = strip_tags(addslashes($_POST[buy2]));
$buy3 = strip_tags(addslashes($_POST[buy3]));

//we check if it is numeric and positive
if (!is_numeric($buy1) OR !is_numeric($buy2) OR !is_numeric($buy3)) { die("Wrong input!"); }
if (($buy1 < 0) OR ($buy2 < 0) OR ($buy3 < 0)) { die("Wrong input!"); }

//we check if we have enough money
$buy_money = 1000*$buy1+2000*$buy2+4000*$buy3;
if ($user[money] < $buy_money) { die("Not enough money to buy so many weapons!"); }

//if script got it to this point it was all ok so we can update
mysql_query("UPDATE users SET money=money-'$buy_money', weapon1=weapon1+'$buy1', weapon2=weapon2+'$buy2', weapon3=weapon3+'$buy3' WHERE id='$user[id]'");
}

//we now make script which will run if we press Sell button
if ($_POST[sell]) {
$sell1 = strip_tags(addslashes($_POST[sell1]));
$sell2 = strip_tags(addslashes($_POST[sell2]));
$sell3 = strip_tags(addslashes($_POST[sell3]));

//we check if it is numeric and positive
if (!is_numeric($sell1) OR !is_numeric($sell2) OR !is_numeric($sell3)) { die("Wrong input!"); }
if (($sell1 < 0) OR ($sell2 < 0) OR ($sell3 < 0)) { die("Wrong input!"); }

//we check if user wants to sell more weapons he has
if (($sell1 > $user[weapon1]) OR ($sell2 > $user[weapon2]) OR ($sell3 > $user[weapon3])) { die("You cannot sell more weapons then you have!"); }

//we calculate how much money we will get (75%)
$sell_money = round((1000*$sell1+2000*$sell2+4000*$sell3)*0.75);

//if script got it to this point it was all ok so we can update
mysql_query("UPDATE users SET money=money+'$sell_money', weapon1=weapon1-'$sell1', weapon2=weapon2-'$sell2', weapon3=weapon3-'$sell3' WHERE id='$user[id]'");
}

This concludes our tutorial. You can see training units and buying weapons scripts are quite the same. Those are basics, but it is up to you and your game logic to create different, better and more complicated systems. (You can make use previous tutorials and add that user gets each hour 1 unit, which he can train to soldier).
In next tutorial we will create basic attack system, which would allow users to attack each other to gain money.

7 comments:

Anonymous said...

Nice tut too, but die isn't good. ;)

Saljutin said...

why not?

Anonymous said...

Because all input which comes afterwards, isn't it carried out. In this case it determines nothing, but what if you have built in him in a lay-out?

Saljutin said...

Well as we talked about this thing on msn...here is solution for all readers.
echo "die msg";
die(include 'file.php');

so this is solution...but Die function is safe and that is what matters :)

Unknown said...

Heya thanks for these fantastic tutorials! I am also in the process of making a php strategy game when i came accross your tutorials. I am currently stuck with the map!

I have tried again and again to display only a section of my map (a 10,000 square grid) with fail. Would u know how to do this? if so maybe a nice tutorial like these ones wouldnt go a miss ;)

Btw my msn is talonofdarkness@hotmail.co.uk add me if you wish and we can maybe help each other at times :)

Thanks for your time.

RRobertEST said...

hello

i used your tutorial but now i have a big problem.. and i think its my fault.. if user types .6 in form there will be still added 1.0 to mysql table.. but prize is paid only for .6

al4crity said...

echo "die msg";
die(include 'file.php');

This code isn't much of a solution, it just loads said page eternal ammount of times into the site.