This is the “micro channel small game development actual combat” series of the second, this series we will start from 0, the production of 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
The use of tables to store and process data in games was covered in the previous section. In this section we will get right into the action and learn how to process data in a table. We will implement the following:
- Create a table with 10 rows and 10 columns (10×10), all set to 0 by default
- Insert a random row of data 1 into the table
- Walk line by line to find the row that meets the condition (i.e. the entire row is 1)
Create a table
Click the New Table button in the Data area to create a global variable called Grid.
You will see a table editing window. Click “Add Row” and “Add Column” to create a table with 10 rows and 10 columns.
Set the data in all the cells to “0” and you should end up looking like this.
This creates a 10×10 table with all zeros by default.
Insert a random row of data 1 into the table
To illustrate, I added two buttons to the scene, one called “Join a row” and one called “Find a Match”. We will place the insert logic and the find logic respectively on the corresponding button object.
You can add your material according to the image below.
Create two local variables on the Add row object: the row number and the column number.
Next, let’s look directly at the code logic.
There is no need to worry if you don’t understand at first glance. Look at the following analysis a few more times.
Since our table has a total of 10 rows, we take a random integer between 1 and 10 as the row to be set, and store this number in a local variable called “line number” (this is usually used to store subsequent data). Set the column number to 1 before the loop starts, because you are going to set it backwards from column 1 to column 10. Let’s say we randomly get to row 1, and we start the loop, and we first set the number of row 1, column 1 in the table to 1, then row 1, column 2, until row 1, column 10, and then the loop ends. So all 10 digits in line 1 change from 0 to 1.
Next, to verify, click the drop-down arrow to the right of the preview scene and select debug Scene.
The debug scenario looks something like this, which you’ll use a lot in the future. Click “System Global” to see the global variable “grid” created by us. When the mouse moves the data of the grid, the data of the current grid will be displayed.
Next, click Add Row, and you can see the two local variables we created for it, Row number and Column number.
Now click on the “Add a row” button in the scene, and you will see that the local variables of “row number” and “column number” have changed. The current “row number” is 9, which means that we have randomly reached the number 9 and set all the data in row 9 of the table to 1.
Look at the grid data in the global system and see if line 9 is set to 1.
The data in line 9 is indeed all set to 1, proving that our logic is fine. You can click the “Add row” button a few more times, set a few more rows of table data, and view them in global variables.
Find the rows that meet the criteria
Our lookup logic looks like this: search each row of the table from the top down, and if all of the data in a row is 1, record the row number until the table lookup is complete.
First, we create four local variables.
Row number and column number are used for the loop. Both 1 indicates whether the data in the current row is all 1. Set this parameter to 1 if the condition is met, and 0 if the condition is not met.
Create a new list-local variable, “Match row,” to record the number of rows that currently meet the criteria. Because there may be multiple rows in the table that meet the criteria, we use the list here to record all the row numbers that meet the criteria.
Let’s look at the logic for finding a match:
One difficulty here is the “double loop”. If you have only one row of data to iterate over, then you only need one loop. However, if you have 10 rows and 10 columns to iterate over, you need the “double loop”. One loop for rows and one loop for columns. It just looks complicated, but it’s not hard to understand if you analyze it carefully.
We start form the first line of the check, imagine we took out the first row of data from the table, and then from the rows of data first check until the last one, see if there are Numbers 1, as long as there is a number to 1, it shows that the line is not in conformity with the conditions, there is no need to check then. Once we check a line, if it matches we write down the line number of that line and put it in the list of “matched lines”. Then fetch the second row and do the same until you have fetched the tenth row.
Click “Debug Scenario” to check if there is a problem with the logic. First click “Add a row” a few times to randomly add a few rows to the table.
Then click the “Find Match” button and select “Find Match” to look at the data for the “Match Row” local variable.
As shown here, we find that lines 7, 6, and 1 satisfy our condition.
Tip: because we always insert data in the first item in the list, the result is [7,6,1], in reverse order. The real process looks like this: check until line 1 meets the condition, insert the list, and the list becomes [1], check until line 6 meets the condition, insert the list, and the list becomes [6,1], and then check until line 7 meets the condition, insert the list, and the list becomes [7,6,1].
To summarize
In this section we learned how to create, set, traverse, and find tables. You also learned to use the debug scenario to look at global and local variables while the game is running to verify that the logic is correct.
practice
Try adding a random column of data to a table and finding all rows and columns that meet the criteria.
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.
If you think the article is good, please click a “like” to give me some motivation, thanks ~