原生JS实现点击数字小游戏,供大家参考,具体内容如下
最近公司在季度测试中出了一道很有趣的测试题,要求使用我们自己的黑科技–IVX来实现,感兴趣的朋友可以去了解哦,是真的黑科技,在这里我还是用原生JS来实现吧,题目是这样的:
实现一个点击数字的小游戏:依次点击容器中随机生成的数字元素,生成的数字元素会在5S后消失,你将凭借记忆点击按照数字升序依次点击生成的数字方可通过该关卡游戏。
话不多说直接看运行效果图:
上代码:
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
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" /> < meta http-equiv = "X-UA-Compatible" content = "IE=edge" /> < meta name = "viewport" content = "width=device-width, initial-scale=1.0" /> < title >点击数字小游戏</ title > < style > #cointainer { margin: auto; height: 600px; width: 400px; background-color: rgb(37, 37, 37); position: relative; } .header { width: auto; text-align: center; margin: auto; } .parm { height: 60px; width: 60px; border-radius: 30px; position: absolute; text-align: center; line-height: 60px; } .parm:hover { cursor: pointer; } .todo { text-align: center; margin-top: 16px; } button { width: 100px; height: 30px; background-color: coral; border: none; outline: none; } </ style > </ head > < body > < div class = "header" > < h1 >点击数字小游戏</ h1 > < p > 5s后数字内容会消失,凭借你的记忆按照数字升序依次点击数字点可顺利通关 </ p > </ div > < div id = "cointainer" ></ div > < div class = "todo" > < button onclick = "restart(6)" >重新开始</ button > < button style = "margin-left: 20px" onclick = "nextPass()" >下一关</ button > < button style = "margin-left: 20px" onclick = "window.clearInterval(timmer2);window.clearTimeout(timmer1)" > 停止计时 </ button > < p >时间</ p > </ div > </ body > < script > let circleList = []; //circle构造器 function getPosition() { let parm = { x: "", y: "" }; parm.x = Math.round(Math.random() * 340); parm.y = Math.round(Math.random() * 540); return parm; } //创建不重叠circle function createCircle(total) { if (circleList.length === 0) { circleList.push(getPosition()); } //限制创建次数200 for (let i = 0; i < 200 ; i++) { if (circleList.length < total) { let circle = getPosition (); let distan = []; for (let n = 0 ; n < circleList.length; n++) { let dis = Math .abs(circle.x - circleList[n].x) ** 2 + Math.abs(circle.y - circleList[n].y) ** 2; distan.push(dis); } if (Math.min(...distan) > 3600) { circleList.push(circle); } } else { break; } } } //创建8个circle createCircle(8); //随机颜色选择器 function selectColor() { let r = 100 + Math.round(Math.random() * 155); let g = 100 + Math.round(Math.random() * 155); let b = 100 + Math.round(Math.random() * 155); return `rgb(${r},${g},${b})`; } //在DOM中创建circle let containner = document.getElementById("cointainer"); //构造关卡 function creatGame(num) { circleList = []; createCircle(num); for (let i = 0; i < circleList.length ; i++) { let node = document .createElement("span"); containner.appendChild(node); node.className = "parm" ; node.innerText = i + 1; node.style.left = circleList [i].x + "px"; node.style.top = circleList [i].y + "px"; node.style.backgroundColor = selectColor (); } } //点击答案 let asw = []; //设置5s后开始游戏 let start = function () { let list = document .querySelectorAll("span"); let right = "" ; for (let i = 0 ; i < list.length; i++) { list[i] .innerText = "" ; list[i] .number = i + 1; right = right + (i + 1); list[i].addEventListener( "click", function () { asw.push(list[i].number); if (asw.length === pass && asw.join("") === right) { window.clearInterval(timmer2); alert("恭喜过关,你的用时为:" + time.toFixed(2) + "s"); asw = []; } else if (asw.length === pass && asw.join("") !== right) { asw = []; window.clearInterval(timmer2); alert("抱歉没能过关"); } }, false ); } }; let time = 0 ; let sumTime = function () { time = time + 0.01; document.querySelectorAll("p")[1] .innerText = time .toFixed(2) + "s"; }; //初始关卡 let pass = 6 ; creatGame(pass); let timmer1 = setTimeout (start, 5000); let timmer2 = setInterval (sumTime, 10); //重新开始 function restart(nowerPass) { while (containner.hasChildNodes()) { containner.removeChild(containner.firstChild); } pass = nowerPass ; creatGame(nowerPass); clearTimeout(timmer1); clearInterval(timmer2); time = 0 ; timmer1 = setTimeout (start, 5000); timmer2 = setInterval (sumTime, 10); } //下一关 function nextPass() { if (pass < 20) { pass++; restart(pass); } } </script> </ html > |
至此一个很有趣的锻炼大脑逻辑的小游戏分享完毕。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_44170536/article/details/115960438