Introduction to Web Game development Tutorial three (simple program application) _php instance

Source: Internet
Author: User
Tags rounds
Introduction to Web Game Development Tutorial II (game mode + system)
Http://www.php.net/article/20724.htm

First, choose the development language
Backstage: java. NET PHP
Front Desk: Flex JavaScript Ajax
Database: MySQL MSSQL
It really doesn't matter what kind of combination you use. The important thing is time and cost. The complexity of the place in data interaction and refinement, rather than the realization of technology or effect. Often encounter some problems. Like how the map is compiled? How do people move? In fact, these problems from the technical implementation are relatively easy. How the data interacts after implementation is difficult. There is no solution to the problem of data interaction, and the realization of these technical points is insignificant. I'm using a php+javascript+mysql.
Reason: Simple, quick to get started. Can be relatively fast out of the product.
Second, the procedure simple application.
Template
For ease of UI modification. So use a template. Smart template is also convenient. Very simple. The code can also be nested within the template. The only problem is that if the art does not program, modify the template to get the program. Not science.
The smart template tutorials are available online. Just say a little. Can be used in templates (. html files) Nesting any code. Get the pass value. Use $_obj[' xxx '] or use $_stack[0][' "] can be written with {XXX} code nested. As with the. php file, there is no difference.
Map
Because the game type is not Ogame mode, the map is not automatically generated. Instead, it is all called from the database. The idea is simple. The map is a whole big picture. Cut into smaller tiles. The absolute coordinates of each small tile corresponding to a large map are recorded in the database. When displayed, a small tile of the corresponding coordinate area is called.
Code similar to:
$sql = "SELECT * from map where mapx between $xxx and $xxx and mapy between $ yyy and $yyy";
It means to get the horizontal axis xx to XX from the map table. All small tiles of ordinate xx to XX. Like 20. Let's say we write a function showmap (x, y) to show all the data we get. Maps can have many layers.
Each small tile is a div. The specific control on the use of CSS on the line. Small tiles can be used as the background of a div. It can also be used as a picture in a div. You can control the left and top of the div. (left and top are the absolute coordinates of the small tile relative to the large tile) Showmap (x, y) is placed in the two layers below.
One layer handles map size:

One layer handles the drag:
Copy CodeThe code is as follows:

Handles the dragged JS code. (Online copy of the. Thanks for this big. )


Note the following code:
Ajaxread (' map.php?mapx= ' + (e.clientx-x_) + ' &mapy= ' + (e.clienty-y_) + ', ' 2 ');
The position of this code is triggered when the mouse is released when the layer is dragged. You can use alert ("Drag the map Here"); Replace. Test the effect. This code means that the coordinates are dragged according to the current map. Call an Ajax. That is to get the map information back from the database. Ajaxread () is an Ajax calling function. You can write it all yourself. It can also be written in frames such as prototype.js.
The code that handles Ajax. (or the online copy, there are slight changes ...) Alas, how old copy? The main purpose is to save development time. The other thing is that my javascript is very rubbish (*^__^*) hehe)
Copy CodeThe code is as follows:
function Ajaxread (file,action)
{
var xmlobj = null;
if (window. XMLHttpRequest)
{
Xmlobj = new XMLHttpRequest ();
}
else if (window. ActiveXObject)
{
Xmlobj = new ActiveXObject ("Microsoft.XMLHTTP");
}
Else
{
Return
}
function Ajaxdo (action)
{
Switch (action)
{
Case "2":
document.getElementById (' Display '). InnerHTML = xmlobj.responsetext;//The display here is your ID on the top of the page. The map code above needs to be placed in this layer. such as write ID and name, is to facilitate the compatibility of Firefox and IE.
Break
}
}
Xmlobj.onreadystatechange = function ()
{
/*
if (xmlobj.readystate = = 1)//loading status.
{
document.getElementById (' Xianshi2 '). InnerHTML = "Loading in";
}
*/
if (xmlobj.readystate = = 4)//When the state is completed.
{
Ajaxdo (action);
}
}
Xmlobj.open (' GET ', file, true);
Xmlobj.reload (' GET ', file, true);
Xmlobj.send (NULL);
Xmlobj.abort (");
}

The whole code means:
When you drag the map to release the mouse, the display layer re-obtains the data. There is no refresh displayed. The pictures in the map all use the PNG32 transparent diagram. Ie7 and FF3 are fine. Encounter IE6 words. Replace it with a GIF. The map.php function. According to the obtained X, y display the corresponding one thank you small tile. This is actually the Showmap (x, y) mentioned above, which is much like the drag of Google Maps. But it's a lot easier. Simple, the effect is good. 2, Angle 2, role attributes
Because of the set requirements. The character needs to have equipment bonus, state bonus (Buff,debuff). At this point, put all the necessary bonus into the character class. is a good way to do it.
Probably like this:
Copy CodeThe code is as follows:
Class role
{
Get the role data.
Getrloe ()
{
Get role information from the database.
}
Get equipment bonus.
Getequip ()
{
Get equipment Bonus information.
}
Get status Bonus
GetState ()
{
Get status Bonus information.
}
Add or subtract, or adjust, the information obtained above.
Returns the role data.
Return xxx
}

Specifically to put this article forward. It's because you didn't put the bonus in the character. Every time you want to fight or do something. After getting the role data, add a lot of code processing bonus. Too many repetitions. Let the code in front of the world is quiet ...
Props
The props are very special. Because of the variety, the use of many ways, there may be multiple storage locations, there may be unique props. One day saw the code of Web Warcraft. Found only one watch for his props. There is a field to handle the prop position, such as (1, Auction house, 2, backpack, 3, Warehouse, 4, Store) This method is very good. However, if the complexity of the props up. For example, different warehouses, different auction houses, need to synthesize and so on. Or just a sub-table.
Basic Props table:
Id
ItemName Name
Itemprice Price
Itemimage Pictures
ItemType type
Uptype Add-on type
Uppoint Add points
AddType add type (permanent)
Addpoint Add points (permanent)
Cleardebuff Clear Debuff
Addbuff Add Buff
Starting from Uptype. Can be written in the form of Xx|yy|zz. Best one by one corresponds. The split symbol can be selected by itself.
When you invoke and manipulate data, you can use the following method:
Copy CodeThe code is as follows:
$uptype = Explode ("|", $iteminfo [' Uptype ']);
$uppoint = Explode ("|", $iteminfo [' uppoint ']);
for ($j =0; $j {
echo $uptype [$j];
echo $uppoint [$j];
}

Warehouses, auction houses, shops, backpacks and so on. A place to carry props. As long as there is an ID field to save the item ID can be. As for the horizontal table or the longitudinal table, choose according to the actual needs. So far, props seem to be working well. At this time, planning to say. The props need to be unique and need to be enchanted. OK, so you fill in the props table with all the combinations. Synthesis is also a+b=c only. A calculation. Like 40 things that might enchant you. 200 items that can be enchanted. 40*200=8000. Obviously, the plan will not agree. So the headache is the program. How to deal with it. Add a table.
Unique Props table:
ID unique item ID (cannot be duplicated with normal item ID.) Convenient backpack and so on call)
temp_id Temporary ID (default 0. It may be used when crafting a prop. )
Itemid original item ID (get the initial value of the item)
fumo_id Enchantment ID. (default 0, i.e. no enchantments)
Enchantment table: (That is, the added attribute)
Id
Uptype Add-on type
Uppoint Add points
Cleardebuff Clear Debuff
Addbuff Add Buff
See feature changes Now
The first is the prop class:
Class Item
{
GetItem ()
{
Previously, it was OK to get the item information directly from the ID.
Added Enchantment now
First, determine if the item ID belongs to the unique item. (such as ordinary props 1-10000.) The unique item ID starts from 10001. If you feel this is not good, then the basic props table, add a field. Determine if the item is unique)
if (prop only)
{
Get the original item ID and enchantment ID from the unique prop table
Based on the original item ID, or the prop base information.
The Enchantment bonus information is obtained based on the Enchantment ID.
Add the values on both sides.
Return Item information.
}
Else
{
Get item information directly.
}
}
}
Enchantment function:
Item A. (Base item) + Prop B. (base prop) = Prop C. (Only props)
That is, the only props are generated when the enchantment function executes. Take a backpack for example. No enchantment before.
Backpack inside props A. The ID is 1.
Backpack in the Prop B. The ID is 2.
When the Enchantment function is performed. Item A, the ID of item B is set to 0 (horizontal table), or deleted (vertical table). Generates a unique number. temp_id. (MD5 generation is OK.) ) to generate a unique prop. At this time, according to TEMP_ID, let A's backpack get the unique item ID again. Props, a relatively perfect solution.
The following sections deal with some business issues, so you can only give ideas, and very little code.
--------------------------------------------------------------------------------
, Time-memory device
After processing waits xx time, executes xx's question. PHP comes with a sleep () function. The wait time can also be controlled.
But obviously, both in terms of use and efficiency. are not enough to support game timings. The idea is simple. All parameters of the event that will require a countdown, as well as the start time and end time. are stored in a table. The foreground uses JavaScript countdown, time to After, through the Ajax call time to the post-processing program. In the background every time, automatically executes the call time to the post-processing program.
A minimum of three PHP pages is required.
A content that is used to write access timing.
A processing foreground time to the end of the operation.
A processing background timed refresh, to determine the time to the end of execution, the time is not processed.
Miracle: Timers are different timers that correspond to different events, or can multiple events call the same timer, and if a player calls a timer to time a building for how long, and then calls this timer to time another building construction for how long, so not OK? Will there be conflicts?
Soot on the keyboard:
Multiple events correspond to 1 timers.
You can add a field to the timer. For example, called ActionType (event type)
Each user can handle multiple things at the same time. It's just that every thing has a fixed number.
For example, your users are allowed to do 5 things at the same time. Then the direct number in ActionType is 1-5. When you call a timer, you know that this is the user's first "thread", depending on the number.
Miracle
If it's a different user, it's not a conflict to call the same timer.
Soot on the keyboard:
Of course not. You see. A userid can be used to identify a user. ActionType can be used to determine the number of threads.
, Event control
Combine timer, process start (), procedure (), End ()
Copy CodeThe code is as follows:
Interface Action
{
function doAction ();
function beginaction ();
function processaction ();
function endaction ();
}
Simple Event Factory
Class Actionfactory
{
Public Function getaction ($what)
{
$ActionName = $what;
return new $ActionName;
}
}
such as mobile
Class Move implements Action
{
function DoAction ()
{
Specific execution function
When is the process of this line? Are here to judge.
}
function Beginaction ()
{
Executed at the beginning of the event.
Here you can save the data in the timer. The data is then taken from the timer.
}
function Processaction ()
{
Data is taken from the timer.
The procedure that the event executes. For example, when users refresh the page. If the countdown is still in. So that's the call here.
}
function Endaction ()
{
Data is taken from the timer.
The process of ending the event.
When the time is up, complete the event.
}
}
The first time it is called.
$Action = new Actionfactory ();
$InstanceAction = $Action->getaction ("Move");
$InstanceAction->set ($parameter);
$InstanceAction->doaction ();
When it is called later.
$Action = new Actionfactory ();
$InstanceAction = $Action->getaction ("Move");
At this time, the parameters or data of the event are obtained from the timer.
$InstanceAction->doaction ();

Fight
Instant and semi-instant round battles (two or multiplayer instant turn-based battles) are cumbersome.
Include at least:
Front desk:
Automatically receive invitation information. Ajax
Shows the battle process. Ajax
The countdown between rounds. Javascript
Background:
Send invitations, accept, reject, time out. a table. Combat data. a table. Save data on both sides or parties, including round time, the first few rounds, etc.
Combat control. A series of functions. Handles the player's actions and saves the operation to the battle data table. Time to perform the operation.
After sending troops, return directly to the battle.
Just write it in the event.
function Endaction ()
{
Data is taken from the timer.
Generate a round and generate a battle.
}
Note: Because of my work, this series may be suspended for a period of time to update, I hope you forgive me ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.