当然可以!下面是一个简单的俄罗斯方块(Tetris)游戏,使用纯 HTML + CSS + JavaScript 实现。
✅ 功能包括:
- 基本方块下落
- 方块旋转
- 方块左右移动
- 方块触底固定
- 消除整行
- 简单计分
✅ 整体目录结构(单文件):
你可以将以下内容复制粘贴为一个完整的 HTML 文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>俄罗斯方块</title>
<style>
body {
background: #111;
display: flex;
flex-direction: column;
align-items: center;
color: white;
font-family: Arial, sans-serif;
margin: 0;
padding: 1rem;
}
h1 {
margin-bottom: 1rem;
}
canvas {
background: #222;
box-shadow: 0 0 20px black;
}
#score {
margin-top: 1rem;
}
</style>
</head>
<body>
<h1>俄罗斯方块</h1>
<canvas id="tetris" width="240" height="400"></canvas>
<div id="score">分数: 0</div>
<script>
const canvas = document.getElementById('tetris');
const context = canvas.getContext('2d');
context.scale(20, 20); // 放大单位格子
const matrix = {
'T': [
[0, 1, 0],
[1, 1, 1],
[0, 0, 0],
],
'O': [
[2, 2],
[2, 2],
],
'L': [
[0, 0, 3],
[3, 3, 3],
[0, 0, 0],
],
'J': [
[4, 0, 0],
[4, 4, 4],
[0, 0, 0],
],
'I': [
[0, 5, 0, 0],
[0, 5, 0, 0],
[0, 5, 0, 0],
[0, 5, 0, 0],
],
'S': [
[0, 6, 6],
[6, 6, 0],
[0, 0, 0],
],
'Z': [
[7, 7, 0],
[0, 7, 7],
[0, 0, 0],
]
};
const colors = [
null,
'#FF0D72',
'#0DC2FF',
'#0DFF72',
'#F538FF',
'#FF8E0D',
'#FFE138',
'#3877FF',
];
const arena = createMatrix(12, 20);
function createMatrix(w, h) {
const matrix = [];
while (h--) {
matrix.push(new Array(w).fill(0));
}
return matrix;
}
function drawMatrix(matrix, offset) {
matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
context.fillStyle = colors[value];
context.fillRect(x + offset.x, y + offset.y, 1, 1);
}
});
});
}
function merge(arena, player) {
player.matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
arena[y + player.pos.y][x + player.pos.x] = value;
}
});
});
}
function collide(arena, player) {
const [m, o] = [player.matrix, player.pos];
for (let y = 0; y < m.length; ++y) {
for (let x = 0; x < m[y].length; ++x) {
if (m[y][x] !== 0 &&
(arena[y + o.y] && arena[y + o.y][x + o.x]) !== 0) {
return true;
}
}
}
return false;
}
function createPiece(type) {
return matrix[type];
}
function playerReset() {
const pieces = 'TJLOSZI';
player.matrix = createPiece(pieces[Math.floor(Math.random() * pieces.length)]);
player.pos.y = 0;
player.pos.x = Math.floor(arena[0].length / 2) - Math.floor(player.matrix[0].length / 2);
if (collide(arena, player)) {
arena.forEach(row => row.fill(0));
player.score = 0;
updateScore();
}
}
function playerDrop() {
player.pos.y++;
if (collide(arena, player)) {
player.pos.y--;
merge(arena, player);
arenaSweep();
playerReset();
}
dropCounter = 0;
}
function playerMove(dir) {
player.pos.x += dir;
if (collide(arena, player)) {
player.pos.x -= dir;
}
}
function playerRotate(dir) {
const pos = player.pos.x;
let offset = 1;
rotate(player.matrix, dir);
while (collide(arena, player)) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > player.matrix[0].length) {
rotate(player.matrix, -dir);
player.pos.x = pos;
return;
}
}
}
function rotate(matrix, dir) {
for (let y = 0; y < matrix.length; ++y) {
for (let x = 0; x < y; ++x) {
[matrix[x][y], matrix[y][x]] = [matrix[y][x], matrix[x][y]];
}
}
if (dir > 0) {
matrix.forEach(row => row.reverse());
} else {
matrix.reverse();
}
}
function arenaSweep() {
let rowCount = 1;
outer: for (let y = arena.length - 1; y >= 0; --y) {
for (let x = 0; x < arena[y].length; ++x) {
if (arena[y][x] === 0) {
continue outer;
}
}
const row = arena.splice(y, 1)[0].fill(0);
arena.unshift(row);
y++;
player.score += rowCount * 10;
rowCount *= 2;
updateScore();
}
}
function updateScore() {
document.getElementById('score').innerText = "分数: " + player.score;
}
function draw() {
context.fillStyle = "#000";
context.fillRect(0, 0, canvas.width, canvas.height);
drawMatrix(arena, {x:0, y:0});
drawMatrix(player.matrix, player.pos);
}
let dropCounter = 0;
let dropInterval = 1000;
let lastTime = 0;
function update(time = 0) {
const deltaTime = time - lastTime;
lastTime = time;
dropCounter += deltaTime;
if (dropCounter > dropInterval) {
playerDrop();
}
draw();
requestAnimationFrame(update);
}
document.addEventListener('keydown', e => {
if (e.key === 'ArrowLeft') {
playerMove(-1);
} else if (e.key === 'ArrowRight') {
playerMove(1);
} else if (e.key === 'ArrowDown') {
playerDrop();
} else if (e.key === 'ArrowUp') {
playerRotate(1);
}
});
const player = {
pos: {x: 0, y: 0},
matrix: null,
score: 0,
};
playerReset();
updateScore();
update();
</script>
</body>
</html>
🎮 操作说明:
↑:旋转方块
↓:快速下落方块
← / →:左右移动
🚀 如何运行:
- 将上述代码保存为
.html 文件,例如 tetris.html。
- 用浏览器打开就是一个可以玩的俄罗斯方块游戏了!
如果你希望扩展功能(例如开始/暂停、游戏结束提示、高分榜、本地存储等),我也可以继续帮你开发 👍
需要优化和美化提示吗?