生成一个俄罗斯方块 html+js 游戏
- User
生成一个俄罗斯方块 html+js 游戏
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
生成一个俄罗斯方块 html+js 游戏
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
下面是一个完整的俄罗斯方块游戏实现,包含HTML、CSS和JavaScript代码。你可以将这段代码保存为一个HTML文件并在浏览器中打开即可玩。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>俄罗斯方块</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
}
h1 {
color: #333;
margin-bottom: 10px;
}
.game-container {
display: flex;
gap: 20px;
margin-top: 20px;
}
#game-board {
border: 2px solid #333;
background-color: #111;
}
.side-panel {
display: flex;
flex-direction: column;
gap: 20px;
}
.next-piece {
border: 2px solid #333;
background-color: #111;
}
.info-panel {
background-color: #222;
color: white;
padding: 15px;
border-radius: 5px;
width: 200px;
}
.controls {
margin-top: 20px;
text-align: center;
}
button {
padding: 10px 15px;
margin: 0 5px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
.game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
display: none;
}
</style>
</head>
<body>
<h1>俄罗斯方块</h1>
<div class="game-container">
<canvas id="game-board" width="300" height="600"></canvas>
<div class="side-panel">
<canvas id="next-piece" width="150" height="150"></canvas>
<div class="info-panel">
<h3>分数: <span id="score">0</span></h3>
<h3>等级: <span id="level">1</span></h3>
<h3>行数: <span id="lines">0</span></h3>
</div>
</div>
</div>
<div class="controls">
<button id="start-btn">开始游戏</button>
<button id="pause-btn">暂停</button>
</div>
<div class="game-over" id="game-over">
<h2>游戏结束!</h2>
<p>最终分数: <span id="final-score">0</span></p>
<button id="restart-btn">重新开始</button>
</div>
<script>
// 游戏常量
const COLS = 10;
const ROWS = 20;
const BLOCK_SIZE = 30;
const COLORS = [
'cyan', 'blue', 'orange', 'yellow', 'green', 'purple', 'red'
];
// 方块形状定义
const SHAPES = [
[[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], // I
[[2, 0, 0], [2, 2, 2], [0, 0, 0]], // J
[[0, 0, 3], [3, 3, 3], [0, 0, 0]], // L
[[4, 4], [4, 4]], // O
[[0, 5, 5], [5, 5, 0], [0, 0, 0]], // S
[[0, 6, 0], [6, 6, 6], [0, 0, 0]], // T
[[7, 7, 0], [0, 7, 7], [0, 0, 0]] // Z
];
// 游戏变量
let canvas, ctx;
let nextCanvas, nextCtx;
let board = [];
let currentPiece, nextPiece;
let score = 0;
let level = 1;
let lines = 0;
let gameOver = false;
let isPaused = false;
let dropStart;
let gameInterval;
// 初始化游戏
function init() {
canvas = document.getElementById('game-board');
ctx = canvas.getContext('2d');
nextCanvas = document.getElementById('next-piece');
nextCtx = nextCanvas.getContext('2d');
// 初始化游戏板
createBoard();
// 生成第一个方块和下一个方块
currentPiece = randomPiece();
nextPiece = randomPiece();
// 设置事件监听器
document.addEventListener('keydown', control);
document.getElementById('start-btn').addEventListener('click', startGame);
document.getElementById('pause-btn').addEventListener('click', togglePause);
document.getElementById('restart-btn').addEventListener('click', restartGame);
// 绘制初始状态
draw();
drawNext();
}
// 创建游戏板
function createBoard() {
board = Array.from(Array(ROWS), () => Array(COLS).fill(0));
}
// 随机生成方块
function randomPiece() {
const randomIndex = Math.floor(Math.random() * SHAPES.length);
const shape = SHAPES[randomIndex];
const color = COLORS[randomIndex];
return {
shape: shape,
color: color,
x: Math.floor(COLS / 2) - Math.floor(shape[0].length / 2),
y: 0
};
}
// 绘制游戏板
function draw() {
// 清空画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制已固定的方块
for (let r = 0; r < ROWS; r++) {
for (let c = 0; c < COLS; c++) {
if (board[r][c]) {
drawBlock(c, r, COLORS[board[r][c] - 1], ctx);
}
}
}
// 绘制当前移动的方块
if (currentPiece) {
currentPiece.shape.forEach((row, y) => {
row.forEach((value, x) => {
if (value) {
drawBlock(
currentPiece.x + x,
currentPiece.y + y,
currentPiece.color,
ctx
);
}
});
});
}
}
// 绘制下一个方块预览
function drawNext() {
nextCtx.clearRect(0, 0, nextCanvas.width, nextCanvas.height);
if (nextPiece) {
const offsetX = (nextCanvas.width / BLOCK_SIZE - nextPiece.shape[0].length) / 2;
const offsetY = (nextCanvas.height / BLOCK_SIZE - nextPiece.shape.length) / 2;
nextPiece.shape.forEach((row, y) => {
row.forEach((value, x) => {
if (value) {
drawBlock(
offsetX + x,
offsetY + y,
nextPiece.color,
nextCtx
);
}
});
});
}
}
// 绘制单个方块
function drawBlock(x, y, color, context) {
context.fillStyle = color;
context.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
context.strokeStyle = '#000';
context.lineWidth = 2;
context.strokeRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
// 控制方块移动
function control(e) {
if (gameOver || isPaused) return;
switch (e.keyCode) {
case 37: // 左箭头
movePiece(-1, 0);
break;
case 38: // 上箭头 - 旋转
rotatePiece();
break;
case 39: // 右箭头
movePiece(1, 0);
break;
case 40: // 下箭头
movePiece(0, 1);