Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
605 lines (515 loc) · 13.5 KB
/
Copy pathscript.js
File metadata and controls
605 lines (515 loc) · 13.5 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/* VARIABLES */
let screen = "home";
let duck;
let duckStage = "egg";
let food = 50;
let happiness = 50;
let health = 50;
let duckImg;
let eggImg;
let ducklingImg;
let roomImg;
let outsideImg;
let breadImg;
let windowImg;
let longOutsideImg;
let bowlImg;
let windowSprite;
let bowl;
let font;
let bread;
let gameState = 1;
let duckImgFlipped;
let score = 0;
let totalScore = 0;
let quack;
let rock;
let coin;
let rockImg;
let coinImg;
let spawn;
let lowerStat;
let statToLower;
let bowlOutlined;
let windowOutlined;
let breadSound;
let taDaAudio;
let bed;
let bedImg;
let bedOutlined;
let sleep = 50;
let notepad;
let notepadImg;
let nightImg;
let timerValue = 0;
let ghostImg;
let ghostFlipped;
let ghost;
let ghostPick;
let windowDarkImg;
let windowDarkOutlinedImg;
let movement;
let duckSleep;
let ducklingSleep;
let ghostNoise;
function preload() {
duckImg = loadImage("assets/duck_eye_open_64 (2).png");
eggImg = loadImage("assets/egg.png");
ducklingImg = loadImage("assets/duckling.png");
roomImg = loadImage("assets/room.png");
outsideImg = loadImage("assets/sunny bg.png");
font = loadFont("assets/BitcountPropSingle-VariableFont_CRSV,ELSH,ELXP,slnt,wght.ttf");
breadImg = loadImage("assets/bread_eye_open (1).png");
duckImgFlipped = loadImage("assets/duck_flipped.png");
windowImg = loadImage("assets/window.png");
bowlImg = loadImage("assets/bowl.png");
quack = loadSound("assets/ytmp3free.cc_quack-sound-effect-youtubemp3free.org.mp3")
longOutsideImg = loadImage("assets/sunny bg long.png");
rockImg = loadImage("assets/New Piskel-1.png (3).png");
coinImg = loadImage("assets/coin.png");
bowlOutlined = loadImage("assets/bowl-outlined.png");
windowOutlined = loadImage("assets/window-outlined.png");
breadSound = loadSound("assets/ytmp3free.cc_toast-falling-over-youtubemp3free.org.mp3");
taDaAudio = loadSound("assets/ytmp3free.cc_ta-da-sound-effects-youtubemp3free.org.mp3");
bedImg = loadImage("assets/bed.png")
bedOutlined = loadImage("assets/bed-outlined.png")
notepadImg = loadImage("assets/notepad.png");
nightImg = loadImage("assets/night bg.png");
ghostImg = loadImage("assets/ghost.png");
ghostFlipped = loadImage("assets/ghost-flipped.png");
windowDarkImg = loadImage("assets/window-dark.png");
windowDarkOutlinedImg = loadImage("assets/window-dark-outline.png");
duckSleep = loadImage("assets/duck-sleep.png");
ducklingSleep = loadImage("assets/duckling-sleeping.png");
ghostNoise = loadSound("assets/ghost noise.mp3");
}
/* SETUP RUNS ONCE */
function setup() {
createCanvas(800,400);
background(224,224,224);
//Create window
windowSprite = new Sprite(windowImg, width/4-100, 125, 148, 148);
windowSprite.collider = "k";
//Create bowl
bowl = new Sprite(bowlImg, width/4+25, 350, 100, 61);
bowl.collider = "k";
//Create bed
bed = new Sprite(bedImg, 75, 350, 128, 75)
bed.collider = "k";
//Create bread
bread = new Sprite(breadImg, width/4, -1000, 32)
bread.rotationLock = true;
//Create duck
duck = new Sprite(eggImg, width/2-50,350, 104, 95);
duck.vel.x = 0;
duck.collider = "k";
duck.overlaps(bowl)
duck.overlaps(bed)
//Create rock
rock = new Sprite(rockImg, 1000, 1000, 50, 50)
rock.rotationLock = true;
rock.collider = "k";
//Create coin
coin = new Sprite(coinImg, 1000, 1000, 50, 50)
coin.rotationLock = true;
coin.collider = "k";
//Create notepad
notepad = new Sprite(notepadImg, 5000, 5000, 100, 200)
//Create ghost
ghost = new Sprite(ghostImg, 1000, 1000, 100, 100);
ghost.rotationlock = true;
ghost.collider = "k";
//Create catcher
catcher = new Sprite(1000,1000,104,95);
catcher.image = duckImg;
catcher.collider = "k";
setInterval(timeIt, 1000)
}
/* DRAW LOOP REPEATS */
function draw() {
if (screen != "happiness") {
image(roomImg, 0, 0, 800, 400);
textFont(font);
textSize(30);
textAlign(LEFT);
text("food: " + food, 425, 50);
text("happiness: " + happiness, 425, 100);
text("sleep: " + sleep, 425, 150)
text("news:", 450, 245);
if (duckStage == "duckling") {
text("Your egg hatched\ninto a duckling!", 450, 275)
}
if (duckStage == "duck") {
text("Your duckling grew\nup to a duck!", 450, 275)
}
}
//Main menu
if (screen == "home") {
//Lower stats randomly
lowerStat = Math.floor(Math.random() * 150);
if (lowerStat == 1) {
statToLower = Math.floor(Math.random() * 3);
if (statToLower == 0) {
food = food - 10;
if (food < 0) {
food = 0;
}
}
else if (statToLower == 1) {
sleep = sleep - 10;
if (sleep < 0) {
sleep = 0;
}
}
else {
happiness = happiness - 10;
if (happiness < 0) {
happiness = 0;
}
}
}
//Have duck move randomly
if (timerValue <= 0) {
movement = Math.floor(Math.random() * 3);
if (movement == 0 || duckStage == "egg") {
duck.vel.x = 0;
}
else if (movement == 1) {
duck.vel.x = 0.5;
if (duckStage == "duck") {
duck.image = duckImgFlipped;
}
}
else {
duck.vel.x = -0.5;
if (duckStage == "duck") {
duck.image = duckImg;
}
}
timerValue = 2;
}
if (duck.x < 50) {
duck.x = 50;
}
if (duck.x > 350) {
duck.x = 350;
}
if (mouseX >= bowl.x-50 && mouseX <= bowl.x+50 &&
mouseY >= bowl.y-31 && mouseY<= bowl.y+31) {
bowl.image = bowlOutlined;
}
else {
bowl.image = bowlImg;
}
if (mouseX >= windowSprite.x-74 && mouseX <= windowSprite.x+74 &&
mouseY >= windowSprite.y-74 && mouseY <= windowSprite.y+74) {
if (sleep <= 30) {
windowSprite.image = windowDarkOutlinedImg;
}
else {
windowSprite.image = windowOutlined;
}
}
else {
if (sleep <= 30) {
windowSprite.image = windowDarkImg;
}
else {
windowSprite.image = windowImg;
}
}
if (mouseX >= bed.x-64 && mouseX <= bed.x+64 &&
mouseY >= bed.y-50 && mouseY <= bed.y+50) {
bed.image = bedOutlined;
}
else {
bed.img = bedImg
}
//Game over sequence if a stat drops below zero
if (food <= 0 || happiness <= 0 || sleep <= 0) {
textSize(40);
hideAllSprites();
color(255,255,255)
notepad.pos = {x: width/4, y: height/2-20};
notepad.draw();
text("Game over :(", 75, height/2-10);
}
if (duck.mouse.presses()) {
quack.play();
}
if (bowl.mouse.presses()) {
leaveHome();
catcher.pos = {x: width/4, y: 350};
if (duckStage == "duck") {
catcher.image = duckImg;
}
else if (duckStage == "duckling") {
catcher.image = ducklingImg;
}
else {
catcher.image = eggImg;
}
resetBread();
gameState = 1;
screen = "food";
}
if (windowSprite.mouse.presses()) {
leaveHome();
duck.pos = {x: 50, y: height/2};
if (duck.image == duckImg) {
duck.image = duckImgFlipped;
}
gameState = 0;
screen = "happiness";
}
if (bed.mouse.presses()) {
leaveHome();
duck.pos = {x: width/4, y: 350}
gameState = 1;
timerValue = 10;
ghost.pos = {x: random(50, width/2-50), y: random(50, height-50)}
screen = "sleep";
}
}
//Food collection game (food)
else if (screen == "food") {
image(outsideImg, 0, 0, 800, 400);
if (gameState == 1) {
textSize(30);
text("Move the duck with the\nleft and right arrow keys \nto catch the falling bread.", width/2 + 15, 30);
fill(0,0,0);
textFont(font);
textSize(30);
textAlign(LEFT);
text("Score: " + score, 20, 50);
//If bread reaches bottom, game over
if (bread.y >= height) {
breadSound.play();
catcher.pos = {x: 1000, y: 1000};
bread.pos = {x: -1000, y: -1000};
bread.vel.y = 0;
food = food + score;
if (food >= 100) {
food = 100;
}
gameState = 2;
}
//Move catcher
if (kb.pressing("left")) {
if (duckStage == "duck") {
catcher.image = duckImg;
}
else {
catcher.image = duck.image;
}
catcher.vel.x = -4;
}
else if (kb.pressing("right")) {
if (duckStage == "duck") {
catcher.image = duckImgFlipped;
}
else {
catcher.image = duck.image;
}
catcher.vel.x = 4;
}
else {
catcher.vel.x = 0;
}
//Stop catcher at the edge of the screen
if (catcher.x < 50) {
catcher.x = 50;
}
else if (catcher.x > 350) {
catcher.x = 350;
}
//If bread collides with catcher, move back to random position at top
if (bread.collides(catcher)) {
score = score + 1
quack.play();
resetBread();
}
}
if (gameState == 2) {
textSize(50);
textAlign(CENTER);
text("Game over", width/4, height/2 - 20);
textSize(20);
text("Press space\nto return.", width/4, height/2 + 5)
if (kb.pressing("space")) {
goHome();
}
}
}
//timing/coordination game (happiness)
else if (screen == "happiness") {
image(longOutsideImg, 0, 0, 800, 400);
textFont(font);
textSize(30);
fill(0,0,0)
textAlign(LEFT);
text("Score: " + score, 20, 50);
if (gameState == 0) {
textSize(30);
textAlign(CENTER);
text("Click the coins as they hit the duck.\nDon't click the rocks!", 300, 125);
textSize(20);
text("Press space to start.", 300, height/2 + 5)
if (kb.pressing("space")) {
respawnCoinRock();
gameState = 1;
}
}
if (gameState == 1) {
if (0 <= coin.x && coin.x <= 150) {
if (mouseIsPressed) {
score = score + 1;
quack.play();
respawnCoinRock();
}
}
if (0 <= rock.x && rock.x <= 150) {
if (mouseIsPressed) {
breadSound.play();
happiness = happiness + score;
if (happiness >= 100) {
happiness = 100;
}
gameState = 2;
rock.x = 1000;
rock.vel.x = 0;
coin.x = 1000;
coin.vel.x = 0;
duck.x = 1000;
}
}
if (coin.x < 0 || rock.x < 0) {
respawnCoinRock();
}
}
if(gameState == 2) {
textSize(50);
textAlign(CENTER);
text("Game over", width/4, height/2 - 20);
textSize(20);
text("Press space\nto return.", width/4, height/2 + 5)
if (kb.pressing("space")) {
goHome();
}
}
}
//point and click game (sleep)
else if (screen == "sleep") {
image(nightImg, 0, 0, 800, 400);
textFont(font);
textSize(30);
fill(0,0,0);
text("Click as many ghosts\nas you can before\ntime runs out!", width/2 + 15, 30);
fill(255,255,255);
textSize(30);
textAlign(LEFT);
text("Score: " + score, 20, 50);
if (duckStage == "duck") {
duck.image = duckSleep;
}
else if (duckStage == "duckling") {
duck.image = ducklingSleep;
}
if (gameState == 1) {
if (timerValue == 0) {
gameState = 2;
}
if (ghost.mouse.presses()) {
//ghostNoise.play();
score++;
ghostPick = Math.floor(Math.random() * 2);
if (ghostPick == 0) {
ghost.image = ghostImg;
}
else {
ghost.image = ghostFlipped;
}
ghost.pos = {x: random(50, width/2-50), y: random(50, height-50)}
}
}
if (gameState == 2) {
ghost.pos = {x: 1000, y: 1000};
textSize(50);
textAlign(CENTER);
text("Time's up!", width/4, height/2 - 20);
textSize(20);
text("Press space\nto return.", width/4, height/2 + 5)
if (kb.pressing("space")) {
score = Math.floor(score / 2);
sleep = sleep + score
fill(0,0,0);
goHome();
}
}
}
}
//Functions
function resetBread() {
bread.y = 0;
bread.x = random(width/2);
bread.vel.y = random(5, 10);
bread.direction = "down";
}
function goHome() {
screen = "home";
timerValue = 2;
checkTotalScore();
windowSprite.pos = {x: width/4-100, y: 125};
duck.pos = {x: width/2-50, y: 350};
bowl.pos = {x: width/4+25, y: 350};
bed.pos = {x: 75, y: 350};
gameState = 1;
}
function leaveHome() {
windowSprite.pos = {x: 1000, y: 1000};
duck.pos = {x: 1000, y: 1000};
duck.vel.x = 0;
bowl.pos = {x: 1000, y: 1000};
bed.pos = {x: 1000, y: 1000};
}
function respawnCoinRock() {
rock.x = 1000;
rock.vel.x = 0;
coin.x = 1000;
coin.vel.x = 0;
spawn = Math.floor(Math.random() * 2);
if (spawn == 0) {
rock.x = width-20;
rock.y = height/2;
rock.vel.x = random(-10, -20);
}
else {
coin.x = width-20;
coin.y = height/2;
coin.vel.x = random(-10, -20);
}
}
function checkTotalScore() {
if ((totalScore + score >= 25 && totalScore < 25) || (totalScore + score >= 75 && totalScore < 75)) {
taDaAudio.play();
}
totalScore = totalScore + score;
score = 0;
if (totalScore >= 75) {
duckStage = "duck";
duck.image = duckImg;
}
else if (totalScore >= 25) {
duckStage = "duckling"
duck.image = ducklingImg;
}
}
function hideAllSprites() {
duck.pos = {x: 1000, y: 1000};
leaveHome();
}
function timeIt() {
if (timerValue > 0) {
timerValue--;
}
}