h5 Game: WEB2048 for Different Size Screens

I. Interface Display and Brief Introduction

First completed and then different sizes of screen effects, the interface imitation of the original 2048:

(768px < screen)

(500px < screen < 768px)

(Ultra-small screen less than 500 px)

It was expected that the list of games would be on the left and the list on the right, but it was not done for the time being.

The bootstrap grid layout is used as a whole. The left and right columns are hidden under the size of xs, and the middle column occupies 12 grids.

Set additional styles when the screen is less than 500 px:

@media screen and (max-width: 500px){

The realization of animation effect has learned the 2048 course of Mu Course Net, which is also a new way of thinking.


Other effects:

· Sliding Operation on Mobile Phone

· Page zoom-in and zoom-out grids follow the movement



II. Major Implementation Functions

Only the main code is posted here, and annotations are added as much as possible so that you can better understand the implementation logic. The complete project code can be clone in the github address at the end of the article.

1. Logic of Moving, Take Left Moving as an Example

//Determine whether an array of board s with 4*4 corresponding lattices can be moved
function canMoveLeft(){
    for (let i = 0; i<4; i++){
        for (let j = 1; j<4; j++){//The leftmost grid cannot be moved without consideration
            if(board[i][j] != 0)//Must be a grid with numbers
                //Are adjacent cells on the left equal in number or space?
                if (board[i][j-1] == 0 || board[i][j-1] == board[i][j])
                    return true
    return false

function moveLeft() {
    if(!canMoveLeft())//Judging whether to move left
    for(let x = 0; x<4; x++){
        //Merged arrays correspond to four lattices of the row to determine whether they have been merged and avoid merging [2,2,2,2] into [8,0,0,0].
        var merged = [false,false,false,false];
        for(let y = 1; y<4; y++) {
            if (board[x][y] !=0){//Looking for numeric points in the current row from left to right
                var currentnum = board[x][y]; //Number of Current Moving Points
                var last = y; //Record the current location in the process of moving the current point or looking for a merge
                for(var n = y-1; n>=0; n--){//Starting from the adjacent lattice on the left of the current point, look left for blanks or combinable lattices
                    if(board[x][n] == 0){
                        board[x][n] = currentnum;
                        board[x][last] = 0;
                        last = n;
                    else if(board[x][n] == currentnum && !merged[n]){
                        board[x][n] = currentnum * 2;
                        score+= currentnum * 2;
                        board[x][last] = 0;
                        last = n;
                        merged[n] = true;
                        break;//Neighboring lattices cannot be moved and merged to end directly
                moveAnimation(x,y,x,n+1,merged[n+1])//Mobile animation
    setTimeout("updateBoardView()",200) //Update View after Moving
    setTimeout("ranNumber()",200)//Waiting for the mobile animation to be completed to regenerate new numbers


2. Realization of animation effect

The idea is that 16 div cells originally written in html are only used to achieve visual effects. Actually, they are not operated on. Instead, 16 divs (number-cell) with position=absolute are generated every time according to the data recorded by the two-dimensional array board. When number-cell is not displayed, the size is 0, and the location is in the center of the real grid-cell, where the number or cell is generated. When moving, animate method is used to operate number-cell. After animation, the view is updated. The code is as follows:

function updateBoardView() { //update display
    for (let i =0; i < 4; i++)
        for (let j = 0; j<4; j++){
            $(".grid-container").append('<div class="number-cell" id="number-cell-'+i+'-'+j+'"></div>');
            var theNumberCell = $('#number-cell-'+i+'-'+j);
            if(board[i][j] == 0){
            }//cellSize is the length and width of each lattice. cellSpace is the spacing between each lattice.
            else {

Mobile Animation Code:

//Input starting point coordinates, end point coordinates and whether they are merging points?
function moveAnimation(fromx,fromy,tox,toy,merged) { //Mobile animation
    var numberCell = $('#number-cell-'+fromx+'-'+fromy);
        top:getPosTop(tox),//Determining top Distance Based on the Number of Rows
        left:getPosLeft(toy)//Determining left Distance Based on Column Number
    if (merged){ //The process of merging animation expansion and restoring



Three, summary

The whole game will take a long time to realize the logic of the whole game. Another difficulty is the realization of animation. Fortunately, the tutorial of Mu Course Net gives a solution. In general, 2048 is a relatively simple project.

Project full code github address: https://github.com/GaoMinjian/2048

Project Online Experience Address 1: https://gaominjian.github.io/2048/index.html

Project Online Experience Address II:

Keywords: Mobile github less

Added by PurpleMonkey on Wed, 15 May 2019 03:06:21 +0300