PDA

View Full Version : PHP Gaming: Part 3


Darkneo
05-19-2008, 12:39 AM
Originally Posted By Shwaza


It's that time again! It's time for another of Shwaza's amazing PHP Gaming tutorials

Since this is part 3 it'd probably be best if you are already familiar with the concepts

used in part 1 and 2:

Part 1 (http://forums.hostmatrix.org/showthread.php?t=3388)
Part 2 (http://forums.hostmatrix.org/showthread.php?t=3389)

Now, to begin, the main aspects we will be covering in this section regard using your

ticks/turns or whatever units you wish to use in your game, and going hand in hand with

that, production of resources, units, etc.

First off, you'll need to decide on your unit of time or currency basically. I personally

use Ticks and turns. You can choose one of those, or make up your own. It's basically all

the same thing in the end.

Next, you need to decide if your item will be used as Time or Currency. For my own game I

wanted to be able to have units for time and currency, (ticks and turns). The difference

between the two is if it can be spent, or if it is naturally occuring.


For the following part, I'm going to pretend we're making a game using ticks that happen

hourly and run a production cycle, so if you are using a unit of Time read this part,

otherwise continue on to the next section.

Time

First of all, what we want to do is create a file that will be accessed just by the server

through Cron Jobs. You can choose to either have that file in a non-public folder, or in a

public folder chmodded to '700'. For this tutorial we're going to have a file "tick.php" in

an non-public folder.

Next you'll want to set-up a cron to run that file hourly, however rather than tell you how

that's done, here's a link to Tim's tutorial on PHP and Cron:

http://forums.hostmatrix.org/showthread.php?t=3392

Now that your cron is setup, we can begin the coding!

Here's a sample code which I'll explain later:
<?php
mysql_connect("localhost", "mysqluser", "mysqlpass");//Connect to the mysql server
mysql_select_db("mysqldb");//Select the Database

$query=mysql_query("SELECT * FROM accounts");//Query all the accounts

while($r=mysql_fetch_array($query)){

$production1=$r['production1'];
$production2=$r['production2'];
//The above are imaginary production figures that you may have. These could be researched

items by your members, purchased, won, etc.
$item1=$r['item1'];
$item2=$r['item2'];
//The above are imaginary items in your game. They are the current values that a player

has.

$new_item1=$item1+$production1;
$new_item2=$item2+$production2;
//The above defines the new versions of a players items.

$username=$r['username'];//Define the players username

mysql_query("UPDATE accounts SET item1=$new_item1, item2=$new_item2 WHERE

username='$username'");//Update the accounts items to be the new values.

}
?>
That's pretty simple right? That's because it is. You can do much many more fancier things

with that however basically that's the most important aspect of it.

Here are some examples of extra addons you could do however:

Set Item Limits:
//Add this inside the "while" loop
if($item1+$production1 > 10){//Check if the new item1 will be greater than 10 (you can use

any number)
$new_item1=10;//Define item1 as 10 by default.
}
else{
$new_item1=$item1+$production1;//Otherwise item1 is it plus the production rate for it.
}
Defining Networth And Ranks:
//Change the query "$result" to:
$result=mysql_query("SELECT * FROM accounts ORDER BY networth DESC");

//Add this directly above the loop:
$rank=0;

//Add this inside the "while" loop:
$networth=($item1 * 5) + ($item2 * 10);//Those are examples, you could add in any items you

wanted, and set the number it's multiplied to to be whatever you want.
$rank++;//Add 1 to the rank each time the loop runs through.

//Change the update query to:
mysql_query("UPDATE accounts SET item1=$new_item1, item2=$new_item2, networth=$networth,

rank=$rank WHERE username='$username");
That's basically how you structure a tick-based game. You define "jobs" to be run by cron

every 'x' minutes which will regulate game-play. For text-based games time usually defines

how it all fits together.


Turns

First of all, what we want to do is create a file that will be accessed just by the server

through Cron Jobs. You can choose to either have that file in a non-public folder, or in a

public folder chmodded to '700'. For this tutorial we're going to have a file "turn.php" in

an non-public folder.

Next you'll want to set-up a cron to run that file any amount of time you choose, however

rather than tell you how that's done, here's a link to Tim's tutorial on PHP and Cron:

http://forums.hostmatrix.org/showthread.php?t=3392

Now that your cron is setup, we can begin the coding!

That's exactly the same as the "Time" section, and that's because it's the same process.


Using turns in a game is a more commonly chosen method. They are usually used as currency

which is distributed evenly to all players at a regulated rate. This controls game play so

that you can't stay online all day and do better than other players.

Only an incredibly simple code is needed to be placed in turn.php. All it needs to do is

add 'x' amount of turns to each players total amount of turns:
<?php
mysql_connect("localhost", "user", "pass");//Connect to mysql
mysql_select_db("db");//Select the database

$result=mysql_query("SELECT * FROM accounts");//Select all the accounts from the table

"accounts"

while($r=mysql_fetch_array($result)){//Begin the loop with the selected accounts
$turns=$r['turns'];//Current turns
$newturns=$turns+1;//New turns. I've used +1 however you can use whatever number you'd like
$username=$r['username'];//Set the players username. This is just for identification

purposes.

mysql_query("UPDATE accounts SET turns=$newturns WHERE username='$username'");

}
?>
That's basically all you can/need to have in your turn file unless you want to update

networth or rank, which you can see above.

You'd then just have players spend their turns in the game as currency, and they'd slowly

be replenished by this script. If you need help understanding how to have players spend

their turns, read PHP Gaming Part 2. It explains how to use Money or other resources you

may have, and the same can go for the spending of turns.


Thank you very much for reading PHP Gaming Part 3. I hope you enjoyed it and found it

helpful!