html俄罗斯方块代码
时间: 2023-08-11 10:03:44 浏览: 83
以下是 HTML 实现俄罗斯方块的基本代码,包括游戏界面和基本逻辑:
```html
<!DOCTYPE html>
<html>
<head>
<title>Tetris</title>
<style>
#container {
width: 300px;
height: 600px;
border: 1px solid black;
position: relative;
}
.block {
width: 30px;
height: 30px;
border: 1px solid black;
position: absolute;
}
</style>
</head>
<body onload="startGame()">
<div id="container"></div>
<script>
var container = document.getElementById("container");
var blocks = [];
var currentBlock = null;
var currentInterval = null;
var blockTypes = [
[[0, 0], [0, 1], [1, 0], [1, 1]],
[[0, 0], [0, 1], [0, 2], [0, 3]],
[[0, 0], [0, 1], [0, 2], [1, 2]],
[[0, 0], [1, 0], [1, 1], [2, 1]],
[[0, 1], [1, 0], [1, 1], [2, 0]],
[[0, 0], [1, 0], [2, 0], [2, 1]],
[[0, 1], [1, 1], [2, 0], [2, 1]]
];
function createBlock() {
var type = Math.floor(Math.random() * 7);
var shape = blockTypes[type];
var block = [];
for (var i = 0; i < shape.length; i++) {
var x = shape[i][0];
var y = shape[i][1];
var div = document.createElement("div");
div.className = "block";
div.style.left = (x * 30) + "px";
div.style.top = (y * 30) + "px";
container.appendChild(div);
block.push(div);
}
return block;
}
function moveBlock() {
for (var i = 0; i < currentBlock.length; i++) {
var div = currentBlock[i];
var x = parseInt(div.style.left);
var y = parseInt(div.style.top);
div.style.top = (y + 30) + "px";
}
}
function checkCollision() {
for (var i = 0; i < currentBlock.length; i++) {
var div = currentBlock[i];
var x = parseInt(div.style.left);
var y = parseInt(div.style.top);
if (y >= 570) {
return true;
}
for (var j = 0; j < blocks.length; j++) {
var block = blocks[j];
for (var k = 0; k < block.length; k++) {
var b = block[k];
var bx = parseInt(b.style.left);
var by = parseInt(b.style.top);
if (x == bx && y + 30 == by) {
return true;
}
}
}
}
return false;
}
function addBlock() {
for (var i = 0; i < currentBlock.length; i++) {
var div = currentBlock[i];
blocks.push(currentBlock);
}
currentBlock = null;
}
function checkRows() {
for (var i = 0; i < 20; i++) {
var row = [];
for (var j = 0; j < blocks.length; j++) {
var block = blocks[j];
for (var k = 0; k < block.length; k++) {
var b = block[k];
var x = parseInt(b.style.left);
var y = parseInt(b.style.top);
if (y == i * 30) {
row.push(b);
}
}
}
if (row.length == 10) {
for (var j = 0; j < row.length; j++) {
container.removeChild(row[j]);
}
blocks = blocks.filter(function(block) {
return row.indexOf(block) == -1;
});
for (var j = 0; j < blocks.length; j++) {
var block = blocks[j];
for (var k = 0; k < block.length; k++) {
var b = block[k];
var x = parseInt(b.style.left);
var y = parseInt(b.style.top);
if (y < i * 30) {
b.style.top = (y + 30) + "px";
}
}
}
i--;
}
}
}
function startGame() {
currentBlock = createBlock();
currentInterval = setInterval(function() {
if (!checkCollision()) {
moveBlock();
} else {
clearInterval(currentInterval);
addBlock();
checkRows();
startGame();
}
}, 500);
}
</script>
</body>
</html>
```
该代码利用 HTML 和 JavaScript 实现了一个基本的俄罗斯方块游戏,包括随机生成方块、方块下落、碰撞检测、方块消除等基本功能。
阅读全文