帮我写个俄罗斯方块的小游戏
时间: 2023-08-08 17:07:55 浏览: 88
好的,让我们一步步来创建一个简单的俄罗斯方块游戏。
首先,我们需要创建一个网格来表示游戏界面。我们可以使用 HTML 和 CSS 来创建一个基本的 10x20 的网格,如下所示:
```html
<!DOCTYPE html>
<html>
<head>
<title>Tetris</title>
<style>
body {
margin: 0;
padding: 0;
}
#game-board {
width: 200px;
height: 400px;
border: 1px solid black;
display: grid;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: repeat(20, 1fr);
}
.cell {
border: 1px solid gray;
}
</style>
</head>
<body>
<div id="game-board">
<div class="cell"></div>
<div class="cell"></div>
<!-- add more cells here -->
</div>
</body>
</html>
```
接下来,我们需要创建一个方块类来表示俄罗斯方块。每个方块由四个小方块组成,可以旋转和移动。我们可以使用 JavaScript 来创建一个简单的方块类:
```javascript
class Block {
constructor(x, y) {
this.x = x;
this.y = y;
this.cells = [
{ x: x, y: y },
{ x: x + 1, y: y },
{ x: x, y: y + 1 },
{ x: x + 1, y: y + 1 }
];
}
moveDown() {
this.y++;
this.cells.forEach(cell => cell.y++);
}
moveLeft() {
this.x--;
this.cells.forEach(cell => cell.x--);
}
moveRight() {
this.x++;
this.cells.forEach(cell => cell.x++);
}
rotate() {
const centerX = this.x + 0.5;
const centerY = this.y + 0.5;
this.cells.forEach(cell => {
const x = cell.x - centerX;
const y = cell.y - centerY;
cell.x = Math.round(centerX - y);
cell.y = Math.round(centerY + x);
});
}
}
```
现在我们可以在网格上显示方块。我们可以使用一个数组来表示当前的方块,使用 setInterval 函数来控制方块的下落速度,并使用键盘事件来控制方块的移动和旋转。完整的 JavaScript 代码如下所示:
```javascript
class Block {
constructor(x, y) {
this.x = x;
this.y = y;
this.cells = [
{ x: x, y: y },
{ x: x + 1, y: y },
{ x: x, y: y + 1 },
{ x: x + 1, y: y + 1 }
];
}
moveDown() {
this.y++;
this.cells.forEach(cell => cell.y++);
}
moveLeft() {
this.x--;
this.cells.forEach(cell => cell.x--);
}
moveRight() {
this.x++;
this.cells.forEach(cell => cell.x++);
}
rotate() {
const centerX = this.x + 0.5;
const centerY = this.y + 0.5;
this.cells.forEach(cell => {
const x = cell.x - centerX;
const y = cell.y - centerY;
cell.x = Math.round(centerX - y);
cell.y = Math.round(centerY + x);
});
}
}
const gameBoard = document.getElementById("game-board");
const rows = 20;
const cols = 10;
const cells = [];
for (let i = 0; i < rows * cols; i++) {
const cell = document.createElement("div");
cell.classList.add("cell");
gameBoard.appendChild(cell);
cells.push(cell);
}
let block = new Block(4, 0);
let intervalId = setInterval(() => {
const canMoveDown = block.cells.every(
cell => cell.y < rows - 1 && !cells[(cell.y + 1) * cols + cell.x].classList.contains("occupied")
);
if (canMoveDown) {
block.moveDown();
} else {
block.cells.forEach(cell => cells[cell.y * cols + cell.x].classList.add("occupied"));
block = new Block(4, 0);
}
block.cells.forEach(cell => {
const index = cell.y * cols + cell.x;
cells[index].classList.add("active");
cells[index].classList.remove("occupied");
});
}, 500);
document.addEventListener("keydown", event => {
switch (event.key) {
case "ArrowLeft":
const canMoveLeft = block.cells.every(
cell => cell.x > 0 && !cells[cell.y * cols + cell.x - 1].classList.contains("occupied")
);
if (canMoveLeft) {
block.moveLeft();
}
break;
case "ArrowRight":
const canMoveRight = block.cells.every(
cell => cell.x < cols - 1 && !cells[cell.y * cols + cell.x + 1].classList.contains("occupied")
);
if (canMoveRight) {
block.moveRight();
}
break;
case "ArrowUp":
const rotatedCells = [...block.cells];
rotatedCells.forEach(cell => {
const x = cell.x - block.x;
const y = cell.y - block.y;
cell.x = Math.round(block.x - y);
cell.y = Math.round(block.y + x);
});
const canRotate = rotatedCells.every(
cell =>
cell.x >= 0 &&
cell.x < cols &&
cell.y >= 0 &&
cell.y < rows &&
!cells[cell.y * cols + cell.x].classList.contains("occupied")
);
if (canRotate) {
block.cells = rotatedCells;
}
break;
}
block.cells.forEach(cell => {
const index = cell.y * cols + cell.x;
cells[index].classList.add("active");
cells[index].classList.remove("occupied");
});
});
```
最后,我们需要添加一些 CSS 样式来美化游戏界面。完整的 CSS 代码如下所示:
```css
body {
margin: 0;
padding: 0;
}
#game-board {
width: 200px;
height: 400px;
border: 1px solid black;
display: grid;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: repeat(20, 1fr);
}
.cell {
border: 1px solid gray;
}
.active {
background-color: #00bfff;
}
.occupied {
background-color: #808080;
}
```
现在你就可以打开浏览器,尝试玩一下这个简单的俄罗斯方块游戏了。
阅读全文