-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.js
476 lines (456 loc) · 15.5 KB
/
Game.js
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
;(() => {
class Game {
constructor() {
this.canvas = document.getElementById('canvas')
this.audio = document.querySelector('audio')
this.draw = canvas.getContext('2d')
this.R = {
"bg_day": "img/bg_day.png",
"bg_night": "img/bg_night.png",
"bird0_0": "img/bird0_0.png",
"bird0_1": "img/bird0_1.png",
"bird0_2": "img/bird0_2.png",
"bird1_0": "img/bird1_0.png",
"bird1_1": "img/bird1_1.png",
"bird1_2": "img/bird1_2.png",
"bird2_0": "img/bird2_0.png",
"bird2_1": "img/bird2_1.png",
"bird2_2": "img/bird2_2.png",
"black": "img/black.png",
"blink_00": "img/blink_00.png",
"blink_01": "img/blink_01.png",
"blink_02": "img/blink_02.png",
"brand_copyright": "img/brand_copyright.png",
"button_menu": "img/button_menu.png",
"button_ok": "img/button_ok.png",
"button_pause": "img/button_pause.png",
"button_play": "img/button_play.png",
"button_rate": "img/button_rate.png",
"button_resume": "img/button_resume.png",
"button_score": "img/button_score.png",
"button_share": "img/button_share.png",
"font_048": "img/font_048.png",
"font_049": "img/font_049.png",
"font_050": "img/font_050.png",
"font_051": "img/font_051.png",
"font_052": "img/font_052.png",
"font_053": "img/font_053.png",
"font_054": "img/font_054.png",
"font_055": "img/font_055.png",
"font_056": "img/font_056.png",
"font_057": "img/font_057.png",
"land": "img/land.png",
"medals_0": "img/medals_0.png",
"medals_1": "img/medals_1.png",
"medals_2": "img/medals_2.png",
"medals_3": "img/medals_3.png",
"new": "img/new.png",
"number_context_00": "img/number_context_00.png",
"number_context_01": "img/number_context_01.png",
"number_context_02": "img/number_context_02.png",
"number_context_03": "img/number_context_03.png",
"number_context_04": "img/number_context_04.png",
"number_context_05": "img/number_context_05.png",
"number_context_06": "img/number_context_06.png",
"number_context_07": "img/number_context_07.png",
"number_context_08": "img/number_context_08.png",
"number_context_09": "img/number_context_09.png",
"number_context_10": "img/number_context_10.png",
"number_score_00": "img/number_score_00.png",
"number_score_01": "img/number_score_01.png",
"number_score_02": "img/number_score_02.png",
"number_score_03": "img/number_score_03.png",
"number_score_04": "img/number_score_04.png",
"number_score_05": "img/number_score_05.png",
"number_score_06": "img/number_score_06.png",
"number_score_07": "img/number_score_07.png",
"number_score_08": "img/number_score_08.png",
"number_score_09": "img/number_score_09.png",
"pipe_down": "img/pipe_down.png",
"pipe_up": "img/pipe_up.png",
"pipe2_down": "img/pipe2_down.png",
"pipe2_up": "img/pipe2_up.png",
"score_panel": "img/score_panel.png",
"text_game_over": "img/text_game_over.png",
"text_ready": "img/text_ready.png",
"title": "img/title.png",
"tutorial": "img/tutorial.png",
"white": "img/white.png",
"baozha":"img/baozha.png"
}
this.scene = 0 // 场景编号
this.score = 0 // 分数
this.autoWH()
this.progress()
if(!localStorage.getItem("score")){
localStorage.setItem("score","[]")
}
}
autoWH() { // canvas自适应宽高
let W = document.documentElement.clientWidth
let H = document.documentElement.clientHeight
this.canvas.width = W > 420 ? 420 : W
this.canvas.height = H > 750 ? 750 : H
}
progress(){ //加载资源
let count = 0 // 统计加载完成的图片
let total = Object.keys(this.R).length // 所有图片的个数
// 遍历对象
for (let key in this.R) {
// 只要图片onload触发了,就证明图片加载成功了
// 自己创建一个img
((src) => {
this.R[key] = new Image()
this.R[key].src = src
this.R[key].onload = () => {
count++
if(count == total){
this.start()
}
}
})(this.R[key])
}
}
clear(){ // 清屏
this.draw.clearRect(0,0,this.canvas.width,this.canvas.height)
}
start(){ // 开始游戏
this.f = 0
// 场景管理器实例
this.sM = new SceneManager()
this.sM.enter(0)
this.timer = setInterval(function(){
this.clear()
this.sM.updataAndRender()
this.f++
}.bind(this), 20)
}
}
window.Game = Game
// 设置游戏背景
class Background{
constructor(){
this.x = 0
this.w = 288
this.h = 512
this.step = 1
}
updata(){
this.x -= this.step
if(this.x <= -this.w) {
this.x = 0
}
}
render(){
// drawImage(图片,sx,sy,sw,sh,x,y,w,h) // sy,sx 切片的位置 sw,sh切片的宽
game.draw.drawImage(game.R["bg_day"], this.x, game.canvas.height - this.h)
game.draw.drawImage(game.R["bg_day"], this.x + this.w, game.canvas.height - this.h)
game.draw.drawImage(game.R["bg_day"], this.x + this.w * 2, game.canvas.height - this.h)
// 将上方空白补上
game.draw.fillStyle = "#4ec0ca"
game.draw.fillRect(0, 0, game.canvas.width, game.canvas.height - this.h)
}
}
// 背景底部背景
class Land{
constructor(){
this.x = 0
this.w = 336
this.h = 112
this.step = 1
}
updata(){
this.x -= this.step
if(this.x <= -this.w) {
this.x = 0
}
}
render(){
game.draw.drawImage(game.R["land"], this.x, game.canvas.height - this.h)
game.draw.drawImage(game.R["land"], this.x + this.w, game.canvas.height - this.h)
game.draw.drawImage(game.R["land"], this.x + this.w * 2, game.canvas.height - this.h)
}
}
// 上下水管设置函数
class Pipe {
constructor(){
// 上管子随机值320-100随机高
this.t1 = Math.round(Math.random() * 220 + 100)
// 空隙
this.space = 140
// 下管子高度
this.b1 = game.canvas.height - 112 - this.t1 - this.space
this.x = game.canvas.width
this.done = true
game.pipeArr.push(this)
}
updata(){
this.x-=1
// 一旦管子走出界面就从数组移除
if(this.x <= -52){
game.pipeArr.shift()
}
this.x1 = this.x
this.x2 = this.x+52
this.y1 = this.t1
this.y2 = this.t1+this.space
// 碰撞判断
if((game.bird.x2>this.x1&&game.bird.y1<this.y1&&game.bird.x1<this.x2)||(game.bird.x2>this.x1&&game.bird.y2>this.y2&&game.bird.x1<this.x2)){
game.sM.enter(3)
}
// 分数判断
if(game.bird.x1>this.x2&&this.done){
game.score++
this.done = false
}
}
render(){
game.draw.drawImage(game.R["pipe_down"],0,320-this.t1,52,this.t1,this.x,0,52,this.t1)
game.draw.drawImage(game.R["pipe_up"],0,0,52,this.b1,this.x,this.t1+this.space,52,this.b1)
}
}
// 小飞机函数
class Bird {
constructor(){
this.x = game.canvas.width/2
this.y = game.canvas.height*(1-0.618)
this.changeY = 0
this.rotate = 0
this.img = [game.R["bird0_0"],game.R["bird0_1"],game.R["bird0_2"]]
this.status = 'drop'
this.cb = 0
}
updata(){
if(this.status == 'drop'){
this.changeY += 0.4
this.y += this.changeY
this.rotate += 0.02
}else if(this.status == 'up'){
this.changeY -= 0.4
if(this.changeY <= 0){
this.status = 'drop'
return
}
this.y-=this.changeY
this.y<=0?this.y=0:null
// 切换图片翅膀状态
this.cb++
this.cb>2?this.cb=0:null
}
this.x1 = this.x-17
this.x2 = this.x+17
this.y1 = this.y-12
this.y2 = this.y+12
// 落地检测
if(this.y>=game.draw.canvas.height-120){
game.sM.enter(3)
}
}
render(){
game.draw.save()
game.draw.translate(this.x,this.y)
game.draw.rotate(this.rotate)
game.draw.drawImage(this.img[this.cb],-24,-24)
game.draw.restore()
}
clickTop(){
this.status = 'up'
this.rotate = -0.5
this.changeY = 7
}
}
// 场景管理
class SceneManager {
constructor(){
this.bindEvent()
}
enter(number){
switch (number){
case 0:
game.scene = 0
this.titieY = 0
this.buttonY = game.canvas.height
this.birdY = 250
this.birChangeY = 1.0
break
case 1:
game.scene = 1
this.tutorialAlpha = 0
this.tutorialAlphaChange = 0.05
break
case 2:
game.scene = 2
game.score = 0
game.bg = new Background()
game.land = new Land()
game.bird = new Bird()
game.pipeArr = []
// 播放音乐
game.audio.play();
game.audio.volume = 0.02
break
case 3:
game.scene = 3
this.bz = false // 是否爆炸
break
case 4:
// 暂停音乐
game.audio.currentTime = 0;
game.audio.pause()
game.scene = 4
this.gameoverY = 0
this.score_panelY = game.canvas.height
// 获取存储的成绩
let arr = JSON.parse(localStorage.getItem("score"))
// 排序获取前三名
arr.sort((a,b) => b-a)
// 现在的成绩是game.score
this.maxscore = arr[0]
if(game.score>arr[0]){
this.model = "medals_1"
this.maxscore = game.score
}else if(game.score>arr[1]){
this.model = "medals_2"
}else if(game.score>arr[2]){
this.model = "medals_3"
}else{
this.model = "medals_0"
}
// 将分数储存到数组,去重
if(!arr.includes(game.score)){
arr.push(game.score)
}
// 存储到浏览器
localStorage.setItem("score",JSON.stringify(arr))
break
}
}
updataAndRender(){
switch (game.scene){
case 0:
game.draw.fillStyle = "#4ec0ca"
game.draw.fillRect(0, 0, game.canvas.width, game.canvas.height)
game.draw.drawImage(game.R["bg_day"],0,game.canvas.height-512)
game.draw.drawImage(game.R["bg_day"],288,game.canvas.height-512)
game.draw.drawImage(game.R["land"],0,game.canvas.height-112)
game.draw.drawImage(game.R["land"],336,game.canvas.height-112)
this.titieY += 5
this.buttonY -= 9
this.birdY += this.birChangeY
this.titieY>=160?this.titieY=160:null
this.buttonY<=380?this.buttonY=380:null
if(this.birdY <=250||this.birdY >= 300){
this.birChangeY *= -1
}
game.draw.drawImage(game.R["title"],(game.canvas.width-178)/2,this.titieY)
game.draw.drawImage(game.R["button_play"],(game.canvas.width-116)/2,this.buttonY)
game.draw.drawImage(game.R["bird0_0"],(game.canvas.width-48)/2,this.birdY)
break
case 1:
game.draw.fillStyle = "#4ec0ca"
game.draw.fillRect(0, 0, game.canvas.width, game.canvas.height)
game.draw.drawImage(game.R["bg_day"],0,game.canvas.height-512)
game.draw.drawImage(game.R["bg_day"],288,game.canvas.height-512)
game.draw.drawImage(game.R["land"],0,game.canvas.height-112)
game.draw.drawImage(game.R["land"],336,game.canvas.height-112)
game.draw.drawImage(game.R["bird0_0"],(game.canvas.width-48)/2,150)
if(this.tutorialAlpha>1||this.tutorialAlpha<0){
this.tutorialAlphaChange *= -1
}
this.tutorialAlpha += this.tutorialAlphaChange
game.draw.save() // 状态保存
game.draw.globalAlpha = this.tutorialAlpha // 透明度
game.draw.drawImage(game.R["tutorial"],(game.canvas.width-114)/2,250)
game.draw.restore() // 状态恢复
break
case 2:
game.bg.updata()
game.bg.render()
game.land.updata()
game.land.render()
game.f%200==0?new Pipe():null
game.pipeArr.forEach((item,index) => {
game.pipeArr[index].updata();
game.pipeArr[index].render()
});
this.scoreRender()
game.bird.updata()
game.bird.render()
break
case 3:
game.bg.render()
game.land.render()
game.bird.render()
for (let i =0;i<game.pipeArr.length;i++){
game.pipeArr[i].render()
}
this.scoreRender() // 分数函数
if(this.bz){
}else{
game.draw.drawImage(game.R["baozha"],game.bird.x1-10,game.bird.y1-20,58,65)
this.enter(4)
}
break
case 4:
game.bg.render()
game.land.render()
this.gameoverY += 5
if(this.gameoverY>=180){this.gameoverY = 180}
game.draw.drawImage(game.R["text_game_over"],(game.canvas.width-204)/2,this.gameoverY)
this.score_panelY -= 10
if(this.score_panelY<=270){this.score_panelY = 270}
game.draw.drawImage(game.R["score_panel"],(game.canvas.width-238)/2,this.score_panelY)
game.draw.drawImage(game.R[this.model],(game.canvas.width-238)/2+32,this.score_panelY+44)
for (let i=0;i<game.score.toString().length;i++){
game.draw.drawImage(game.R["number_score_0"+(parseInt(game.score.toString()[i]))],(game.canvas.width-238)/2+171+i*16,this.score_panelY+36)
}
for (let i=0;i<this.maxscore.toString().length;i++){
game.draw.drawImage(game.R["number_context_0"+(parseInt(this.maxscore.toString()[i]))],(game.canvas.width-238)/2+168+i*12,this.score_panelY+80)
}
game.draw.drawImage(game.R["button_ok"],(game.canvas.width-80)/2,this.score_panelY+150)
break
}
}
bindEvent(){
// 只能给canvas绑定事件,页面上只有它一个元素,需要判断鼠标点的位置判断clientX和clientY
// 不同场景下都可能有点击事件,需要先判断场景
game.canvas.onclick = (e) => {
switch(game.scene) {
case 0:
if(document.documentElement.clientWidth > 420){
this.enter(1)
}else{
if(e.clientY>this.buttonY&&e.clientY<this.buttonY+70&&e.clientX>game.canvas.width/2-58&&e.clientX<game.canvas.width/2+58){
this.enter(1)
}
}
break
case 1:
this.enter(2)
break
case 2:
game.bird.clickTop()
break
case 3:
break
case 4:
if(document.documentElement.clientWidth > 420){
this.enter(2)
}else{
if(e.clientY>418&&e.clientY<448&&e.clientX>game.canvas.width/2-40&&e.clientX<game.canvas.width/2+40){
this.enter(2)
}
}
break
}
}
}
scoreRender(){ // 渲染分数
let scorestr = game.score.toString()
let cenLine = game.canvas.width/2-scorestr.length*30/2
for (let i=0;i<scorestr.length;i++){
game.draw.drawImage(game.R["font_0"+(parseInt(scorestr[i])+48)],cenLine+i*30,100)
}
}
}
})()