// ゲームに関連する変数と定数を設定 const canvas = document.getElementById('game-board'); const ctx =…
- User
// ゲームに関連する変数と定数を設定 const canvas = document.getElementById('game-board'); const ctx = canvas.getContext('2d'); const blockSize = 50; const gridWidth = 8; const gridHeight = 16; const colors = ['red', 'blue', 'green', 'yellow', 'purple'];
let gameGrid; let currentPair; let score; let chainCount; let paused; let lastTime; let currentSpeed = 1;
// ゲームの初期化 function initializeGame() { gameGrid = createEmptyGrid(); currentPair = createNewPair(); score = 0; chainCount = 0; paused = false; displayScore(); drawGameGrid(); lastTime = performance.now(); gameLoop(); }
// 空のグリッドを作成 function createEmptyGrid() { const grid = []; for (let y = 0; y < gridHeight; y++) { const row = []; for (let x = 0; x < gridWidth; x++) { row.push(null); } grid.push(row); } return grid; }
// 新しいブロックペアを作成 function createNewPair() { const color1 = colors[Math.floor(Math.random() * colors.length)]; const color2 = colors[Math.floor(Math.random() * colors.length)]; const block1 = { x: Math.floor(gridWidth / 2) - 1, y: 0, color: color1 }; const block2 = { x: Math.floor(gridWidth / 2), y: 0, color: color2 }; return { block1, block2 }; }
// ゲームのメインループ function gameLoop() { const currentTime = performance.now(); if (currentTime - lastTime > 1000 / currentSpeed) { if (!paused) { if (!moveCurrentPair(0, 1)) { lockCurrentPair(); if (gameOver()) { displayOverlay("Game Over"); return; } checkForClearsAndChains(); currentPair = createNewPair(); } } } drawGameGrid(); requestAnimationFrame(gameLoop); }
// 現在のブロックペアを移動 function moveCurrentPair(dx, dy) { const newBlock1 = { x: currentPair.block1.x + dx, y: currentPair.block1.y + dy, }; const newBlock2 = { x: currentPair.block2.x + dx, y: currentPair.block2.y + dy, };
if (isValidMove(newBlock1) && isValidMove(newBlock2)) { currentPair.block1 = newBlock1; currentPair.block2 = newBlock2; return true; } return false; }
// 現在のブロックペアを回転 function rotateCurrentPair(clockwise) { if (paused) return;
const centerBlock = currentPair.block1; const rotatingBlock = currentPair.block2; const dx = rotatingBlock.x - centerBlock.x; const dy = rotatingBlock.y - centerBlock.y; const newDx = clockwise ? dy : -dy; const newDy = clockwise ? -dx : dx; const newRotatingBlock = { x: centerBlock.x + newDx, y: centerBlock.y + newDy, color: rotatingBlock.color, };
if (isValidMove(newRotatingBlock)) { currentPair.block2 = newRotatingBlock; } }
// 移動が有効かどうかを判断 function isValidMove(block) { return ( block.x >= 0 && block.x < gridWidth && block.y >= 0 && block.y < gridHeight && gameGrid[block.y][block.x] === null ); }
// 現在のブロックペアを固定 function lockCurrentPair() { gameGrid[currentPair.block1.y][currentPair.block1.x] = currentPair.block1.color; gameGrid[currentPair.block2.y][currentPair.block2.x] = currentPair.block2.color; }
// ゲームオーバー判定 function gameOver() { return ( gameGrid[currentPair.block1.y][currentPair.block1.x] !== null || gameGrid[currentPair.block2.y][currentPair.block2.x] !== null ); }
- Assistant
以下のように修正しました:
initializeGame関数で、spawnNextPairをcreateNewPairに戻しました。- 不要な
initGame関数を削除しました。
修正後のコード:
// ゲームに関連する変数と定数を設定 const canvas = document.getElementById('game-board'); const ctx = canvas.getContext('2d'); const blockSize = 50; const gridWidth = 8; const gridHeight = 16; const colors = ['red', 'blue', 'green', 'yellow', 'purple']; let gameGrid; let currentPair; let score; let chainCount; let paused; let lastTime; let currentSpeed = 1; // ゲームの初期化 function initializeGame() { gameGrid = createEmptyGrid(); currentPair = createNewPair(); // 修正: createNewPairに戻す score = 0; chainCount = 0; paused = false; displayScore(); drawGameGrid(); lastTime = performance.now(); gameLoop(); } // ゲームのメインループ function gameLoop() { currentTime = performance.now(); if (currentTime - lastTime > 1000 / currentSpeed) { if (!paused) { if (!moveCurrentPair(0, 1)) { lockCurrentPair(); if (gameOver()) { displayOverlay("Game Over"); return; } checkForClearsAndChains(); createNewPair(); // 修正: createNewPairに戻す } } lastTime = currentTime; } drawGameGrid(); requestAnimationFrame(gameLoop); } // 以下の関数は削除されました // function initGame() { // gameGrid = createEmptyGrid(); // score = 0; // chainCount = 0; // displayScore(); // spawnNextPair(); // } initializeGame();
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.