PHP game programming 25 script codes

Source: Internet
Author: User
Some functions are frequently used during php game development. For more information, see.

Some functions are frequently used during php game development. For more information, see.

Listing 1. Simple throttling
Many games and game systems require dice. Let's start with the simple part: throw a six-sided dice. In fact, rolling a six-sided dice means selecting a random number from 1 to 6. In PHP, this is very simple: echo rand );.
In many cases, this is basically simple. However, when dealing with probability games, we need some better implementations. PHP provides a better random number generator: mt_rand (). Without in-depth research on the differences between the two, mt_rand can be considered as a faster and better random number generator: echo mt_rand );. If the random number generator is put into the function, the effect will be better.
Listing 1. Using the mt_rand () random number generator Function

The Code is as follows:


Function roll (){
Return mt_rand (1, 6 );
}
Echo roll ();


Then, you can pass the dice type to be rolled to the function as a parameter.
Listing 2. Passing the dice type as a parameter

The Code is as follows:


Function roll ($ sides ){
Return mt_rand (1, $ sides );
}
Echo roll (6); // roll a six-sided die
Echo roll (10); // roll a ten-sided die
Echo roll (20); // roll a twenty-sided die


Starting from here, we can continue to scroll multiple dice at a time as needed and return an array of results. We can also scroll multiple different types of dice at a time. However, most tasks can use this simple script.
Random name Generator
If you are running a game, writing a story, or creating a large number of characters at a time, you may sometimes get tired of new names. Let's take a look at a simple random name generator that can be used to solve this problem. First, let's create two simple Arrays-one for the first name and the other for the last name.
Listing 3. Two simple arrays of first and last names

The Code is as follows:


$ Male = array (
"William ",
"Henry ",
"Filbert ",
"John ",
"Pat ",
);
$ Last = array (
"Smith ",
"Jones ",
"Winkler ",
"Cooper ",
"Cline ",
);


Then you can select a random element from each array: echo $ male [array_rand ($ male)]. ''. $ last [array_rand ($ last)];. To extract multiple names at a time, you only need to mix the array and extract the names as needed.
Listing 4. Mixed name Array

The Code is as follows:


Shuffle ($ male );
Shuffle ($ last );
For ($ I = 0; $ I <= 3; $ I ++ ){
Echo $ male [$ I]. ''. $ last [$ I];
}


Based on this basic concept, we can create a text file that saves the first and last names. If you store a name in each line of a text file, you can easily use line breaks to separate the file content to build an array of source code.
Listing 5. Create a text file with the name

The Code is as follows:


$ Male = explode ('\ n', file_get_contents('names.female.txt '));
$ Last = explode ('\ n', file_get_contents('names.last.txt '));


After building or searching for some good name files (some files are included in the Code archive), we will never need to worry about names.
Scenario Generator
With the same basic principles used to build a name generator, we can build a scenario generator. This generator is useful not only in role-playing games, but also in scenarios where a pseudo-random environment set (can be used for role-playing, impromptu creation, and writing) is required. One of my favorite games, Paranoia included "mission blender" in its GM Pack )". The task mixer can be used to integrate a complete task when rolling the dice quickly. Let's integrate our scenario generator.
Consider the following scenario: You wake up and find yourself lost in the jungle. You know you have to go to New York, but you don't know why. You can hear the sounds of dogs nearby and clear enemy searches. You are cold, trembling, and have no weapons. Each sentence in this scenario describes specific aspects of the scenario:
"You wake up and find yourself lost in the jungle"-setting will be set up.
"You know you have to go to New York"-this sentence will describe the target.
"You can hear the call of a dog"-This sentence introduces the enemy.
"You are cold, trembling, and have no weapons"-This sentence adds complexity.
Just like creating a text file with the first name and last name, create a text file with the settings, target, enemy, and complexity respectively. The sample file is included in the Code archive. After you have these files, the generated scenario code is basically the same as the generated name code.
Listing 6. Generation scenarios

The Code is as follows:


$ Settings = explode ("\ n", file_get_contents('scenario.settings.txt '));
$ Objectives = explode ("\ n", file_get_contents('scenario.objectives.txt '));
$ Antagonists = explode ("\ n", file_get_contents('scenario.antagonists.txt '));
$ Complicati ***** = explode ("\ n", file_get_contents ('scenario. complicati *****. txt '));
Shuffle ($ settings );
Shuffle ($ objectives );
Shuffle ($ antagonists );
Shuffle ($ complicati ****);
Echo $ settings [0]. ''. $ objectives [0].''. $ antagonists [0].''
. $ Complicati ***** [0]."
\ N ";


You can add elements to a scenario by adding a new text file, or you may want to add multiple complexity levels. The more content you add to a basic text file, the more scenarios change over time.
Card group creator (Deck builder) and equipment (shuffler)
If you want to play cards and process card-related scripts, we need to integrate a deck builder with tools in the equipment. First, let's build a pair of standard cards. You need to construct two Arrays-one to save the cards of the same color, and the other to save the cards. If you need to add a new group card or card type later, doing so will provide great flexibility.
Listing 7. Building a pair of standard playing cards

The Code is as follows:


$ Suits = array (
"Spades", "Hearts", "Clubs", "Diamonds"
);
$ Faces = array (
"Two", "Three", "Four", "Five", "Six", "Seven", "Eight ",
"Nine", "Ten", "Jack", "Queen", "King", "Ace"
);


Create a deck array to save all card values. You only need to use a pair of foreach loops to complete this operation.
Listing 8. Building a deck Array

The Code is as follows:


$ Deck = array ();
Foreach ($ suits as $ suit ){
Foreach ($ faces as $ face ){
$ Deck [] = array ("face" => $ face, "suit" => $ suit );
}
}


After building an array of playing cards, we can easily shuffles and randomly draw a card.
Listing 9. Shuffling and randomly picking a card

The Code is as follows:

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.