2048游戏制作html,一个自制的2048小游戏(一)
时间: 2023-11-03 14:05:41 浏览: 40
非常好的问题!制作一个自己的2048小游戏是很有趣的事情,而且可以很好地锻炼编程技能。下面是一个简单的HTML实现2048小游戏的步骤:
1. 编写HTML页面结构
首先,我们需要创建一个HTML页面来作为游戏的基础。在HTML页面中,我们需要有一个游戏板块的区域,以及一些用于控制游戏的按钮。
```html
<!DOCTYPE html>
<html>
<head>
<title>2048 Game</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>2048 Game</h1>
<div id="game-container">
<!-- 游戏板块区域 -->
</div>
<div id="btn-container">
<!-- 游戏控制按钮区域 -->
</div>
<script src="game.js"></script>
</body>
</html>
```
在上面的代码中,我们创建了一个 `game-container` 的 `div` 元素,用于显示游戏的面板。我们还创建了一个 `btn-container` 的 `div` 元素,用于放置游戏的控制按钮。注意,我们还引入了一个名为 `style.css` 的 CSS 文件和一个名为 `game.js` 的 JavaScript 文件。
2. 编写CSS样式
接下来,我们需要编写一些CSS样式来渲染游戏页面。我们将使用 `flexbox` 布局来创建游戏面板,并使用 `grid` 布局来创建游戏控制按钮。
```css
body {
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
margin-top: 20px;
}
#game-container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
width: 400px;
height: 400px;
margin: 0 auto;
background-color: #BBADA0;
padding: 10px;
box-sizing: border-box;
}
.tile {
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
font-weight: bold;
color: #776E65;
width: 90px;
height: 90px;
border-radius: 5px;
background-color: #CDC1B4;
margin-bottom: 10px;
}
#btn-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
margin-top: 20px;
}
.btn {
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
font-weight: bold;
color: #FFFFFF;
width: 80px;
height: 40px;
border-radius: 5px;
background-color: #8F7A66;
cursor: pointer;
}
```
在上面的代码中,我们为 `game-container` 容器定义了 `flexbox` 布局,用于显示游戏面板。我们还使用 `grid` 布局来创建了 `btn-container` 容器,用于显示游戏控制按钮。
3. 编写JavaScript代码
现在,我们需要编写一些JavaScript代码来实现游戏逻辑。我们将使用一个二维数组来表示游戏面板,并使用随机函数来生成新的方块。
```javascript
const gameContainer = document.getElementById('game-container');
const btnContainer = document.getElementById('btn-container');
const scoreContainer = document.getElementById('score-container');
const ROW = 4;
const COL = 4;
let score = 0;
let board = [];
function init() {
// 初始化游戏面板
for (let i = 0; i < ROW; i++) {
board[i] = [];
for (let j = 0; j < COL; j++) {
board[i][j] = 0;
const tile = document.createElement('div');
tile.className = 'tile';
tile.id = i + '-' + j;
gameContainer.appendChild(tile);
}
}
// 随机生成两个方块
generateNewTile();
generateNewTile();
}
function generateNewTile() {
// 随机生成一个新方块
const randomNumber = Math.random() < 0.5 ? 2 : 4;
const emptyTiles = [];
board.forEach((row, rowIndex) => {
row.forEach((col, colIndex) => {
if (col === 0) {
emptyTiles.push({ rowIndex, colIndex });
}
});
});
if (emptyTiles.length > 0) {
const randomIndex = Math.floor(Math.random() * emptyTiles.length);
const { rowIndex, colIndex } = emptyTiles[randomIndex];
board[rowIndex][colIndex] = randomNumber;
const tile = document.getElementById(rowIndex + '-' + colIndex);
tile.innerText = randomNumber;
tile.style.backgroundColor = getTileColor(randomNumber);
}
}
function getTileColor(number) {
// 根据方块数字返回不同的颜色
switch (number) {
case 2:
return '#EEE4DA';
case 4:
return '#EDE0C8';
case 8:
return '#F2B179';
case 16:
return '#F59563';
case 32:
return '#F67C5F';
case 64:
return '#F65E3B';
case 128:
return '#EDCF72';
case 256:
return '#EDCC61';
case 512:
return '#EDC850';
case 1024:
return '#EDC53F';
case 2048:
return '#EDC22E';
default:
return '#CDC1B4';
}
}
init();
```
在上面的代码中,我们定义了 `init` 函数,用于初始化游戏面板。我们还定义了 `generateNewTile` 函数,用于随机生成新的方块。在 `generateNewTile` 函数中,我们使用 `board` 数组来记录游戏面板上的数字,然后使用 `getTileColor` 函数来返回不同数字的方块颜色。
4. 添加游戏控制按钮
最后,我们需要添加一些游戏控制按钮来控制游戏的进行。我们将添加四个按钮,分别用于上下左右移动方块。
```javascript
const upBtn = document.createElement('div');
upBtn.className = 'btn';
upBtn.innerText = 'Up';
upBtn.addEventListener('click', () => {
console.log('Up');
});
const downBtn = document.createElement('div');
downBtn.className = 'btn';
downBtn.innerText = 'Down';
downBtn.addEventListener('click', () => {
console.log('Down');
});
const leftBtn = document.createElement('div');
leftBtn.className = 'btn';
leftBtn.innerText = 'Left';
leftBtn.addEventListener('click', () => {
console.log('Left');
});
const rightBtn = document.createElement('div');
rightBtn.className = 'btn';
rightBtn.innerText = 'Right';
rightBtn.addEventListener('click', () => {
console.log('Right');
});
btnContainer.appendChild(upBtn);
btnContainer.appendChild(downBtn);
btnContainer.appendChild(leftBtn);
btnContainer.appendChild(rightBtn);
```
在上面的代码中,我们使用 `createElement` 函数来创建游戏控制按钮,并使用 `addEventListener` 函数为每个按钮添加点击事件。注意,我们还需要将按钮添加到 `btn-container` 容器中。
这样就完成了一个简单的2048小游戏的制作!当然,这只是一个开始,你可以继续添加更多的功能,让游戏更加完善。