Files
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

752 lines
26 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>🎮 俄罗斯方块 | 小游戏中心</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<!-- Windows浏览器兼容性修复 -->
<script>
// 修复 crypto.randomUUID 兼容性问题
if (!window.crypto?.randomUUID) {
window.crypto = window.crypto || {};
window.crypto.randomUUID = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
}
// 模拟Poki SDK(如果加载失败)
window.PokiSDK = window.PokiSDK || {
init: () => Promise.resolve(),
gameLoadingFinished: () => console.log('🎮 游戏加载完成'),
gameplayStart: () => console.log('🎮 游戏开始'),
gameplayStop: () => console.log('🎮 游戏停止'),
commercialBreak: () => Promise.resolve()
};
</script>
<!-- 本地PlayCanvas库 -->
<script>
// 简化的PlayCanvas模拟(如果CDN加载失败)
if (typeof pc === 'undefined') {
console.warn('⚠️ PlayCanvas CDN加载失败,使用简化版本');
window.pc = {
Application: class {
constructor(canvas, options) {
this.canvas = canvas;
this.graphicsDevice = {
resize: () => console.log('📐 调整画布大小')
};
console.log('🎮 PlayCanvas应用已创建');
}
start() { console.log('▶️ PlayCanvas已启动'); }
setCanvasFillMode() {}
},
FILLMODE_NONE: 'none',
Mouse: class {},
TouchDevice: class {},
ElementInput: class {}
};
}
</script>
<!-- 延迟加载外部库 -->
<script src="https://game-cdn.poki.com/scripts/v2/poki-sdk.js" async defer onerror="console.warn('⚠️ Poki SDK加载失败,使用模拟版本')"></script>
<script src="https://code.playcanvas.com/playcanvas-stable.min.js" async defer onerror="console.warn('⚠️ PlayCanvas加载失败,使用模拟版本')"></script>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
overflow: hidden;
background: #0a0a1a;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
touch-action: none;
}
#game-container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding: 10px;
}
canvas {
display: block;
box-shadow: 0 0 40px rgba(0, 100, 255, 0.3);
border-radius: 8px;
background: #111827;
}
/* 虚拟控制按钮容器 */
.controls-container {
margin-top: 20px;
display: flex;
flex-direction: column;
align-items: center;
gap: 15px;
width: 100%;
}
.control-row {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
max-width: 500px;
}
.action-button {
padding: 12px 24px;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s ease;
display: flex;
align-items: center;
gap: 8px;
min-width: 100px;
justify-content: center;
}
.action-button.start {
background: linear-gradient(145deg, #10b981 0%, #047857 100%);
color: white;
}
.action-button.restart {
background: linear-gradient(145deg, #f59e0b 0%, #d97706 100%);
color: white;
}
.control-button {
width: 65px;
height: 65px;
background: linear-gradient(145deg, #1e293b 0%, #0f172a 100%);
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
font-size: 28px;
color: #fff;
cursor: pointer;
user-select: none;
border: 2px solid rgba(255, 255, 255, 0.1);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
transition: all 0.1s ease;
}
.control-row:nth-child(2) {
justify-content: center;
gap: 5px;
}
.info-panel {
width: 100%;
max-width: 500px;
background: rgba(255, 255, 255, 0.1);
border-radius: 8px;
padding: 15px;
display: flex;
justify-content: space-around;
margin-top: 10px;
}
.info-item {
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
}
.info-label {
color: #aaa;
font-size: 12px;
}
.info-value {
color: #fff;
font-weight: bold;
font-size: 20px;
}
.status-message {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.8);
color: white;
padding: 10px 20px;
border-radius: 5px;
font-size: 14px;
z-index: 1000;
display: none;
}
.debug-info {
position: fixed;
bottom: 10px;
left: 10px;
background: rgba(0, 0, 0, 0.8);
color: #68d391;
padding: 10px;
border-radius: 5px;
font-family: monospace;
font-size: 12px;
max-width: 300px;
max-height: 200px;
overflow-y: auto;
}
.loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-size: 18px;
text-align: center;
}
.error-message {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(220, 38, 38, 0.9);
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
max-width: 400px;
display: none;
}
</style>
</head>
<body>
<div class="loading" id="loading">🎮 加载俄罗斯方块...</div>
<div class="error-message" id="error-message"></div>
<div class="status-message" id="status-message"></div>
<div id="game-container">
<canvas id="application" width="540" height="720"></canvas>
<div class="controls-container">
<!-- 第一行:开始/旋转/重来按钮 -->
<div class="control-row">
<button class="action-button start" id="start-btn">
<span>▶️</span> 开始
</button>
<div class="control-button rotate" id="rotate-btn">↻ 旋转</div>
<button class="action-button restart" id="restart-btn">
<span>🔄</span> 重来
</button>
</div>
<!-- 第二行:方向控制按钮 -->
<div class="control-row">
<div class="control-button left" id="left-btn">←</div>
<div class="control-button down" id="down-btn">↓</div>
<div class="control-button right" id="right-btn">→</div>
</div>
<!-- 游戏信息面板 -->
<div class="info-panel">
<div class="info-item">
<span class="info-label">分数</span>
<span class="info-value" id="score">0</span>
</div>
<div class="info-item">
<span class="info-label">等级</span>
<span class="info-value" id="level">0</span>
</div>
<div class="info-item">
<span class="info-label">消行</span>
<span class="info-value" id="lines">0</span>
</div>
<div class="info-item">
<span class="info-label">速度</span>
<span class="info-value" id="speed">1.0x</span>
</div>
</div>
</div>
</div>
<div class="debug-info" id="debug-info">
<div>调试信息:</div>
</div>
<script>
// 调试日志
function debugLog(message) {
const debugEl = document.getElementById('debug-info');
const logEntry = document.createElement('div');
logEntry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
debugEl.appendChild(logEntry);
debugEl.scrollTop = debugEl.scrollHeight;
console.log(message);
}
// 显示状态消息
function showStatus(message, duration = 2000) {
const statusEl = document.getElementById('status-message');
statusEl.textContent = message;
statusEl.style.display = 'block';
setTimeout(() => {
statusEl.style.display = 'none';
}, duration);
}
// 显示错误
function showError(message) {
const errorEl = document.getElementById('error-message');
errorEl.innerHTML = `<h3>⚠️ 错误</h3><p>${message}</p><button onclick="location.reload()" style="margin-top: 10px; padding: 8px 16px; background: white; color: #dc2626; border: none; border-radius: 5px; cursor: pointer;">刷新页面</button>`;
errorEl.style.display = 'block';
}
// 隐藏加载界面
function hideLoading() {
document.getElementById('loading').style.display = 'none';
}
// 游戏状态
const game = {
canvas: null,
ctx: null,
gridWidth: 10,
gridHeight: 20,
cellSize: 24,
board: [],
currentPiece: null,
score: 0,
level: 0,
lines: 0,
speed: 1.0,
isPaused: true,
isGameOver: false,
gameLoop: null,
lastTime: 0
};
// 方块类型
const pieceTypes = [
{ type: 'I', shape: [[1,1,1,1]], color: '#00f5ff' },
{ type: 'J', shape: [[1,0,0],[1,1,1]], color: '#0000ff' },
{ type: 'L', shape: [[0,0,1],[1,1,1]], color: '#ff7f00' },
{ type: 'O', shape: [[1,1],[1,1]], color: '#ffff00' },
{ type: 'S', shape: [[0,1,1],[1,1,0]], color: '#00ff00' },
{ type: 'T', shape: [[0,1,0],[1,1,1]], color: '#800080' },
{ type: 'Z', shape: [[1,1,0],[0,1,1]], color: '#ff0000' }
];
// 初始化游戏
function initGame() {
debugLog('初始化游戏...');
// 获取画布和上下文
game.canvas = document.getElementById('application');
game.ctx = game.canvas.getContext('2d');
// 清空游戏板
game.board = Array(game.gridHeight).fill().map(() => Array(game.gridWidth).fill(0));
// 重置状态
game.score = 0;
game.level = 0;
game.lines = 0;
game.speed = 1.0;
game.isPaused = false;
game.isGameOver = false;
// 生成新方块
spawnNewPiece();
// 开始游戏循环
startGameLoop();
// 更新UI
updateUI();
render();
debugLog('游戏初始化完成');
showStatus('🎮 游戏开始!');
}
// 生成新方块
function spawnNewPiece() {
const randomType = pieceTypes[Math.floor(Math.random() * pieceTypes.length)];
game.currentPiece = {
type: randomType.type,
shape: randomType.shape,
color: randomType.color,
x: Math.floor(game.gridWidth / 2) - Math.floor(randomType.shape[0].length / 2),
y: 0
};
debugLog(`生成新方块: ${game.currentPiece.type}`);
}
// 游戏循环
function startGameLoop() {
if (game.gameLoop) cancelAnimationFrame(game.gameLoop);
function gameLoop(timestamp) {
if (!game.lastTime) game.lastTime = timestamp;
const deltaTime = timestamp - game.lastTime;
if (deltaTime > 1000 / game.speed) {
if (!game.isPaused && !game.isGameOver) {
movePiece(0, 1);
game.lastTime = timestamp;
}
}
render();
game.gameLoop = requestAnimationFrame(gameLoop);
}
game.gameLoop = requestAnimationFrame(gameLoop);
}
// 移动方块
function movePiece(dx, dy) {
if (!game.currentPiece || game.isPaused || game.isGameOver) return;
const newX = game.currentPiece.x + dx;
const newY = game.currentPiece.y + dy;
if (isValidMove(game.currentPiece.shape, newX, newY)) {
game.currentPiece.x = newX;
game.currentPiece.y = newY;
} else if (dy > 0) {
// 如果向下移动失败,固定方块
lockPiece();
checkLines();
spawnNewPiece();
// 检查游戏是否结束
if (!isValidMove(game.currentPiece.shape, game.currentPiece.x, game.currentPiece.y)) {
gameOver();
}
}
}
// 旋转方块
function rotatePiece() {
if (!game.currentPiece || game.isPaused || game.isGameOver) return;
const rotated = rotateMatrix(game.currentPiece.shape);
if (isValidMove(rotated, game.currentPiece.x, game.currentPiece.y)) {
game.currentPiece.shape = rotated;
}
}
// 旋转矩阵
function rotateMatrix(matrix) {
const result = [];
for (let i = 0; i < matrix[0].length; i++) {
result[i] = [];
for (let j = 0; j < matrix.length; j++) {
result[i][j] = matrix[matrix.length - 1 - j][i];
}
}
return result;
}
// 检查移动是否有效
function isValidMove(shape, x, y) {
for (let row = 0; row < shape.length; row++) {
for (let col = 0; col < shape[row].length; col++) {
if (shape[row][col]) {
const newX = x + col;
const newY = y + row;
if (newX < 0 || newX >= game.gridWidth || newY >= game.gridHeight) {
return false;
}
if (newY >= 0 && game.board[newY][newX]) {
return false;
}
}
}
}
return true;
}
// 固定方块
function lockPiece() {
const { shape, x, y, color } = game.currentPiece;
for (let row = 0; row < shape.length; row++) {
for (let col = 0; col < shape[row].length; col++) {
if (shape[row][col]) {
const boardX = x + col;
const boardY = y + row;
if (boardY >= 0) {
game.board[boardY][boardX] = color;
}
}
}
}
}
// 检查并消除完整的行
function checkLines() {
let linesCleared = 0;
for (let y = game.gridHeight - 1; y >= 0; y--) {
if (game.board[y].every(cell => cell !== 0)) {
// 移除该行
game.board.splice(y, 1);
// 在顶部添加新行
game.board.unshift(Array(game.gridWidth).fill(0));
linesCleared++;
y++; // 重新检查同一位置(因为行下移了)
}
}
if (linesCleared > 0) {
game.lines += linesCleared;
game.score += linesCleared * 100 * (game.level + 1);
game.level = Math.floor(game.lines / 10);
game.speed = 1.0 + game.level * 0.1;
debugLog(`消除 ${linesCleared} 行,得分: ${linesCleared * 100 * (game.level + 1)}`);
}
}
// 游戏结束
function gameOver() {
game.isGameOver = true;
cancelAnimationFrame(game.gameLoop);
showStatus(`游戏结束!最终得分: ${game.score}`);
debugLog('游戏结束');
}
// 暂停/继续游戏
function togglePause() {
game.isPaused = !game.isPaused;
if (game.isPaused) {
showStatus('⏸️ 游戏暂停');
debugLog('游戏暂停');
} else {
showStatus('▶️ 游戏继续');
debugLog('游戏继续');
}
updateUI();
}
// 渲染游戏
function render() {
const ctx = game.ctx;
const canvas = game.canvas;
// 清空画布
ctx.fillStyle = '#111827';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制网格
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.lineWidth = 1;
for (let x = 0; x <= game.gridWidth; x++) {
ctx.beginPath();
ctx.moveTo(x * game.cellSize, 0);
ctx.lineTo(x * game.cellSize, canvas.height);
ctx.stroke();
}
for (let y = 0; y <= game.gridHeight; y++) {
ctx.beginPath();
ctx.moveTo(0, y * game.cellSize);
ctx.lineTo(canvas.width, y * game.cellSize);
ctx.stroke();
}
// 绘制已固定的方块
for (let y = 0; y < game.gridHeight; y++) {
for (let x = 0; x < game.gridWidth; x++) {
if (game.board[y][x]) {
ctx.fillStyle = game.board[y][x];
ctx.fillRect(x * game.cellSize, y * game.cellSize, game.cellSize, game.cellSize);
ctx.strokeStyle = 'white';
ctx.lineWidth = 2;
ctx.strokeRect(x * game.cellSize, y * game.cellSize, game.cellSize, game.cellSize);
}
}
}
// 绘制当前方块
if (game.currentPiece) {
const { shape, x, y, color } = game.currentPiece;
ctx.fillStyle = color;
for (let row = 0; row < shape.length; row++) {
for (let col = 0; col < shape[row].length; col++) {
if (shape[row][col]) {
ctx.fillRect((x + col) * game.cellSize, (y + row) * game.cellSize, game.cellSize, game.cellSize);
ctx.strokeStyle = 'white';
ctx.lineWidth = 2;
ctx.strokeRect((x + col) * game.cellSize, (y + row) * game.cellSize, game.cellSize, game.cellSize);
}
}
}
}
}
// 更新UI
function updateUI() {
document.getElementById('score').textContent = game.score;
document.getElementById('level').textContent = game.level;
document.getElementById('lines').textContent = game.lines;
document.getElementById('speed').textContent = game.speed.toFixed(1) + 'x';
const startBtn = document.getElementById('start-btn');
if (!game.currentPiece && !game.isGameOver) {
startBtn.innerHTML = '<span>▶️</span> 开始';
startBtn.className = 'action-button start';
} else if (game.isPaused) {
startBtn.innerHTML = '<span>▶️</span> 继续';
startBtn.className = 'action-button start';
} else if (game.isGameOver) {
startBtn.innerHTML = '<span>🔄</span> 重新开始';
startBtn.className = 'action-button restart';
} else {
startBtn.innerHTML = '<span>⏸️</span> 暂停';
startBtn.className = 'action-button start';
}
}
// 设置控件
function setupControls() {
debugLog('设置控件事件...');
// 开始按钮
document.getElementById('start-btn').addEventListener('click', () => {
debugLog('开始按钮点击');
if (!game.currentPiece && !game.isGameOver) {
initGame();
} else if (game.isPaused) {
togglePause();
} else if (game.isGameOver) {
initGame();
} else {
togglePause();
}
});
// 重来按钮
document.getElementById('restart-btn').addEventListener('click', () => {
debugLog('重来按钮点击');
initGame();
});
// 方向按钮
document.getElementById('left-btn').addEventListener('click', () => {
debugLog('左按钮点击');
movePiece(-1, 0);
});
document.getElementById('right-btn').addEventListener('click', () => {
debugLog('右按钮点击');
movePiece(1, 0);
});
document.getElementById('down-btn').addEventListener('click', () => {
debugLog('下按钮点击');
movePiece(0, 1);
});
document.getElementById('rotate-btn').addEventListener('click', () => {
debugLog('旋转按钮点击');
rotatePiece();
});
// 键盘控制
document.addEventListener('keydown', (e) => {
debugLog(`键盘按键: ${e.key}`);
// 如果游戏还没开始,按任意键开始游戏
if (!game.currentPiece && !game.isGameOver) {
initGame();
return;
}
if (game.isPaused || game.isGameOver) return;
switch(e.key) {
case 'ArrowLeft':
movePiece(-1, 0);
break;
case 'ArrowRight':
movePiece(1, 0);
break;
case 'ArrowDown':
movePiece(0, 1);
break;
case 'ArrowUp':
case ' ':
rotatePiece();
break;
case 'p':
case 'P':
togglePause();
break;
}
});
debugLog('控件事件设置完成');
}
// 页面加载完成
window.addEventListener('load', () => {
debugLog('页面加载完成');
debugLog(`浏览器: ${navigator.userAgent}`);
debugLog(`平台: ${navigator.platform}`);
// 检查浏览器兼容性
if (!window.crypto?.randomUUID) {
debugLog('⚠️ crypto.randomUUID 需要兼容性修复');
}
if (typeof pc === 'undefined') {
debugLog('⚠️ PlayCanvas 未加载,使用Canvas 2D渲染');
}
// 设置控件
setupControls();
updateUI();
// 隐藏加载界面
hideLoading();
// 初始渲染
render();
showStatus('✅ 俄罗斯方块已加载,点击开始按钮或按任意键开始游戏', 3000);
});
// 错误处理
window.addEventListener('error', (e) => {
console.error('全局错误:', e);
debugLog(`错误: ${e.message}`);
showError(`JavaScript错误: ${e.message}`);
});
</script>
</body>
</html>