-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.html
More file actions
232 lines (221 loc) · 12.3 KB
/
Copy pathgame.html
File metadata and controls
232 lines (221 loc) · 12.3 KB
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
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>开幕游戏</title>
<style>
body, html { margin: 0; padding: 0; width: 100vw; height: 100vh; overflow: hidden; font-family: '微软雅黑', Arial, sans-serif; }
#container { width: 100vw; height: 100vh; position: relative; }
.background { position: absolute; width: 100vw; height: 100vh; object-fit: cover; z-index: 0; }
.center { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 2; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.btn { padding: 16px 32px; font-size: 1.5em; border: none; border-radius: 8px; background: #0078d7; color: #fff; cursor: pointer; margin: 16px; transition: background 0.2s; }
.btn:hover { background: #005fa3; }
.options { display: flex; justify-content: center; gap: 48px; }
.text { font-size: 2em; color: #fff; text-shadow: 2px 2px 8px #000; margin-bottom: 32px; text-align: center; }
.game-title { font-size: 4em; line-height: 1.05; display: inline-block; }
.fade-in { animation: fadeIn .6s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
#soundBtn { position: absolute; top: 12px; right: 12px; z-index: 10; background: rgba(0,0,0,.55); color: #fff; border: 1px solid rgba(255,255,255,.4); padding: 8px 14px; border-radius: 20px; font-size: 14px; cursor: pointer; backdrop-filter: blur(4px); transition: background .2s; }
#soundBtn:hover { background: rgba(0,0,0,.75); }
.video-progress { position:absolute; right:16px; bottom:16px; z-index:11; width:25vw; max-width:25vw; min-width:120px; height:8px; background:rgba(255,255,255,.25); border-radius:4px; overflow:hidden; backdrop-filter:blur(4px); box-shadow:0 2px 6px rgba(0,0,0,.4); }
.video-progress .bar { height:100%; width:0%; background:linear-gradient(90deg,#00c6ff,#0078d7); transition:width .15s linear; }
.skip-btn { position:absolute; left:16px; bottom:12px; z-index:11; background:rgba(0,0,0,.55); color:#fff; border:1px solid rgba(255,255,255,.35); padding:8px 18px; font-size:14px; border-radius:22px; cursor:pointer; backdrop-filter:blur(4px); letter-spacing:1px; transition:background .2s, transform .15s; }
.skip-btn:hover { background:rgba(0,0,0,.75); }
.skip-btn:active { transform:scale(.95); }
#pauseBtn { position:absolute; top:12px; left:12px; z-index:12; background:rgba(0,0,0,.55); color:#fff; border:1px solid rgba(255,255,255,.4); padding:8px 16px; border-radius:20px; font-size:14px; cursor:pointer; backdrop-filter:blur(4px); transition:background .2s; }
#pauseBtn:hover { background:rgba(0,0,0,.75); }
.pause-overlay { position:absolute; inset:0; z-index:11; background:rgba(0,0,0,.35); display:flex; align-items:center; justify-content:center; pointer-events:auto; }
.pause-overlay span { font-size:3em; color:#fff; text-shadow:0 2px 8px #000; }
</style>
</head>
<body>
<div id="container"></div>
<button id="pauseBtn" style="display:none">⏸ 暂停</button>
<button id="soundBtn" style="display:none">🔊 声音已开启(点击关闭)</button>
<script>
const container = document.getElementById('container');
const soundBtn = document.getElementById('soundBtn');
let choices = [];
let audioUnlocked = true; // 用户期望是否有声音
let autoplayBlocked = false; // 浏览器是否阻止带声音自动播放
let paused = false; // 是否处于暂停
let pendingCallback = null; // 暂停时存储的下一个步骤回调
let currentVideo = null; // 当前 video 引用
const preloadList = [ 'background1.jpg','background2.jpg','background3.jpg','background4.jpg', 'video1.mp4','video2.mp4','video3.mp4','video4.mp4','video5.mp4','video6.mp4','video7.mp4','video8.mp4' ];
function preloadAssets(list) {
list.forEach(src => {
if (/\.(jpe?g|png)$/i.test(src)) { const img = new Image(); img.src = src; }
else if (/\.mp4$/i.test(src)) { const v = document.createElement('video'); v.src = src; v.preload = 'auto'; v.muted = true; }
});
}
function playVideo(src, callback) {
container.innerHTML = `<video class='background fade-in' src='${src}' autoplay playsinline></video><div class='video-progress'><div class='bar'></div></div><button class='skip-btn'>跳过动画</button>`;
const video = container.querySelector('video');
const progressWrap = container.querySelector('.video-progress');
const progressBar = progressWrap.querySelector('.bar');
const skipBtn = container.querySelector('.skip-btn');
currentVideo = video;
video.muted = !audioUnlocked || autoplayBlocked;
if (!video.muted) video.volume = 1;
const p = video.play();
if (p && p.catch) {
p.catch(() => {
if (audioUnlocked && !autoplayBlocked) {
autoplayBlocked = true;
video.muted = true;
const retry = video.play(); if (retry && retry.catch) retry.catch(()=>{});
updateSoundBtn();
}
});
}
const endHandler = () => {
video.onended = null;
if (progressWrap) progressWrap.remove();
if (skipBtn) skipBtn.disabled = true;
if (paused) { // 若暂停则挂起回调
pendingCallback = callback;
} else {
callback && callback();
}
};
video.onended = endHandler;
function updateProgress(){
if (!video.duration || isNaN(video.duration)) return;
const ratio = Math.min(1, Math.max(0, video.currentTime / video.duration));
if (progressBar) progressBar.style.width = (ratio * 100).toFixed(2) + '%';
}
video.addEventListener('timeupdate', updateProgress);
video.addEventListener('loadedmetadata', updateProgress);
video.addEventListener('progress', updateProgress);
if (skipBtn) {
skipBtn.onclick = () => {
if (video.readyState > 0) {
try { video.currentTime = video.duration; } catch(e) {}
}
endHandler();
};
}
if (paused) { // 若刚好处于暂停状态,立刻暂停视频
try { video.pause(); } catch(e) {}
}
}
function showBackground(src, contentHtml = '') { container.innerHTML = `<img class='background fade-in' src='${src}' />${contentHtml}`; }
document.addEventListener('keydown', e => {
const options = container.querySelector('.options'); if (!options) return;
if (e.key === 'ArrowLeft') { const first = options.querySelector('.btn'); first && first.click(); }
else if (e.key === 'ArrowRight') { const btns = options.querySelectorAll('.btn'); if (btns.length) btns[btns.length - 1].click(); }
});
preloadAssets(preloadList);
playVideo('opening.mp4', () => {
showBackground('background1.jpg', `
<div class='center fade-in'>
<div class='text'>
<span>欢迎来到</span><br>
<span class='game-title'>开幕游戏</span>
</div>
<button class='btn' onclick='startGame()'>开始游戏</button>
</div>
`);
});
function updateSoundBtn() {
if (audioUnlocked && !autoplayBlocked) soundBtn.textContent = '🔊 声音已开启(点击关闭)';
else if (audioUnlocked && autoplayBlocked) soundBtn.textContent = '🔇 自动播放被阻止(点击开启)';
else soundBtn.textContent = '🔇 声音已关闭(点击开启)';
}
function toggleSound() {
const v = container.querySelector('video');
if (audioUnlocked && !autoplayBlocked) {
audioUnlocked = false; if (v) v.muted = true;
} else {
audioUnlocked = true; autoplayBlocked = false; if (v) { v.muted = false; v.volume = 1; const r = v.play(); if (r && r.catch) r.catch(()=>{}); }
}
updateSoundBtn();
}
soundBtn.style.display = 'block';
soundBtn.onclick = toggleSound; updateSoundBtn();
// 暂停/恢复逻辑
const pauseBtn = document.getElementById('pauseBtn');
pauseBtn.style.display = 'block';
pauseBtn.onclick = () => {
if (!paused) {
paused = true;
pauseBtn.textContent = '▶ 继续';
// 暂停当前视频
if (currentVideo) { try { currentVideo.pause(); } catch(e) {} }
// 添加遮罩
if (!document.querySelector('.pause-overlay')) {
const overlay = document.createElement('div');
overlay.className = 'pause-overlay';
overlay.innerHTML = '<span>已暂停</span>';
container.appendChild(overlay);
}
} else {
paused = false;
pauseBtn.textContent = '⏸ 暂停';
// 移除遮罩
const ov = document.querySelector('.pause-overlay');
if (ov) ov.remove();
// 恢复视频
if (currentVideo && currentVideo.paused && !currentVideo.ended) {
const r = currentVideo.play(); if (r && r.catch) r.catch(()=>{});
}
// 若之前挂起了回调且视频已经结束
if (pendingCallback) {
const cb = pendingCallback; pendingCallback = null; cb();
}
}
};
window.startGame = function() {
playVideo('video1.mp4', () => {
playVideo('video2.mp4', () => {
showBackground('background2.jpg', `
<div class='center fade-in'>
<div class='text'>请选择你的道路</div>
<div class='options'>
<button class='btn' onclick='chooseRoad(1)'>road1</button>
<button class='btn' onclick='chooseRoad(2)'>road2</button>
<button class='btn' onclick='chooseRoad(3)'>road3</button>
</div>
</div>
`);
});
});
}
window.chooseRoad = function(road) {
playVideo('video3.mp4', () => {
playVideo('video4.mp4', () => {
playVideo('video5.mp4', () => {
playVideo('video6.mp4', () => {
showBackground('background3.jpg', `
<div class='center fade-in'>
<div class='text'>是</div>
<div class='options'>
<button class='btn' onclick='chooseSide(0)'>左</button>
<button class='btn' onclick='chooseSide(1)'>右</button>
</div>
</div>
`);
});
});
});
});
}
window.chooseSide = function(side) {
choices.push(side);
if (choices.length === 1) {
showBackground('background4.jpg', `
<div class='center fade-in'>
<div class='options'>
<button class='btn' onclick='chooseSide(0)'>左</button>
<button class='btn' onclick='chooseSide(1)'>右</button>
</div>
</div>
`);
} else if (choices.length === 2) {
if (choices[0] === choices[1]) playVideo('video8.mp4');
else playVideo('video7.mp4');
}
}
</script>
</body>
</html>