How to use vue to implement the 2048 mini game
This time I will show you how to use vue to implement the 2048 mini game. What are the precautions for using vue to implement the 2048 mini game. The following is a practical case, let's take a look.
Usage:
git clone npm i npm run dev
The implementation ideas are as follows:
Use vue-cli to build the project. It may be a bit cumbersome for this project, but I am also lazy. Then build a
4X4 square and store it in a two-dimensional array. After binding, you only care about this two-dimensional array and leave other things to vue
Listen to the keyboardEvents
The core part of 2048 is the movement merging algorithm. Because it is a 4X4 matrix, as long as To implement the left shift algorithm, moving in other directions only requires rotating the matrix, merging the moves, rotating it back, and rendering the dom
Bind styles with different values
Score calculation, and using localstorage to store the highest score
##Key implementation
DOM
<p class="box"> <p class="row" v-for="row in list"> <p class="col" :class="'n-'+col" v-for="col in row">{col}}</p> </p> </p>
Shift left
Mainly caused by the following situations:- 2 2 2 => 4 4 0 0
- 4 2 2 2 => 4 4 2 0
- 0 4 2 2=> 4 4 0 0 ##2 2 4 2 => 4 4 2 0
- Take the example of a single row of data,
- Traverse the single row array, if there is data, record it as cell, look for the cell to the left Move to the farthest empty position farthest
- Determine whether the left side of farthest exists. If it does not exist, move directly to farthest
- If it exists, Then judge whether the value of farthest - 1 is the same as that of cell
- Same=> Merge
- Not the same=>Move to farthest position
- After moving, clear the cell
- Next round
- Because one round of movement is in progress, A number can only be merged once, so each grid must have a merged parameter to record whether it has been merged.
Main code:
_list.forEach(item => { let farthest = this.farthestPosition(list, item) let next = list[farthest - 1] if (next && next === item.value && !_list[farthest - 1].merged) { //合并 list[farthest - 1] = next * 2 list[item.x] = undefined item = { x: farthest - 1, merged: true, value: next * 2 } this.score += next * 2 } else { if (farthest != item.x) { list[farthest] = item.value list[item.x] = undefined item.x = farthest } } })
Matrix rotation Because moving up, moving down, moving left, moving right actually It's the same. You can write it 4 times, but it's easy to make mistakes, so I directly rotate the matrix and then move it.
For example, as long as the matrix is rotated counterclockwise once, the upward shift becomes a left shift. After the moves are merged, as long as the matrix is rotated counterclockwise 4-1 times, the matrix will be the same as a simple upward shift. Moved the same.
Counterclockwise rotation algorithm:
rotate(arr, n) { n = n % 4 if (n === 0) return arr let tmp = Array.from(Array(this.size)).map(() => Array(this.size).fill(undefined)) for (let i = 0; i < this.size; i++) { for (let j = 0; j < this.size; j++) { tmp[this.size - 1 - i][j] = arr[j][i] } } if (n > 1) tmp = this.rotate(tmp, n - 1) return tmp },
At this point, it is 80% complete. It just needs to be improved, adding points, restarting and other functions.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use vee-validate in Vuenode uses express http-proxy- across domains Detailed explanation of middleware stepsThe above is the detailed content of How to use vue to implement the 2048 mini game. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Realizing love animation effects through Java code In the field of programming, animation effects are very common and popular. Various animation effects can be achieved through Java code, one of which is the heart animation effect. This article will introduce how to use Java code to achieve this effect and give specific code examples. The key to realizing the heart animation effect is to draw the heart-shaped pattern and achieve the animation effect by changing the position and color of the heart shape. Here is the code for a simple example: importjavax.swing.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

When we use the win10 operating system, we want to know whether the built-in game Minesweeper from the old version is still saved after the win10 update. As far as the editor knows, we can download and install it in the store, as long as it is in the store Just search for microsoftminesweeper. Let’s take a look at the specific steps with the editor~ Is there a Minesweeper game for Windows 10? 1. First, open the Win10 Start menu and click. Then search and click Search. 2. Click on the first one. 3. Then you may need to enter a Microsoft account, that is, a Microsoft account. If you do not have a Microsoft account, you can install it and be prompted to register. Enter the account password and click Next. 4. Then start downloading

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.
