-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.html
428 lines (408 loc) · 13.7 KB
/
code.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
<meta
name="viewport"
content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no, viewport-fit=cover"
/>
<title></title>
<link rel="stylesheet" href="/js/highlight/default.min.css" />
<script src="/js/highlight/highlight.min.js"></script>
<script
src="https://unpkg.com/[email protected]/dist/pixi.min.js"
defer
onload="pixiLoad()"
></script>
<style media="screen">
html,
body {
width: 100%;
height: 100%;
margin: 0 auto;
font-family: "Microsoft YaHei", "微软雅黑", Verdana, Arial, Helvetica;
}
* {
margin: 0;
padding: 0;
border: 0;
-webkit-overflow-scrolling: touch;
}
</style>
</head>
<body>
<div style="background-color: pink;">
<div>111</div>
<span style="font-size: 15px;">222</span>
</div>
<div style="background-color: orange;">
<div>111</div><span style="font-size: 15px;">222</span></div>
<div id="pop_star">
<div id="targetScore">目标分数:1500</div>
<div id="nowScore">当前分数:0</div>
<div id="selectScore">0块0分</div>
</div>
<div class="codecopy-btn">复制</div>
<pre>
<code>
const generateMinesPo = () => {
let r = randomNum(1, 25);
if (mapVal.value[r] != undefined) {
return generateMinesPo();
} else {
return r;
}
};
</code></pre>
<script type="text/javascript">
hljs.highlightAll();
console.log("hljs", hljs);
var table;
var squareWidth = 50; //每个小方块的宽度
var boderWidth = 10; //横竖各有多少个小方块
var squareSet = []; //存放小方块
var choose = [];
var timer;
var baseScore = 5; //基础分
var stepScore = 10; //递增分
var select_score = document.getElementById("selectScore"); //几块几分
var nowScore = document.getElementById("nowScore"); //当前分数
var totalScore = 0; //总分数
var targetScore = 1500; //目标分数
var flag = true;
var tempSquare = null; //消除过程中如果输入移入其他方块,进行记录
//创建小方块
function createSquare(value, row, col) {
var temp = document.createElement("div");
temp.style.width = squareWidth + "px";
temp.style.height = squareWidth + "px";
temp.style.background = "red";
temp.style.display = "inline-block"; //使小方块能够横向排列
temp.style.position = "absolute";
temp.style.borderRadius = "12px";
temp.style.boxSizing = "border-box";
temp.num = value; //颜色
temp.row = row;
temp.col = col;
return temp;
}
//检查周围相同颜色的小方块
function checkLinked(square, arr) {
if (square == null) {
return;
}
arr.push(square);
//向左
/*
1.不能是最左边的
2.左边得存在一个小块
3.左边的小方块和当前的小方块颜色相同
4.左边的小方块没有被收录进数组
*/
if (
square.col > 0 &&
squareSet[square.row][square.col - 1] &&
squareSet[square.row][square.col - 1].num == square.num &&
arr.indexOf(squareSet[square.row][square.col - 1]) == -1
) {
checkLinked(squareSet[square.row][square.col - 1], arr);
}
//向右
if (
square.col < boderWidth - 1 &&
squareSet[square.row][square.col + 1] &&
squareSet[square.row][square.col + 1].num == square.num &&
arr.indexOf(squareSet[square.row][square.col + 1]) == -1
) {
checkLinked(squareSet[square.row][square.col + 1], arr);
}
//向上
if (
square.row < boderWidth - 1 &&
squareSet[square.row + 1][square.col] &&
squareSet[square.row + 1][square.col].num == square.num &&
arr.indexOf(squareSet[square.row + 1][square.col]) == -1
) {
checkLinked(squareSet[square.row + 1][square.col], arr);
}
//向下
if (
square.row > 0 &&
squareSet[square.row - 1][square.col] &&
squareSet[square.row - 1][square.col].num == square.num &&
arr.indexOf(squareSet[square.row - 1][square.col]) == -1
) {
checkLinked(squareSet[square.row - 1][square.col], arr);
}
}
//方块闪烁
function flicker(arr) {
var num = 0;
timer = setInterval(function () {
for (var i = 0; i < arr.length; i++) {
arr[i].style.border = "3px solid #BFEFFF";
arr[i].style.transform =
"scale(" + (0.9 + 0.05 * Math.pow(-1, num)) + ")";
}
num++;
}, 300);
}
//计算分数
function selectScore() {
var score = 0;
for (var i = 0; i < choose.length; i++) {
score += baseScore + i * stepScore; //基础分加上递增分
}
if (score <= 0) {
return;
}
select_score.innerHTML = choose.length + "块" + score + "分";
select_score.style.transition = null; //此处必须还原
select_score.style.opacity = 1; //马上显示
setTimeout(function () {
select_score.style.transition = "opacity 1s"; //渐渐消失
select_score.style.opacity = 0;
}, 1000);
}
//鼠标移走,小方块恢复大小
function goBack() {
if (timer != null) {
clearInterval(timer);
}
for (var i = 0; i < squareSet.length; i++) {
for (var j = 0; j < squareSet[i].length; j++) {
if (squareSet[i][j] == null) {
continue;
}
squareSet[i][j].style.border = "0px solid #BFEFFF";
squareSet[i][j].style.transform = "scale(0.95)";
}
}
}
//鼠标悬停事件
function mouseOver(obj) {
//obj代表的是小方块
if (!flag) {
tempSquare = obj;
return;
}
goBack();
choose = [];
checkLinked(obj, choose);
//只有一个小方块无法被消灭
if (choose.length <= 1) {
choose = [];
return;
}
flicker(choose);
// console.log(choose);
selectScore(); //计算分数
}
//小方块渲染
function refresh() {
for (var i = 0; i < squareSet.length; i++) {
for (var j = 0; j < squareSet[i].length; j++) {
if (squareSet[i][j] == null) {
continue;
}
squareSet[i][j].row = i;
squareSet[i][j].col = j;
squareSet[i][j].style.transition = "left 0.3s, bottom 0.3s";
squareSet[i][j].style.left =
squareSet[i][j].col * squareWidth + "px";
squareSet[i][j].style.bottom =
squareSet[i][j].row * squareWidth + "px";
squareSet[i][j].style.backgroundImage =
"url('./pic/" + squareSet[i][j].num + ".png')";
squareSet[i][j].style.backgroundSize = "cover";
squareSet[i][j].style.transform = "scale(0.95)";
}
}
}
//小方块往下掉
function move() {
for (var i = 0; i < boderWidth; i++) {
//列
var pointer = 0;
for (var j = 0; j < boderWidth; j++) {
//行
if (squareSet[j][i] != null) {
if (j != pointer) {
squareSet[pointer][i] = squareSet[j][i];
squareSet[j][i].row = pointer;
squareSet[j][i] = null;
}
pointer++;
}
}
}
//横向移动
for (var i = 0; i < squareSet[0].length; i++) {
if (squareSet[0][i] == null) {
for (var j = 0; j < boderWidth; j++) {
squareSet[j].splice(i, 1);
}
continue;
}
}
refresh();
}
//判断是否结束
function isFinish() {
for (var i = 0; i < squareSet.length; i++) {
for (var j = 0; j < squareSet[i].length; j++) {
var temp = [];
checkLinked(squareSet[i][j], temp);
if (temp.length > 1) {
return false;
}
}
}
return true;
}
//初始化
function init() {
table = document.getElementById("pop_star");
for (var i = 0; i < boderWidth; i++) {
squareSet[i] = new Array();
for (var j = 0; j < boderWidth; j++) {
var square = createSquare(Math.floor(Math.random() * 5), i, j); //创建小方块
square.onmousemove = function () {
//鼠标悬停事件,鼠标放在某个小方块上时触发
mouseOver(this);
};
square.onclick = function () {
//鼠标点击事件
if (!flag || choose.length == 0) {
return;
}
flag = false;
tempSquare = null;
//加分数
var score = 0;
for (var i = 0; i < choose.length; i++) {
score += baseScore + i * stepScore;
}
totalScore += score;
nowScore.innerHTML = "当前分数:" + totalScore;
//消灭星星
for (var i = 0; i < choose.length; i++) {
(function (i) {
setTimeout(function () {
squareSet[choose[i].row][choose[i].col] = null;
table.removeChild(choose[i]);
}, i * 100);
})(i);
}
//移动
setTimeout(function () {
move();
//判断是否结束
setTimeout(function () {
var is = isFinish();
if (is) {
if (totalScore >= targetScore) {
alert("恭喜获胜");
} else {
alert("游戏失败");
}
} else {
choose = [];
flag = true;
mouseOver(tempSquare);
}
}, 300 + choose.length * 150);
}, choose.length * 100);
};
squareSet[i][j] = square;
table.appendChild(square);
}
}
refresh();
}
window.onload = function () {
init();
};
</script>
<script>
// function pixiLoad () {
// 引入 pixi.js 库
const app = PIXI.Application;
const stage = app.stage;
const resources = PIXI.loader.resources;
const loader = PIXI.loader;
// 加载图片资源
loader.add('宝石', '宝石.png');
loader.add('背景', '背景.png');
loader.load((loader, resources) => {
// 创建背景精灵
const backgroundSprite = new PIXI.Sprite(resources['背景'].texture);
backgroundSprite.width = app.screen.width;
backgroundSprite.height = app.screen.height;
stage.addChild(backgroundSprite);
// 创建宝石精灵
const gemTexture = resources['宝石'].texture;
const gemSprites = [];
for (let i = 0; i < 10; i++) {
const gemSprite = new PIXI.Sprite(gemTexture);
gemSprite.x = 100 + i * 60;
gemSprite.y = 100;
gemSprites.push(gemSprite);
stage.addChild(gemSprite);
}
// 监听鼠标点击事件
stage.interactive = true;
stage.on('pointerdown', (event) => {
// 获取点击位置的精灵
const clickedSprite = getSpriteAtPosition(event.data.global.x, event.data.global.y);
if (clickedSprite) {
// 检查点击的精灵是否与相邻的精灵匹配
const matchedSprites = findMatchedSprites(clickedSprite, gemSprites);
if (matchedSprites.length >= 3) {
// 消除匹配的精灵
for (const sprite of matchedSprites) {
stage.removeChild(sprite);
}
// 增加分数
score += 10;
}
}
});
// 显示分数
const scoreText = new PIXI.Text('分数: ' + score, {
fontFamily: 'Arial',
fontSize: 30,
fill: 0xFFFFFF
});
scoreText.x = 10;
scoreText.y = 10;
stage.addChild(scoreText);
});
// 获取指定位置上的精灵
function getSpriteAtPosition(x, y) {
for (const sprite of gemSprites) {
if (sprite.contains(x, y)) {
return sprite;
}
}
return null;
}
// 查找与指定精灵匹配的相邻精灵
function findMatchedSprites(sprite, sprites) {
const matchedSprites = [];
for (const otherSprite of sprites) {
if (otherSprite!== sprite && areSpritesMatched(sprite, otherSprite)) {
matchedSprites.push(otherSprite);
}
}
return matchedSprites;
}
// 判断两个精灵是否匹配
function areSpritesMatched(sprite1, sprite2) {
return sprite1.texture === sprite2.texture;
}
</script>
</body>
</html>