PHP web game development introductory tutorial 1 (webgame+design)_PHP tutorial

WBOY
Release: 2016-07-21 15:43:23
Original
1371 people have browsed it

1. Simple program framework.
The webgame program consists of:
Three parts.
The first is data flow. The second is procedure. The third is art.
Among them, the data flow includes functions. Data flow can only be reflected in functions.
The data process is quite troublesome and will be discussed later.
For example, the simplest way to sell and buy products.
To implement this function.
Then you need to have a basic product table, a detailed product table, a store table, and a backpack table. If the scalability is stronger, the corresponding double tables are indispensable.
The questions on the table are all simple. The key is what this item is used for. In this way, there is a lot of data about the source of the items, and a lot of data about the direction of the items.
Finally, the data has to go around in a circle.
Circles are a difficult thing to do. Especially when there are more functions and props. The difficulty is 2 to the nth power.

Before going around in circles, if you are more proficient in design patterns. Then the process can be simplified. The difficulty changes from 2 to the nth power to 1.
Only need to have three virtual classes: controller, event factory, and abstract prop factory; plus timer and task editor, these two general classes. That is, you can build a robust and highly scalable webgame.
In webgames, controllers are almost equivalent to pages. It can be easily processed by using any template technology.
The event factory is an abstract class. All events, such as work, combat, movement, etc., are produced by the event factory. And the interface is the same, which is convenient for controller control. Factory mode.
Abstract prop factory is an abstract class. All props, such as cities, maps, equipment, etc., are produced by abstract prop factory. And the interface is the same, factory mode, the combination of events and props is a bridge mode.

Art:
UI. There are always benefits to a simple and beautiful interface. Small icon. Props, maps, equipment. At least 10 in one category, right? Roughly hundreds of them are needed.
The program is divided into 5 parts:
Server timer. (C language or setting up your own server) Execute a certain piece of code in a timed loop. This code is mainly updated based on the data in the database. You can find a C language programmer to do this. For C language programmers, this function is quite simple. Of course, you need to write the specific judgment of processing data and operating the database yourself. Just ask the C programmer to give you a piece of standard code. Fully supports sql statements.
For php, you can configure corn to implement it. But no matter what operating system it is, the minimum configuration time is 1 minute. So, if you want to deal with the situation of refreshing once every second. You also need a special timer program to handle it, or the PHP that is scheduled to be executed needs to include sleep(). Of course, even if there is real-time interaction, the server side can be ignored. Only the client handles both sides of the interaction. js and ajax implementation.
Function pages and functions. The main ones are data access, judgment, and data direction.
It will be easier to use abstract classes. However, the explosion of subcategories is indispensable.
ajax function. (Optional) Used by certain functions that require pseudo-real-time.
To make the game look cooler. Use it.
javascript function. (Optional) Simulate client-side data calculations. That is, the time-related data of webgame. Divided into two parts. Part of it is real data, calculated by a timer on the server side. The other part only has the initial value, which is used for client display. No real-time synchronization is required, only simulated synchronization is required.
Some beautiful UI effects are also included here. It is a game after all.
Database. A bunch of basic data tables and detailed data tables. Basic data table: For example, the initial attribute values ​​of users from level 1 to level 100. Detailed data table: specific attributes for each user.
Optimize the database as much as possible. If the structure can use 1 byte, don't use 2 bytes.

2. A detailed example.
Simply discussing data flow is painful.
It is also more painful to discuss the program without giving the code.
php+mysql is used here. Also, this example does not use classes. If there is enough time, by the end of this year, I will provide a simple webgame code and core classes with real-time interaction to illustrate the benefits of using design patterns.
Let’s discuss it in terms of a super simple webgame. Paired with appropriate code. Should help. Please point out any shortcomings, it will be helpful to me personally.
We don’t consider the game’s playability, numerical balance, etc. Let us first consider only the implementation of a simple example.
So what are the basic contents of a webgame?
Database: players, maps, cities, buildings, weapons, soldiers.
Function: Login, upgrade, personal battle, battle between soldiers, battle with the city, build buildings, craft weapons, buy and sell props.
(Note: Each function must correspond to one or more data tables. What is listed in the database above is only the basis of the basics.)
The first is maps, cities, and buildings.
It is believed here that there can be multiple maps, the city is on the map, and the buildings are in the city.
Map table
Map:Map_ID, X coordinate, Y coordinate, City_ID (city ID), description.
Where Map_ID refers to the id of the map. Not automatic numbering. A map is a Map_ID, which can be repeated.
City table
City:City_ID, city name, city owner, city level, city resources, description.
Building table
Build: ID, City_ID, building name, building level, building function.
Among them, the map table determines the location of the city, the city table determines the relevant data and owners of the city, and multiple pieces of information in the building table belong to a certain city.
After the table is created, it will be displayed.
A for loop. Just take out the entire map and it will be ok.
It’s not much different from the news list on ordinary websites. The difference is that you need to get the X coordinate and Y coordinate positioning. You can use tabels or divs.


Code

Copy code The code is as follows:

class Map//Map class
{
var $Map_ID;
function Map_bg_css($Map_ID) {

$this->Map_ID = $Map_ID;

mysql_select_db($db_name,$link);
$sql="select * from map where Map_ID='".$this->Map_ID."' limit 1";
$result=mysql_query($sql,$link);
echo "