代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>消灭方块</title>
<style>
#gameBoard {
width: 300px;
height: 300px;
margin: 20px auto;
position: relative;
border: 2px solid #000;
}
.block {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
cursor: pointer;
}
</style>
</head>
<body>
<div id="gameBoard"></div>
<script>
const gameBoard = document.getElementById('gameBoard');
const blockCount = 9;
let score = 0;
function createBlock(index) {
const block = document.createElement('div');
block.classList.add('block');
block.style.left = (index % 3) * 100 + 'px';
block.style.top = Math.floor(index / 3) * 100 + 'px';
block.addEventListener('click', () => {
block.remove();
score++;
document.getElementById('score').innerText = `Score: ${score}`;
if (document.querySelectorAll('.block').length === 0) {
alert('You win!');
}
});
gameBoard.appendChild(block);
}
for (let i = 0; i < blockCount; i++) {
createBlock(i);
}
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。