This is the 12th “micro channel game development actual combat” series, this series we will start from 0, make a 1010 game.
If you don’t have any experience in game development, you can read my “Anyone Can Make Games” series, which will guide you hand in hand to make your first wechat game.
The small game development tool used in the tutorial is wechat official small game production tool: wechat small game production tool
Refined 1010 game project has been open source to the community: project address
You can take the project and study it.
In refined 1010, there are 10 different types of shapes, and in this section we’ll look at how you can control the probability of forming different shapes by using lists.
The figure shows the shapes of the 10 types used in the game. I have numbered them. Some types contain only one (7, 8, 10), some have two (1,2,3,4), and some have four (5,6,9).
There are three shape slots at the bottom of the game, each of which randomly generates a shape. We can adjust the difficulty of the game by setting the probability of different types of shapes being generated. For example, the difficulty of the game will be increased if the probability of 1,8 shapes is high. If not, the difficulty is reduced.
Now, let’s implement how to generate shapes randomly according to the set probability.
First, a list is created to set the weights of 10 types of shapes (the weights can be interpreted as probabilities; the higher the value, the higher the probability of the shape appearing).
As shown in the figure, the list contains 10 values, and each value corresponds to a type of shape. For example, the first value in the list corresponds to the generation probability of shape 1. By default, I set the weight of all 10 shapes to 10, which means that all 10 shapes are equally likely to appear.
Random algorithm
Next, let’s look at a simple algorithm for calculating randomness.
Suppose we currently want to randomize 3 terms, each of which has a weight of 10. First we calculate that the total weight is 30, and then we pick a random number from 1 to 30, so let’s say we get to 25. Next, loop through the comparison:
1: 25 is larger than the first number 10, so it’s not the first term. Subtract 10 from 25 and go to the next cycle.
Loop 2:15 is larger than the first number 10, so it’s not number 2. Subtract 10 from 15 to go to the next loop.
3: 5 is smaller than the first number 10, so it’s term 3, and at the end of the loop you find the final result, and you randomly get term 3.
This algorithm USES only one list is realized according to the different weights for the function of the random, a list of items that can correspond to any thing, for example, you can set up the second and third prize, respectively to the list of item 1, 2, 3, and then may be determined by setting the value of three random probability, such as it can be set 1100, 10,000, which is a pretty good chance of third prize. So, you know how the various dials and raffles in games and apps work.
Some people might say, why don’t I just use if, then, logic? Don’t bother with lists and loops. Now there are only 3 random terms, so you can use if, then logic, but what if there are 30 terms? What if the weights of each item need to be set and adjusted frequently?
Using this method, no matter how many items there are, no matter how the value of each item is adjusted, you can simply adjust the value in the list to meet the requirements. It’s pretty darn good, so you can put it in your library, and you can use it whenever you have a random function where you need to set weights.
Random shape generation implementation
Next, look at the concrete implementation.
First, iterate through the list, adding up all the weights to calculate the total weight. Then a random number is chosen between 1 and the total weight. Then, we iterate through the list item by item until we find that the current value is less than the current value, and the current index is the random item we are looking for.
After obtaining the random items, the next step is to generate the corresponding shapes.
The logic is very simple, which is to generate different types of shapes according to the value of random items.
As you can see in the picture, here I have wrapped “shape generation” into “function”. Now let’s see how to use function in wechat mini game making tool.
Using the function
Find the “Function” category in the building blocks area and click the “New Function” button.
Give the function a name, click OK, and the current block area will show the current function to be created.
Parameters can be added and removed by clicking the “plus” and “minus” buttons on the blocks. The argument’s job is to pass something into a function for calculation and processing.
If you’ve had any programming experience, you know that there are two types of functions, those that have a return type and those that don’t, based on their return type. However, in wechat mini game development tools, there is only one type of function that does not return.
Suppose our current function has the ability to add the values of the two parameters to get the result. Since the function cannot return a value, here we need a global variable to store the result value of the function processing.
After the function is made, you can directly drag and drop the corresponding function blocks for use. The result of the function’s calculation is stored in the global-summation result value. If we need to use the result value processed by the function, we can use the global variable directly.
All created functions are displayed under the functions module in Explorer and can be renamed or deleted by right-clicking on the corresponding function.
Generate an implementation of the corresponding shape
Finally, let’s look at the concrete implementation of the generated shape 5 in the project.
Shape 5 in the game contains four different forms.
The function that generates shape 5, depending on the parameters passed in, generates shapes of different shapes.
The use of the generate shape 5 function, passing in a random number from 1 to 4.
Take a look at the effect of generating new shapes in the final game.
That’s all I have to do in this video. To summarize, we know that there is a very useful random algorithm for setting weights that can be used in a variety of situations using just a list. In addition, we learned about the creation and use of functions in the micro game production tools. Finally, we looked at the specific implementation of random shape generation function in the game.
If you are interested in game development and want to know more original content and tutorials related to game development, please follow my official account: Little Ant Game Development.
Welcome to experience my first small game work, wechat search “delicate 1010”, a delicate and warm small game.
If you think the article is good, please click a “like” to give me some motivation, thanks ~