Detailed process of implementing the 2048 mini game in WeChat applet

Detailed process of implementing the 2048 mini game in WeChat applet

Rendering

Example Code

Today we are going to use the WeChat applet to implement the 2048 game. The effect diagram is shown above. The rules of the game are very simple. You need to control all the blocks to move in the same direction. Two blocks with the same numbers will merge into their sum after colliding. After each operation, a 2 or 4 will be randomly generated. You win if you finally get a "2048" block.

 // Construct an empty matrix [[null,..,size.length],[]]
    empty: function() {
        var cells = [];

        for (var x = 0; x < this.size; x++) {
            var row = cells[x] = [];

            for (var y = 0; y < this.size; y++) {
                row.push(null);
            }
        }

        // [[{x:0,y:0},{x:0,y:1}],[]]
        return cells;
    },

The first thing we need to do is divide the main body of the game into 16 grids. Use Grid to represent these grids, and then these grids have the following operations:

   // Randomly select a cell from the empty cells randomAvailableCell: function() {
    
    // Get the fillable grid coordinates availableCells: function() {
    
    // Is there an empty cell cellsAvailable: function() 
    
    /* 
     * Get cell content * @param {object} cell {x:0,y:0} cell coordinates */
    cellContent: function(cell) {

The above functions are all designed to make subsequent development easier, so that they can be operated directly.

 // Initialize data addStartTiles: function() {
        for (var x = 0; x < this.startTiles; x++) {
            this.addRandomTiles();
        }
    },

    // Randomly fill a random cell with 2 or 4
    addRandomTiles: function() {

        if (this.grid.cellsAvailable()) {
            var value = Math.random() < 0.9 ? 2 : 4;
            var cell = this.grid.randomAvailableCell();
            var tile = new Tile(cell, value);
            this.grid.insertTile(tile); // insert a cell}

    },

Initially, the data is initialized to generate 2 with a 90% probability and 4 with a 10% probability.

touchStart: function(events) {

        // Multi-finger operation this.isMultiple = events.touches.length > 1;
        if (this.isMultiple) {
            return;
        }

        var touch = events.touches[0];

        this.touchStartClientX = touch.clientX;
        this.touchStartClientY = touch.clientY;

    },

    touchMove: function(events) {
        var touch = events.touches[0];
        this.touchEndClientX = touch.clientX;
        this.touchEndClientY = touch.clientY;
    },

    touchEnd: function(events) {
        if (this.isMultiple) {
            return;
        }

        var dx = this.touchEndClientX - this.touchStartClientX;
        var absDx = Math.abs(dx);
        var dy = this.touchEndClientY - this.touchStartClientY;
        var absDy = Math.abs(dy);

        if (Math.max(absDx, absDy) > 10) {
            var direction = absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0);

            var data = this.GameManager.move(direction) || {
                grids: this.data.grids,
                over: this.data.over,
                won: this.data.won,
                score: this.data.score
            };

        }

The game gesture starts and ends the movement. The above section mainly determines the direction of finger movement. Finally, the direction is determined and passed to this.GameManager.move(direction) for movement.

The difficulty here lies in the construction of the data structure, which is relatively easy as long as you understand the grid. Then comes the judgment of finger sliding, which is clearly shown in the above code.

Summarize

This is the end of this article about how to implement the 2048 game in WeChat Mini Program. For more information about the 2048 game in WeChat Mini Program, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat Mini Program Version of Flip Card Game
  • WeChat applet implements puzzle game
  • How to implement a lucky wheel game in WeChat applet

<<:  What to do if the auto-increment primary key in MySQL is used up

>>:  Detailed explanation of Docker container network port configuration process

Recommend

How to embed other web pages in a web page using iframe

How to use iframe: Copy code The code is as follo...

CSS inheritance method

Given a div with the following background image: ...

Summary of common functions and usage methods of WeChat applet development

Here, I have mainly sorted out some commonly used...

Detailed explanation of mysql exists and not exists examples

Detailed explanation of mysql exists and not exis...

HTML imitates Baidu Encyclopedia navigation drop-down menu function

HTML imitates the Baidu Encyclopedia navigation d...

Solution to MySQL replication failure caused by disk fullness

Table of contents Case scenario Solving the probl...

MySQL subqueries and grouped queries

Table of contents Overview Subqueries Subquery Cl...

In-depth understanding of slot-scope in Vue (suitable for beginners)

There are already many articles about slot-scope ...

React Diff Principle In-depth Analysis

Table of contents Diffing Algorithm Layer-by-laye...

Example code for css flex layout with automatic line wrapping

To create a flex container, simply add a display:...

How to configure static network connection in Linux

Configuring network connectivity for Linux system...

JavaScript to achieve text expansion and collapse effect

The implementation of expanding and collapsing li...

Detailed explanation of triangle drawing and clever application examples in CSS

lead Some common triangles on web pages can be dr...

Goodbye Docker: How to Transform to Containerd in 5 Minutes

Docker is a very popular container technology. Th...