h5上下左右小游戏 源码
时间: 2023-08-01 18:01:27 浏览: 209
H5小游戏源码
H5上下左右小游戏源码如下:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>H5上下左右小游戏</title>
<style>
body {
margin: 0;
background-color: #f1f1f1;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-family: Arial, sans-serif;
}
#game {
width: 300px;
height: 300px;
border: 1px solid #ccc;
display: flex;
flex-wrap: wrap;
align-content: flex-start;
}
.cell {
width: 100px;
height: 100px;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
font-weight: bold;
}
.up {
background-color: #ffcc00;
}
.down {
background-color: #ff6666;
}
.left {
background-color: #99ccff;
}
.right {
background-color: #33cc33;
}
</style>
</head>
<body>
<div id="game">
<div class="cell up">上</div>
<div class="cell down">下</div>
<div class="cell left">左</div>
<div class="cell right">右</div>
</div>
<script>
let selectedCell = null;
document.querySelectorAll('.cell').forEach(cell => {
cell.addEventListener('click', () => {
if (selectedCell) {
selectedCell.classList.remove('selected');
}
selectedCell = cell;
selectedCell.classList.add('selected');
});
});
</script>
</body>
</html>
```
这个游戏使用HTML和CSS创建了一个300x300像素的游戏界面,分为上、下、左、右四个方向的格子。点击格子时,会给所选格子添加一个选中的样式,并取消之前选中的格子的样式。用户可以根据自己的需求自定义格子的样式以及点击后的动作。
阅读全文