用Visual Studio Code写一个2048
时间: 2024-04-06 21:07:48 浏览: 48
用vc 写的2048 想要的就来拿吧
好的,下面是一个简单的2048游戏,使用JavaScript编写,你可以在Visual Studio Code中运行它:
```html
<!DOCTYPE html>
<html>
<head>
<title>2048</title>
<style type="text/css">
.board {
border: 5px solid #776e65;
width: 400px;
height: 400px;
margin: 0 auto;
position: relative;
}
.tile {
position: absolute;
font-size: 50px;
font-weight: bold;
text-align: center;
line-height: 100px;
width: 100px;
height: 100px;
border-radius: 10px;
background-color: #eee4da;
}
.tile2 {
background-color: #eee4da;
color: #776e65;
}
.tile4 {
background-color: #ede0c8;
color: #776e65;
}
.tile8 {
background-color: #f2b179;
color: #fff;
}
.tile16 {
background-color: #f59563;
color: #fff;
}
.tile32 {
background-color: #f67c5f;
color: #fff;
}
.tile64 {
background-color: #f65e3b;
color: #fff;
}
.tile128 {
background-color: #edcf72;
color: #fff;
}
.tile256 {
background-color: #edcc61;
color: #fff;
}
.tile512 {
background-color: #edc850;
color: #fff;
}
.tile1024 {
background-color: #edc53f;
color: #fff;
}
.tile2048 {
background-color: #edc22e;
color: #fff;
}
</style>
</head>
<body>
<div class="board"></div>
<script type="text/javascript">
var board = document.querySelector(".board");
var tiles = [];
var score = 0;
function start() {
for (var i = 0; i < 16; i++) {
var tile = document.createElement("div");
tile.classList.add("tile");
board.appendChild(tile);
tiles.push(tile);
}
addTile();
addTile();
}
function addTile() {
var emptyTiles = [];
for (var i = 0; i < 16; i++) {
if (!tiles[i].innerHTML) {
emptyTiles.push(i);
}
}
if (emptyTiles.length > 0) {
var index = emptyTiles[Math.floor(Math.random() * emptyTiles.length)];
tiles[index].innerHTML = Math.random() < 0.9 ? 2 : 4;
tiles[index].classList.add("tile" + tiles[index].innerHTML);
}
}
function moveLeft() {
var moved = false;
for (var i = 0; i < 16; i++) {
if (i % 4 === 0) {
var row = [
tiles[i],
tiles[i + 1],
tiles[i + 2],
tiles[i + 3]
];
var newRow = move(row);
tiles[i].innerHTML = newRow[0];
tiles[i + 1].innerHTML = newRow[1];
tiles[i + 2].innerHTML = newRow[2];
tiles[i + 3].innerHTML = newRow[3];
updateClasses(row, newRow);
if (!moved) {
moved = !arraysEqual(row, newRow);
}
}
}
if (moved) {
addTile();
}
}
function moveRight() {
var moved = false;
for (var i = 0; i < 16; i++) {
if (i % 4 === 0) {
var row = [
tiles[i + 3],
tiles[i + 2],
tiles[i + 1],
tiles[i]
];
var newRow = move(row);
tiles[i + 3].innerHTML = newRow[0];
tiles[i + 2].innerHTML = newRow[1];
tiles[i + 1].innerHTML = newRow[2];
tiles[i].innerHTML = newRow[3];
updateClasses(row, newRow);
if (!moved) {
moved = !arraysEqual(row, newRow);
}
}
}
if (moved) {
addTile();
}
}
function moveUp() {
var moved = false;
for (var i = 0; i < 4; i++) {
var column = [
tiles[i],
tiles[i + 4],
tiles[i + 8],
tiles[i + 12]
];
var newColumn = move(column);
tiles[i].innerHTML = newColumn[0];
tiles[i + 4].innerHTML = newColumn[1];
tiles[i + 8].innerHTML = newColumn[2];
tiles[i + 12].innerHTML = newColumn[3];
updateClasses(column, newColumn);
if (!moved) {
moved = !arraysEqual(column, newColumn);
}
}
if (moved) {
addTile();
}
}
function moveDown() {
var moved = false;
for (var i = 0; i < 4; i++) {
var column = [
tiles[i + 12],
tiles[i + 8],
tiles[i + 4],
tiles[i]
];
var newColumn = move(column);
tiles[i + 12].innerHTML = newColumn[0];
tiles[i + 8].innerHTML = newColumn[1];
tiles[i + 4].innerHTML = newColumn[2];
tiles[i].innerHTML = newColumn[3];
updateClasses(column, newColumn);
if (!moved) {
moved = !arraysEqual(column, newColumn);
}
}
if (moved) {
addTile();
}
}
function move(array) {
var newArray = array.filter(val => val);
var missing = 4 - newArray.length;
var zeros = Array(missing).fill(0);
newArray = newArray.concat(zeros);
for (var i = 0; i < 3; i++) {
if (newArray[i] === newArray[i + 1]) {
newArray[i] *= 2;
score += newArray[i];
newArray[i + 1] = 0;
}
}
newArray = newArray.filter(val => val);
newArray = newArray.concat(Array(4 - newArray.length).fill(0));
return newArray;
}
function updateClasses(oldArray, newArray) {
for (var i = 0; i < 4; i++) {
if (oldArray[i] !== newArray[i]) {
tiles[tiles.indexOf(oldArray[i])].classList.remove("tile" + oldArray[i]);
tiles[tiles.indexOf(oldArray[i])].classList.add("tile" + newArray[i]);
}
}
}
function arraysEqual(arr1, arr2) {
for (var i = 0; i < 4; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}
start();
document.addEventListener("keydown", function(event) {
if (event.keyCode === 37) {
moveLeft();
} else if (event.keyCode === 38) {
moveUp();
} else if (event.keyCode === 39) {
moveRight();
} else if (event.keyCode === 40) {
moveDown();
}
document.querySelector(".score").innerHTML = "Score: " + score;
});
</script>
<p class="score">Score: 0</p>
</body>
</html>
```
这个游戏可能不是最完美的,但是可以作为一个基础,你可以在此基础上添加更多的功能和改进。
阅读全文