用html编写一个简易版连连看
时间: 2023-09-12 09:06:39 浏览: 182
好的,以下是一个简单的 HTML 连连看游戏示例代码,您可以在浏览器中打开它来玩。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>连连看</title>
<style type="text/css">
.container {
margin: 0 auto;
width: 500px;
height: 500px;
background-color: #f9f9f9;
border: 1px solid #ccc;
position: relative;
overflow: hidden;
}
.box {
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
background-color: #fff;
border: 1px solid #ccc;
box-sizing: border-box;
text-align: center;
line-height: 50px;
font-size: 20px;
cursor: pointer;
transition: all 0.3s ease-in-out;
}
.box:hover {
background-color: #ccc;
}
.active {
background-color: #f00;
}
</style>
</head>
<body>
<div class="container">
</div>
<script type="text/javascript">
var data = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]
];
var container = document.querySelector('.container');
var boxes = [];
// 创建游戏盘面
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
var box = document.createElement('div');
box.className = 'box';
box.dataset.row = i;
box.dataset.col = j;
box.textContent = data[i][j];
container.appendChild(box);
boxes.push(box);
}
}
// 处理点击事件
var activeBox = null;
function handleClick(e) {
var box = e.target;
if (box === activeBox) {
box.classList.remove('active');
activeBox = null;
return;
}
if (!activeBox) {
box.classList.add('active');
activeBox = box;
} else {
if (isLinkable(activeBox, box)) {
removeBoxes(activeBox, box);
} else {
activeBox.classList.remove('active');
box.classList.add('active');
activeBox = box;
}
}
}
// 判断两个格子是否可以连通
function isLinkable(box1, box2) {
var row1 = parseInt(box1.dataset.row);
var col1 = parseInt(box1.dataset.col);
var row2 = parseInt(box2.dataset.row);
var col2 = parseInt(box2.dataset.col);
if (row1 === row2 && col1 === col2) {
return false;
}
if (box1.textContent !== box2.textContent) {
return false;
}
if (row1 === row2) {
if (isRowLinkable(row1, col1, col2)) {
return true;
}
}
if (col1 === col2) {
if (isColLinkable(col1, row1, row2)) {
return true;
}
}
if (isVLinkable(row1, col1, row2, col2)) {
return true;
}
if (isVLinkable(row2, col2, row1, col1)) {
return true;
}
return false;
}
// 判断两个格子在同一行是否可连通
function isRowLinkable(row, col1, col2) {
if (col1 > col2) {
var temp = col1;
col1 = col2;
col2 = temp;
}
for (var i = col1 + 1; i < col2; i++) {
if (data[row][i] !== 0) {
return false;
}
}
return true;
}
// 判断两个格子在同一列是否可连通
function isColLinkable(col, row1, row2) {
if (row1 > row2) {
var temp = row1;
row1 = row2;
row2 = temp;
}
for (var i = row1 + 1; i < row2; i++) {
if (data[i][col] !== 0) {
return false;
}
}
return true;
}
// 判断两个格子在竖直方向是否可连通
function isVLinkable(row1, col1, row2, col2) {
var dx = col2 - col1;
var dy = row2 - row1;
var x = col1;
var y = row1;
if (dx !== 0 && dy !== 0) {
return false;
}
if (dy > 0) {
for (var i = 1; i <= dy; i++) {
y = row1 + i;
if (data[y][x] !== 0) {
return false;
}
}
} else {
for (var i = -1; i >= dy; i--) {
y = row1 + i;
if (data[y][x] !== 0) {
return false;
}
}
}
return true;
}
// 移除连通的两个格子
function removeBoxes(box1, box2) {
var row1 = parseInt(box1.dataset.row);
var col1 = parseInt(box1.dataset.col);
var row2 = parseInt(box2.dataset.row);
var col2 = parseInt(box2.dataset.col);
data[row1][col1] = 0;
data[row2][col2] = 0;
box1.remove();
box2.remove();
activeBox = null;
if (isGameOver()) {
alert('游戏结束!');
}
}
// 判断游戏是否结束
function isGameOver() {
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
if (data[i][j] !== 0) {
return false;
}
}
}
return true;
}
// 绑定事件处理程序
for (var i = 0; i < boxes.length; i++) {
boxes[i].addEventListener('click', handleClick);
}
</script>
</body>
</html>
```
注意,这只是一个简单的示例,实际上,连连看游戏的复杂度很高,需要考虑很多因素,例如不同形状的图案、不同的连通规则、提示和动画效果等等。如果您想要制作更完整、更流畅的游戏,还需要进一步研究和开发。
阅读全文