So You Want to Build a PHP Game Huh?

This type of question plagues the DIC board and dominates the PHP forum most of the time. How does one go about building a PHP game like ___________ (fill in the blank)? I have had my fair share of web based PHP gaming startups and will tell you more about the leg work needed into creating a solid game foundation in PHP…. on this entry of the Programming Underground!

During my over a decade of web experience I have had the chance to participate in various levels of web game development. I have also consulted on many projects showing newbie programmers how to setup their game system to create those games like Utopia or Lords etc. I am here to tell you, it is not for the faint of heart! It takes a lot of work because you are essentially building a complete web application and all the support tools that go behind it. I cover this topic briefly in a pinned thread in the PHP forum but I will expand a bit more on here since it is such a popular topic.

1) Draw up your plans and make them as complete as possible – Any good game design company always draws up (often called storyboarding) their game. They develop the characters (both main and supporting) and the whole event time line. From the very beginning of the game where princess Zelda is taken away to the moment Mario saves her for the last damn time at Bowser’s castle. You do this drawing up with good old fashion paper and pencil and you write out the story. You know how many times I have jumped on board a project to find out they don’t know where to go with the game’s story? It is almost certain doom for a project. So don’t let this happen to you!

Describe the characters, how do they act, where do they come from, how do they learn throughout the game. The more detail you can put down in your plans the less you have to actually think about it during the programming part. This rule goes for all games, web, computer, console or whatever.

List out all the pages you might need to pull it all off… you won’t be able to name them all, but try to get as many as you can. Things like login.php, main.php, logout.php, accounts.php, resources.php, etc etc. At times I have had as many as 20 – 30 pages listed and still needed to make more later. This list is just going to serve as the basics and give you an idea of the scope of your project. If you got too many files listed, then perhaps you need to scale the project down a little and grow it later.

Lastly you may want to draw up how you are going to charge players (if you want to make money on this otherwise you can leave it alone and I suggest you do at the very beginning… focus on the game), how the economy works, how players trade etc.

2) Figure out what you are going to need to pull everything off – This tip is about resource gathering. Based on your nice and complete plans you listed in part one, you can get a better idea of what you actually need. You probably will need a way to store the characteristics of a player, maybe store where they are at on the map or how much gold they have. Storage in the world world comes in two main forms… text files and databases. Text files are great for quick storage but quickly breaks down when you get into more extravagant scenarios. They can be hard to sort, time consuming to read and write to, and if you have thousands of players it can get down right horrible trying to share one file or provide a file for each player just to track their gold usage! That is where databases come in and one of the best that works well with PHP is MySQL. It is free, it is known as an indexed database which means that it stores its records in a very efficient lookup system that makes reading data fast.

Besides storage to hold all the user data and character stats, you will need a way to actually create pages and post them on a website to show to the players. For that you are going to need a SOLID web host. One that is reliable, one that has great response times and is almost always up. Remember, if your players are playing your smash hit and the site goes down, they get pissed and leave, you lose capital on advertising opportunities and soon no one is playing anymore.

Your typical web host can cost you a bit but it always depends on the package you get and its “specs”. How much space does it have to store pages? How much bandwidth does it have (the amount of data it lets you send back and forth to the host… aka traffic)? What does it have installed (does it have PHP and MySQL databases? If so, how many databases)? Lastly, what do they charge if you go over your limits? Hey, your game might become bigger than you think and you don’t want to get hit with huge charges when your advertisers have not yet arrived! The average host that provides good service can cost you anywhere between $6 – $15 USD a month which often includes a domain name for an extra $15 a year (and yes you will need one of those too).

So here is the list of everything you will need as a checklist…

1) A webhost that offers PHP 4 (5 preferably)
2) One or more MySQL databases
3) A few hundred megabytes of space
4) At LEAST 5 gigabytes a month in bandwidth traffic
5) Cron job capabilities (the ability to run scripts on a schedule)
6) And for solid performance I recommend hosting it on a Linux machine with a good processor and connection to the Internet.
7) At least a 99.8% uptime guarantee. (Don’t want your game to go down later, costing you money)
8‌) LEARN PHP! – Find some books and good websites along with boards (like DIC) to help you learn the language. Start from the bottom and work your way up to more advanced topics. Be an intermediate PHP programmer before continuing with the game. You don’t want to be a seasoned pro at the end and see stupid mistakes you made earlier which is effecting your game’s operation.

3) Create the database schema – Now that you got your plan on paper, your equipment setup and ready, you need to get to work on putting your plan into motion and I always prefer to start on the database first. I do this first because a lot of your pages are going to be built on top of your databases data. So how do you build it? Well, you come up with a list of objects in your game and simply convert them to database tables.

For instance… I am going to have players. What is a game without the players? So I will have a table called “accounts” which will have fields that describe the players. But most importantly it is going to have a field that gives the account a unique number that no other players have. This number will be used to tie it to other tables to keep track of the player’s assets like “units” and “resources”. So that table may look like this…

Table “Accounts”

acctID – Unique ID for this account
firstname – player’s first name
lastname – player’s last name
created – when the account was created
packageplan – what type of account is this? Gold, silver, premium, special etc (for charging a fee later if you want)

Now that you have an accounts table, perhaps you are going to want tables for other objects like “units” which will describe the user’s units for the account. The number of knights or soldiers or pikemen, archers, swordsmen etc. It might look something like this..

Table “Units”

ID – primary key for the table just to identify the row
acctID – Unique player’s ID that ties this row of the table back to the player’s account it belongs to
knights – number of knights the player has
archers – number of archers they have
swordsmen – number of swordsmen

Now that we have a general relationship here (a player and their units) you can do the same for tables like “resources” which will track gold, wood, stone, whatever. Remember this doesn’t just apply to something medieval, it can apply to any type of game. Your storyline and game style will dictate what types of tables you have.

4) Build a template, you won’t regret it! – Once you got a few tables going then you will want to set out to make a nice “template” web page which will serve you as the framework of all pages yet to come. This one page should be tested thoroughly and put through all the tests you can muster up. The better you test it, the more solid it will be for your game. Test it in different browsers, plan out where all your game elements are going to be. Where is the menus? Where do they manage their account? what are the battle screens going to look like and be added to the template?

This template is also going to be the perfect place to add in your style sheets, database connections, perhaps some features to handle multiple languages etc. Anything that will be intrinsic to just about every page of your site. If you set it up in the template then when you go to replicate the template, all that functionality goes with it. I always recommend that you make your database connection and CSS files in separate pages and “include” them into your template. That way later you can change the separate files and it will change for all your pages.

Once you have a solid template, save it in a folder by itself and call it something like “template.php”. Now each time you want to make a new page for the game, you can take a copy of this page and fill in the specific content. Your database connection and styles already available and ready to go.

5) Replicate your pages for the basics and nothing more – Just start small and then build up as you go. Get the main pages out of the way, your login screen, your main screen display, any error pages you will show, logging in and logging out mechanism, and your main category pages (resource overview, unit overview, purchasing etc). Only replicate the pages you need to get a basic game scenario running. Once the basics are in, then you can expand into the finer pages. A test you can do at this point is login, move around the screens, see if it is all looking ok, logout and login again. Is everything working as it should? Good!

6) Add more pages showing more details – This is where you expand on the “core” pages you made in step 5. This time you put in the finer detail screens, stat pages, account management pages, maybe some nicer images etc. Fill it with better content, write in a help system as you go along etc.

7) Get a few friends to try it out – I do stress a “few” here. Pick up a small team of testers and give them an account. I wouldn’t do more than maybe 4 or 5 close friends that you know will test it harshly but not take advantage of some security hole. Instead they can report it to you. Fix any problems they find and listen to their input! Often more times than not their first impressions are going to catch things that totally skipped past you and they will also echo the problems all the other users will find later if not fixed. A good solid testing period should be a few months.

8‌) Launch to a bigger group of testers – Expand your circle of testers to those people who know about you but maybe you wouldn’t trust with your life. These testers are your “beta” testers who will pick up the smaller stuff like display problems, maybe catch the more rare bugs etc. Have a forum or message board they can talk to you on and collaborate on so that they can report the problems to you and other users. Get the collaboration in and participate with the users!

9) Launch the game to the masses – Ok, it is your moment to shine! Let the horde in! Let them sign up and monitor what happens closely. I like to launch during a weekend or some time I know I will be on the computer for a few days straight to see what happens and fix bugs quickly. This is where good design will help you in that you can quickly narrow a potential threat and neutralize it if it becomes a problem.

10) Now your game is out there and picking up steam – How to expand it? Well, you have your template right? You can always add more pages and attach them to existing pages but go slowly. Don’t try to add more than one or two pages over a given week. You want to expand and develop new features but not scare away the masses!

11) OPTIONAL: If you are looking to charge money to play, let them know when they signup and start off cheap for those people have helped you test and jumped on first. The idea is that you want people who have been on to stay on the game while getting new recruits to bring in the money. Later you can bring them all up to the same level after you think it is safe to do so. The last thing you need is people running off because you are imposing a huge fee right off the bat!

So I hope this blog entry has helped you get an idea of the steps needed to make a successful game. If you have questions be sure to drop them on Dream.In.Code… that is what we are there for. Maybe you can even gets some game books for general game programming and apply the theory to your project. The sky is the limit!

Thanks for reading.

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 25 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.