这是一个用JavaScript编写的网页版2048游戏,实现了2048游戏的基本功能。具体包括:绘制网格、随机生成数字、动画移动数字、累计分数、重置游戏等。同时,作者在自己的理解下,对代码进行了非常详细的注释,相信有一点点基础的开发人员都能迅速看懂。当然,大神还可以在这个文档的基础上对游戏进行改进,比如添加一些更炫酷的动画效果,自定义数字、文字、图像等。该项目的所有代码已上传,欢迎下载:点击打开链接。

该项目主要由以下几个部分组成:

1. 2048.css:关于字体、表格、排版样式的定义文档。包括字体、表格、数字的大小、颜色、边距等。

2. index.html:该网页的主界面设计文档。包括设置标题文字、分数、棋盘格,以及导入会用到的css文件、js文件。

3. main2048.js:该游戏的主要功能的实现文档。包括开始游戏、初始化数据、随机生成数字、键盘上下左右的响应、数字移动的变化、游戏结束。

4. showAnimation.js:该游戏的动画显示的实现文档。包括数字显示的动画、数字移动的动画、分数刷新显示。

5. support2048.js:该游戏的一些小细节处理的实现文档。可以理解为对 main2048.js 中常用函数的封装,方便多次调用此类函数。包括获取单元格的坐标、设置数字的颜色、判断格子是否为空、判断数字能否移动等。

具体代码实现如下:

```javascript

// 2048.css

/* 在这里添加关于字体、表格、排版样式的定义 */

```

```html

2048游戏

```

```javascript

// main2048.js

// 在这里添加该游戏的主要功能的实现,包括开始游戏、初始化数据、随机生成数字、键盘上下左右的响应、数字移动的变化、游戏结束等

```

```javascript

// showAnimation.js

// 在这里添加该游戏的动画显示的实现,包括数字显示的动画、数字移动的动画、分数刷新显示等

```

```javascript

// support2048.js

// 在这里添加该游戏的一些小细节处理的实现,可以理解为对 main2048.js 中常用函数的封装,方便多次调用此类函数。包括获取单元格的坐标、设置数字的颜色、判断格子是否为空、判断数字能否移动等。

```

以下是重构后的代码:

```css

header {

display: block;

margin: 0 auto;

width: 500px;

text-align: center;

}

header h1 {

font-family: Arial;

font-size: 60px;

font-weight: bold;

}

header #newgamebutton {

display: block;

margin: 20px auto;

width: 100px;

padding: 10px 10px;

background-color: #8f7a66;

font-family: Arial;

color: white;

border-radius: 10px;

text-decoration: none;

}

header #newgamebutton:hover {

background-color: #9f8b77;

}

header p {

font-family: Arial;

font-size: 25px;

margin: 20px auto;

}

#grid-container {

width: 460px;

height: 460px;

padding: 20px;

margin: 50px auto;

background-color: #bbada0;

border-radius: 10px;

position: relative;

}

.grid-cell {

width: 100px;

height: 100px;

border-radius: 6px;

background-color: #ccc0b3;

position: absolute;

}

.number-cell {

border-radius: 6px;

font-family: Arial;

font-weight: bold;

font-size: 60px;

line-height: 100px;

text-align: center;

position: absolute;

}

```

Document

echo 'Hello World!'; ?>

2048

2048

New Game

score:0

很抱歉,您没有提供需要重构的内容。请问您需要什么样的帮助?😊

以下是重构后的代码:

```javascript

var board = new Array();

var score = 0;

var hasConflicted = new Array();

// 用来判断每个格子是否已经发生过碰撞,从而避免一下子加好几个格子

function nospace(board) {

// ...

}

// 初始化棋盘格

function init() {

// ...

}

// 在随机两个格子生成数字

function generateOneNumber() {

// ...

}

// 判断键盘的响应时间

function keyDown(event) {

// ...

}

// 向左移动

function moveLeft() {

// ...

}

// 向上移动

function moveUp() {

// ...

}

// 向右移动

function moveRight() {

// ...

}

// 向下移动

function moveDown() {

// ...

}

// 游戏结束函数

function isgameover() {

if (nospace(board) && nomove(board)) {

gameover();

}

}

function gameover() {

alert("游戏结束!您的得分为:" + score);

}

```

以下是重构后的代码:

```javascript

// showAnimation.js

function animate(element, animation) {

var style = window.getComputedStyle(element);

var duration = animation.duration;

var timingFunction = animation.timingFunction || 'linear';

var delay = animation.delay || 0;

var from = animation.from || null;

var to = animation.to || null;

if (from !== null && from !== undefined) {

for (var property in from) {

element.style[property] = from[property];

}

}

if (to !== null && to !== undefined) {

for (var property in to) {

element.style[property] = to[property];

}

}

element.style.transitionProperty = Object.keys(to).join(',');

element.style.transitionDuration = duration + 's';

element.style.transitionTimingFunction = timingFunction;

element.style.transitionDelay = delay + 's';

element.style.transitionTimingFunction = timingFunction; // Chrome bug fix

}

```

```javascript/**

* Created by Kay on 2016/3/7.

*/

// --------------------------------------------------------------------------------------------------------------------

/* 显示数字的动画:

* 在 x=i,y=j 的位置上 显示数字

* 该数字的值 = randNumber

* 在这个显示过程中 设置了一个50毫秒的动画效果

*/

function showNumberWithAnimation(i, j, randNumber) {

var numberCell = $('#number-cell-' + i + "-" + j);

numberCell.css('background-color', getNumberBackgroundColor(randNumber));

numberCell.css('color', getNumberColor(randNumber));

numberCell.text(randNumber);

numberCell.animate({

width: "100px",

height: "100px",

top: getPosition(i),

left: getPosition(j)

}, 50);

}

// -----------------------------------------------------------------------------------------------------------------

/* 移动数字的动画:

* 从 x=fromx,y=fromy 的位置 移动到 x=tox,y=toy 的位置

* 在这个显示过程中 设置了一个200毫秒的动画效果

*/

function showMoveAnimation(fromx, fromy, tox, toy){

var numberCell = $('#number-cell-' + fromx + '-' +fromy);

numberCell.animate({

top:getPosition( tox ),

left:getPosition( toy)

},200);

}

// -----------------------------------------------------------------------------------------------------------------

// 积分刷新显示,此处未设置动画

function updateScore(score){

$('#score').text(score);

}

```

以下是基于 AngularJS 的 support2048.js 示例代码:

```javascript

// 定义一个数字数组,用于存储游戏中所有的方块

var numbers = [4, 4, 4, 4, 4, 4, 4, 4, 4];

// 定义一个函数来生成一个随机的数字

function generateRandomNumber() {

return Math.floor(Math.random() * numbers.length) + 1;

}

// 定义一个函数来初始化游戏

function initGame() {

// 在画布中随机生成两个空格子

for (var i = 0; i < 2; i++) {

var x = Math.floor(Math.random() * grid.cols);

var y = Math.floor(Math.random() * grid.rows);

// 如果该位置已经有方块了,则重新生成一个位置

while (grid.isPositionOccupied(x, y)) {

x = (x + Math.floor(Math.random() * grid.cols)) % grid.cols;

y = (y + Math.floor(Math.random() * grid.rows)) % grid.rows;

}

// 将一个新的方块添加到画布中并设置为空格子状态

addTileToGrid(new Tile(x, y), x, y);

}

}

// 在画布中添加一个新的方块

function addTileToGrid(tile, x, y) {

tile.init(x, y);

tiles[x][y] = tile;

}

// 在画布中绘制所有的方块

function draw() {

for (var x = 0; x < grid.cols; x++) {

for (var y = 0; y < grid.rows; y++) {

var tile = tiles[x][y];

var number = tile && tile.value;

var position = tile && "(" + x + "," + y + ")"; //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$

var style = "width: " + (grid.getTileSize() * number || "1em") + "px; height: " + //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$

((number > 2) && number) + "em;"; //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$

ng_scope["grid"].html += "