1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
| class GemGame { constructor() { this.board = []; this.selectedGem = null; this.score = 0; this.colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange']; this.boardSize = 6; this.gameBoard = document.getElementById('gameBoard'); this.scoreElement = document.getElementById('score'); this.resetButton = document.getElementById('resetButton'); this.init(); this.addResetListener(); }
init() { this.createBoard(); this.renderBoard(); this.addEventListeners(); }
addResetListener() { this.resetButton.addEventListener('click', () => { this.resetGame(); }); }
resetGame() { this.score = 0; this.scoreElement.textContent = this.score; this.createBoard(); this.renderBoard(); }
createBoard() { for (let i = 0; i < this.boardSize; i++) { this.board[i] = []; for (let j = 0; j < this.boardSize; j++) { this.board[i][j] = this.getRandomColor(); } } }
getRandomColor() { return this.colors[Math.floor(Math.random() * this.colors.length)]; }
renderBoard() { this.gameBoard.innerHTML = ''; for (let i = 0; i < this.boardSize; i++) { for (let j = 0; j < this.boardSize; j++) { const gem = document.createElement('div'); gem.className = `gem ${this.board[i][j]}`; gem.dataset.row = i; gem.dataset.col = j; this.gameBoard.appendChild(gem); } } }
addEventListeners() { this.gameBoard.addEventListener('click', (e) => { const gem = e.target.closest('.gem'); if (!gem) return;
const row = parseInt(gem.dataset.row); const col = parseInt(gem.dataset.col);
if (this.selectedGem) { const prevRow = parseInt(this.selectedGem.dataset.row); const prevCol = parseInt(this.selectedGem.dataset.col);
if (this.isAdjacent(row, col, prevRow, prevCol)) { this.swapGems(row, col, prevRow, prevCol); if (this.checkMatches()) { this.playMatchAnimation(); setTimeout(() => { this.removeMatches(); this.updateScore(); this.fillEmptySpaces(); }, 600); // 等待动画完成 } else { this.swapGems(row, col, prevRow, prevCol); } } this.selectedGem.classList.remove('selected'); this.selectedGem = null; } else { this.selectedGem = gem; gem.classList.add('selected'); } }); }
isAdjacent(row1, col1, row2, col2) { return (Math.abs(row1 - row2) === 1 && col1 === col2) || (Math.abs(col1 - col2) === 1 && row1 === row2); }
swapGems(row1, col1, row2, col2) { const temp = this.board[row1][col1]; this.board[row1][col1] = this.board[row2][col2]; this.board[row2][col2] = temp; this.renderBoard(); }
checkMatches() { let hasMatches = false; // 检查水平匹配 for (let i = 0; i < this.boardSize; i++) { for (let j = 0; j < this.boardSize - 2; j++) { if (this.board[i][j] === this.board[i][j + 1] && this.board[i][j] === this.board[i][j + 2]) { hasMatches = true; } } }
// 检查垂直匹配 for (let i = 0; i < this.boardSize - 2; i++) { for (let j = 0; j < this.boardSize; j++) { if (this.board[i][j] === this.board[i + 1][j] && this.board[i][j] === this.board[i + 2][j]) { hasMatches = true; } } }
return hasMatches; }
playMatchAnimation() { const toRemove = new Set();
// 标记水平匹配 for (let i = 0; i < this.boardSize; i++) { for (let j = 0; j < this.boardSize - 2; j++) { if (this.board[i][j] === this.board[i][j + 1] && this.board[i][j] === this.board[i][j + 2]) { toRemove.add(`${i},${j}`); toRemove.add(`${i},${j + 1}`); toRemove.add(`${i},${j + 2}`); } } }
// 标记垂直匹配 for (let i = 0; i < this.boardSize - 2; i++) { for (let j = 0; j < this.boardSize; j++) { if (this.board[i][j] === this.board[i + 1][j] && this.board[i][j] === this.board[i + 2][j]) { toRemove.add(`${i},${j}`); toRemove.add(`${i + 1},${j}`); toRemove.add(`${i + 2},${j}`); } } }
// 为匹配的宝石添加闪烁动画 toRemove.forEach(pos => { const [row, col] = pos.split(',').map(Number); const gem = this.gameBoard.querySelector(`[data-row="${row}"][data-col="${col}"]`); if (gem) { gem.classList.add('matched'); } }); }
removeMatches() { const toRemove = new Set();
// 标记水平匹配 for (let i = 0; i < this.boardSize; i++) { for (let j = 0; j < this.boardSize - 2; j++) { if (this.board[i][j] === this.board[i][j + 1] && this.board[i][j] === this.board[i][j + 2]) { toRemove.add(`${i},${j}`); toRemove.add(`${i},${j + 1}`); toRemove.add(`${i},${j + 2}`); } } }
// 标记垂直匹配 for (let i = 0; i < this.boardSize - 2; i++) { for (let j = 0; j < this.boardSize; j++) { if (this.board[i][j] === this.board[i + 1][j] && this.board[i][j] === this.board[i + 2][j]) { toRemove.add(`${i},${j}`); toRemove.add(`${i + 1},${j}`); toRemove.add(`${i + 2},${j}`); } } }
// 移除标记的宝石 toRemove.forEach(pos => { const [row, col] = pos.split(',').map(Number); this.board[row][col] = null; }); }
fillEmptySpaces() { // 从底部开始填充空位 for (let col = 0; col < this.boardSize; col++) { let emptyRow = this.boardSize - 1; for (let row = this.boardSize - 1; row >= 0; row--) { if (this.board[row][col] === null) { // 找到上方最近的宝石 for (let above = row - 1; above >= 0; above--) { if (this.board[above][col] !== null) { this.board[row][col] = this.board[above][col]; this.board[above][col] = null; break; } } } } }
// 填充顶部的空位 for (let col = 0; col < this.boardSize; col++) { for (let row = 0; row < this.boardSize; row++) { if (this.board[row][col] === null) { this.board[row][col] = this.getRandomColor(); } } }
this.renderBoard(); }
updateScore() { this.score += 10; this.scoreElement.textContent = this.score; } }
// 初始化游戏 window.onload = () => { new GemGame(); };
|