Alright so, I promised I'd give back to community, and I'm pretty much a fan of the smooth service I've enjoyed here with Vlexofree so far, so I thought I'd contribute a tutorial on how to make the most basic version of web-based game.
Skills You Will Need
Knowledge Of PHP
Experience With MySQL
Now let me get into explaining what this tutorial is. It's not a grand tutorial telling you how to make all the convenient features that you all enjoy in most games, but it is giving you the basic back-end set up as to how to make a game. What I'm not including is the smooth transitions, a styled CSS's, nor the log in protocols that would be needed for a game like this incase you wanted to keep some kind of record. USE THE SUGGESTED FILE NAMES OR THIS TUTORIAL WON"T WORK!!!
Let's start it out with the MySQL, we're going to need at least 2 databases, one for your account information, and one for keeping track of your quest information.
mysql code
-- Database: `tutorial` -- -- -------------------------------------------------------- -- -- Table structure for table `accountdb` -- CREATE TABLE IF NOT EXISTS `accountdb` ( `id` int(11) NOT NULL AUTO_INCREMENT, `acc_info` longtext NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; -- -- Dumping data for table `accountdb` -- -- -------------------------------------------------------- -- -- Table structure for table `questdb` -- CREATE TABLE IF NOT EXISTS `questdb` ( `questid` int(11) NOT NULL AUTO_INCREMENT, `quest_info` longtext NOT NULL, PRIMARY KEY (`questid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; -- -- Dumping data for table `questdb` --
Pretty basic looking huh? Why aren't there more fields? Well because we're going to be storing some sexy arrays, and yes long funky blobs of text can be sexy, don't doubt my knowledge
Now we're going to add a basic database access script, so pay attention to the following lines:
Quote
define("DB_SERVER", "localhost");
define("DB_USER", "ENTER HERE");
define("DB_PASS", "ENTER HERE");
define("DB_NAME", "ENTER HERE");
define("DB_USER", "ENTER HERE");
define("DB_PASS", "ENTER HERE");
define("DB_NAME", "ENTER HERE");
Database.php
<?
define("DB_SERVER", "localhost");
define("DB_USER", " ");
define("DB_PASS", " ");
define("DB_NAME", " ");
class MySQLDB
{
var $connection;
function MySQLDB(){
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
}
function query($query){
return mysql_query($query, $this->connection);
}
};
$database = new MySQLDB;
?>
Alrighty now, let's get our hands dirty and start to have a little bit of fun...create a document called, Input.php, and copy the following lines into it.
Input.php
<?
require_once('Database.php');
$Quest_Name= 'Quest Name';
$Quest_Description= 'Quest Description';
$Level_Restriction= 'Level Restriction';
$Energy_Cost= 'Energy Cost';
$EXP= 'EXP GAINED';
$MONEY= 'MONEY GAINED';
$quest = array($Quest_Name,$Quest_Description,intval($Level_Restriction),intval($Energy_Cost),intval($EXP),intval($MONEY));
$quest= serialize($quest);
$q=MYSQL_QUERY("INSERT INTO questdb (quest_info)".
"VALUES ('$quest')");
?>
So basically the above is how you add new quests directly from your CP, usually I'd create a CMS method, but I'm sure most of you know basic HTML, so please direct your eyes to these lines:
Quote
$Quest_Name= 'Quest Name';
$Quest_Description= 'Quest Description';
$Level_Restriction= 'Level Restriction';
$Energy_Cost= 'Energy Cost';
$EXP= 'EXP GAINED';
$MONEY= 'MONEY GAINED';
$Quest_Description= 'Quest Description';
$Level_Restriction= 'Level Restriction';
$Energy_Cost= 'Energy Cost';
$EXP= 'EXP GAINED';
$MONEY= 'MONEY GAINED';
Change these values accordingly to how you want the quest to be...now your probably wondering how to execute this? Simply view the file. It should show up in your mySQL db, under "QuestDB", if it doesn't you've done something wrong.Please make a quest now, or else you'll think your doing something wrong later down the line, but keep the energy cost below 100.
Now let's go do something about making your account...
Create.html
<html> <title>The Game Tutorial</title> <form method="post" action="Protocol.php"> ACCOUNT NAME: <input type="text" name="acc_name" maxlength="30"> <input type="submit" name="new_acc" value="Create Account"> </form> </html>
Pretty much the most basic form in the world...but like Mafia Wars, your account name is your user log in...now we're going to add the back end to that tiny form and set up the account data and inject it into mysql. I added in the level manually, and a neat little integer at the end for kicks later in this tutorial.
Protocol.php (Part 1)
<?
require_once('Database.php');
if($_POST['new_acc']){
$account_name= mysql_real_escape_string($_POST['acc_name']);
$account_energy = '100';
$account_exp = '0';
$account_money = '0';
$account = array($account_name,1,intval($account_energy),intval($account_exp),intval($account_money),0);
$account = serialize($account);
$q=MYSQL_QUERY("INSERT INTO accountdb (acc_info)".
"VALUES ('$account')");
header('Quest.php');
}
//Add Second Part Here
?>
Alright so now you can create you account if you so please, but it will send you to a page that doesn't exist quite yet...and that page is the page where all the quests are listed...
Quest.php
<?
require_once('Database.php');
$query = "SELECT id,acc_info FROM accountdb ORDER BY id DESC LIMIT 1";
$result = $database->query($query);
$num_rows = mysql_numrows($result);
for($i=0; $i<$num_rows2; $i++){
$acc_id = mysql_result($result,$i,"id");
$acc_info = mysql_result($result,$i,"acc_info");
$acc_info = unserialize($acc_info);
}
?>
<table width="100%">
<tr>
<td width="50%">
<? echo 'Level '.$acc_info[1].' '.$acc_info[0]; ?>
</td>
<td width="50%">
<? echo 'Energy: '.$acc_info[2]; ?>
</td>
</tr>
<tr>
<td colspan="2">
<div style="height:300px; width:100%; border:1px solid black; overflow-y:scroll;">
<?
$query = "SELECT questid,quest_info FROM questdb";
$result = $database->query($query);
$num_rows = mysql_numrows($result);
for($i=0; $i<$num_rows2; $i++){
$questid = mysql_result($result,$i,"questid");
$questinfo = mysql_result($result,$i,"quest_info");
$questinfo = unserialize($questinfo);
echo "
<table width=\"100%\">
<tr><td width=\"50%\">
$questinfo[0]<br>
$questinfo[1]
</td><td width=\"10%\">
Lv. Restriction<br>
$questinfo[2]
</td><td width=\"10%\">
Energy Cost<br>
$questinfo[3]
</td><td width=\"20%\">
Award<br>
$questinfo[4] EXP<br>
$questinfo[5] CASH
</td><td width=\"10%\">
<form method=\"post\" action=\"Protocol.php\">
<input type=\"hidden\" name=\"quest_id\" value=\"$questid\">
<input type=\"hidden\" name=\"acc_id\" value=\"$acc_id\">
<input type=\"submit\" name=\"do_quest\" value=\"Do Quest\">
</form>
</td></tr>
</table>";
}
?>
</div><br>
<? echo "Number of Completed Quests: '.$acc_info[5]; ?>
</td>
</tr>
</table>
?>
This is the basic interface for how you'll be doing your quests, mind you this is just the raw code your seeing inside of a table. I'd leave it up to you to style it later on. Anyway, the above code sends a hidden value when you click the "Do Quest" button...taking us back to the 2nd part of the Protocol.php...add this where it says "Add Second Part Here"...
Protocol.php (Part 2)
if($_POST['do_quest']){
$questid= intval($_POST['quest_id']);
$acc_id= mysql_real_escape_string($_POST['acc_id']);
//Getting Quest Info
$query = "SELECT quest_info FROM questdb";
$result = $database->query($query);
$num_rows = mysql_numrows($result);
for($i=0; $i<$num_rows2; $i++){
$questinfo = mysql_result($result,$i,"quest_info");
$questinfo = unserialize($questinfo);
}
//Getting Account Info
$query = "SELECT acc_info FROM accountdb WHERE `id`='$acc_id'";
$result = $database->query($query);
$num_rows = mysql_numrows($result);
for($i=0; $i<$num_rows2; $i++){
$acc_info = mysql_result($result,$i,"acc_info");
$acc_info = unserialize($acc_info);
}
$acc_info[2]=$acc_info[2]-$questinfo[3];
$acc_info[3]=$acc_info[3]+$questinfo[4];
//Check for level-up...
if($acc_info[3]>=100){
$acc_info[3]=$acc_info[3]-100;
$acc_info[2]++;
}
$acc_info[4]=$acc_info[4]+$questinfo[5];
$acc_info[5]++;
$acc_info= serialize($acc_info);
$q = MYSQL_QUERY("UPDATE accountdb SET `acc_info`='$acc_info' WHERE `id`='$acc_id'");
header('quest.php?');
}
If you get lost a bit, here is what the entire Protocol.php should look like...
Protocol.php
<?
require_once('database.php');
if($_POST['new_acc']){
$account_name= mysql_real_escape_string($_POST['acc_name']);
$account_energy = '100';
$account_exp = '0';
$account_money = '0';
$account = array($account_name,1,intval($account_energy),intval($account_exp),intval($account_money),0);
$account = serialize($account);
$q=MYSQL_QUERY("INSERT INTO accountdb (acc_info)".
"VALUES ('$account')");
header('quest.php?');
}
if($_POST['do_quest']){
$questid= intval($_POST['quest_id']);
$acc_id= mysql_real_escape_string($_POST['acc_id']);
//Getting Quest Info
$query = "SELECT quest_info FROM questdb";
$result = $database->query($query);
$num_rows = mysql_numrows($result);
for($i=0; $i<$num_rows2; $i++){
$questinfo = mysql_result($result,$i,"quest_info");
$questinfo = unserialize($questinfo);
}
//Getting Account Info
$query = "SELECT acc_info FROM accountdb WHERE `id`='$acc_id'";
$result = $database->query($query);
$num_rows = mysql_numrows($result);
for($i=0; $i<$num_rows2; $i++){
$acc_info = mysql_result($result,$i,"acc_info");
$acc_info = unserialize($acc_info);
}
$acc_info[2]=$acc_info[2]-$questinfo[3];
$acc_info[3]=$acc_info[3]+$questinfo[4];
//Check for level-up...
if($acc_info[3]>=100){
$acc_info[3]=$acc_info[3]-100;
$acc_info[2]++;
}
$acc_info[4]=$acc_info[4]+$questinfo[5];
$acc_info[5]++;
$acc_info= serialize($acc_info);
$q = MYSQL_QUERY("UPDATE accountdb SET `acc_info`='$acc_info' WHERE `id`='$acc_id'");
header('quest.php?');
}
?>
Whelp after you do that, go test it. It should work, when you create a 2nd account, it won't over-write the first, it will simply add a new entry. So if you know enough PHP, or already have a standard log in, plug it in, and there you go. I've given you all the necessary tools to make your own Mafia Wars type game. Feel free to post questions, or even report a glitch in the code. If you use the code, please credit Vlexofree.com somewhere on your website!!! And on a side note, HAPPY GAMING!!!












