-
Notifications
You must be signed in to change notification settings - Fork 1
/
FleeSquirrel.html
388 lines (327 loc) · 11.3 KB
/
FleeSquirrel.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
<!doctype html>
<html>
<head>
<title>松鼠逃生</title>
<meta charset="utf-8">
<meta name="viewport" content="width=550">
<style type="text/css">
html{margin:0;padding:0;height:100%; width: 100%}
body{margin:0;padding:0;overflow:hidden;color:#333;background:#333;height:100%;}
canvas{display: block;}
</style>
</head>
<body>
<canvas id="Games"></canvas>
<script type="text/javascript">
var GameWidth=window.innerWidth, //根据屏幕得到游戏的宽度
GameHeight=window.innerHeight, //根据屏幕得到游戏的高度
EARTH_POWER = .4, //上下移动加速度
FLOOR_POWER = .7, //左右移动加速度
FPS = 60, //动画帧数
size=30, //松鼠大小
isOver= true, //游戏初始标识
squirrel,
floor
var canvas=document.getElementById("Games")
oOtherPara = {
num:10,
vs:2,
canvas:canvas,
nVSpaceParaForHardness:100
};
oRole = {
hs:10, //水平速度
vs:30, //垂直速度
width:20, //人物宽度
height:30 //人物高度
};
//设置游戏CANVAS大小
canvas.setAttribute("width",GameWidth)
canvas.setAttribute("height",GameHeight)
//得到游戏的框架区域数值
stageWidth=canvas.offsetWidth,
stageHeight=canvas.offsetHeight;
var context=canvas.getContext("2d");
//填充CANVAS
context.fillStyle="rgb(128, 128, 128)"
context.fillRect(0,0,GameWidth,GameHeight);
/*游戏基础参数*/
/*松鼠基础类*/
function Squirrel(){
this.size = size; //松鼠大小
this.playerX = GameWidth/2; //初始X坐标
this.playerY = 0; //初始Y坐标
this.vSpeed = 2.5;
this.xSpeed = 0;
this.ySpeed = 0;
this.turnRight = true;
this.leftKeyDown = false;
this.rightKeyDown = false;
this.player();
}
Squirrel.prototype={
player:function(){ //最开始,将松鼠画入
context.fillStyle = 'blue';
context.fillRect( this.playerX, this.playerY, this.size, this.size);
},
move:function(){ //识别键盘/重力感应操作,
if(this.leftKeyDown){
this.turnRight = false;
this.xSpeed += this.vSpeed;
}else if(this.rightKeyDown){
this.turnRight = true;
this.xSpeed += this.vSpeed;
}
this.xSpeed *= FLOOR_POWER;
this.ySpeed += EARTH_POWER;
if(this.turnRight){
this.playerX += this.xSpeed;
} else {
this.playerX -= this.xSpeed;
}
this.playerY += this.ySpeed;
if(this.playerX < 0){
this.playerX = 0;
}else if(this.playerX > stageWidth - this.size){
this.playerX = stageWidth - this.size;
}
if (this.hitFloor()){
this.ySpeed = -2.5;
EARTH_POWER=0
}
else{EARTH_POWER=.4}
if(this.playerY > stageHeight || this.playerY < 0){
gameOver();
return;
}
//console.log(this.playerX)
},
render : function(){ //将左右移动松鼠后的结果进行绘制
this.move();
context.fillStyle = 'blue';
context.fillRect(this.playerX, this.playerY, this.size, this.size);
//console.log(this.y)
},
hitFloor : function(){
//deadNum = 0;
for (_i = 0, _len = aryBoards.length; _i < _len; _i++) {
value = aryBoards[_i];
console.log(_i)
if(this.playerX < value.nX+value.nWidth && this.playerX+this.size > value.nX && this.playerY+this.size > value.nY && this.playerY < value.nY){
console.log("已碰撞")
return true;
} else {
// return false;
}
}
},
destroy : function(){
}
}
/*松鼠挂了*/
function gameOver(){
isOver = true;
alert("over")
}
/*基础循环*/
function mainLoop(){
if(isOver) return; //如果游戏结束,则不进行循环
context.fillStyle="rgb(128, 128, 128)"
context.fillRect(0,0,GameWidth,GameHeight);
if (aryBoards.length === 1) {
// clearInterval(clarr);
aryBoards = fCreateBoards(oRole, oOtherPara);
}
else {
fDrawBoards(aryBoards, oOtherPara);
}
squirrel.render();
// addFloors();
setTimeout(mainLoop,1000/FPS);
}
/*开始游戏*/
function startGame(){
isOver = false;
//floor=new Floor();
squirrel=new Squirrel();
aryBoards = fCreateBoards(oRole, oOtherPara);
console.log(aryBoards.length)
mainLoop();
//绑定按键
document.onkeydown = function(e){
e = e || window.event;
if(!squirrel) return;
switch(e.keyCode){
case 37 :
squirrel.leftKeyDown = true;
break;
case 39 :
squirrel.rightKeyDown = true;
break;
}
}
document.onkeyup = function(e){
e = e || window.event;
if(!squirrel) return;
switch(e.keyCode){
case 37 :
squirrel.leftKeyDown = false;
break;
case 39 :
squirrel.rightKeyDown = false;
break;
}
}
//
window.ondeviceorientation = function(e)
{
var ang;
var o = window.orientation; //获取设备方向
if(o == 90){
ang = e.beta; //设备横向1
}
else if(o == -90){
ang = -e.beta; //设备横向2
}
else if(o == 0){
ang = e.gamma; //设备纵向
}
if(ang > 5)
{
squirrel.rightKeyDown = true;
}
else if(ang < -5)
{
squirrel.leftKeyDown = true;
}
else
{
squirrel.leftKeyDown = false;
squirrel.rightKeyDown = false;
}
}
//
}
/*
nX:木板横坐标
nY:木板纵坐标
nWidth:木板宽度
nHeight:木板高度
nVSpace:木板距离上一个木板的垂直距离,下一个木板的上边缘同上一个木板的下边缘的距离
sColor:颜色值
nLiveState:0:未进入屏幕 1:在屏幕中 2:从屏幕中消失
nType:1:普通木板 2:滚动木板 3:锯齿木板
*/
(function () {
this.Board = (function () {
function Board(nX, nY, nWidth, nHeight, nVSpace, sColor, nLiveState, nType) {
this.nX = nX;
this.nY = nY;
this.nWidth = nWidth != null ? nWidth : screen.availWidth * 0.25;
this.nHeight = nHeight != null ? nHeight : 30;
this.nVSpace = nVSpace != null ? nVSpace : 0;
this.sColor = sColor != null ? sColor : 'white';
this.nLiveState = nLiveState != null ? nLiveState : 1;
this.nType = nType != null ? nType : 1;
}
return Board;
})();
}).call(this);
(function() {
this.fCreateBoards = function(oRole, oOtherPara) {
var aryBoards, curBoard, fGetRandomBy, firBoard, i, lastBoard, lower, nBoardMaxVSpace, nBoardMinVSpace, nBoardVSpeed, nBorderNum, nCanvasHeight, nCanvasWidth, nRoleHSpeed, nRoleHeight, nRoleVSpeed, nRoleWidth, nVSpaceParaForHardness, rangeX, t, upper, vSpace;
fGetRandomBy = function(num1, num2) {
var over, result, under;
if (num1 > num2) {
under = num2;
over = num1;
} else {
under = num1;
over = num2;
}
return result = (Math.random() * (over - under + 1) >>> 0) + under;
};
nBorderNum = oOtherPara.num;
nBoardVSpeed = oOtherPara.vs;
nCanvasHeight = +oOtherPara.canvas.getAttribute('height');
nCanvasWidth = +oOtherPara.canvas.getAttribute('width');
nVSpaceParaForHardness = oOtherPara.nVSpaceParaForHardness;
nRoleHSpeed = oRole.hs;
nRoleVSpeed = oRole.vs;
nRoleWidth = oRole.width;
nRoleHeight = oRole.height;
aryBoards = [];
firBoard = new Board();
firBoard.nX = fGetRandomBy(0, screen.availWidth - firBoard.nWidth);
firBoard.nY = nCanvasHeight - firBoard.nHeight;
firBoard.nWidth = nCanvasWidth * 0.25;
aryBoards.push(firBoard);
nBoardMinVSpace = nRoleHeight;
nBoardMaxVSpace = nCanvasHeight - firBoard.nHeight * 2 - nRoleHeight - nVSpaceParaForHardness;
i = 0;
while (i < nBorderNum - 1) {
lastBoard = aryBoards[i++];
curBoard = new Board();
curBoard.nLiveState = 0;
curBoard.nWidth = nCanvasWidth * 0.25;
vSpace = fGetRandomBy(nBoardMinVSpace, nBoardMaxVSpace);
t = (vSpace + lastBoard.nHeight) / (nBoardVSpeed + nRoleVSpeed);
upper = (nRoleHSpeed * t - nRoleWidth) + lastBoard.nX + lastBoard.nWidth;
lower = lastBoard.nX - (nRoleHSpeed * t - nRoleWidth) - curBoard.nWidth;
rangeX = upper + curBoard.nWidth > screen.availWidth ? lower : upper;
curBoard.nX = fGetRandomBy(rangeX, lastBoard.nX);
curBoard.nVSpace = vSpace;
aryBoards.push(curBoard);
}
// console.log(aryBoards)
return aryBoards;
};
}).call(this);
// Generated by CoffeeScript 1.4.0
(function() {
this.fDrawBoards = function(aryBoards, oOtherPara) {
var ctx, deadNum, lastBoardY, nBoardVSpeed, nCanvasHeight, nCanvasWidth, value, _i, _len;
nBoardVSpeed = oOtherPara.vs;
ctx = oOtherPara.canvas.getContext('2d');
nCanvasWidth = oOtherPara.canvas.getAttribute('width');
nCanvasHeight = oOtherPara.canvas.getAttribute('height');
ctx.clearRect(0, 0, nCanvasWidth, nCanvasHeight);
//ctx.fillStyle = 'black';
ctx.fillRect(0, 0, nCanvasWidth, nCanvasHeight);
ctx.fillStyle = 'white';
deadNum = 0;
for (_i = 0, _len = aryBoards.length; _i < _len; _i++) {
value = aryBoards[_i];
// console.log(_i+':hah')
if (value.nLiveState === 1) {
if (value.nY >= 0) {
ctx.fillStyle = value.sColor;
ctx.fillRect(value.nX, value.nY, value.nWidth, value.nHeight);
value.nY -= nBoardVSpeed;
lastBoardY = value.nY + value.nHeight;
} else {
deadNum++;
value.nLiveState = 2;
}
} else {
if (value.nLiveState === 0 && value.nVSpace <= nCanvasHeight - lastBoardY - value.nHeight) {
value.nLiveState = 1;
value.nY = lastBoardY + value.nVSpace;
lastBoardY = value.nY + value.nHeight;
ctx.fillStyle = value.sColor;
ctx.fillRect(value.nX, value.nY, value.nWidth, value.nHeight);
}
break;
}
}
while (deadNum--) {
aryBoards.shift();
}
return aryBoards;
};
}).call(this);
//
startGame()
</script>
</body>
</html>