-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
313 lines (266 loc) · 7.53 KB
/
app.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
/**
* Created by Mykyta Khmel([email protected]) on 26.02.2015.
*/
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var hero1 = new Image();
var hero2 = new Image();
var hero3 = new Image();
var hero4 = new Image();
var hero5 = new Image();
var hero6 = new Image();
var hero7 = new Image();
var hero8 = new Image();
var ground = new Image();
var sky = new Image();
var pie = new Image();
var cloud = new Image();
var direction;
var speed = 2;
var hero_speed = 3;
function keys(e) {
"use strict";
e = e || event;
if (e.type === "keydown" || e.type === "touchstart") {
//console.log(e);
if(e.type === "touchstart"){
direction = 'up';
return "up";
}else if (e.which === 87 || e.which === 38 || e.which === 32) {
direction = 'up';
return "up";
}
return "";
}
}
window.onkeydown= keys;
window.touchstart= keys;
function init() {
"use strict";
hero1.src = 'images/hero1.png';
hero2.src = 'images/hero2.png';
hero3.src = 'images/hero3.png';
hero4.src = 'images/hero4.png';
hero5.src = 'images/hero5.png';
hero6.src = 'images/hero6.png';
hero7.src = 'images/hero7.png';
hero8.src = 'images/hero8.png';
cloud.src = 'images/cloud.png';
pie.src = 'images/pie.png';
ground.src = 'images/ground.png';
sky.src = 'images/sky.png';
hero1.height = 50;
hero1.width = 35;
window.requestAnimationFrame(draw);
}
var g = 0;
var m = 0;
var hero_frame = 0;
var run_count = 0;
var cloud_arr = [];
var cloud_countX = 0;
var random_cloudY = [];
var random_cloud_size = [];
var pie_arr = [];
var pie_count = 0;
var incriment = 500;
var screen_points = 0;
function reset_game() {
'use strict';
pie_arr = [];
pie_count = 0;
hero_frame = 0;
screen_points = 0;
run_count = 0;
speed = 2;
hero_speed = 3
}
function heroAnimation() {
"use strict";
var hero1X = 25, hero1Y;
hero1Y = canvas.height - hero1.height - 25;
hero_frame = hero_frame + 0.35;
//console.log(hero_frame);
if (hero_frame > (16 * hero_speed)) {
hero_frame = 0;
}
if (hero_frame >= 0 && hero_frame < (2 * hero_speed)) {
ctx.drawImage(hero1, hero1X, hero1Y);
ctx.restore();
} else if (hero_frame >= (2 * hero_speed) && hero_frame < (4 * hero_speed)) {
ctx.drawImage(hero2, hero1X, hero1Y);
ctx.restore();
} else if (hero_frame >= (4 * hero_speed) && hero_frame < (6 * hero_speed)) {
ctx.drawImage(hero3, hero1X, hero1Y);
ctx.restore();
} else if (hero_frame >= (6 * hero_speed) && hero_frame < (8 * hero_speed)) {
ctx.drawImage(hero4, hero1X, hero1Y);
ctx.restore();
} else if (hero_frame >= (8 * hero_speed) && hero_frame < (10 * hero_speed)) {
ctx.drawImage(hero5, hero1X, hero1Y);
ctx.restore();
} else if (hero_frame >= (10 * hero_speed) && hero_frame < (12 * hero_speed)) {
ctx.drawImage(hero6, hero1X, hero1Y);
ctx.restore();
} else if (hero_frame >= (12 * hero_speed) && hero_frame < (14 * hero_speed)) {
ctx.drawImage(hero7, hero1X, hero1Y);
ctx.restore();
} else if (hero_frame >= (14 * hero_speed)) {
ctx.drawImage(hero8, hero1X, hero1Y);
ctx.restore();
}
}
function hero_actions(ctx) {
"use strict";
var hero1X = 25, hero1Y;
hero1Y = canvas.height - hero1.height - 25;
//hero_frame = hero_frame + 4;
//jump
if (direction === 'up') {
ctx.save();
if (g >= 0 && g <= 20 && m === 0) {
g++;
ctx.translate(0, -g * hero_speed);
ctx.drawImage(hero1, hero1X, hero1Y);
ctx.restore();
if (g >= 20) {
m = 1;
}
} else if (m === 1 && g <= 20 && g > 0) {
g--;
ctx.translate(0, -g * hero_speed);
ctx.drawImage(hero1, hero1X, hero1Y);
ctx.restore();
} else if (m === 1 && g < 1) {
m = 0;
g = 0;
direction = '';
ctx.drawImage(hero1, hero1X, hero1Y);
ctx.restore();
}
if (speed === 0) {
reset_game();
}
} else {
heroAnimation();
}
}
function ground_move(ctx) {
"use strict";
run_count = run_count - speed;
ctx.fillStyle = ctx.createPattern(ground, "repeat-x");
ctx.translate(run_count, canvas.height - ground.height);
ctx.fillRect(0, 0, canvas.width * 1000, 50);
ctx.restore();
}
function sky_draw(ctx) {
"use strict";
ctx.save();
ctx.drawImage(sky, 0, 0);
ctx.restore();
}
function isNumber(n) {
"use strict";
return !isNaN(parseFloat(n)) && isFinite(n);
}
function pie_position_calc(num) {
"use strict";
if (isNumber(pie_arr[num])) {
pie_arr[num] = pie_arr[num] - speed;
} else {
pie_arr[num] = 0;
}
return pie_arr[num];
}
function cloud_position_calc(num) {
"use strict";
if (isNumber(cloud_arr[num])) {
cloud_arr[num] = cloud_arr[num] - speed / 6;
} else {
cloud_arr[num] = 0;
random_cloudY[num] = Math.floor((Math.random() * 200) + 1);
random_cloud_size[num] = Math.floor((Math.random() * 50) + 1);
}
return cloud_arr[num];
}
function random_pie() {
"use strict";
var frequency;
if (isNumber(pie_arr[pie_arr.length - 1]) === false) {
frequency = 1;
} else if (pie_arr[pie_arr.length - 1] < -150) {
frequency = 1;
} else {
frequency = 0;
}
if (Math.floor((Math.random() * 2) + 1) === 1 && Math.floor((Math.random() * 10) + 1) === 1 && frequency === 1) {
pie_count++;
}
}
function random_cloud(ctx) {
"use strict";
var random_cloud_frequency = Math.floor((Math.random() * 150) + 1), i;
if (random_cloud_frequency === 1) {
cloud_countX++;
}
for (i = 0; i < cloud_countX; i++) {
ctx.save();
ctx.translate(cloud_position_calc(i), 0);
ctx.drawImage(cloud, 0, 0, 50, 50, canvas.width + cloud.width + 50, random_cloudY[i], random_cloud_size[i], random_cloud_size[i]);
ctx.restore();
}
}
function impact_detection(target_position) {
"use strict";
var impact_pie_start = -550;
if (target_position < impact_pie_start && target_position > impact_pie_start - pie.width && g < 5) {
speed = 0;
hero_speed = 0;
}
}
function pie_draw(ctx) {
"use strict";
var i;
for (i = 0; i < pie_count; i++) {
ctx.save();
ctx.translate(pie_position_calc(i), 0);
if (speed !== 0) {
ctx.drawImage(pie, canvas.width, 350);
}
ctx.restore();
impact_detection(pie_arr[i]);
}
}
function writeMessage(context, message) {
"use strict";
context.font = '10pt Calibri';
context.fillStyle = 'black';
context.fillText(message, 5, 12);
}
function speed_conrol(pts) {
"use strict";
if (pts > incriment) {
speed = speed + (incriment / 500);
incriment = incriment + incriment;
}
}
function points_count(run_pts) {
"use strict";
return Math.round((run_pts * -1) / 100);
}
function draw() {
"use strict";
screen_points = points_count(run_count);
speed_conrol(screen_points);
ctx.clearRect(0, 0, canvas.width, canvas.height); // clear canvas
ctx.save(); //save clear canvar on tick
sky_draw(ctx);
random_cloud(ctx);
ground_move(ctx);
hero_actions(ctx, g);
random_pie();
pie_draw(ctx);
writeMessage(ctx, "Points: " + screen_points);
window.requestAnimationFrame(draw);
}
init();