-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
486 lines (436 loc) · 11.5 KB
/
main.ts
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import './style.css'
import anime from 'animejs'
import Swal from 'sweetalert2'
import generateMinesweeperMap from './genMap'
import { Leafer, Image, Box, PointerEvent } from 'leafer-ui'
import '@leafer-in/state'
import defaultSkin from './skin/default/bg.jpg'
import openSound from './icon/open_sound.svg'
import closeSound from './icon/close_sound.svg'
import boomAudio from './audio/boom.mp3'
import openAudio from './audio/open.mp3'
import markAudio from './audio/mark.mp3'
const soundBtn = document.getElementById('sound_btn') as HTMLButtonElement
const soundBtnImage = document.getElementById('sound_btn_image') as HTMLImageElement
let turnOnSound = parseInt(localStorage.getItem('turnOnSound') || '0')
soundBtnImage.src = turnOnSound ? openSound : closeSound
async function playAudio(type: string) {
if (!turnOnSound) return
let audio = new Audio()
audio.volume = 0.05
audio.src = type
audio.play()
// 只播放2s
await sleep(2000)
audio.pause()
// 释放
audio.remove()
}
soundBtn.onclick = toggleSound
function toggleSound() {
turnOnSound = turnOnSound === 1 ? 0 : 1
localStorage.setItem('turnOnSound', turnOnSound.toString())
soundBtnImage.src = turnOnSound ? openSound : closeSound
}
function sleep(time: number) {
return new Promise<void>(resolve => {
setTimeout(() => {
resolve()
}, time)
})
}
let leafer = new Leafer({
view: 'app', // 支持 window 、div、canvas 标签对象, 可使用id字符串(不用加 # 号)
width: 800, // 不能设置为 0, 否则会变成自动布局
height: 800,
type: "draw"
})
// 全局鼠标状态
let globalPointButtons = -1
let gameOver = false
let grid_count = 20 // 网格数量
let landmine_count = 40 // 地雷数量 不能大于 grid_count * grid_count
// 网格数据
let gridDataMap: number[][];
// 当前鼠标停留的格子
let currentMoveSquare: SquareGrid | null = null
// 所有格子
let squareGridList: SquareGrid[][] = []
// 所有地雷
let landmineList: SquareGrid[] = []
// 空白格子
let blankGridList: SquareGrid[] = []
const grid_count_input = document.getElementById('grid_count') as HTMLInputElement
const grid_count_value = document.getElementById('grid_count_value') as HTMLSpanElement
const landmine_count_input = document.getElementById('landmine_count') as HTMLInputElement
const landmine_count_value = document.getElementById('landmine_count_value') as HTMLSpanElement
// 初始化值
grid_count_input.value = grid_count.toString()
grid_count_value.innerText = grid_count.toString()
landmine_count_input.value = landmine_count.toString()
landmine_count_value.innerText = landmine_count.toString()
grid_count_input.oninput = function () {
grid_count = parseInt(grid_count_input.value)
grid_count_value.innerText = grid_count.toString()
// 限制雷的数量不能大于网格数量
if (landmine_count > grid_count * grid_count) {
landmine_count = grid_count * grid_count
}
// 设置max
landmine_count_input.setAttribute('max', landmine_count.toString())
}
landmine_count_input.oninput = function () {
landmine_count = parseInt(landmine_count_input.value)
landmine_count_value.innerText = landmine_count.toString()
}
// start_game
const start_game = document.getElementById('start_game') as HTMLButtonElement
start_game.onclick = function () {
renderGrid()
}
// 渲染初始网格
function renderGrid() {
// 重置
gridDataMap = []
currentMoveSquare = null
globalPointButtons = -1
gameOver = false
landmineList = []
blankGridList = []
// 销毁网格
squareGridList.forEach(item => {
item.forEach(item => {
item?.destroy()
})
})
squareGridList = []
// 初始化数据
let w = leafer.width!
let h = leafer.height!
let grid_width = w / grid_count
let grid_height = h / grid_count
gridDataMap = generateMinesweeperMap(grid_count, landmine_count)
// 生成 gridDataMap 数据 二维数组
for (let i = 0; i < grid_count; i++) {
for (let j = 0; j < grid_count; j++) {
// 0 标识正常 1标识地雷 随机生成地雷位置 但是不能超过地雷数量
let isLandmine = gridDataMap[i][j] === -1
let item = new SquareGrid(grid_width, grid_height, i, j, isLandmine, gridDataMap[i][j])
leafer.add(item.box)
squareGridList[i] = squareGridList[i] || []
squareGridList[i][j] = item
if (isLandmine) {
landmineList.push(item)
} else {
blankGridList.push(item)
}
}
}
}
class SquareGrid {
// 尺寸
width: number
height: number
// 盒子
box: Box
image: Image
// 位置
x: number
y: number
key: string
// 数字
tipNum: number
// 地雷
lei: boolean
// 左右键的按下状态
// -1 未按下
// 1 左键按下
// 2 右键按下
// 3 左右键同时按下
pointButtons: number
// 状态
// 0 空白
// 1 数字
// 2 数字
// 3 数字
// 4 数字
// 5 数字
// 6 数字
// 7 数字
// 8 数字
// 9 默认状态
// 10 插旗
// 11 问号
// 12 地雷
// 13 爆炸
status: number
constructor(width: number, height: number, x_num: number, y_num: number, lei = false, tipNum: number) {
this.key = `${x_num}_${y_num}`
this.width = width
this.height = height
this.x = x_num
this.y = y_num
this.lei = lei
this.tipNum = tipNum
this.status = 9
// 左右键的按下状态
this.pointButtons = -1
// 盒子
this.box = new Box({
x: x_num * width,
y: y_num * height,
opacity: 1,
width: width,
height: height,
overflow: 'hide'
})
// 2*7的雪碧图
this.image = new Image({
url: defaultSkin,
width: width * 7,
height: height * 2,
x: 0,
y: 0,
})
// 默认在9 初始位置
this.setImage()
this.box.add(this.image)
let self = this
// 鼠标进入
this.box.on(PointerEvent.OVER, function () {
if (gameOver) return
currentMoveSquare = self
self.box.set({ opacity: 0.9 })
if (globalPointButtons === 3) {
self.detection()
}
})
// 鼠标离开
this.box.on(PointerEvent.OUT, function () {
if (gameOver) return
currentMoveSquare = null
self.box.set({ opacity: 1 })
// 移出
self.unDetection() // 取消探测
})
const mouseup = () => {
if (gameOver) return
self.unDetection() // 取消探测
// 左键点击打开方块
if (self.pointButtons === 1) {
self.openItem()
}
else if (self.pointButtons === 2) {
self.markItem()
}
}
// 鼠标松开
this.box.on(PointerEvent.UP, mouseup)
}
flash() {
this.box.opacity = 0.5
anime({
targets: this.box,
opacity: 1,
duration: 20,
easing: 'linear'
})
}
/**
* 打开格子
*/
async openItem() {
if (gameOver) return
// 如果当前格子是雷 则游戏结束
if (this.lei) {
gameOver = true
this.boom()
// 动态爆炸每一颗地雷
await sleep(20)
for (let i = 0; i < grid_count; i++) {
for (let item of landmineList) {
if (item.key !== this.key && item.y === i) {
item.boom()
item.box.opacity = 0.5
}
}
await sleep(20)
}
// 所有爆炸执行完毕后 提示游戏结束
executeGameOver('很遗憾,你输了')
} else {
this.status = this.tipNum
this.setImage()
playAudio(openAudio)
this.flash()
sleep(100).then(() => {
isGameOver()
})
// 如果当前格子是0 则打开周围的8个格子
if (this.tipNum === 0) {
let surArr = this.getSur()
setTimeout(() => {
surArr.forEach(item => {
// 存在并且是未打开的格子
if (item && item.status === 9) {
item.openItem()
}
})
}, 20)
}
}
}
/**
* 爆炸
*/
boom() {
this.status = 13
this.setImage()
playAudio(boomAudio)
}
/**
* 标记格子
*/
markItem() {
if (this.status === 9) {
this.status = 10
} else if (this.status === 10) {
this.status = 11
} else if (this.status === 11) {
this.status = 9
} else {
return
}
playAudio(markAudio)
this.setImage()
}
/**
* 设置图片
* @param num 传入值临时改变状态 通过设置status来永久改变状态
*/
setImage(num?: number) {
let _num = num ?? this.status
let x = 0 - (_num % 7) * this.width
let y = 0 - (_num > 6 ? 1 : 0) * this.height
this.image.set({
x: x,
y: y
})
}
getSur(includeSelf = false) {
const getItem = (x: number, y: number) => {
if (squareGridList[x] && squareGridList[x][y]) {
return squareGridList[x][y]
} else {
return null
}
}
let arr = []
// 第一行
arr[0] = getItem(this.x - 1, this.y - 1)
arr[1] = getItem(this.x, this.y - 1)
arr[2] = getItem(this.x + 1, this.y - 1)
// 第二行
arr[3] = getItem(this.x - 1, this.y)
arr[4] = includeSelf ? getItem(this.x, this.y) : null // 自己
arr[5] = getItem(this.x + 1, this.y)
// 第三行
arr[6] = getItem(this.x - 1, this.y + 1)
arr[7] = getItem(this.x, this.y + 1)
arr[8] = getItem(this.x + 1, this.y + 1)
return arr
}
/**
* 探测周围八个格子
*/
detection() {
// 探测周围八个格子
let itemArr = this.getSur()
itemArr.forEach(item => {
if (item && item.status === 9) {
item.setImage(0)
}
})
}
/**
* 取消探测
*/
unDetection() {
// 探测周围八个格子
let itemArr = this.getSur()
itemArr.forEach(item => {
item?.setImage()
})
}
/**
* 销毁
*/
destroy() {
this.box.off(PointerEvent.OVER)
this.box.off(PointerEvent.OUT)
this.box.off(PointerEvent.UP)
this.image.destroy()
this.box.destroy()
}
}
/**
* 判断游戏是否结束
*/
function isGameOver() {
// 已经结束不判断
if (gameOver) return
// 游戏结束条件
// 1. 所有地雷都标记
// 1.1 插旗的数量
let flagCount = landmineList.filter(item => item.status === 10).length
// 1.2 判断插旗的数量等于地雷的数量
if (flagCount === landmine_count) {
// 2. 所有非地雷的格子都打开 状态都小于9
let openCount = blankGridList.filter(item => item.status < 9).length
if (openCount === grid_count * grid_count - landmine_count) {
executeGameOver()
return
}
}
}
/**
* 执行游戏结束
*/
function executeGameOver(msg?: string) {
gameOver = true
if (msg) {
Swal.fire({
icon: "error",
title: "很遗憾, 你输了",
});
} else {
Swal.fire({
icon: "success",
title: "恭喜你,你赢了",
});
}
}
document.addEventListener('mousedown', function (e) {
if (gameOver) return
if (currentMoveSquare) {
if (e.buttons === 1) {
// console.log('左键按下', currentMoveSquare.x, currentMoveSquare.y, currentMoveSquare.lei)
globalPointButtons = 1
currentMoveSquare.pointButtons = 1
} else if (e.buttons === 2) {
// console.log('右键按下', currentMoveSquare.x, currentMoveSquare.y, currentMoveSquare.lei)
globalPointButtons = 2
currentMoveSquare.pointButtons = 2
} else if (e.buttons === 3) {
// console.log('同时按下左右键', currentMoveSquare.x, currentMoveSquare.y, currentMoveSquare.lei)
globalPointButtons = 3
currentMoveSquare.pointButtons = 3
currentMoveSquare.detection()
}
}
})
document.addEventListener('mouseup', function () {
if (gameOver) return
globalPointButtons = -1
})
renderGrid()