forked from targetkiller/climbUp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
386 lines (357 loc) · 9.8 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
/*
* @author:tqtan;
* @date:14/5/12;
* @content:游戏逻辑代码;
*/
window.RAF =
(function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback, element) {
var start,
finish;
window.setTimeout( function () {
start = +new Date();
callback(start);
finish = +new Date();
self.timeout = 1000 / 60 - (finish - start);
}, self.timeout);
};
}
)
();
// 键盘控制响应.......................................................
window.onkeydown = function (e) {
var key = e.keyCode;
if (key === 80) { // 'p'
// 暂停
}
if (key === 37) { // left arrow
climbUp.roleX = climbUp.toLeft;
}
else if (key === 39) { // right arrow
climbUp.roleX = climbUp.toRight;
}
};
// 鼠标或触摸响应.......................................................
document.addEventListener('touchstart',function(ev){
if(ev.touches[0].pageX<climbUp.Bwidth/2){
climbUp.roleX = climbUp.toLeft;
}
else{
climbUp.roleX = climbUp.toRight;
}
},false);
// 重力感应
// function Orientation(selector) {
// }
// Orientation.prototype.init = function(){
// window.addEventListener('deviceorientation', this.orientationListener, false);
// }
// Orientation.prototype.orientationListener = function(evt) {
// var gamma = evt.gamma
// var beta = evt.beta
// var alpha = evt.alpha
// if(climbUp.changeSideDir==0){
// if(gamma<0){
// climbUp.roleX = climbUp.toLeft;
// }
// else{
// climbUp.roleX = climbUp.toRight;
// }
// }
// else{
// if(gamma>0){
// climbUp.roleX = climbUp.toLeft;
// }
// else{
// climbUp.roleX = climbUp.toRight;
// }
// }
// }
// 重新开始游戏.......................................................
var restart = document.getElementById('restart');
restart.addEventListener('click',function(ev){
climbUp.reset();
},false);
var ClimbUp = function(){
// init ---------------------------
this.canvas = document.getElementById('game'),
this.ctx = this.canvas.getContext('2d'),
this.Bwidth = this.canvas.clientWidth,
this.Bheight = this.canvas.clientHeight,
this.canvas.width = this.Bwidth,
this.canvas.height = this.Bheight,
// setting ------------------------
// this.coinSound = document.getElementById('coin-sound'),
// this.audioTracks = [new Audio()],
// this.COIN_VOLUME = 1.0,
// this.coinSound.volume = this.COIN_VOLUME,
this.treeWidth = 40,
this.treeHeight = this.Bheight,
this.treeColor = "#770000",
this.branchWidth = 150,
this.branchHeight = 30,
this.branchColor = "#770000",
this.branchs = [],
this.branchsSpeed = 5,
this.branchsVelocity = 0.005,
this.branchsSpeedMax = 25,
this.branchsNum = 1,
this.roleWidth = 40,
this.roleHeight = 40,
this.roleColor = "#ffff00",
this.toLeft = (this.Bwidth-this.treeWidth)/2-this.roleWidth,
this.toRight = (this.Bwidth+this.treeWidth)/2,
this.roleX = this.toLeft;
this.roleY = this.Bheight-50;
this.score = 0;
this.scoreVelocity = 1;
this.startTime = +new Date(),
this.changeSideBaseTime = 5000,
this.changeSideRanTime = 10000,
this.changeSideTime = Math.floor(Math.random()*this.changeSideBaseTime)+this.changeSideRanTime,
this.changeSideDir = 0;//0:正,1:反
this.stopGame = false;
// control ------------------------
this.branchsInit = {
execute:function(){
for(var i = 0; i < climbUp.branchsNum; i++){
this.branchInit();
}
},
branchInit:function(){
var _x = Math.floor(Math.random()*2)==1?
((climbUp.Bwidth-climbUp.treeWidth)/2-climbUp.branchWidth+1):
((climbUp.Bwidth+climbUp.treeWidth)/2-1);
var _y = -(Math.floor(Math.random()*(climbUp.Bheight))+climbUp.branchHeight*2);
climbUp.branchs.push({x:_x,y:_y});
}
}
};
// prototype extend
ClimbUp.prototype = {
start:function(){
// init
this.branchs=[];
this.branchsInit.execute();
RAF(function(){climbUp.animate();});
},
animate:function(){
this.ctx.clearRect(0,0,this.Bwidth,this.Bheight);
this.drawAll();
RAF(function(){climbUp.animate();});
},
reset:function(){
this.branchsVelocity = 0.01;
this.branchsSpeed = 5;
this.score = 0;
this.scoreVelocity = 1;
this.branchs=[];
this.branchsInit.execute();
document.getElementById('gameover').style.display="none";
document.getElementById('score').innerText=this.score;
this.stopGame = false;
},
drawAll:function(){
this.drawTree();
this.drawBranch();
this.drawRole();
if(this.isCollide()){
// 发生碰撞
document.getElementById('gameover').style.display="block";
document.getElementById('finalScore').innerText=this.score;
this.branchsVelocity = 0;
this.branchsSpeed = 0;
this.scoreVelocity = 0;
this.branchs = [];
this.stopGame = true;
}
if(!this.stopGame){
if(this.isNeedChangeSide()){
// console.log("changesize!");
this.changeSide();
}
}
// 游戏加速
if(this.branchsSpeed<this.branchsSpeedMax){
this.branchsSpeed += this.branchsVelocity;
// document.getElementById('speed').innerText=this.branchsSpeed;
}
//document.getElementById('speed').innerText=this.branchsSpeed.toFixed(2);
},
drawTree:function(){
this.ctx.save();
this.ctx.fillStyle = this.treeColor;
this.ctx.fillRect((this.Bwidth-this.treeWidth)/2,0,this.treeWidth,this.treeHeight);
this.ctx.restore();
},
drawBranch:function(){
this.ctx.save();
this.ctx.fillStyle = this.branchColor;
for(var i = 0; i < this.branchs.length; i++){
// 枝叶的y值每次减去相应距离
this.branchs[i].y += this.branchsSpeed;
this.ctx.fillRect(this.branchs[i].x,this.branchs[i].y,this.branchWidth,this.branchHeight);
// 枝叶超出高度新建枝叶
if(this.branchs[i].y>this.Bheight){
// 游戏增分
this.score += this.scoreVelocity;
document.getElementById('score').innerText=this.score;
this.showFunnyInfo();
// climbUp.coinSound.play();
document.getElementById('coin-sound').play();
this.branchs=[];
this.branchsInit.execute();
}
}
this.ctx.restore();
},
drawRole:function(){
this.ctx.save();
this.ctx.fillStyle = this.roleColor;
this.ctx.fillRect(this.roleX,this.roleY,this.roleWidth,this.roleHeight);
this.ctx.restore();
},
showFunnyInfo:function(){
var now = this.score;
var result = "平地";
if(now < 5){
result = "小别墅";
}
else if(now < 10){
result = "普通大厦";
}
else if(now < 20){
result = "高级大厦";
}
else if(now < 30){
result = "台北101";
}
else if(now < 40){
result = "吉隆波双子塔";
}
else if(now < 50){
result = "上海东方明珠";
}
else if(now < 60){
result = "安纳普尔纳峰";
}
else if(now < 70){
result = "南迦帕尔巴特峰";
}
else if(now < 80){
result = "马纳斯鲁峰";
}
else if(now < 90){
result = "道拉吉里峰";
}
else if(now < 100){
result = "卓奥友峰";
}
else if(now < 110){
result = "马卡鲁峰";
}
else if(now < 120){
result = "洛子峰";
}
else if(now < 130){
result = "干城章嘉峰";
}
else if(now < 140){
result = "乔戈里峰";
}
else if(now < 150){
result = "珠穆朗玛峰";
}
else if(now < 160){
result = "云层";
}
else if(now < 170){
result = "天堂";
}
else if(now < 180){
result = "地球表面";
}
else{
result = "外太空";
}
document.getElementById('funnyInfo').innerText=result;
},
// 碰撞检测
isCollide:function(){
var top = this.roleY;
var left = this.roleX;
var right = this.roleWidth + left;
var bottom = this.roleHeight + top;
var centerX = left+(this.roleWidth/2);
var centerY = top+(this.roleHeight/2);
var branchTop = 0;
var branchLeft = 0;
var branchWidth = 0;
var branchHeight = 0;
for(var i = 0; i < this.branchs.length; i++){
branchTop = this.branchs[i].y;
branchLeft = this.branchs[i].x;
branchWidth = this.branchWidth;
branchHeight = this.branchHeight;
}
this.ctx.beginPath();
this.ctx.rect(branchLeft, branchTop, branchWidth, branchHeight);
return this.ctx.isPointInPath(left, top) ||
this.ctx.isPointInPath(right, top) ||
this.ctx.isPointInPath(centerX, centerY) ||
this.ctx.isPointInPath(left, bottom) ||
this.ctx.isPointInPath(right, bottom);
},
// 判断转向的发生时机
isNeedChangeSide:function(){
var now = +new Date();
return (now-this.startTime)>this.changeSideTime;
},
// 操作转向
changeSide:function(){
var left,right;
// 转向,0是正,1是反,默认是正
if(this.changeSideDir==1){
left = climbUp.toLeft;
right = climbUp.toRight;
document.getElementsByTagName('body')[0].style.backgroundColor = "#333";
this.changeSideDir = 0;
}
else{
left = climbUp.toRight;
right = climbUp.toLeft;
document.getElementsByTagName('body')[0].style.backgroundColor = "#aaa";
this.changeSideDir = 1;
}
// 键盘控制响应.......................................................
window.onkeydown = function (e) {
var key = e.keyCode;
if (key === 37) { // left arrow
climbUp.roleX = left;
}
else if (key === 39) { // right arrow
climbUp.roleX = right;
}
};
// 鼠标或触摸响应.......................................................
document.addEventListener('touchstart',function(ev){
if(ev.touches[0].pageX<climbUp.Bwidth/2){
climbUp.roleX = left;
}
else{
climbUp.roleX = right;
}
},false);
// 重新设置下一次转向时间
this.startTime = +new Date();
this.changeSideTime = Math.floor(Math.random()*this.changeSideBaseTime)+this.changeSideRanTime;
}
}
var climbUp = new ClimbUp();
climbUp.start();
// (new Orientation()).init();