-
Notifications
You must be signed in to change notification settings - Fork 2
/
sketch.js
379 lines (331 loc) Β· 8.01 KB
/
sketch.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
/*
* Ijo Jetpack π
* remake from Android Jetpack: https://developer.anplayer.com/10/?linkId=58619083 π
* using JavaScript
* Library: p5.js
*
* https://github.com/hilmizul/ijo-jetpack
* https://hilmizul.github.io/ijo-jetpack
*
* 29 Oktober 2018.
* Zul Hilmi
*/
var player;
var capcakes = [];
var donuts = [];
var gingerbreads = [];
var oreos = [];
var bugs = [];
var capcake;
var donut;
var gingerbread;
var oreo;
var bug;
var bg;
var theme;
var pause = false;
var play = true;
let gasCapacity = 100;
var gas = 100;
var distance = 0;
var jump, flying, eat, gameover, hitbug, sfxEat;
var droidImg, cupcakeImg, donutImg, bugImg, oreoImg, base, bgImg, bg2Img, bgNightImg, bg2NightImg, awan;
var font;
function preload() {
droidImg = loadImage('assets/img/droid.png');
capcakeImg = loadImage('assets/img/capcake.png');
donutImg = loadImage('assets/img/donut.png');
gingerbreadImg = loadImage('assets/img/gingerbread.png');
oreoImg = loadImage('assets/img/oreo.png');
bugImg = loadImage('assets/img/bug.png');
bg0 = loadImage('assets/img/jakarta0.png');
bg0_night = loadImage('assets/img/jakarta0-night.png');
bg0_clone = loadImage('assets/img/jakarta0-clone.png');
bg0_night_clone = loadImage('assets/img/jakarta0-night-clone.png');
bg1 = loadImage('assets/img/jakarta1.png');
bg1_night = loadImage('assets/img/jakarta1-night.png');
bg2 = loadImage('assets/img/jakarta2.png');
bg2_night = loadImage('assets/img/jakarta2-night.png');
base = loadImage('assets/img/base.png');
awan = loadImage('assets/img/awan.png');
flying = loadSound('assets/sfx/flying.mp3');
gameover = loadSound('assets/sfx/gameover.mp3');
hitbug = loadSound('assets/sfx/hitbug.mp3');
sfxEat = loadSound('assets/sfx/eat.wav');
font = loadFont('assets/font/FredokaOne.ttf');
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function setup() {
var canvas;
canvas = createCanvas(windowWidth, windowHeight);
canvas.style('z-index', '-1');
theme = color(244, 250, 255);
player = new Player();
capcake = new Capcake();
donut = new Donut();
gingerbread = new GingerBread();
oreo = new Oreo();
bug = new Bugs();
bg = new Latar();
for (let i = 0; i < capcake.total; i++) {
capcakes.push(new Capcake());
}
for (let i = 0; i < donut.total; i++) {
donuts.push(new Donut());
}
for (let i = 0; i < gingerbread.total; i++) {
gingerbreads.push(new GingerBread());
}
for (let i = 0; i < oreo.total; i++) {
oreos.push(new Oreo());
}
for (let i = 0; i < bug.total; i++) {
bugs.push(new Bugs());
}
}
function draw() {
if (play) {
background(theme);
textFont(font);
bg.showSun();
bg.updateSun();
bg.edgeSun();
bg.showAwan();
bg.updateAwan();
bg.edgeAwan();
// bg.showBg2();
// bg.updateBg2();
// bg.edgeBg2();
// bg.showBg1();
// bg.updateBg1();
// bg.edgeBg1();
bg.showBgClone();
bg.updateBgClone();
bg.edgeBgClone();
bg.showBg();
bg.updateBg();
bg.edgeBg();
for (let i = 0; i < width; i++) {
image(base, i, height - base.height, base.width, base.height);
i += base.height;
}
// SELAMA MASIH IDUP, JARAK (DISTANCE) BERTAMAH (meter)
if (frameCount % 30 == 0) {
distance++;
// GAS JUGA BERKURANG... :D
gas--;
}
// NGAMBIL CAPCAKE DARI ARRAY
for (let i = 0; i < capcakes.length; i++) {
capcakes[i].show();
capcakes[i].update();
capcakes[i].edges();
// CEK APAKAH SI ROBOT IJO MAKAN BISKUIT?
if (player.eat(capcakes[i])) {
sfxEat.play();
// JIKA MAKAN, HAPUS MAKANANNYA
capcakes.splice(i, 1);
// BUAT LAGI MAKANAN BARU DENGAN POSISI ACAK
capcakes.push(new Capcake());
if (gas < gasCapacity) {
gas += Math.round(capcakes[i].r / 20);
}
}
}
// NGAMBIL DONUT DARI ARRAY
for (let i = 0; i < donuts.length; i++) {
donuts[i].show();
donuts[i].update();
donuts[i].edges();
// CEK APAKAH SI ROBOT IJO MAKAN BISKUIT?
if (player.eat(donuts[i])) {
sfxEat.play();
// JIKA MAKAN, HAPUS MAKANANNYA
donuts.splice(i, 1);
// BUAT LAGI MAKANAN BARU DENGAN POSISI ACAK
donuts.push(new Donut());
if (gas < gasCapacity) {
gas += Math.round(donuts[i].r / 20);
}
}
}
// NGAMBIL GINGERBREAD DARI ARRAY
for (let i = 0; i < gingerbreads.length; i++) {
gingerbreads[i].show();
gingerbreads[i].update();
gingerbreads[i].edges();
// CEK APAKAH SI ROBOT IJO MAKAN BISKUIT?
if (player.eat(gingerbreads[i])) {
sfxEat.play();
// JIKA MAKAN, HAPUS MAKANANNYA
gingerbreads.splice(i, 1);
// BUAT LAGI MAKANAN BARU DENGAN POSISI ACAK
gingerbreads.push(new GingerBread());
if (gas < gasCapacity) {
gas += Math.round(gingerbreads[i].r / 10);
}
}
}
// NGAMBIL OREO DARI ARRAY
for (let i = 0; i < oreos.length; i++) {
oreos[i].show();
oreos[i].update();
oreos[i].edges();
// CEK APAKAH SI ROBOT IJO MAKAN BISKUIT?
if (player.eat(oreos[i])) {
sfxEat.play();
// JIKA MAKAN, HAPUS MAKANANNYA
oreos.splice(i, 1);
// BUAT LAGI MAKANAN BARU DENGAN POSISI ACAK
oreos.push(new Oreo());
if (gas < gasCapacity) {
gas += Math.round(oreos[i].r / 10);
}
}
}
if (frameCount % 160 == 0) {
bugs.push(new Bugs());
}
// NGAMBIL BUGS DARI ARRAY
for (let i = 0; i < bugs.length; i++) {
bugs[i].show();
bugs[i].update();
if (bugs[i].edges()) {
bugs.splice(i, 1);
}
bugs[i].jiggling();
// CEK APAKAH SI ROBOT IJO MAKAN BISKUIT?
if (player.hit(bugs[i])) {
hitbug.play();
// JIKA MAKAN, HAPUS MAKANANNYA
bugs.splice(i, 1);
// BUAT LAGI MAKANAN BARU DENGAN POSISI ACAK
bugs.push(new Bugs());
if (gas < gasCapacity) {
gas -= 5;
}
}
}
// AKTOR UTAMA :D
player.show();
player.update();
player.jumping();
player.jatohKeTanah();
// LOGIC KAPASITAS GAS
// JIKA KAPASITAS < 1 MAKA GAME OVER dan PLAY SOUND EFFECT NYA
// JIKA GAS SAMA DENGAN KAPASITAS, MAKA GAS PENUH GABOLEH LEBIH 100
// JIKA GAS LEBIH DARI KAPASITAS (SAAT BERTAMBAH) MAKA KEMBALIKAN KE NILAI KAPASITAS (100)
if (gas <= 0) {
play = false;
gas = gas;
} else if (gas >= gasCapacity) {
gas = gasCapacity;
}
// KAPASITAS GAS
// label
push();
fill(0, 100);
stroke(255);
strokeWeight(5);
textSize(30);
textAlign(CENTER);
text(gas, 30, 40);
pop();
// BAR GAS YANG DINAMIS
push();
var barColor;
if (gas < 40) {
barColor = color(250, 0, 0);
} else if (gas < 70) {
barColor = color(250, 250, 0);
} else {
barColor = color(0, 250, 0);
}
fill(barColor);
noStroke();
rect(70, 15, gas * 2, 30, 9);
pop();
// BAR GAS STATIS. (FRAME-NYA CEUNAH)
push();
noFill();
stroke(255);
strokeWeight(5);
rect(70, 15, 100 * 2, 30, 10);
pop();
// DISTANCE DALAM SATUAN m (METER)
push();
fill(0, 100);
stroke(255);
strokeWeight(5);
textSize(50);
textAlign(CENTER);
text(distance + 'm', width - 100, 50);
pop();
// GAME PAUSED
if (pause) {
push();
background(250, 50, 100, 90);
stroke(255);
strokeWeight(10);
fill(250, 70, 150);
textSize(100);
textAlign(CENTER);
text("PAUSE", width / 2, height / 2);
pop();
}
} else {
gameover.play();
push();
background(250, 50, 100, 90);
fill(250, 70, 150);
stroke(255);
strokeWeight(5);
rectMode(CENTER);
rect(width / 2, height / 2 - 50, 700, 400, 20);
stroke(255);
strokeWeight(10);
fill(250, 70, 150);
textSize(100);
textAlign(CENTER);
text('GAME OVER', width / 2, height / 2 - 100);
textSize(50);
text('You Travelled: ' + distance + 'm', width / 2, height / 2);
text('-------------------------', width / 2, height / 2 + 50);
fill(100, 70, 255, 200);
text('PRESS ENTER 2x', width / 2, height / 2 + 120);
pop();
noLoop();
}
}
function keyPressed() {
if (key == ' ') {
if (play) {
flying.play();
player.jump = true;
gas--;
distance++;
}
if (pause) {
flying.stop();
}
}
if (keyCode === ESCAPE) {
pause = true;
noLoop();
} else if (keyCode === RETURN) {
if (!play) {
player.reset();
gas = 100;
distance = 0;
play = true;
} else {
pause = false;
loop();
}
}
}
function keyReleased() {
player.jump = false;
}