-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path小恐龙.html
273 lines (248 loc) · 6.44 KB
/
小恐龙.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
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
alert('电脑游戏,空格跳')
</script>
<style>
body {
/* padding: 20px; */
padding: 0;
margin: 0;
background-color: white;
display: flex;
justify-content: center;
align-items: center;
}
.game {
width: 600px;
height: 300px;
border: 1px solid black;
position: relative;
}
.sha {
background: url(img/sha.jpg);
background-size: 100% 100%;
background-position: 0px;
}
.dino {
width: 80px;
height: 100px;
background-color: green;
background: url(img/long.webp);
background-size: 100% 100%;
position: absolute;
left: 30px;
bottom: 0px;
}
.jumpClass {
animation: jump 0.7s linear;
}
@keyframes jump {
0% {
bottom: 0px;
}
20% {
bottom: 60px;
}
35% {
bottom: 120px;
}
65% {
bottom: 120px;
}
80% {
bottom: 60px;
}
100% {
bottom: 0px;
}
}
.block {
background-color: blue;
width: 40px;
height: 60px;
position: absolute;
bottom: 0px;
left: 580px;
background: url(img/xian.webp);
background-size: 100% 100%;
}
.active {
animation: block 1.8s linear infinite;
}
.qiu {
width: 80px;
height: 80px;
background: url(img/qiu.webp);
background-size: 100% 100%;
}
@keyframes block {
0% {
left: 580px;
}
100% {
left: -20px;
}
}
.t {
width: 60px;
height: 60px;
background-color: #ffc0c0;
font-size: 20px;
line-height: 60px;
text-align: center;
}
.c {
width: 60px;
height: 60px;
background-color: #ffc0c0;
font-size: 40px;
line-height: 60px;
text-align: center;
}
</style>
</head>
<body>
<audio src="background_music.mp3" loop autoplay id="audio"></audio>
<div class="game sha">
<div class="t">开始</div>
<div class="dino"></div>
<div class="block"></div>
</div>
</body>
<script>
const audio = document.querySelector('audio')
const game = document.querySelector('.game')
const dino = document.querySelector('.dino');
const block = document.querySelector('.block');
const t = document.querySelector('.t');
let time = 0
let x = 30
let d = 0
let tt = 0
let timel = null
let timed = null
let timex = null
let timej = null
// 跳
window.addEventListener('keydown', event => {
console.log(event);
if (event.code == 'Space') {
console.log('按下空格');
dino.classList.add('jumpClass');
setTimeout(() => {
dino.classList.remove('jumpClass');
}, 700);
}
});
var color = ['#f00', 'rgb(210, 210, 0)', 'blue', 'gold', 'orange', 'gray', 'pink', 'maroon', 'green', 'rgb(42, 75, 165)', '#009393', '#24f024', '#b7b7ff', 'purple'];
setInterval(function () {
var math = Math.floor(Math.random() * color.length);
// var math = Math.floor(Math.random() * color.length +1)-1;
t.style.color = color[math];
}, 1000)
t.addEventListener('click', function () {
if (t.innerHTML == '开始') {
audio.pause()
// 倒计时
t.classList.add('c')
t.innerHTML = '3'
setTimeout(function () {
t.innerHTML = '2'
}, 1000);
setTimeout(function () {
t.innerHTML = '1'
}, 2000);
setTimeout(function () {
t.innerHTML = 'go'
audio.volume = 0.7
audio.currentTime = 0
}, 3000);
// 游戏结束判断
setTimeout(function () {
audio.play()
t.style.display = 'none'
block.classList.add('active')
block.style.animationPlayState = ''
timel = setInterval(function () {
x++
dino.style.left = x + "px"
if (x >= 400) {
end()
alert('游戏胜利!!!')
}
}, 100);
timed = setInterval(function () {
d++
game.style.backgroundPosition = -d + 'px'
}, 30);
timex = setInterval(function () {
if (time == 0) {
let timex = setInterval(() => {
let dinoBottom = parseFloat(
getComputedStyle(dino).getPropertyValue('bottom')
);
let blockLeft = parseFloat(
getComputedStyle(block).getPropertyValue('left')
);
if (blockLeft < 58 + x && blockLeft > x - 30 && dinoBottom <= 55) {
alert('游戏结束');
end()
}
}, 10);
} else if (time == 1) {
setInterval(() => {
let dinoBottom = parseFloat(
getComputedStyle(dino).getPropertyValue('bottom')
);
let blockLeft = parseFloat(
getComputedStyle(block).getPropertyValue('left')
);
if (blockLeft < 60 + x && blockLeft > x - 30 && dinoBottom <= 56) {
alert('游戏结束');
end()
}
}, 10);
}
}, 10);
}, 3200);
// 换背景
timej = setTimeout(function () {
game.style.background = "url('img/xue1.jpg')";
// block.classList.remove("sha");
block.classList.add("qiu");
time = 1
return (time)
}, 19400);
}
// 结束函数
let end = function () {
t.style.display = 'block';
t.classList.remove('c')
t.innerHTML = '开始'
audio.currentTime = 0
audio.pause()
clearTimeout(timej);
clearInterval(timel);
clearInterval(timed);
clearInterval(timex);
timel = null
timed = null
timex = null
timej = null
x = 30
d = 0
dino.style.left = x + "px"
game.style.backgroundPosition = 0 + 'px'
block.classList.remove('active')
block.style.left = 580 + 'px'
block.classList.remove("qiu");
game.style.background = "url('img/sha.jpg')";
}
})
</script>
</html>