PHP is a programming language that is easy to use, easy to learn, and widely available. It is ideal for developing simple scripts that can be used in various games. Whether it's a simple, paper-and pen-based game, a group of people playing complex desktop role-playing games, or any type
PHP is a programming language that is easy to use, easy to learn, and widely available. It is ideal for developing simple scripts that can be used in various games. Whether it's a simple, paper-and pen-based game, a group of people playing complex desktop role-playing games, or any type of online games, this series provides content suitable for you. Each article in the "30 game scripts that can be written with PHP" series will introduce 10 scripts in less than 300 words (3d10 indicates "throwing three dice "), these introductory texts are even simple for new developers and useful to experienced game players. The purpose of this series is to provide you with content that can be modified to meet your own needs, so that you can impress friends and players by displaying your notebook at the next game exchange meeting.
Before getting started
As a game expert, designer, and developer, I often find that I rarely write useful utilities and scripts when running, planning, and playing games. Sometimes I need to come up with ideas quickly. In other cases, I only need to compile a bunch of Non-Player Character (NPC) names. Occasionally, I also need to handle numbers, handle exceptions, or integrate some text games into the game. You only need to complete a script in advance to better manage these tasks.
This article will explore 10 basic scripts that can be used in various games. The compressed code package contains the complete source code of each script discussed, and you can view the actual running status of the script in chaoticneutral.
We will quickly introduce these scripts. This section does not describe how to find hosts or set servers. Many Web hosting companies provide PHP, and XAMPP installation programs are easy to use if you need to install your own PHP. We will not spend much time talking about PHP best practices or game design technologies. The script described in this article is easy to understand, easy to use, and can be quickly mastered.
Simple flipper
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
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
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
$ 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
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
$ 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
$ 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
$ 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
$ 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
Shuffle ($ deck ); $ Card = array_shift ($ deck ); Echo $ card ['face']. 'of'. $ card ['suit']; |
Now, we have achieved a shortcut to extract multiple deck cards or build a multi-layer card box (multideck shoe.
Winning rate calculator: Licensing
Since the cards and colors of each card are tracked separately when building cards, you can use this card in programming to calculate the probability of a specific card. First, five cards are drawn for each hand.
Listing 10. five cards for each hand
$ Hands = array (1 => array (), 2 => array ()); For ($ I = 0; $ I <5; $ I ++ ){ $ Hands [1] [] = implode ("of", array_shift ($ deck )); $ Hands [2] [] = implode ("of", array_shift ($ deck )); } |
Then you can check the deck to see how many cards are left and the probability of getting a specific card. It is easy to view the remaining number of cards. You only need to calculate the number of elements contained in the $ deck array. To get the chance to draw a specific card, we need a function to traverse the entire deck and estimate the remaining cards to see if they match.
Listing 11. calculate the probability of winning a specific card
Function calculate_odds ($ draw, $ deck ){ $ Remaining = count ($ deck ); $ Odds = 0; Foreach ($ deck as $ card ){ If ($ draw ['face'] = $ card ['face'] & $ draw ['suit'] = $ Card ['suit']) | ($ Draw ['face'] = ''& $ draw ['suit'] = $ card ['suit']) | ($ Draw ['face'] = $ card ['face'] & $ draw ['suit'] = '')){ $ Odds ++; } } Return $ odds. 'in' $ remaining; } |
Now you can select the cards to be pulled. For simplicity, input an array that looks like a card. We can find a specific card.
Listing 12. search for a specified card
$ Draw = array ('face' => 'acs', 'suit' => 'spades '); Echo implode ("of", $ draw). ':'. calculate_odds ($ draw, $ deck ); |
Alternatively, you can find a card with a specified card surface or color.