项目为静态网页,在本地即可运行

源码共三个文件:index.html、game.js、style.css

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>宝石连连看</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="game-container">
<div class="game-board" id="gameBoard"></div>
<div class="score-container">
<span>得分:</span>
<span id="score">0</span>
</div>
<button id="resetButton" class="reset-button">重新开始</button>
</div>
<script src="game.js"></script>
</body>
</html>

game.js

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();
};

style.css

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
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #2c3e50;
font-family: Arial, sans-serif;
}

.game-container {
text-align: center;
}

.game-board {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 5px;
background: #34495e;
padding: 10px;
border-radius: 10px;
margin-bottom: 20px;
}

.gem {
width: 50px;
height: 50px;
border-radius: 50%;
cursor: pointer;
position: relative;
transition: transform 0.2s;
box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

.gem::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-radius: 50%;
background: linear-gradient(135deg, rgba(255,255,255,0.3) 0%, rgba(255,255,255,0) 50%);
}

.gem.selected {
transform: scale(1.1);
box-shadow: 0 0 15px rgba(255, 255, 255, 0.8);
}

.gem.matched {
animation: blink 0.3s ease-in-out 2;
}

@keyframes blink {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.2); opacity: 0.5; }
100% { transform: scale(1); opacity: 1; }
}

.gem.red { background: #e74c3c; }
.gem.blue { background: #3498db; }
.gem.green { background: #2ecc71; }
.gem.yellow { background: #f1c40f; }
.gem.purple { background: #9b59b6; }
.gem.orange { background: #e67e22; }

.score-container {
color: white;
font-size: 24px;
margin-top: 20px;
margin-bottom: 20px;
}

.reset-button {
padding: 10px 20px;
font-size: 18px;
background: #3498db;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}

.reset-button:hover {
background: #2980b9;
}

@media (max-width: 600px) {
.gem {
width: 40px;
height: 40px;
}
}