Author Topic: How PHP Games Work  (Read 6107 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
How PHP Games Work
« on: October 15, 2006, 09:35:42 AM »
I've seen a lot of people register on the forums here to learn how to make a game. They want to learn PHP quickly, or get the scripts and learn how they work. Unfortunately, it's not so simple. :( Learning to code is only part of the way. To really create a game by yourself, from scratch, or close to it, you need to understand how most games function. And I am writing this article to teach you how some classic games work :)


RPG Games
There used to be many browser-based RPG games when Exofusion was still being actively developed. Everybody could just download the script, put it on their site, and voila! They suddenly became game owners. A lot of those games died out because the owners didn't understand the game script very well, and they could further add new stuff to their games.

The old PHP games functioned like this:
You would click a link, or submit a form by clicking a button, and you would be referred to another page. On that page, the backend of the script processes your input, and updates the database tables, then displays a text like "You gained 2 Strength!". This was pretty much how everything went. Some pages were more complex with more actions, but essentially, it's just like this.
It's the same with the battles. There was not much user interaction. You simply clicked a button or link, and you saw the results:
Quote
You hit Monster1 for 4 damage! (5 left)
Monster1 hit you for 2 damage! (6 left)
You hit Monster1 for 3 damage! (2 left)
Monster1 hit you for 3 damage! (3 left)
You hit Monster1 for 5 damage! (-3 left)

You killed Monster!
You gained 3 EXP and 5 Gold!

That was pretty much how it looked like. These games are fairly simple to code, the only relatively complex parts would be the battles. If you are just starting out, you should try coding one of these. Of course, you will need to implement something that would limit the number of battles a user can perform. A limiting factor such as 'energy', or number of battles per day/hour.


Strategy Games
Now, these games are much harder to code, and also much harder to learn and play. These types of games come in many different forms, but they usually have similar cores:
Ticks and/or Turns

Ticks are events that run every so often, maybe every 10 minutes or every 30 minutes. Between each tick, you may perform whatever action you want, but not much of it is processed until the tick occurs. If you attack somebody, the results only come in on the tick. If you buy soldiers or other stuff, that will only have an effect once the tick has happened. These games are more 'strategic' since you have time to think what action you want to perform, and try to predict what enemy players might do.

Turns are also a limiting factor. Every time you perform an action, your own number of 'turns' decreases. Once it has reached zero, you must wait for a 'reset' where your turns will be reset or added to. Maybe different actions take up different amounts of turn for variation. Bigger actions take more turns, while smaller actions can be performed more times since they only take up a few turns. The strategic part of this type of game is in deciding what you will spend your turns on. After you have decided, there may still be other limiting factors, such as cash or skill level. Different requirements for different actions make sure you think out your actions carefully.

Strategy games often require heavy use of 'cron jobs'. Cron jobs are actions run by the server at certain times. In CPanel, you can set different files to be run every so often, or at specific times. This is useful so that the ticks happen at the right time, regularly. Often, your cron files will be one of the largest files in your game because all the tick processing happens in that file. Everything from small purchasing actions to calculating the complete results in hundreds of battles.


Pet Games
Many pet games now are following the footsteps of NeoPets. The main idea is that you create a pet, feed it, collect items, and play games to earn cash.
Most actions here are just simply clicking an navigating to pages where actions are processed, but the games that are being played to earn cash are often more complex. These games are usually made in flash. These flash games will need to be able to send the scores to a PHP page where the score can be converted into cash and where the user's cash level can be updated.



Taking it further
Nowadays, there are new technologies such as AJAX, and new ideas. Here are a few that can be tough to code, but will really spice up your text game:
  • Turn-based Battles - This can be implemented in both strategy games and rpg games. In RPG games, you process each action immediately, and give the enemy (possibly AI) a chance to select their own action, in real-time. In strategy games using ticks, only give the results of one round of the battle. Then, before the next tick, the user must choose their action. If no action is chosen, then a default or random action will be chosen at the tick.
  • Flash battles - You will need a real flash expert for this. It must at least be able to check and update results to the server, and check if both players are present.
  • Maps - There are many variations of maps in games. It can be top-down "Zelda" style using images, and seeing the blocks all around you, or it can be a text-based map where you only see the current section your are in. There should be a table in your database that lists every field of the map, and gives information on what is on the field, and maybe what actions can be performed there.
  • Customizable characters - Give your players power! Let them change their own user profile and customize their character looks. Maybe even update the character's image depending on what equipment they have on!


I hope this article has helped you a little in your understanding of text-based browser games. This isn't meant to be a miracle article which can help you create your own game, but it should have given you an idea of the basics in a game. From here on, you should make a plan of your game, including all the features and seperate pages, and how information is processed. Having the database's table structures in mind can help too. :)

Offline drepac

  • Level 2
  • *
  • Posts: 5
  • Reputation: +0/-0
    • View Profile
Re: How PHP Games Work
« Reply #1 on: October 24, 2006, 03:16:13 AM »
thanks zeggy.  this is a great article that puts things into perspective.  I'd appreciate if you could talk some more about the use of AJAX and what framework to use with php.  There are so many AJAX implementations out there but none seem mature enough, so your feedback will be appreciated.
Thanks

Offline Mgccl

  • Level 7
  • *
  • Posts: 33
  • Reputation: +1/-0
    • View Profile
    • WebDevLogs
Re: How PHP Games Work
« Reply #2 on: November 09, 2006, 10:44:22 PM »
Ajax.
Most PHP game does not use Ajax.
Because PHP games are made to be
-Simple
And that's what I like to change...
I like to make games uses more Ajax

Ajax framework with PHP?
my-bic (google it... the first result is what you are looking for)

but I sometime think using moo.ajax + write our own PHP script will be good too

Offline npshmear

  • Level 6
  • *
  • Posts: 26
  • Reputation: +0/-0
    • View Profile
Re: How PHP Games Work
« Reply #3 on: January 09, 2007, 11:20:07 PM »

Strategy Games
Now, these games are much harder to code, and also much harder to learn and play. These types of games come in many different forms, but they usually have similar cores:
Ticks and/or Turns

Ticks are events that run every so often, maybe every 10 minutes or every 30 minutes. Between each tick, you may perform whatever action you want, but not much of it is processed until the tick occurs. If you attack somebody, the results only come in on the tick. If you buy soldiers or other stuff, that will only have an effect once the tick has happened. These games are more 'strategic' since you have time to think what action you want to perform, and try to predict what enemy players might do.

Turns are also a limiting factor. Every time you perform an action, your own number of 'turns' decreases. Once it has reached zero, you must wait for a 'reset' where your turns will be reset or added to. Maybe different actions take up different amounts of turn for variation. Bigger actions take more turns, while smaller actions can be performed more times since they only take up a few turns. The strategic part of this type of game is in deciding what you will spend your turns on. After you have decided, there may still be other limiting factors, such as cash or skill level. Different requirements for different actions make sure you think out your actions carefully.

Strategy games often require heavy use of 'cron jobs'. Cron jobs are actions run by the server at certain times. In CPanel, you can set different files to be run every so often, or at specific times. This is useful so that the ticks happen at the right time, regularly. Often, your cron files will be one of the largest files in your game because all the tick processing happens in that file. Everything from small purchasing actions to calculating the complete results in hundreds of battles.

Hey, I have been working on a game for a few months now.  I've got a ton of the game programming done, but I'm still having a rough time with one aspect of the game, and you mentioned it point black her ein this article.

I am trying to learn how to set up a tick timer for 10 minutes.  something that repeats (which im sure i'll need the cron job for).

I've already got a few crons running, so I'm assuming to repeat the timer i'll need the cron. 

I just need a 10 minute counter on my page 09:59  (where the seconds count down etc)

Can you help me build one.  refer me to a place where i can learn how this works, or any help at all, I'm getting stressed!

Thanks a bunch

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: How PHP Games Work
« Reply #4 on: January 11, 2007, 02:26:28 AM »
Well, assuming you want to have your ticks at exactly every 10 minutes (12:00, 12:10, etc), thn there's a much easier way you can do this :)
You grab the current timestamp, then find the remained when divided by 10 minutes, which will give you the amount of seconds that has passed since the last tick. Then you just need to subtract that number from the value of 10 minutes.
Something like (10*60)/(timestamp % (10*60)).
I'm not sure about the details, I'll get back to you about that :)

Offline npshmear

  • Level 6
  • *
  • Posts: 26
  • Reputation: +0/-0
    • View Profile
Re: How PHP Games Work
« Reply #5 on: January 11, 2007, 11:37:12 AM »
thanks, I'll try to come up with something.  I have to read a lot more abotu timestamps.  I was going to do this yesterday, but the internet was out and I was unable to open php.net.  or the w3schools.

But please do get back to me, I have a feeling I might have trouble, though when I get it I will let you know!

nps

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: How PHP Games Work
« Reply #6 on: January 11, 2007, 12:12:30 PM »
Here's a little snippet that I used to use in an older game of mine:

Code: [Select]
<?php
$s 
= (int) date("s");
$m = (int) date("i");
$t = (1+intval($m/10+($s-1)/600))*600-$m*60-$s;
printf("Time until next tick: %02d:%02d"$t 60$t 60); 
?>
The 'time until next tick' is the time until the next 10 minutes.

Offline npshmear

  • Level 6
  • *
  • Posts: 26
  • Reputation: +0/-0
    • View Profile
Re: How PHP Games Work
« Reply #7 on: January 11, 2007, 12:29:16 PM »
Sweet,  I see how it works now.  And i also see, that you used a bit of "C" in there, I didn't know that you could do that, mix programming languages with php...  I guess I have a lot to learn.

Well, I'll try to understand more of those date functions.  I think I can figure out how to "REFRESH" it so that you see the actual seconds counting down. 

(I'm still trying to get showered, and breakfast/late lunch in, and check the forums haha, so I havn't had the time to actually sit down and figure it out.  Just checking forums inbetween actions!  But still, thanks for the help)

NPSHMEAR

Offline npshmear

  • Level 6
  • *
  • Posts: 26
  • Reputation: +0/-0
    • View Profile
Re: How PHP Games Work
« Reply #8 on: January 11, 2007, 01:53:05 PM »
ok, about having the seconds refresh, so that the user can see them actually counting down from 9:59 to 00:00, I have no idea :(.  I've searched, and read and what not but I just cant figure it out... :(

Offline Mr.Smiley

  • Level 7
  • *
  • Posts: 29
  • Reputation: +0/-0
    • View Profile
    • MoonBaseProductions.net
Re: How PHP Games Work
« Reply #9 on: January 12, 2007, 12:53:12 AM »
I think a much better idea would lie in Javascript.  I don't think anyone wants to have their page refreshing every second, and even though I know very little about Javascript, I feel certain you could find a better answer there.

Offline npshmear

  • Level 6
  • *
  • Posts: 26
  • Reputation: +0/-0
    • View Profile
Re: How PHP Games Work
« Reply #10 on: January 12, 2007, 02:09:55 AM »
You're right, i understand that with the script that I have I would have to make th page refresh lol... But using java, i dont understand how i would get teh seconds to refresh without the page =/

ugh!  Much to learn ...

Offline Mr.Smiley

  • Level 7
  • *
  • Posts: 29
  • Reputation: +0/-0
    • View Profile
    • MoonBaseProductions.net
Re: How PHP Games Work
« Reply #11 on: January 12, 2007, 07:46:36 PM »
I found that by googling "JavaScript Countdown", I could find some very good articles on how to do it.  You could probably use Php to find out when the next tick will be, and use JavaScript to actually count down from that point.  There may be some quirks to work out, but I am sure it will do fine.  If there are any JavaScript savvy people out there, would you mind helping with this?

Offline Mr.Smiley

  • Level 7
  • *
  • Posts: 29
  • Reputation: +0/-0
    • View Profile
    • MoonBaseProductions.net
Re: How PHP Games Work
« Reply #12 on: January 12, 2007, 08:24:50 PM »
From searching, I "think" this article would be very helpful in making a more advanced countdown (Yes, you will have to change plenty for your situation) easiest.  But also, correct me if I'm wrong.  http://andrewu.co.uk/clj/countdown/pro/   :D

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: How PHP Games Work
« Reply #13 on: January 13, 2007, 07:39:53 AM »
It's not a simple process to get the page to show sync'd time with what the server time is. In fact without AJAX polling the server every second to get the new time it's impossible.

What you have to do is give javascript the current server time and let the snippet of code countdown itself. Doing this you are putting the actual countdown in the hands of each individual's browser and computer so it's not highly accurate but "good enough" for web games ;)

Step 1:
have your PHP page output the current server time. You can do this one of two ways have PHP output the code for the javascript or put the time in a hidden form variable. In my games I opted for outputting the code....

Code: [Select]
<script type='text/javascript'>
var t = '<? =date("H:i:s")?>';
</script>

Step 2:
Create the place where this time is going to be displayed. To do this you use a readonly textbox inside a form field.. Since the display of the tick time in my games goes with the flow of the layout I didn't want an ugly textbox in the middle of the page so I set it like so
Code: [Select]
<form name="frmticker" style="display: inline">
<input type="text" name="tick" READONLY style="text-align: center;width: 110px; border: 0px; background-color: transparent; color: #aaaaaa; font-family: tahoma; font-size: 8pt;">
</form>

Step 3:
You now need to create a timer in javascript that will call a function to decriment the time. A timer is basically the same as a scheduled task.. You give the timer how many milliseconds to wait and the function name to call when the wait is over. The "gotcha" in this is you can't start calling functions or whatever until the entire page renders on the browser so you will need to call the countdown script in your body tag. You can forgo this step and it might work for YOU but it may not work on the next load of the page, for the other players etc etc because without calling it from the onLoad event its subject to how and when the individual browser receives and renders the page.
Code: [Select]
<body onLoad="initTicker();">

Step 4:
Finally the actual javascript to make everything work :)
Code: [Select]
<script type='text/javascript'>
// Init vars
var tArr = t.split(':')
var mins = parseInt(tArr[1])
var secs = parseInt(tArr[2])

var muliplier;
if( mins >= 0  && mins < 10 ) muliplier = 1;
if( mins >= 10 && mins < 20 ) muliplier = 2;
if( mins >= 20 && mins < 30 ) muliplier = 3;
if( mins >= 30 && mins < 40 ) muliplier = 4;
if( mins >= 40 && mins < 50 ) muliplier = 5;
if( mins >= 50 && mins < 60 ) muliplier = 6;

var tmins = (muliplier * 10) - mins - 1;
var tsecs = 60 - secs;

// Start the timer, will call the timer function every second
function initTicker() {
var doTime = setInterval('timer()',1000);
}

// timer function to decriment time and display on page
function timer() {
  tsecs--;
  if(tsecs < 0) {
    tmins--;
    tsecs = 59;
  }
  if(tmins < 0) tmins = 9;
if( (tmins == 0 && tsecs < 5) || (tmins == 9 && tsecs > 50) )
  document.frmticker.tick.value = "Processing"
else
  document.frmticker.tick.value = "tick: " + ((tmins < 10) ? '0' : '') + tmins + ':' + ((tsecs < 10) ? '0' : '') + tsecs;
}
</script>

You don't need the multiplier stuff above.. I used it because of a function in my game where I needed to know what tick it was in the hour.

So there you have it, it's how I do it.. It's pretty good with accuracy unless someone just sits there with the page open for hours and hours and thus the javascript hasn't gotten a good "refresh" of the server time but that, really, has never happened (but it could LOL).

Creating online addictions, one game at a time:

Offline toto

  • Level 12
  • *
  • Posts: 82
  • Reputation: +1/-0
    • View Profile
Re: How PHP Games Work
« Reply #14 on: January 13, 2007, 09:08:50 AM »
Thanks for the snippet of code, codestryke. I guess I'll be using a similar one in my next project (I usually left pages static)

Offline npshmear

  • Level 6
  • *
  • Posts: 26
  • Reputation: +0/-0
    • View Profile
Re: How PHP Games Work
« Reply #15 on: January 22, 2007, 10:40:42 AM »
Hey guys, I've been so busy with classes, that I've just now been able to look at this code.  Been reading "Running Money"  About Hedge funds etc and marketing intellectual property!  Very nice.

Thanks for the snippets, I will be reviewing them to learn more probably this evening.


Offline npshmear

  • Level 6
  • *
  • Posts: 26
  • Reputation: +0/-0
    • View Profile
Re: How PHP Games Work
« Reply #16 on: January 22, 2007, 02:10:21 PM »
Hey codestrike, I've read and tried to apply this to my code but I'm not sure where to put the actual JAVASCRIPT.  I figured I needed to put this inbetween my <head> </head>  so I did, then it gave me the error:  tick: undefined:NaN, so Now I'm going to look through my code and see if I did something wrong.

Any ideas?

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: How PHP Games Work
« Reply #17 on: January 27, 2007, 09:29:10 AM »
Well, the NaN stands for Not a Number, so somewhere in your javascript, a variable is not what it should be (it should be a number).
To figure out the problem, try using alert() to show your different variables, like alert(t), alert(mins), etc. Check that all of these are numbers. If they're not, you will just need to find the line where that variable is set.

Offline npshmear

  • Level 6
  • *
  • Posts: 26
  • Reputation: +0/-0
    • View Profile
Re: How PHP Games Work
« Reply #18 on: January 27, 2007, 09:34:49 AM »
Hey, thanks for that info Zeg, I have been trying on my own in my freetime between classes to learn java script.  I have this class, its internet computing, and we're supposed to learn "Java" and unfortunately for me I have to wait until NEXT FALL, to get to the higher level class where we do the Javascript and more webbased server/client side programming.  Though I've been going through this website here for good javascript help and tutorials (best i found)

http://www.w3schools.com/js/default.asp

You can sticky that in the javascript forums i suppose, its a very nice resource next to this forum!

Offline Shapulin

  • Level 9
  • *
  • Posts: 50
  • Reputation: +2/-4
    • View Profile
Re: How PHP Games Work
« Reply #19 on: May 08, 2007, 12:24:05 PM »
excelent!!!!

u must do a tutorial , and this could be chapter 1

 :D

Offline sokii

  • Level 14
  • *
  • Posts: 107
  • Reputation: +2/-1
  • Bored? Yea me too... :/
    • View Profile
    • Pyloth
Re: How PHP Games Work
« Reply #20 on: November 08, 2007, 06:53:13 PM »
yes very nice :]
Forum Designer Since: March 23, 2006
We are still looking for staff at Pyloth! Send me a PM at my forums located here.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal