-
Notifications
You must be signed in to change notification settings - Fork 14
/
053.飞机大战.html
423 lines (368 loc) · 13.8 KB
/
053.飞机大战.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
<!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">
<link rel="stylesheet" href="./assets/global.css">
<style>
* {
-webkit-touch-callout: none;
}
body {
height: 100vh;
width: 100vw;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>
</head>
<body>
<canvas id="game"></canvas>
<script type="module">
import { Randoms } from "https://unpkg.com/@3r/tool/lib/randoms.js";
import { aspectFill, aspectFit, scaleToFill } from "https://unpkg.com/@3r/tool/lib/picture.js";
/** @type {HTMLCanvasElement} */
let canvas = document.querySelector("#game")
let config = {
width: 480,
height: 852
}
let ctx = canvas.getContext('2d')
let width = canvas.clientWidth;
let height = canvas.clientHeight;
// 游戏背景
let gameBackground = null;
// 背景滚动速度
let gameBackgroundScrollingSpeed = 2;
// 背景滚动位置
let gameBackgroundScrollingY = 0
// 英雄图片
let gameHeroSprite = null
let gameHeroDieSprite = null
let gameHero = {
x: 0,
y: 0,
w: 0,
h: 0,
moveX: 0,
speed: 4,
score: 0,
isFire: true,
isDie: false,
isDestory: false,
get sprite() {
if (this.isDie) return gameHeroDieSprite
return gameHeroSprite
}
}
// 敌人图片
let gameEnemySprite = null
let gameEnemyDieSprite = null
let gameEnemy = []
// 游戏子弹
let gameBulletSprite = null;
let gameBullet = []
let lastGenerateEnemyTime = 0;
let lastGenerateBulletTime = 0
let isMouseDown = false;
let isTouchDown = false;
let updateIntervalId = null
let isGameOver = false;
// 禁止长按浮窗
canvas.addEventListener("contextmenu", (e) => {
e.preventDefault()
})
// 判断两矩形是否重叠
function isRectOverlap(rect1, rect2) {
if (!rect1) return false
if (!rect2) return false
if (rect1.x + rect1.w < rect2.x) return false
if (rect1.y + rect1.h < rect2.y) return false;
if (rect2.x + rect2.w < rect1.x) return false
if (rect2.y + rect2.h < rect1.y) return false;
return true
}
/**
* 加载图
* @param {string} src
* @returns {Promise<HTMLImageElement>}
*/
function loadImage(src) {
return new Promise((resolve) => {
let image = new Image()
image.src = src;
image.onload = (ev) => {
resolve(image)
}
})
}
// 滚动背景
function scrollingBackgrounds() {
gameBackgroundScrollingY += gameBackgroundScrollingSpeed;
let gameBackgroundHeight = gameBackground.height;
ctx.drawImage(gameBackground, 0, gameBackgroundScrollingY % gameBackgroundHeight - gameBackgroundHeight)
ctx.drawImage(gameBackground, 0, gameBackgroundScrollingY % gameBackgroundHeight)
ctx.drawImage(gameBackground, 0, gameBackgroundScrollingY % gameBackgroundHeight + gameBackgroundHeight)
}
// 更新玩家信息
function updateHero() {
if (gameHero.isDestory) return gameOver();
if (!gameHero.isDie) {
gameHero.x += gameHero.moveX * gameHero.speed;
if (gameHero.x < 0) {
gameHero.x = 0
}
else if (gameHero.x + gameHero.w > config.width) {
gameHero.x = config.width - gameHero.w
}
let x = gameHero.x;
let y = gameHero.y;
let w = gameHero.w;
let h = gameHero.h
}
ctx.drawImage(gameHero.sprite, gameHero.x, gameHero.y)
}
// 敌人速度区间
const ENEMY_MIN_SPEED = 3;
const ENEMY_MAX_SPEED = 10;
// 40帧 1个
const ENEMY_GENERATE_SPEED = 40;
// 生成敌人
function generateEnemy() {
lastGenerateEnemyTime++;
if (lastGenerateEnemyTime % ENEMY_GENERATE_SPEED == 0) {
let x = Randoms.getRandomInt(0, config.width - gameEnemySprite.width)
let y = -gameEnemySprite.height;
let w = gameEnemySprite.width;
let h = gameEnemySprite.height;
let moveY = 1;
let speed = Randoms.getRandomInt(ENEMY_MIN_SPEED, ENEMY_MAX_SPEED);
let isDie = false
let isDestory = false
gameEnemy.push({
x, y, w, h, moveY, speed, isDie, isDestory,
get sprite() {
if (this.isDie) return gameEnemyDieSprite
return gameEnemySprite
}
})
console.log("生成一个敌人", gameEnemy);
}
}
// 更新敌人信息
function updateEnemy() {
gameEnemy = gameEnemy.filter(e => e && !e.isDestory)
for (let i = 0; i < gameEnemy.length; i++) {
const tempEnemy = gameEnemy[i];
tempEnemy.y += tempEnemy.moveY * tempEnemy.speed;
let x = tempEnemy.x;
let y = tempEnemy.y;
let w = tempEnemy.w;
let h = tempEnemy.h
ctx.drawImage(tempEnemy.sprite, x, y)
if (tempEnemy.y > config.height) {
gameEnemy[i] = undefined
}
if (isRectOverlap(gameHero, tempEnemy)) {
die.call(gameHero)
die.call(tempEnemy)
}
}
}
// 子弹速度
const BULLET_SPEED = 8;
// 子弹方向
const BULLET_DIRECTION = -1
// 子弹起始点与飞机的位置Y轴偏移量
const BULLET_OFFSET_Y = 2
// 开火
function fire() {
let self = this;
if (!self.isFire) return;
let x = self.x + self.w / 2 - gameBulletSprite.width / 2
let y = self.y - gameBulletSprite.height - BULLET_OFFSET_Y;
let w = gameBulletSprite.width;
let h = gameBulletSprite.height;
let moveY = BULLET_DIRECTION;
let speed = BULLET_SPEED;
gameBullet.push({
x, y, w, h, moveY, speed
})
console.log("生成一发子弹", gameBullet);
}
// 死亡延迟 200ms
const DIE_DELAY = 200
// 死亡
function die() {
let self = this;
self.isDie = true;
self.moveY = 0;
self.isFire = false;
setTimeout(() => {
self.isDestory = true;
}, DIE_DELAY);
}
// 游戏结束
function gameOver() {
clearInterval(updateIntervalId);
ctx.fillStyle = 'rgba(255,255,255,.4)'
ctx.fillRect(0, 0, config.width, config.height)
ctx.fillStyle = '#000'
ctx.textAlign = "center"
ctx.font = "bold 36px serif"
ctx.fillText(`游戏结束`, config.width / 2, config.height * .3)
ctx.font = "500 26px serif"
ctx.fillText(`得分为: ${gameHero.score}分`, config.width / 2, config.height * .4, config.width * .7)
ctx.fillText(`点击屏幕,再来一局`, config.width / 2, config.height * .6 + 50, config.width * .7)
isGameOver = true
}
// 20帧 1个
const BULLET_GENERATE_SPEED = 20;
// 生成子弹
function generateBullet() {
lastGenerateBulletTime++;
if (lastGenerateBulletTime % BULLET_GENERATE_SPEED == 0) {
fire.call(gameHero)
}
}
// 更新子弹信息
function updateBullet() {
gameBullet = gameBullet.filter(e => e)
let tempGameEnemy = gameEnemy.filter(e => e && !e.isDie)
for (let i = 0; i < gameBullet.length; i++) {
const tempBullet = gameBullet[i];
tempBullet.y += tempBullet.moveY * tempBullet.speed;
let x = tempBullet.x;
let y = tempBullet.y;
let w = tempBullet.w;
let h = tempBullet.h
ctx.drawImage(gameBulletSprite, x, y)
// 检查是否击中
for (let j = 0; j < tempGameEnemy.length; j++) {
const tempEnemy = tempGameEnemy[j];
if (isRectOverlap(tempBullet, tempEnemy)) {
die.call(tempEnemy)
gameBullet[i].moveY = 0;
gameBullet[i] = undefined
gameHero.score++;
}
}
if (tempBullet.y < -h) {
gameBullet[i] = undefined
}
}
}
// 更新得分
function updateScore() {
ctx.textAlign = 'left'
ctx.textBaseline = "top"
ctx.font = "bold 28px serif"
ctx.fillText(`得分:${gameHero.score}`, 10, 10)
}
// 鼠标操作
function mousedown(e) {
if (isGameOver) start()
else isMouseDown = true;
gameHero.moveX = Math.sign(e.offsetX - config.width / 2)
}
function mousemove(e) {
if (isMouseDown)
gameHero.moveX = Math.sign(e.offsetX - config.width / 2)
}
function mouseup(e) {
gameHero.moveX = 0
isMouseDown = false;
}
// 手机操作
function touchstart(e) {
e.preventDefault();
if (isGameOver) start()
else isTouchDown = true;
let [touch] = e.changedTouches
gameHero.moveX = Math.sign(touch.clientX - config.width / 2)
}
function touchmove(e) {
if (isTouchDown) {
let [touch] = e.changedTouches
gameHero.moveX = Math.sign(touch.clientX - config.width / 2)
}
}
function touchend(e) {
gameHero.moveX = 0
isTouchDown = false;
}
// 开始
async function start() {
isGameOver = false;
gameBackground = gameBackground || await loadImage('./assets/plane-wars/background.png')
gameHeroSprite = gameHeroSprite || await loadImage('./assets/plane-wars/hero.png')
gameHeroDieSprite = gameHeroDieSprite || await loadImage('./assets/plane-wars/hero_die.png')
gameEnemySprite = gameEnemySprite || await loadImage('./assets/plane-wars/enemy.png')
gameEnemyDieSprite = gameEnemyDieSprite || await loadImage('./assets/plane-wars/enemy_die.png')
gameBulletSprite = gameBulletSprite || await loadImage('./assets/plane-wars/bullet.png')
// 展示长边
let aspectFitRes = aspectFit(gameBackground.width, gameBackground.height, document.body.clientWidth, document.body.clientHeight)
config.width = aspectFitRes.width;
config.height = Math.max(aspectFitRes.height, document.body.clientHeight)
canvas.setAttribute('width', config.width);
canvas.setAttribute('height', config.height);
ctx = canvas.getContext('2d')
width = canvas.clientWidth;
height = canvas.clientHeight;
canvas.width = width;
canvas.height = height;
gameHero = {
x: 0,
y: 0,
w: 0,
h: 0,
moveX: 0,
speed: 4,
score: 0,
isFire: true,
isDie: false,
isDestory: false,
get sprite() {
if (this.isDie) return gameHeroDieSprite
return gameHeroSprite
}
}
gameBullet = []
gameEnemy = []
gameHero.w = gameHeroSprite.width;
gameHero.h = gameHeroSprite.height;
gameHero.x = - gameHero.w / 2 + config.width / 2
gameHero.y = config.height * 4 / 5
canvas.removeEventListener("mousedown", mousedown)
canvas.removeEventListener("mousemove", mousemove)
document.removeEventListener("mouseup", mouseup)
canvas.removeEventListener("touchstart", touchstart)
canvas.removeEventListener("touchmove", touchmove)
document.removeEventListener("touchend", touchend)
canvas.addEventListener("mousedown", mousedown)
canvas.addEventListener("mousemove", mousemove)
document.addEventListener("mouseup", mouseup)
canvas.addEventListener("touchstart", touchstart)
canvas.addEventListener("touchmove", touchmove)
document.addEventListener("touchend", touchend)
updateIntervalId = setInterval(update, 1000 / 60);
}
// 更新
function update() {
ctx.clearRect(0, 0, config.width, config.height)
scrollingBackgrounds();
generateEnemy();
updateEnemy();
updateHero();
generateBullet();
updateBullet();
updateScore();
}
start()
</script>
</body>
</html>