用html5编写一个拼图小游戏
时间: 2023-08-22 15:09:35 浏览: 88
好的,下面是用HTML5和JavaScript编写的简单拼图小游戏,供参考:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>拼图小游戏</title>
<style>
#game-board {
width: 360px;
height: 360px;
margin: 0 auto;
border: 1px solid #ccc;
position: relative;
}
.game-tile {
width: 120px;
height: 120px;
position: absolute;
background-color: #fff;
border: 1px solid #ccc;
box-sizing: border-box;
transition: all 0.5s ease-in-out;
}
.game-tile img {
width: 100%;
height: 100%;
}
.game-tile.col-1.row-1 {
top: 0;
left: 0;
}
.game-tile.col-2.row-1 {
top: 0;
left: 120px;
}
.game-tile.col-3.row-1 {
top: 0;
left: 240px;
}
.game-tile.col-1.row-2 {
top: 120px;
left: 0;
}
.game-tile.col-2.row-2 {
top: 120px;
left: 120px;
}
.game-tile.col-3.row-2 {
top: 120px;
left: 240px;
}
.game-tile.col-1.row-3 {
top: 240px;
left: 0;
}
.game-tile.col-2.row-3 {
top: 240px;
left: 120px;
}
.game-tile.col-3.row-3 {
top: 240px;
left: 240px;
}
.game-tile.blank {
background-color: transparent;
border: none;
}
</style>
</head>
<body>
<h1>拼图小游戏</h1>
<div id="game-board">
<div class="game-tile col-1 row-1"><img src="tile-1.png"></div>
<div class="game-tile col-2 row-1"><img src="tile-2.png"></div>
<div class="game-tile col-3 row-1"><img src="tile-3.png"></div>
<div class="game-tile col-1 row-2"><img src="tile-4.png"></div>
<div class="game-tile col-2 row-2"><img src="tile-5.png"></div>
<div class="game-tile col-3 row-2"><img src="tile-6.png"></div>
<div class="game-tile col-1 row-3"><img src="tile-7.png"></div>
<div class="game-tile col-2 row-3"><img src="tile-8.png"></div>
<div class="game-tile col-3 row-3 blank"></div>
</div>
<button id="shuffle-btn">打乱拼图</button>
<script src="puzzle.js"></script>
</body>
</html>
```
上面的代码中,我们创建了一个`div`元素作为游戏面板,其中包含9个小格子,每个小格子里面放置了一个图片,同时给每个小格子分配了一个`col-x`和`row-x`的类名,用于控制它们的位置。其中最后一个小格子是空白的,用于实现拼图游戏。
JavaScript代码:
```javascript
window.onload = function() {
var board = document.getElementById('game-board');
var tiles = board.children;
var shuffleBtn = document.getElementById('shuffle-btn');
var blankTile = tiles[tiles.length - 1];
var blankTilePos = {
col: 3,
row: 3
};
// 将拼图打乱
shuffleBtn.onclick = function() {
shuffleTiles();
}
// 移动拼图
for (var i = 0; i < tiles.length; i++) {
tiles[i].onclick = function() {
var clickedTile = this;
var clickedTilePos = getTilePos(clickedTile);
var isMovable = checkMovable(clickedTilePos);
if (isMovable) {
moveTile(clickedTile, clickedTilePos);
}
}
}
// 获取拼图位置
function getTilePos(tile) {
var classes = tile.className.split(' ');
for (var i = 0; i < classes.length; i++) {
if (classes[i].indexOf('col-') === 0) {
var col = classes[i].split('-')[1];
}
if (classes[i].indexOf('row-') === 0) {
var row = classes[i].split('-')[1];
}
}
return {
col: parseInt(col),
row: parseInt(row)
};
}
// 检查拼图是否可移动
function checkMovable(tilePos) {
var colDiff = Math.abs(tilePos.col - blankTilePos.col);
var rowDiff = Math.abs(tilePos.row - blankTilePos.row);
if ((colDiff === 1 && rowDiff === 0) || (colDiff === 0 && rowDiff === 1)) {
return true;
} else {
return false;
}
}
// 移动拼图
function moveTile(tile, tilePos) {
var blankTileNewPos = {
col: tilePos.col,
row: tilePos.row
};
var blankTileNewClasses = 'game-tile col-' + blankTileNewPos.col + ' row-' + blankTileNewPos.row + ' blank';
var blankTileOldClasses = 'game-tile col-' + blankTilePos.col + ' row-' + blankTilePos.row;
blankTile.className = blankTileNewClasses;
tile.className = blankTileOldClasses;
blankTilePos = blankTileNewPos;
}
// 随机打乱拼图
function shuffleTiles() {
for (var i = 0; i < 100; i++) {
var movableTiles = [];
for (var j = 0; j < tiles.length; j++) {
var tilePos = getTilePos(tiles[j]);
var isMovable = checkMovable(tilePos);
if (isMovable) {
movableTiles.push(tiles[j]);
}
}
var randomTile = movableTiles[Math.floor(Math.random() * movableTiles.length)];
var randomTilePos = getTilePos(randomTile);
moveTile(randomTile, randomTilePos);
}
}
}
```
上面的JavaScript代码中,我们首先获取了拼图面板、所有小格子、打乱按钮和空白小格子,然后通过循环给每个小格子添加了`onclick`事件,当小格子被点击时,会检查该小格子是否可移动,如果可以移动,则调用`moveTile`函数移动拼图。`moveTile`函数会将空白小格子和当前小格子的位置互换,同时更新空白小格子的位置。最后,我们还实现了一个`shuffleTiles`函数,用于随机打乱拼图。
完整的拼图小游戏代码已经完成,可以下载图片和JavaScript文件,将它们放在同一个文件夹下运行游戏。
阅读全文