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> </p><p> </p><p>{col}}</p>The DOM of the main game part is very simple. It is rendered with a two-dimensional array and dynamically bound to the style
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 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!

我们在使用win10操作系统的时候,想要知道win10更新了之后是不是还保存有以前老版本中的自带游戏扫雷,据小编所知,我们可以在商店中进行下载安装,只要在商店中搜索microsoftminesweeper即可。具体步骤就来和小编一起看一下吧~windows10有扫雷游戏吗1,首先打开win10开始菜单,点击。然后搜索,点击搜索。2,点击排在第一位的。3,然后就可能需要输入微软账户,即Microsoft账户。没有Microsoft账户的可以安装提示注册即可。输入账户密码,点击下一步。4,然后开始下

随着互联网的发展,SEO(SearchEngineOptimization,搜索引擎优化)已经成为了网站优化的重要一环。如果您想要使您的PHP网站在搜索引擎中获得更高的排名,就需要对SEO的内容有一定的了解了。本文将会介绍如何在PHP中实现SEO优化,内容包括网站结构优化、网页内容优化、外部链接优化,以及其他相关的优化技巧。一、网站结构优化网站结构对于S

win10系统的小游戏扫雷在哪里?很多用户在使用Win7系统的时候最喜欢玩的就是Win7自带的扫雷了,甚至有些小伙伴会为了比拼扫雷的速度一直在玩。但是不少用户在升级了Windows10系统后发现电脑的扫雷不见了,很多小伙伴不知道怎么详细操作,小编下面整理了Win10扫雷小游戏的位置分享,如果你感兴趣的话,跟着小编一起往下看看吧! Win10扫雷小游戏的位置分享 1,首先打开win10开始菜单,点击【应用商店】。然后搜索【microsoftminesweeper】,点击搜索。 2,点击排

谷歌浏览器怎么玩小游戏?谷歌浏览器之中是有着非常多充满人文关怀的功能设计的,各位可以在其中获得非常多样化的乐趣。在谷歌浏览器之中,有这一个非常好玩的彩蛋小游戏,即小恐龙游戏,很多小伙伴都非常喜欢这个游戏,但却不清楚该怎么触发进行游玩,下面就由小编为大家带来恐龙小游戏进入教程。谷歌浏览器怎么玩小游戏方法一:【电脑断网】如果你的电脑使用有线网络,请拔掉网线;如果你的电脑使用无线网络,请在电脑右下角点击无线网络连接断开。②在你电脑断网的前提下,打开谷歌浏览器(GoogleChrome)即可出现谷歌浏览

随着电子商务和企业管理的发展,许多企业开始寻找更好的方法来处理其日常业务流程。ERP系统是一种能够整合企业各种业务流程的软件工具。它提供了全面的功能,包括生产、销售、采购、库存、财务等方面,帮助企业提高效率、控制成本和提高客户满意度。而在PHP编程语言中,也能够实现ERP系统,这就需要我们掌握一些基本的知识和技术。下面,我们将深入探讨如何在PHP中实现ERP

随着物联网技术的发展和普及,越来越多的应用场景需要使用PHP语言进行物联网开发。PHP作为一种广泛应用于Web开发的脚本语言,它的易学易用、开发速度快、可扩展性强等特点,使其成为开发物联网应用的一种优秀选择。本文将介绍在PHP中实现物联网开发的常用技术和方法。一、传输协议和数据格式物联网设备通常使用TCP/IP或UDP协议进行数据传输,而HTTP协议是一个优

随着企业的发展,客户管理变得越来越重要。为了提高客户满意度和忠诚度,越来越多的企业采用客户关系管理系统(CRM)来帮助其管理客户关系。而PHP是一种流行的编程语言,因其简单易学、灵活和强大而被广泛应用于Web开发。那么,如何在PHP中实现CRM系统呢?本文将为您介绍实现CRM系统的步骤和技巧。Step1:需求分析在开始开发CRM系统之前,您需要进行需求分析

UniApp实现小游戏原生组件的扩展与使用技巧引言:UniApp是一款跨平台的前端开发框架,它支持同时开发多个主流小程序平台的应用。在UniApp中,我们可以通过扩展小游戏原生组件来提升应用的性能和用户体验。本文将介绍UniApp实现小游戏原生组件扩展与使用的技巧,并给出相应的代码示例供大家参考。一、为什么要扩展小游戏原生组件小游戏原生组件具有更好的性能和更


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool