Darkneo
05-19-2008, 12:33 AM
Originally Posted by Shwaza
After a long wait, PHP Gaming: Part 2 is here! Sorry for not exactly following any kind of schedule, but here it is, late or not!
First of all, this tutorial assumes that you:
-Have a list of game resources
-Have a list of the usages of your resources
-Have a list of all units, buildings, etc.
-Have a list of the creation of those units, buildings, etc.
-Have a way to obtain these resources, through production, research, or anything else you can think of
-Have decided to use ticks, turns, a combination, or something else!
If you do not have all of the above, i strongly suggest that you read "PHP Gaming: Part 1". However please do not reply to that tutorial, because it is rather old. Any questions, comments etc. about either tutorial should be posted here.
Now, assuming that you have those above items, let's get to PHP Gaming: Part 2!
First of all, we're going to skip the login and registration part of this, because I'm sure you all know how many login systems, user systems, and session tutorials there are just in these forums alone!
So, providing that you already have a way to login, and set two sessions (We'll call them $_SESSION['username'] and $_SESSION['password'] for now), we'll continue into actually creating your game.
Now, the easiest, however necessary step for a game, is to create an Overview page. You don't have to call it that, however it's pretty much necessary. This page should include things such as total amounts of resources, units, buildings, and possibly production if you want to. I personally have a seperate page for production, but you can do whatever you like.
For this tutorial, we'll also assume, that you have a table "Accounts" with all the accounts stored in it. Each row in this, will have different fields such as "Money", "Networth", etc.
On this main page, we'll call these values out of the database, and how we do that, is by selecting information from the row that has the username and password that is the same as the session. To do this, use a code similar to this:
<?php
mysql_connect("localhost", "user", "pass") or die("There was an error connecting to the mysql server.");
mysql_select_db("database");
$username=$_SESSION['username'];
$password=$_SESSION['password'];
$result=mysql_query("SELECT * FROM accounts WHERE username='$username' AND password='$password'");
if(mysql_num_rows($result) == 0){
die("Sorry, you are no longer logged in, please login again to continue playing.");
}
else{
while($r=mysql_fetch_assoc($result)){
//This is where you would define all your resources, buildings, units, etc.
$example_resource=$r['example_resource'];
//Use the format such as the one above, except with the names of your resources, buildings, units, etc, and continue doing that with all the values you need to take.
}
}
?>
Then, after you have defined all those variables, you can use them again, to display in a table, or whatever else you prefer to use.
Example:
<table>
<tr>
<td><?php echo $unit; ?></td>
<td><?php echo $resource; ?></td>
</tr>
<tr>
<td><?php echo $unit2; ?></td>
<td><?php echo $resource2; ?></td>
</tr>
</table>
However just make up how you would like to display them.
That's pretty much all you need for the first page of your game! You're on the road to success with PHP Gaming!
Now, if you want to have extra pages similar to that, that display stats, and don't update any rows or anything, you can follow the same pattern. I myself have 3 information pages, one as an overview, one for production and one for militar, but the choice is yours!
Now though, we'll attempt to make, probably the easiest interactive page, purchase. You can call it what you want, but we'll use it as a page that exchanges units, other resources, or whatever you want, for money.
This part will require your list of units, as well as their prices. For this, i'm going to use a static price for the units, however in my own game i use dynamic prices, which increase as your amount of units increase. I'll show you how to do that as well, however to keep the actual code short and simple, we'll use static values.
First off, we'll do the same sort of query as above for the overview page, to get the current values of the units. So after you do that, use this code:
<?php
//This should be the same as above
//Now, since we're using static prices, we don't need to define prices before hand, so we can go right into the updating of the row.
if($_POST['submit']){
$unit1_buy=$_POST['unit1'];
$unit2_buy=$_POST['unit2'];
//The above are just simple examples of units you could buy. We'll just use 2 to keep this quick, but you can have as many as you want.
$money_spent=($unit1_buy * 50) + ($unit2_buy * 100); //That sets how much it will cost to purchase the amount of units, providing that unit1 costs 50 and unit2 costs 100
//Now we'll check to see if there's enough money to buy all that assuming we already defined the variable $money in the origional query.
if($money_spent > $money){
echo "Sorry, this action would cost $money_spent and you only have $money";
}
else{
$newmoney=$money-$money_spent;
$newunit1=$unit1+$unit1_buy;
$newunit2=$unit2+$unit2_buy;
//The above defines the new variables that will be updated shortly. They are the new values for your money, unit1 and unit2. Again, if you have more than 2 units, do the same sort of thing, but with more units.
//Now we'll run the query to update the row, and add on the new units, and take away the money
mysql_query("UPDATE accounts SET unit1='$newunit1', unit2='$newunit2', money='$newmoney' WHERE username='$username' AND password='$password'");
echo "Thank you, your purchases have been made, and you spend $money_spent";
}
}
?>
That's all the php you need, providing that you have used static values, and in a moment i'll show you how to use dynamic values as well.
Now though, you need to create a form, that has a submit button called "submit" and as many fields as you want, with names of the units you want to buy. Here's an example using the units we're using for this tutorial:
<form action="<?php echo $PHP_SELF; ?>" method="post">
<table>
<tr>
<td><?php echo $unit1; ?></td>
<td>50</td>
<td><input type="text" name="unit1" /></td>
</tr>
<tr>
<td><?php echo $unit2; ?></td>
<td>100</td>
<td><input type="text" name="unit2" /></td>
</tr>
</table>
<input type="submit" name="submit" value="Purchase" />
</form>
That would display three colomns and two rows. The first colomn would display a users current amount, the second is the price, and the third is an input to tell how much you would purchase.
That's it for purchasing, so if you followed that, you're one big step closer to your own php game!
Now, as i promised, i'll show you how you could use dynamic prices.
<?php
$unit1price=$unit1 / 100;
//The above is an example of a price, that would always be 1/100 of your amount of unit1's. You would then replace the static "50" with "$unit1price".
?>
As you can see, it's very simple and adds a whole new aspect to your game.
This just about wraps up PHP Gaming: Part 2, and i hope to have a few more. Next time we might get into a research or production aspect, as well as buildings, and using your turns or ticks.
Also, these functions are very handy when making php games:
<?php
ceil();
number_format();
?>
Ceil() with an integer inside will round that number up always. This is handy when defining prices, such as $unit1price. Number format is great for displaying large integers with comma seperation. For example number_format(999999999) would equal: 999,999,999
Thank you very much for reading my tutorial, and i hope it can help out as many people as possible!!
This entire tutorial is my property, none of it taken from other sources except my own brain (lol), so reproduction of it without my permission is forbidden.
After a long wait, PHP Gaming: Part 2 is here! Sorry for not exactly following any kind of schedule, but here it is, late or not!
First of all, this tutorial assumes that you:
-Have a list of game resources
-Have a list of the usages of your resources
-Have a list of all units, buildings, etc.
-Have a list of the creation of those units, buildings, etc.
-Have a way to obtain these resources, through production, research, or anything else you can think of
-Have decided to use ticks, turns, a combination, or something else!
If you do not have all of the above, i strongly suggest that you read "PHP Gaming: Part 1". However please do not reply to that tutorial, because it is rather old. Any questions, comments etc. about either tutorial should be posted here.
Now, assuming that you have those above items, let's get to PHP Gaming: Part 2!
First of all, we're going to skip the login and registration part of this, because I'm sure you all know how many login systems, user systems, and session tutorials there are just in these forums alone!
So, providing that you already have a way to login, and set two sessions (We'll call them $_SESSION['username'] and $_SESSION['password'] for now), we'll continue into actually creating your game.
Now, the easiest, however necessary step for a game, is to create an Overview page. You don't have to call it that, however it's pretty much necessary. This page should include things such as total amounts of resources, units, buildings, and possibly production if you want to. I personally have a seperate page for production, but you can do whatever you like.
For this tutorial, we'll also assume, that you have a table "Accounts" with all the accounts stored in it. Each row in this, will have different fields such as "Money", "Networth", etc.
On this main page, we'll call these values out of the database, and how we do that, is by selecting information from the row that has the username and password that is the same as the session. To do this, use a code similar to this:
<?php
mysql_connect("localhost", "user", "pass") or die("There was an error connecting to the mysql server.");
mysql_select_db("database");
$username=$_SESSION['username'];
$password=$_SESSION['password'];
$result=mysql_query("SELECT * FROM accounts WHERE username='$username' AND password='$password'");
if(mysql_num_rows($result) == 0){
die("Sorry, you are no longer logged in, please login again to continue playing.");
}
else{
while($r=mysql_fetch_assoc($result)){
//This is where you would define all your resources, buildings, units, etc.
$example_resource=$r['example_resource'];
//Use the format such as the one above, except with the names of your resources, buildings, units, etc, and continue doing that with all the values you need to take.
}
}
?>
Then, after you have defined all those variables, you can use them again, to display in a table, or whatever else you prefer to use.
Example:
<table>
<tr>
<td><?php echo $unit; ?></td>
<td><?php echo $resource; ?></td>
</tr>
<tr>
<td><?php echo $unit2; ?></td>
<td><?php echo $resource2; ?></td>
</tr>
</table>
However just make up how you would like to display them.
That's pretty much all you need for the first page of your game! You're on the road to success with PHP Gaming!
Now, if you want to have extra pages similar to that, that display stats, and don't update any rows or anything, you can follow the same pattern. I myself have 3 information pages, one as an overview, one for production and one for militar, but the choice is yours!
Now though, we'll attempt to make, probably the easiest interactive page, purchase. You can call it what you want, but we'll use it as a page that exchanges units, other resources, or whatever you want, for money.
This part will require your list of units, as well as their prices. For this, i'm going to use a static price for the units, however in my own game i use dynamic prices, which increase as your amount of units increase. I'll show you how to do that as well, however to keep the actual code short and simple, we'll use static values.
First off, we'll do the same sort of query as above for the overview page, to get the current values of the units. So after you do that, use this code:
<?php
//This should be the same as above
//Now, since we're using static prices, we don't need to define prices before hand, so we can go right into the updating of the row.
if($_POST['submit']){
$unit1_buy=$_POST['unit1'];
$unit2_buy=$_POST['unit2'];
//The above are just simple examples of units you could buy. We'll just use 2 to keep this quick, but you can have as many as you want.
$money_spent=($unit1_buy * 50) + ($unit2_buy * 100); //That sets how much it will cost to purchase the amount of units, providing that unit1 costs 50 and unit2 costs 100
//Now we'll check to see if there's enough money to buy all that assuming we already defined the variable $money in the origional query.
if($money_spent > $money){
echo "Sorry, this action would cost $money_spent and you only have $money";
}
else{
$newmoney=$money-$money_spent;
$newunit1=$unit1+$unit1_buy;
$newunit2=$unit2+$unit2_buy;
//The above defines the new variables that will be updated shortly. They are the new values for your money, unit1 and unit2. Again, if you have more than 2 units, do the same sort of thing, but with more units.
//Now we'll run the query to update the row, and add on the new units, and take away the money
mysql_query("UPDATE accounts SET unit1='$newunit1', unit2='$newunit2', money='$newmoney' WHERE username='$username' AND password='$password'");
echo "Thank you, your purchases have been made, and you spend $money_spent";
}
}
?>
That's all the php you need, providing that you have used static values, and in a moment i'll show you how to use dynamic values as well.
Now though, you need to create a form, that has a submit button called "submit" and as many fields as you want, with names of the units you want to buy. Here's an example using the units we're using for this tutorial:
<form action="<?php echo $PHP_SELF; ?>" method="post">
<table>
<tr>
<td><?php echo $unit1; ?></td>
<td>50</td>
<td><input type="text" name="unit1" /></td>
</tr>
<tr>
<td><?php echo $unit2; ?></td>
<td>100</td>
<td><input type="text" name="unit2" /></td>
</tr>
</table>
<input type="submit" name="submit" value="Purchase" />
</form>
That would display three colomns and two rows. The first colomn would display a users current amount, the second is the price, and the third is an input to tell how much you would purchase.
That's it for purchasing, so if you followed that, you're one big step closer to your own php game!
Now, as i promised, i'll show you how you could use dynamic prices.
<?php
$unit1price=$unit1 / 100;
//The above is an example of a price, that would always be 1/100 of your amount of unit1's. You would then replace the static "50" with "$unit1price".
?>
As you can see, it's very simple and adds a whole new aspect to your game.
This just about wraps up PHP Gaming: Part 2, and i hope to have a few more. Next time we might get into a research or production aspect, as well as buildings, and using your turns or ticks.
Also, these functions are very handy when making php games:
<?php
ceil();
number_format();
?>
Ceil() with an integer inside will round that number up always. This is handy when defining prices, such as $unit1price. Number format is great for displaying large integers with comma seperation. For example number_format(999999999) would equal: 999,999,999
Thank you very much for reading my tutorial, and i hope it can help out as many people as possible!!
This entire tutorial is my property, none of it taken from other sources except my own brain (lol), so reproduction of it without my permission is forbidden.