html+css+js写个军旗游戏代码
时间: 2023-08-27 09:06:41 浏览: 162
好的,我给你一个简单的军旗游戏的代码示例,使用 HTML、CSS 和 JavaScript 实现。
HTML 代码:
```html
<div id="game-board"></div>
```
CSS 代码:
```css
#game-board {
width: 300px;
height: 300px;
display: flex;
flex-wrap: wrap;
}
.square {
width: calc(300px / 10);
height: calc(300px / 10);
border: 1px solid black;
box-sizing: border-box;
cursor: pointer;
}
.square.red {
background-color: red;
}
.square.blue {
background-color: blue;
}
```
JavaScript 代码:
```javascript
const board = document.getElementById("game-board");
let squares = [];
let currentPlayer = "red";
// 创建游戏面板
function createBoard(size) {
for (let i = 0; i < size * size; i++) {
const square = document.createElement("div");
square.classList.add("square");
square.setAttribute("data-index", i);
square.addEventListener("click", handleClick);
board.appendChild(square);
squares.push(square);
}
}
// 处理点击事件
function handleClick(event) {
const square = event.target;
const index = parseInt(square.getAttribute("data-index"));
if (currentPlayer === "red") {
square.classList.add("red");
currentPlayer = "blue";
} else {
square.classList.add("blue");
currentPlayer = "red";
}
}
createBoard(10);
```
以上就是一个简单的军旗游戏代码示例,你可以根据这个示例进行修改和扩展。
阅读全文