Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, September 19, 2007

Basic Strategy game - upgrade money gain (with bank)

Today I will try to explain how to create Bank structure, which would allow us to make more money per turn.

First theory of this addon:

  • each level of bank adds 5%
  • level 1 cost 500 money to upgrade
  • each level costs 30% more then last one
Now we create formulas that we will code later.
Each level of bank adds 5%, so our idea is to multiply basic amount with some coefficient. In our case this is 1.05 for level 1, level 2 will be 1.05*1.05, level 3 will be 1.05*1.05*1.05. So you can see where this is leading; we can create universal formula by using level and basic amount.
Cash we gain is then: 1.05^banklevel , in PHP this is used by pow() function.
Exactly the same we will use for costs of next level, but percentage is higher so coefficient would be 1.30 in this case.

Now to coding! Lets open config.php (from last post) and modify it.
After this:
//money amount to add for each round
$add_money = 1000;
Add:
//coefficient to use (5% addon = 1.05)
$k_money = 1.05;

Now find:
//calculate how much money user gained
$all_money = $nr_rounds * $add_money;
And change it to:
//calculate how much money user gained
$all_money = $nr_rounds * $add_money * pow($k_money, $user[bank]);

This "pow($k_money, $user[bank])" will work like we wanted to (1.05 ^ banklevel).

Now lets create new php file like upgrade.php (don't forget to include config.php in beginning of file).
//calculate money needed for next level of bank upgrade (php part of file)
$need_money = 500 * pow(1.30, $user[bank]);

//we must create form for upgrades (html part of file) with button.

<form>
Bank level: <? echo $user[bank]; ?> <br>
Next level: <input type="submit" value="<? echo number_format($need_money) ?> money" name="upgradebank">
</form>

//now we create another php part of this file (you could put that on top right after when you calculate $need_money), where we will tell what to do when button is pressed.
if ($_POST[upgradebank]) {

//we check if user has enough money to upgrade bank
if ($user[money] < $need_money) {
//we use die function to stop script execution and to show message what is wrong
die ("You don't have enough money to upgrade Bank!");
}

//everything is OK, we update our user table
mysql_query("UPDATE users SET money=money-'$need_money', bank=bank+'1' WHERE id='$user[id]'");

}


This concludes our tutorial, come back for more. :)
Next time we will create system which allows user to train units.

Any questions, ideas and comments are welcome.

Friday, September 14, 2007

Basic Strategy game - automated hourly money gain

Today I will try to explain how to create automated money gain (each hour)
Last time we created really basic system, so now it is time to upgrade it.

Main idea is that user gains some amount of money every hour (starting from his registration time). So theory is like this:

  • registration time (Unix timestamp - in seconds from 1970)
  • check in table when user money was last updated (row: lastupdate)
  • if this time is greater then 1 hour, then we update money
lets open our config.php file (which we will include_once in all our other documents). At bottom add:
//money amount to add for each round
$add_money = 1000;
//time now, and round time in seconds (1h=60*60seconds=3600)
$time_now = time();
$round = 3600;

//time of last update+time needed must be smaller then time now to update
if (($user[lastupdate]+$round) <= $time_now) {
//see how many rounds (hours) were there from last update
$nr_rounds = floor(($time_now-$user[lastupdate])/$round);
//calculate how much money user gained
$all_money = $nr_rounds * $add_money;
//calculate how many rounds in seconds (how many hours in seconds)
$add_time = $nr_rounds * $round;

//lets update users table
mysql_query("UPDATE users SET lastupdate=lastupdate+'$add_time', money=money+'$all_money' WHERE id = '$user[id]'") or die(mysql_error());

//here you can refresh page (so you can see progress) or select user again using login
}

You would say....oh my god it is so simple to make games! Well actually you need really good plan, then coding part is easy, although it needs time.

So in this lesson we learned how to add money to user every time he login. Script just checks for last update, and if there should be new update it does it. It adds money and it changes time of last update.
In next post I will explain how to allow users to spend their money to build and level-up economical structure (Bank), each level of Bank will add bonus of money gained each turn.

Feel free to comment and ask anything :)

Thursday, September 13, 2007

Starting Basic Strategy game

I have chosen to pick Action/Strategy as genre for our next game.
So basic ideas:

  • player has money (he gains it every hour - from his registration time)
  • he can train units (1 different type), and this unit can be equipped with weapon (1 out of 3)
  • he can build defensive or economical structures (to get more defense or gain more money)
  • he can attack others and steal their money
How to start?
We need database with 1 table. Structure will be like this:
ID / username / password (protect it, if nothing else then md5) / email
also we must add other things, so it would look like that:
ID / username / password / email / lastupdate / money / units / weapon1 / weapon2 / weapon3 / wall / bank

Do not forget to optimize table and set money, units, weapon1-3, wall, bank to 0 as default!
Lets say this table is called 'users' so we connect to it using config.php and selecting user that is logged in (SELECT * FROM users WHERE password='$password' AND username='$username'). We use php function to fetch this row:
$user = mysql_fetch_array($selection);
to use any thing from our logged user we can use $user[X], where X is thing we like to use. For example if we want to see how much money does logged user has, we use:
echo "Logged user has: ".$user[money]." money".
Also you must create login and registration system* (in registration when adding new user do not forget to add $time_now ($time_now = time();) variable to lastupdate column!

So lets create main page (main.php), where we will show user information:
<?
echo "Username: $user[username]<br>
E-mail: $user[email]<br>
Money: $user[money]<br>
Units: $user[units]<br>
Number of Weapon 1: $user[weapon1]<br>
Number of Weapon 2: $user[weapon2]<br>
Number of Weapon 3: $user[weapon3]<br>
Wall level: $user[wall]<br>
Bank level: $user[level]<br>";
?>

This is all we need to start our basic game.
In next post I will explain how to make that money updates automatically every hour.

*You can find tutorials on login/register system on several tutorial sites.

Start is Hard

Starting is always hard.
Not enough ideas, not good plan, voices in your head (like: why am I doing it?!)... but hey let's just start!

Warning!
This blog is not going to be like all blogs which like to be only theoretical about PHP games.
But this post is :)

Browser based games can be divided in next genres:

  1. Strategy
  2. Action
  3. RPG
  4. Sports
  5. Business
So when you have decided about genre you need to make GOOD plan for your game. Coding is easy with good plan and good economy. But how to achieve that?
  1. Try to write down everything about your future game (in short form)
  2. Create Game Design Document (where you explain all mechanism of your game)
  3. USE Excel (MS or Open ones...it does not matter), to produce your starting economy (will explain this more later)
This is all you need BEFORE you even start coding ... if you want to produce decent game.
So more about economy; it depends on what game are you making, but basically it goes like this:
  • try to write all prices down and all items (if any)
  • make and test formulas you would use, and compare different versions
  • use graphs to see your work
  • try to make everything connected (so if you change one price, then everything connected to this value changes - so you do not need to fix several things)
Now we have prepared all we need and coding can start.