search
HomeWeb Front-endJS TutorialWrite 2048 mini game example code using native js

This article mainly introduces the sample code for writing 2048 mini-game using native js, which has a very good reference value. Let’s take a look at the effect with the editor.

Picture:

Write 2048 mini game example code using native js

The code is as follows:

<!DOCTYPE html>
<html>
<head>
 <title> 2048-game </title>
 <meta charset="utf-8" />
 <style media="screen">
 #game {
 display: none;
 position: absolute;
 left: 0px;
 top: 0px;
 right: 0px;
 bottom: 0px;
 background-color: #9DA5C3;
 opacity: 0.5;
 z-index: 1;
 }
 .clear:after {
 content: "";
 display: table;
 clear: both;
 }
 .left {
 float: left;
 }
 .right {
 float: right;
 }
 .scoreShow {
 height: 50px;
 text-align: center;
 line-height: 50px;
 }
 .model {
 text-decoration: none;
 color: white;
 background-color: #bbada0;
 font-size: 36px;
 border-radius: 10px;
 }
 .head {
 width: 480px;
 height: 50px;
 margin: 0 auto;
 font-size: 25px;
 }
 #gridPanel {
 width: 480px;
 height: 480px;
 margin: 0 auto;
 background-color: #bbada0;
 border-radius: 10px;
 position: relative;
 z-index: 1;
 }
 .grid,
 .cell {
 width: 100px;
 height: 100px;
 border-radius: 6px;
 }
 .grid {
 background-color: #ccc0b3;
 float: left;
 margin: 16px 0 0 16px;
 }
 .cell {
 position: absolute;
 font-size: 60px;
 text-align: center;
 line-height: 100px;
 color: #fff;
 }
 .n2 {
 background-color: #eee3da
 }
 .n4 {
 background-color: #ede0c8
 }
 .n8 {
 background-color: #f2b179
 }
 .n16 {
 background-color: #f59563
 }
 .n32 {
 background-color: #f67c5f
 }
 .n64 {
 background-color: #f65e3b
 }
 .n128 {
 background-color: #edcf72
 }
 .n256 {
 background-color: #edcc61
 }
 .n512 {
 background-color: #9c0
 }
 .n1024 {
 background-color: #33b5e5
 }
 .n2048 {
 background-color: #09c
 }
 .n4096 {
 background-color: #a6c
 }
 .n8192 {
 background-color: #93c
 }
 .n2,
 .n4 {
 color: #776e65
 }
 #gameover {
 width: 100%;
 display: none;
 position: fixed;
 left: 50%;
 right: 50%;
 top: 148px;
 width: 220px;
 height: 200px;
 border-radius: 10px;
 background-color: white;
 margin-left: -110px;
 text-align: center;
 z-index: 5;
 }
 #gameover>a {
 display: inline-block;
 width: 170px;
 height: 50px;
 border-radius: 10px;
 text-decoration: none;
 background-color: #9F8D77;
 color: white;
 font-size: 36px;
 }
 </style>
</head>
<body>
 <p id="game">
 </p>
 <p class="head clear">
 <p class="scoreShow left">
 <span>Score:</span>
 <span id="score"></span>
 </p>
 <p class="selction right" onclick="getModel(event)">
 <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="model" value="3">3X3</a>
 <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="model" value="4">4X4</a>
 <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="model" type="button">5X5</a>
 <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="model" type="button">6X6</a>
 <!-- <input type="text" id="model"> -->
 <!-- <button type="button" name="button" id="set">设置游戏</button> -->
 </p>
 </p>
 <p id="gridPanel">
 </p>
 <p id="gameover">
 <h1 id="Score"></h1>
 <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="again" onclick="obj.gameStart()">Try again</a>
 </p>
 <script type="text/javascript">
 var arr = [];
 function $(id) {
 return document.getElementById(id);
 }
 function C(cls) {
 return document.getElementsByClassName(cls);
 }
 var obj = {
 ROW: 4,
 CELL: 4,
 r: 0,
 c: 0,
 f: 0, //r行 c列 f查找的下一位置
 keyCd: 0,
 score: 0,
 createEle: 0, //是否需要创建元素
 eleFragment: "", //文档片段变量
 //游戏开始
 gameStart: function() {
 obj.init();
 document.onkeydown = function(e) { //自动获得事件对象
 switch (e.keyCode) { //判断按键号
 case 37:
 obj.keyCd = 1;
 obj.moveLeft();
 break;
 case 38:
 obj.keyCd = 2;
 obj.moveUp();
 break;
 case 39:
 obj.keyCd = 1;
 obj.moveRight();
 break;
 case 40:
 obj.keyCd = 2;
 obj.moveDown();
 break;
 }
 $("score").innerHTML = obj.score; //更新分数
 }
 },
 //初始化
 init: function() {
 obj.eleFragment = document.createDocumentFragment();
 for (r = 0; r < obj.ROW; r++) {
 arr.push([]);
 for (c = 0; c < obj.CELL; c++) {
 arr[r][c] = 0;
 if (obj.createEle == 1) {
 obj.create(r, c);
 }
 }
 }
 if (obj.createEle == 1) {
 obj.createEle = 0;
 $("gridPanel").innerHTML = ""; //清空原有的元素
 $("gridPanel").appendChild(obj.eleFragment); //添加元素
 }
 obj.score = 0;
 $("score").innerHTML = obj.score;
 $("game").style.display = "none";
 $("gameover").style.display = "none";
 obj.random(); //开始游戏随机生成两个数
 obj.random();
 obj.updateView();
 },
 //创建p元素,添加到gridPanel中
 create: function(r, c) {
 var grid, cell;
 var increment = 14,
 grWidth, grHeight, grMarginTop, grMarginLeft, ceWidth, ceHight;
 grid = document.createElement("p");
 cell = document.createElement("p");
 grid.id = "g" + r + c;
 grid.className = "grid";
 cell.id = "c" + r + c;
 cell.className = "cell";

 if (obj.ROW == 3) {
 increment = 24;
 } else if (obj.ROW == 4) {
 increment = 18;
 }
 grWidth = grHeight = ceWidth = ceHight = 66 + (6 - obj.ROW) * increment; //优化后
 grMarginTop = grMarginLeft = (480 - grWidth * obj.ROW) / (obj.ROW + 1);
 grid.style.width = grWidth + "px";
 grid.style.height = grHeight + "px";
 grid.style.marginTop = grMarginTop + "px";
 grid.style.marginLeft = grMarginLeft + "px";
 cell.style.width = ceWidth + "px";
 cell.style.height = ceHight + "px";
 cell.style.top = grMarginTop + r * (grMarginTop + ceWidth) + "px";
 cell.style.left = grMarginLeft + c * (grMarginLeft + ceHight) + "px";
 cell.style.lineHeight = ceHight + "px";
 cell.style.fontSize = 30 + (6 - obj.ROW) * 10 + "px";
 //优化前
 /*if (obj.ROW == 3) {
 grid.style.width = "140px";
 grid.style.height = "140px";
 grid.style.margin = "15px 0 0 15px";
 cell.style.width = "140px";
 cell.style.height = "140px";
 cell.style.top = 15 + r * 155 + "px"; //设置距离上一位置的高度
 cell.style.left = 15 + c * 155 + "px"; //设置离左一位置的距离
 cell.style.lineHeight = "140px";
 } else if (obj.ROW == 4) {
 grid.style.width = "100px";
 grid.style.height = "100px";
 grid.style.margin = "16px 0 0 16px";
 cell.style.width = "100px";
 cell.style.height = "100px";
 cell.style.top = 16 + r * 116 + "px";
 cell.style.left = 16 + c * 116 + "px";
 cell.style.lineHeight = "100px";
 } else if (obj.ROW == 5) {
 grid.style.width = "75px";
 grid.style.height = "75px";
 grid.style.margin = "17.5px 0 0 17.5px";
 cell.style.width = "75px";
 cell.style.height = "75px";
 cell.style.top = 17.5 + r * 92.5 + "px";
 cell.style.left = 17.5 + c * 92.5 + "px";
 cell.style.fontSize = "40px";
 cell.style.lineHeight = "75px";
 } else if (obj.ROW == 6) {
 grid.style.width = "66px";
 grid.style.height = "66px";
 grid.style.margin = "12px 0 0 12px";
 cell.style.width = "66px";
 cell.style.height = "66px";
 cell.style.top = 12 + r * 78 + "px";
 cell.style.left = 12 + c * 78 + "px";
 cell.style.fontSize = "30px";
 cell.style.lineHeight = "66px";
 }*/
 obj.eleFragment.appendChild(grid);
 obj.eleFragment.appendChild(cell);
 },
 //随机产生一个新的数
 random: function() {
 while (1) {
 var row = Math.floor(Math.random() * obj.ROW);
 var cell = Math.floor(Math.random() * obj.CELL);
 if (arr[row][cell] == 0) { //判断生成的随机数位置为0才随机生成2或4
 arr[row][cell] = (Math.random() > 0.5) ? 4 : 2;
 break;
 }
 }
 // var row = Math.floor(Math.random() * 4);
 // var cell = Math.floor(Math.random() * 4);
 // if (arr[row][cell] == 0) { //判断生成的随机数位置为0才随机生成2或4
 // arr[row][cell] = (Math.random() > 0.5) ? 4 : 2;
 // return;
 // }
 // obj.random();//递归影响执行效率
 },
 //更新页面
 updateView: function() {
 var win = 0;
 for (r = 0; r < obj.ROW; r++) {
 for (c = 0; c < obj.CELL; c++) {
 if (arr[r][c] == 0) { //值为0的不显示
 $("c" + r + c).innerHTML = ""; //0不显示
 $("c" + r + c).className = "cell" //清除样式
 } else {
 $("c" + r + c).innerHTML = arr[r][c];
 $("c" + r + c).className = "cell n" + arr[r][c]; //添加不同数字的颜色
 if (obj.ROW == 3 && arr[r][c] == 1024) {
 win = 1;
 } else if (obj.ROW == 4 && arr[r][c] == 2048) {
 win = 1;
 } else if (obj.ROW == 5 && arr[r][c] == 4096) {
 win = 1;
 } else if (obj.ROW == 6 && arr[r][c] == 8192) {
 win = 1;
 }
 }
 }
 }
 if (win == 1) { //通关
 $("game").style.display = "block";
 $("gameover").style.display = "block";
 $("Score").innerHTML = "You win!<br>Score:" + obj.score;
 }
 if (obj.isGameOver()) { //游戏失败
 $("game").style.display = "block";
 $("gameover").style.display = "block";
 $("Score").innerHTML = "GAME OVER!<br>Score:" + obj.score;
 console.log("gameover");
 }
 },
 //游戏失败
 isGameOver: function() {
 for (r = 0; r < obj.ROW; r++) {
 for (c = 0; c < obj.CELL; c++) {
 if (arr[r][c] == 0) { //有0还不是gameover
 return false;
 } else if (c != obj.CELL - 1 && arr[r][c] == arr[r][c + 1]) { //左往右 前一个和下一个不相等
 return false;
 } else if (r != obj.ROW - 1 && arr[r][c] == arr[r + 1][c]) { //上往下 上一个和下一个不相等
 return false;
 }
 }
 }
 return true;
 },
 //查找下一个不为0的数值的位置
 find: function(r, c, start, condition, direction) {
 if (obj.keyCd == 2) { //上下按键
 if (direction == 1) { //向上按键 f++
 for (var f = start; f < condition; f += direction) {
 if (arr[f][c] != 0) {
 return f;
 }
 }
 } else { //向下按键 f--
 for (var f = start; f >= condition; f += direction) {
 if (arr[f][c] != 0) {
 return f;
 }
 }
 }
 } else { //左右按键
 if (direction == 1) { //左按键 f++
 for (var f = start; f < condition; f += direction) {
 if (arr[r][f] != 0) {
 return f;
 }
 }
 } else { //右按键 f--
 for (var f = start; f >= condition; f += direction) {
 if (arr[r][f] != 0) {
 return f;
 }
 }
 }
 }
 return null; //循环结束仍然没有找到!=0的数值,返回null
 },
 //左按键的处理
 dealToLeft: function(r) {
 var next;
 for (c = 0; c < obj.ROW; c++) {
 next = obj.find(r, c, c + 1, obj.CELL, 1); //找出第一个不为0的位置
 if (next == null) break; //没有找到就返回
 //如果当前位置为0
 if (arr[r][c] == 0) {
 arr[r][c] = arr[r][next]; //把找到的不为0的数值替换为当前位置的值
 arr[r][next] = 0; //找到的位置清0
 c--; //再次循环多一次,查看后面否有值与替换后的值相同,
 } else if (arr[r][c] == arr[r][next]) { //如果当前位置与找到的位置数值相等,则相加
 arr[r][c] *= 2;
 arr[r][next] = 0;
 obj.score += arr[r][c];
 }
 }
 },
 move: function(itertor) {
 var before, //没处理前
 after; //after处理后
 before = arr.toString();
 itertor(); //执行for函数
 after = arr.toString();
 if (before != after) { //前后对比,如果不同就update
 obj.random();
 obj.updateView();
 }
 },
 moveLeft: function() {
 obj.move(function() {
 for (r = 0; r < obj.ROW; r++) {
 obj.dealToLeft(r);
 }
 })
 // if 当前位置 不为零
 // 从当前位置,下一个成员开始,遍历,
 // 如果找到,与当前位置相等的数,
 // 两者相加,并把不为零的成员,设置为零
 // 如果 当前位置是 零
 // 从当前位置下一个成员开始遍历
 // 如果找到 第一个不为零的成员
 // 当前位置数值设置为这个不为零的成员的值 ,并且把那个不为零的成员设置为 0
 },
 //右按键处理
 dealToRight: function(r) {
 var next;
 for (c = obj.CELL - 1; c >= 0; c--) {
 next = obj.find(r, c, c - 1, 0, -1); //找出第一个不为0的位置
 if (next == null) break; //没有找到就返回
 //如果当前位置为0
 if (arr[r][c] == 0) {
 arr[r][c] = arr[r][next]; //把找到的不为0的数值替换为当前位置的值
 arr[r][next] = 0; //找到的位置清0
 c++; //再次循环多一次,查看后面否有值与替换后的值相同,
 } else if (arr[r][c] == arr[r][next]) { //如果当前位置与找到的位置数值相等,则相加
 arr[r][c] *= 2;
 arr[r][next] = 0;
 obj.score += arr[r][c];
 }
 }
 },
 moveRight: function() {
 obj.move(function() {
 for (r = 0; r < obj.ROW; r++) {
 obj.dealToRight(r);
 }
 })
 },
 //上按键处理
 dealToUp: function(c) {
 var next;
 for (r = 0; r < obj.ROW; r++) {
 next = obj.find(r, c, r + 1, obj.ROW, 1); //找出第一个不为0的位置
 if (next == null) break;
 //如果当前位置为0
 if (arr[r][c] == 0) {
 arr[r][c] = arr[next][c]; //把找到的不为0的数值替换为当前位置的值
 arr[next][c] = 0; //找到的位置清0
 r--; //再次循环多一次,查看后面否有值与替换后的值相同
 } else if (arr[r][c] == arr[next][c]) { //如果当前位置与找到的位置数值相等,则相加
 arr[r][c] *= 2;
 arr[next][c] = 0;
 obj.score += arr[r][c];
 }
 }
 },
 moveUp: function() {
 obj.move(function() {
 for (c = 0; c < obj.CELL; c++) {
 obj.dealToUp(c);
 }
 })
 },
 //下按键处理
 dealToDown: function(c) {
 var next;
 for (r = obj.ROW - 1; r >= 0; r--) {
 next = obj.find(r, c, r - 1, 0, -1); //找出第一个不为0的位置
 if (next == null) {
 break;
 }
 //如果当前位置为0
 if (arr[r][c] == 0) {
 arr[r][c] = arr[next][c]; //把找到的不为0的数值替换为当前位置的值
 arr[next][c] = 0; //找到的位置清0
 r++; //再次循环多一次,查看后面否有值与替换后的值相同
 } else if (arr[r][c] == arr[next][c]) { //如果当前位置与找到的位置数值相等,则相加
 arr[r][c] *= 2;
 arr[next][c] = 0;
 obj.score += arr[r][c];
 }
 }
 },
 moveDown: function() {
 obj.move(function() {
 for (c = 0; c < obj.CELL; c++) {
 obj.dealToDown(c);
 }
 })
 }
 }
 window.onload = function() {
 obj.createEle = 1;
 obj.gameStart();
 }
 //切换模式
 function getModel(e) { //事件冒泡获取a元素
 var a = e.target,
 modelValue = 4;
 if (a.nodeName == "A") {
 if (a.innerHTML == "3X3") {
 modelValue = 3;
 } else if (a.innerHTML == "4X4") {
 modelValue = 4;
 } else if (a.innerHTML == "5X5") {
 modelValue = 5;
 } else if (a.innerHTML == "6X6") {
 modelValue = 6;
 }
 obj.ROW = obj.CELL = modelValue;
 obj.createEle = 1; //需要创建格子p元素的标志
 obj.gameStart();
 }
 }
 // var modelValue = parseInt($("model").value);
 // if (isNaN(modelValue)) {
 // modelValue = 4; //默认是4*4
 // }
 // if (modelValue <= 2 || modelValue > 6) return; //2格或者大于6格无效
 // obj.ROW = modelValue;
 // obj.CELL = modelValue;
 // obj.createEle = 1;
 // obj.gameStart();
 // console.log(modelValue);
 // }
 </script>
</body>
</html>

The above is the detailed content of Write 2048 mini game example code using native js. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.