-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
280 lines (273 loc) · 7.9 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>gravity</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
body {
background-color: #000;
}
canvas {
background-color: #ddd;
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script type="text/javascript">
// state vars
var width = window.innerWidth,
height = window.innerHeight,
gra = .1,
// springGra = .1,
frac = .01,
radius = 10,
speed = 0,
radians = 280 * Math.PI / 180,
opacity = .3,
count = 1000 / 30,
poiArr = [],
gameStatus = true,
rectagleHdis = 0,
collStatus = true,
touchX = 0,
tempX = 0,
stepY = [],
stepX = [],
ball_speed = -18,
tempCamera = 0,
score = 0;
// touch var
var mousePos = {};
// just height
var scrollH = 832;
var step_width = 50, step_height = 30;
var cameraLine;
height > 500 ? (cameraLine = 150, speed = 7) : (cameraLine = 100, speed = 6);
// create step
var bitY, bitX, stepConut;
height < 500 ? bitY = 40 : bitY = 60, stepConut = 7;
width < 330 ? bitX = 65 : bitX = 75, stepConut = 11;
for (var i = 0; i < stepConut; i++) {
stepY[i] = i * bitY,
stepX[i] = bitX * (Math.random() * 3);
stepX[i] <= 10 && (stepX[i] += Math.random() * 10 + 11.1);
// stepX[i]+step_width>=width && (stepX[i]-=step_width+10);
stepX[i] + step_width >= width && (stepX[i] -= step_width * 1.2);
}
console.log(stepY);
// get canvas dom
var canvas = document.getElementById("canvas"), ctx = canvas.getContext("2d");
canvas.width = width,
canvas.height = height;
function drawScreen() {
gameStatus && ((!canvas && canvas.getContext) || false);
ctx.clearRect(0, 0, width, height);
tempCamera = cameraLine - ball._nextY;
tempCamera < 1 ? tempCamera = 0 : score += tempCamera / 5;
tempCamera > 37 && (tempCamera = 37);
bg._y -= tempCamera / 23;
bg._y < 0 && (bg._y = 0);
for (var _i = stepConut - 1; _i > 0; _i--) {
ctx.beginPath();
ctx.fillStyle = "yellow";
stepY[_i] += tempCamera;
ctx.fillRect(stepX[_i], stepY[_i], step_width, step_height);
stepY[_i] > 3 * height / 4 && (stepY[_i] -= height, stepX[_i] > 80 && (stepX[_i] = width - stepX[_i]));
}
ctx.beginPath();
ctx.arc(ball._nextX, ball._nextY, ball._r, 0, Math.PI * 2, false);
tempX > 0 ? (ctx.fillStyle = "blue") : (ctx.fillStyle = "red");
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.fillStyle = spring._bg;
ctx.fillRect(0, spring._y, spring._width, spring._height);
ctx.closePath();
ctx.fill();
tempX = touchX - ball._x;
ball._nextX = (ball._x += tempX / 15);
collStatus = true;
ball._nextY = ball._y += ball_speed + tempCamera;
ball_speed += .6;
ball_speed > 8 && (ball_speed = 8);
(ball_speed > 0) && (ball._nextY + ball._r >= spring._nextY) && (ball_speed = -15, collStatus = false, spring._yUnit = 4);
if (ball_speed > 2) {
var a = ball._nextY + ball._r;
for (var _j = 0; _j < stepConut; _j++) {
/*judge issues:
1.ball right line over step poisition in X
2.ball left line in step poistion in X add its widh
3.judge ball position in Y whether is in a field which is step in Y +-ball._r
*/
if (ball._nextX + ball._r > stepX[_j] && ball._nextX - ball._r < stepX[_j] + step_width && stepY[_j] - ball._r < a && a < stepY[_j] + ball._r) {
a <= stepY[_j] && (ball_speed = -12, stepY[_j] -= height);
}
}
}
// when game first in ,draw spring and is not confict with ball's collision logic
if (gameStatus) {
//just coming once
spring._yUnit += gra;
spring._Vy = spring._yUnit;
gameStatus = false;
}
//when collision is happan ,then set spring's height to start self's change
//when collision is no hanppen yet , just draw spring as rule
//collStatus default value is true,when collision is happening ,collStatus is change to false
if (collStatus) {
spring._yUnit += gra * .5;
spring._Vy = spring._yUnit;
} else {
score < 50 ? spring._nextY = spring._y = height * .91 : console.log("gameOver!");
}
// watch spring position in Y
if (score <= 50) {
if (spring._nextY <= height * .9) {
// spring is static
spring._nextY = spring._y = height * .9
} else if (spring._nextY > height * .9 && spring._nextY < height) {
// spring is moving
spring._nextY = (spring._y += spring._Vy);
} else if (spring._nextY >= height) {
spring._yUnit = -spring._yUnit;
//cause next spring is loop is in (spring._nextY > height*.9 && spring._nextY<height), so set spring 's next height is height*.99
spring._nextY = spring._y = height * .99999;
}
} else {
spring._nextY = spring._y = height + 1;
}
}
function drawTrail() {
!!poiArr.length || false;
for (var __i = 0, len_ = poiArr.length; __i < len_; __i++) {
var point = poiArr[__i];
ctx.beginPath();
ctx.arc(point.x, point.y, point.r, 0, Math.PI * 2, false);
ctx.fillStyle = "#000";
ctx.closePath();
ctx.fill();
}
}
function Spring() {
this._width = width,
this._height = height / 10,
this._bg = "#000",
this._Vy = speed,
this._yUnit = Math.sin(280 * Math.PI / 180) * this._Vy * .6,
this._y = height * .98,
this._nextY = this._y;
}
function Point() {
this._r = radius,
this._x = width * (1 / 3),
this._y = height,
this._nextX = this._x,
this._nextY = this._y,
this._speed = speed,
this._angle = radians,
// init ball's x , y speed
// this._xUnit = this._speed * Math.cos(this._angle),
// this._xUnit = 0,
this._yUnit = this._speed * Math.sin(this._angle),
// this._vX = this._xUnit,
this._vY = this._yUnit;
}
function Bg() {
this._yUint = 0;
this._vY = this._yUint;
this._y = scrollH;
this._nextY = this._y;
this._width = width;
this._height = 400;
}
// game init here
var ball = new Point();
touchX = ball._x;
var spring = new Spring();
var bg = new Bg();
drawScreen();
setInterval(function () {
drawScreen()
}, count);
// 绑定触摸事件
canvas.addEventListener("touchstart", touchStart, !1);
canvas.addEventListener("touchend", touchEnd, !1);
canvas.addEventListener("touchmove", touchMove, !1);
canvas.addEventListener("touchcancel", deleteEvent, !1);
// 绑定手势事件
canvas.addEventListener("gesturestart", deleteEvent, !1);
canvas.addEventListener("gesturechange", deleteEvent, !1);
canvas.addEventListener("gestureend", deleteEvent, !1);
// 绑定鼠标点击事件
// canvas.addEventListener("mousedown", mouseDown, !1);
// canvas.addEventListener("mouseup", mouseUp, !1);
// canvas.addEventListener("mousemove", mouseMove, !1);
// //mousedown
// function mouseDown(e) {
// mousePos = {
// x: e.layerX,
// y: e.layerY
// };
// e.preventDefault()
// }
// //mousemove
// function mouseMove(e) {
// mousePos = {
// x: e.layerX,
// y: e.layerY
// };
// }
// //mouseup
// function mouseUp(e) {
// mousePos = {
// x: e.layerX,
// y: e.layerY
// };
// }
//touchstart
function touchStart(e) {
mousePos = {
x: e.touches[0].pageX,
y: e.touches[0].pageY
};
mv(mousePos);
e.preventDefault()
}
//touchmove
function touchMove(e) {
mousePos = {
x: e.touches[0].pageX,
y: e.touches[0].pageY
};
mv(mousePos);
event.preventDefault()
}
//touchend
function touchEnd(e) {
mousePos = {
x: e.changedTouches[0].pageX,
y: e.changedTouches[0].pageY
};
mv(mousePos);
e.preventDefault()
}
//touchcancel,gesturestart,gesturechange,gestureend
function deleteEvent(e) {
e.preventDefault()
}
function mv(e) {
touchX = e.x - 8;
4 > touchX && (touchX = 4);
touchX > width && (touchX = width);
}
</script>
</body>
</html>