25 PHP game programming script codes

Source: Internet
Author: User
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. 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 (1, 6 );

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 (1, 6 );

If the random number generator is put into the function, the effect will be better.

List1. use 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.

List2. pass 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.

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.