-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
633 lines (560 loc) · 28.5 KB
/
Copy pathscript.js
File metadata and controls
633 lines (560 loc) · 28.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// DOM Elements
const gameContainer = document.getElementById('game-container');
const scoreDisplay = document.getElementById('score');
const restartBtn = document.getElementById('restart-btn');
const messageBox = document.getElementById('message-box');
// Initial references (will be updated when the level loads)
let bunny = document.getElementById('bunny');
let gate = document.getElementById('gate');
// These arrays store the DOM elements for the collectibles in the current level
let currentCarrots = [];
let currentCabbages = [];
let currentDandelions = [];
let currentLilies = [];
let currentMushrooms = [];
// Variables for the level index, bunny position, speed and score
let currentLevelIndex = 0; // Start at the first level (index 0)
let bunnyX = 50;
let bunnyY = 50;
let bunnySpeed = 10; // Changed to 'let' to allow modification
let score = 0;
let gameOver = false;
let windForce = 0;
let windInterval = 0;
let windTimer = null;
let windDirection = { x: 0, y: 0 };
// Variables for the mushroom effect
let originalBunnyWidth = 64; // Store original bunny width
let originalBunnyHeight = 64; // Store original bunny height
let originalBunnySpeed = 10; // Store original bunny speed
let mushroomEffectTimer = null; // setTimeout ID
// Game boundaries inside the container
let gameWidth = gameContainer.offsetWidth;
let gameHeight = gameContainer.offsetHeight;
// Level data
const levels = [
{
name: "Level 1: The Sunny Meadow",
bunnyStart: { x: 50, y: 50 },
gatePosition: { x: 1100, y: 600 },
collectibles: {
carrots: [
{ x: 600, y: 200 }, { x: 800, y: 200 }, { x: 600, y: 100 },
{ x: 800, y: 100 }, { x: 600, y: 10 }, { x: 300, y: 200 },
{ x: 500, y: 100 }, { x: 300, y: 100 }, { x: 600, y: 400 },
{ x: 900, y: 300 }, { x: 900, y: 100 }, { x: 500, y: 200 },
{ x: 300, y: 500 }, { x: 700, y: 200 }, { x: 500, y: 400 },
{ x: 800, y: 400 }, { x: 600, y: 500 }, { x: 300, y: 600 },
{ x: 500, y: 600 }, { x: 900, y: 600 }, { x: 900, y: 500 },
{ x: 300, y: 10 }, { x: 400, y: 300 }
],
cabbages: [
{ x: 800, y: 10 }, { x: 500, y: 300 }, { x: 400, y: 100 },
{ x: 800, y: 300 }, { x: 700, y: 500 }, { x: 300, y: 400 },
{ x: 400, y: 10 }, { x: 300, y: 300 }, { x: 700, y: 400 },
{ x: 400, y: 600 }, { x: 400, y: 500 }, { x: 700, y: 300 }
],
dandelions: [
{ x: 900, y: 10 }, { x: 700, y: 10 }, { x: 400, y: 200 },
{ x: 800, y: 500 }, { x: 600, y: 300 }, { x: 600, y: 600 },
{ x: 500, y: 500 }, { x: 900, y: 400 }
],
lilies: [
{ x: 900, y: 200 }, { x: 400, y: 400 }, { x: 700, y: 100 },
{ x: 500, y: 10 }, { x: 800, y: 600 }, { x: 700, y: 600 }
],
mushrooms: [] // No mushrooms in level 1
},
scoreToUnlockGate: 20,
backgroundStyle: 'repeating-linear-gradient(#b3ecb3, #a3e1a3 10px)'
},
{
name: "Level 2: The Mushroom Forest",
bunnyStart: { x: 50, y: 600 }, // Start at bottom left
gatePosition: { x: 1100, y: 50 }, // Gate at top right
collectibles: {
carrots: [
{ x: 600, y: 200 }, { x: 800, y: 10 }, { x: 600, y: 100 },
{ x: 800, y: 100 }, { x: 600, y: 10 }, { x: 300, y: 200 },
{ x: 500, y: 100 }, { x: 700, y: 500 }, { x: 600, y: 400 },
{ x: 900, y: 300 }, { x: 900, y: 100 }, { x: 500, y: 200 },
{ x: 300, y: 500 }, { x: 700, y: 200 }, { x: 500, y: 400 },
{ x: 800, y: 400 }, { x: 600, y: 500 }, { x: 300, y: 600 },
{ x: 500, y: 600 }, { x: 900, y: 600 }, { x: 900, y: 500 },
{ x: 300, y: 10 }
],
cabbages: [
{ x: 800, y: 200 }, { x: 500, y: 300 }, { x: 400, y: 100 },
{ x: 800, y: 300 }, { x: 400, y: 600 }, { x: 300, y: 400 },
{ x: 400, y: 10 }, { x: 300, y: 300 }, { x: 700, y: 400 }
],
dandelions: [
{ x: 900, y: 10 }, { x: 700, y: 10 }, { x: 400, y: 200 },
{ x: 800, y: 500 }, { x: 600, y: 300 }, { x: 600, y: 600 },
{ x: 500, y: 500 }, { x: 900, y: 400 }
],
lilies: [
{ x: 900, y: 200 }, { x: 400, y: 400 }, { x: 700, y: 100 },
{ x: 500, y: 10 }, { x: 800, y: 600 }, { x: 700, y: 600 }
],
mushrooms: [
{ x: 700, y: 300 },
{ x: 400, y: 500 },
{ x: 400, y: 300 },
{ x: 300, y: 100 }
]
},
scoreToUnlockGate: 40,
backgroundStyle: 'repeating-linear-gradient(#b3ecb3, #a3e1a3 10px)'
},
{
name: "Level 3: The Snowy Mountain",
bunnyStart: { x: 50, y: 350 }, // Start in middle left
gatePosition: { x: 1100, y: 350 }, // Gate in middle right
collectibles: {
carrots: [
{ x: 600, y: 200 }, { x: 800, y: 200 }, { x: 600, y: 100 },
{ x: 800, y: 100 }, { x: 600, y: 10 }, { x: 300, y: 200 },
{ x: 500, y: 100 }, { x: 300, y: 100 }, { x: 600, y: 400 },
{ x: 900, y: 300 }, { x: 900, y: 100 }, { x: 500, y: 200 },
{ x: 300, y: 500 }, { x: 700, y: 200 }, { x: 500, y: 400 },
{ x: 800, y: 400 }, { x: 600, y: 500 }, { x: 300, y: 600 },
{ x: 500, y: 600 }, { x: 900, y: 600 }, { x: 900, y: 500 },
{ x: 300, y: 10 }, { x: 400, y: 300 }
],
cabbages: [
{ x: 800, y: 10 }, { x: 500, y: 300 }, { x: 400, y: 100 },
{ x: 800, y: 300 }, { x: 700, y: 500 }, { x: 300, y: 400 },
{ x: 400, y: 10 }, { x: 300, y: 300 }, { x: 700, y: 400 },
{ x: 400, y: 600 }, { x: 400, y: 500 }, { x: 700, y: 300 }
],
dandelions: [
{ x: 900, y: 10 }, { x: 700, y: 10 }, { x: 400, y: 200 },
{ x: 800, y: 500 }, { x: 600, y: 300 }, { x: 600, y: 600 },
{ x: 500, y: 500 }, { x: 900, y: 400 }
],
lilies: [
{ x: 900, y: 200 }, { x: 400, y: 400 }, { x: 700, y: 100 },
{ x: 500, y: 10 }, { x: 800, y: 600 }, { x: 700, y: 600 }
],
mushrooms: [] // No mushrooms in level 3
},
scoreToUnlockGate: 60,
slippery: true,
backgroundStyle: 'repeating-linear-gradient(#ffffff, #fffafa 10px)'
},
{
name: "Level 4: The Midnight Marsh",
bunnyStart: { x: 50, y: 600 }, // Start at bottom left
gatePosition: { x: 1100, y: 50 }, // Gate at top right
collectibles: {
carrots: [
{ x: 600, y: 200 }, { x: 800, y: 10 }, { x: 600, y: 100 },
{ x: 800, y: 100 }, { x: 600, y: 10 }, { x: 300, y: 200 },
{ x: 500, y: 100 }, { x: 700, y: 500 }, { x: 600, y: 400 },
{ x: 900, y: 300 }, { x: 900, y: 100 }, { x: 500, y: 200 },
{ x: 300, y: 500 }, { x: 700, y: 200 }, { x: 500, y: 400 },
{ x: 800, y: 400 }, { x: 600, y: 500 }, { x: 300, y: 600 },
{ x: 300, y: 10 }, { x: 900, y: 600 }, { x: 900, y: 500 }
],
cabbages: [
{ x: 700, y: 400 }, { x: 500, y: 300 }, { x: 400, y: 100 },
{ x: 800, y: 300 }, { x: 400, y: 600 }, { x: 300, y: 400 },
{ x: 400, y: 10 }, { x: 300, y: 300 }
],
dandelions: [
{ x: 900, y: 10 }, { x: 700, y: 10 }, { x: 400, y: 200 },
{ x: 800, y: 500 }, { x: 600, y: 300 }, { x: 600, y: 600 },
{ x: 500, y: 500 }
],
lilies: [
{ x: 900, y: 200 }, { x: 400, y: 400 }, { x: 700, y: 100 },
{ x: 500, y: 10 }, { x: 800, y: 600 }
],
mushrooms: [
{ x: 700, y: 300 },
{ x: 400, y: 500 },
{ x: 400, y: 300 },
{ x: 300, y: 100 },
{ x: 500, y: 600 },
{ x: 800, y: 200 },
{ x: 900, y: 400 },
{ x: 700, y: 600 }
],
fireflies: [ { x: 1000, y: 200 }, { x: 200, y: 400 }, { x: 1100, y: 300 },
{ x: 100, y: 500 }, { x: 1000, y: 600 }, { x: 50, y: 600 },
{ x: 1200, y: 100 }, { x: 200, y: 700 }, { x: 1100, y: 500 },
{ x: 100, y: 200}, { x: 50, y: 300 }
]
},
scoreToUnlockGate: 80,
backgroundStyle: 'linear-gradient(to top, #225544 0%, #334466 60%, #112233 100%)'
},
{
name: "Level 5: The Windy Cliffside",
bunnyStart: { x: 50, y: 600 }, // Start at bottom left
gatePosition: { x: 1100, y: 50 }, // Gate at top right
collectibles: {
carrots: [
{ x: 600, y: 200 }, { x: 800, y: 200 }, { x: 600, y: 100 },
{ x: 800, y: 100 }, { x: 600, y: 10 }, { x: 300, y: 200 },
{ x: 500, y: 100 }, { x: 300, y: 100 }, { x: 600, y: 400 },
{ x: 900, y: 300 }, { x: 900, y: 100 }, { x: 500, y: 200 },
{ x: 300, y: 500 }, { x: 700, y: 200 }, { x: 500, y: 400 },
{ x: 800, y: 400 }, { x: 600, y: 500 }, { x: 300, y: 600 },
{ x: 500, y: 600 }, { x: 900, y: 600 }, { x: 900, y: 500 },
{ x: 300, y: 10 }, { x: 400, y: 300 }
],
cabbages: [
{ x: 800, y: 10 }, { x: 500, y: 300 }, { x: 400, y: 100 },
{ x: 800, y: 300 }, { x: 700, y: 500 }, { x: 300, y: 400 },
{ x: 400, y: 10 }, { x: 300, y: 300 }, { x: 700, y: 400 },
{ x: 400, y: 600 }, { x: 400, y: 500 }, { x: 700, y: 300 }
],
dandelions: [
{ x: 900, y: 10 }, { x: 700, y: 10 }, { x: 400, y: 200 },
{ x: 800, y: 500 }, { x: 600, y: 300 }, { x: 600, y: 600 },
{ x: 500, y: 500 }, { x: 900, y: 400 }
],
lilies: [
{ x: 900, y: 200 }, { x: 400, y: 400 }, { x: 700, y: 100 },
{ x: 500, y: 10 }, { x: 800, y: 600 }, { x: 700, y: 600 }
],
mushrooms: [] // No mushrooms in level 5
},
scoreToUnlockGate: 100,
windy: true,
backgroundStyle: 'repeating-linear-gradient(0deg, #deb887 0px, #deb887 20px, #a9a9a9 20px, #a9a9a9 40px, #d2b48c 40px, #f5deb3 60px)'
}
];
// Game functions
// Function to show temporary messages (e.g., level start, game over)
function showMessage(msg, duration = 3000) {
messageBox.textContent = msg;
messageBox.style.display = 'block';
setTimeout(() => {
messageBox.style.display = 'none';
}, duration);
}
// Function to update the score and check whether the gate should be visible or not
function updateScore(amount) {
if (gameOver) return;
score += amount;
scoreDisplay.textContent = 'Score: ' + score;
if (score < 0) {
endGame();
}
// Check whether the gate should become visible based on the current level's score boundaries
const currentLevelData = levels[currentLevelIndex];
if (currentLevelData && score >= currentLevelData.scoreToUnlockGate) {
gate.style.display = 'block';
} else {
gate.style.display = 'none';
}
}
// Function to deal with game over
function endGame() {
gameOver = true;
showMessage('Bunny ate too many poisonous plants! Game Over!');
bunny.style.filter = 'grayscale(100%)';
}
// Function to deal with collision detection for two objects
function checkCollision(rect1, rect2) {
return rect1.left < rect2.right &&
rect1.right > rect2.left &&
rect1.top < rect2.bottom &&
rect1.bottom > rect2.top;
}
// Function to apply the mushroom effect (this shrinks and speeds up bunny for a short time)
function applyMushroomEffect() {
// Clear any existing timer to reset the effect duration
if (mushroomEffectTimer) {
clearTimeout(mushroomEffectTimer);
}
// Apply the shrinking effect (shrink bunny to half its original size)
bunny.style.width = (originalBunnyWidth / 2) + 'px';
bunny.style.height = (originalBunnyHeight / 2) + 'px';
// Apply the speed up effect (bunny moves at double speed)
bunnySpeed = originalBunnySpeed * 2;
// Add the glow effect if on Level 4 (bunny eats glowing mushrooms)
if (currentLevelIndex === 3) {
bunny.classList.add('glow');
}
// Set a timer to revert the mushroom effect after 5 seconds
mushroomEffectTimer = setTimeout(revertMushroomEffect, 5000);
showMessage("Mushroom power! Bunny has shrunk and gained speed!", 2000);
}
// Function to revert mushroom effect, show message and restore original bunny
function revertMushroomEffect() {
bunny.style.width = originalBunnyWidth + 'px';
bunny.style.height = originalBunnyHeight + 'px';
bunnySpeed = originalBunnySpeed;
// Remove the glow effect if it was applied
if (currentLevelIndex === 3) {
bunny.classList.remove('glow');
}
mushroomEffectTimer = null; // Clear the timer ID
showMessage("Mushroom effect wore off.", 1500);
}
// Function to increment the level index and load the next level
function nextLevel() {
loadLevel(currentLevelIndex + 1);
}
// Function to reset the entire game to the first level
function resetGame() {
score = 0; // Reset score on full game restart
revertMushroomEffect(); // Ensure that the mushroom effect is cleared on game restart
if (windTimer) { // Clear the wind timer on game restart
clearTimeout(windTimer);
}
loadLevel(0); // Load the first level
showMessage("Game restarted!"); // Show information about game restarting
};
// Function to apply wind effect
function applyWindEffect() {
if (gameOver) return;
const currentLevelData = levels[currentLevelIndex];
if (currentLevelData.windy) {
// Generate a random wind force and direction
windForce = Math.random() * 5 + 1; // Random force between 1 and 6
windDirection = {
x: Math.random() * 2 - 1, // Random value between -1 and 1
y: Math.random() * 2 - 1
};
// Normalise the vector to ensure a consistent speed regardless of direction
const magnitude = Math.sqrt(windDirection.x * windDirection.x + windDirection.y * windDirection.y);
if (magnitude > 0) {
windDirection.x /= magnitude;
windDirection.y /= magnitude;
}
// Set a timer to change the wind effect again after a few seconds
if (windTimer) {
clearTimeout(windTimer);
}
windTimer = setTimeout(applyWindEffect, Math.random() * 3000 + 2000); // New wind effect every 2-5 seconds
}
}
// Function to load elements for the current level
function loadLevel(levelIndex) {
// Revert any active mushroom effects when loading a new level
revertMushroomEffect();
// Check whether all levels have been completed
if (levelIndex >= levels.length) {
showMessage("Congratulations! You've completed all levels!", 5000);
gameOver = true;
gate.style.display = 'none'; // Hide gate if all levels are done
if (windTimer) {
clearTimeout(windTimer);
}
return;
}
currentLevelIndex = levelIndex;
const levelData = levels[currentLevelIndex]; // Get the data for the new level
// Check if the level is windy and start the wind effect
if (levelData.windy) {
applyWindEffect();
} else if (windTimer) {
// Clear the wind timer if the next level is not windy
clearTimeout(windTimer);
windForce = 0; // Reset wind force
}
gameContainer.style.background = levelData.backgroundStyle;
gameContainer.style.borderColor = levelData.backgroundStyle === '#ffffff' ? '#add8e6' : 'green';
// Clear all collectibles for the previous level from the DOM
const collectiblesLayer = document.getElementById('collectibles-layer');
collectiblesLayer.innerHTML = ''; // Clear all old collectible image elements
// Reset the bunny's position to the start point of the new level
bunnyX = levelData.bunnyStart.x;
bunnyY = levelData.bunnyStart.y;
bunny.style.left = bunnyX + 'px';
bunny.style.top = bunnyY + 'px';
bunny.style.filter = ''; // Ensure bunny is not in grayscale
// Position the gate for the new level
gate.style.left = levelData.gatePosition.x + 'px';
gate.style.top = levelData.gatePosition.y + 'px';
gate.style.display = 'none'; // Gate starts hidden on new level
// Create and position the collectibles for the new level
// Reset the arrays so that they hold references to the new DOM elements
currentCarrots = [];
currentCabbages = [];
currentDandelions = [];
currentLilies = [];
currentMushrooms = [];
// Helper function to create and append a collectible image
const createCollectible = (type, src, x, y) => {
const img = document.createElement('img');
img.src = src;
img.alt = type;
img.classList.add(type); // Add class for styling (e.g., .carrot, .cabbage)
img.style.left = x + 'px';
img.style.top = y + 'px';
collectiblesLayer.appendChild(img); // Add to the collectibles layer
return img; // Return the created image element
};
// Populate the collectible arrays by creating images from levelData
levelData.collectibles.carrots.forEach(pos => {
currentCarrots.push(createCollectible('carrot', 'carrot.png', pos.x, pos.y));
});
levelData.collectibles.cabbages.forEach(pos => {
currentCabbages.push(createCollectible('cabbage', 'cabbage.png', pos.x, pos.y));
});
levelData.collectibles.dandelions.forEach(pos => {
currentDandelions.push(createCollectible('dandelion', 'dandelion.png', pos.x, pos.y));
});
levelData.collectibles.lilies.forEach(pos => {
currentLilies.push(createCollectible('lily', 'lily.png', pos.x, pos.y));
});
// Add mushrooms to the level if applicable
if (levelData.collectibles.mushrooms) {
levelData.collectibles.mushrooms.forEach(pos => {
const mushroomImg = createCollectible('mushroom', 'mushroom.png', pos.x, pos.y);
currentMushrooms.push(mushroomImg);
// Add the glow effect for mushrooms in level 4
if (currentLevelIndex === 3) {
mushroomImg.classList.add('glow');
}
});
}
// Add fireflies for visual effect
if (levelData.collectibles.fireflies) {
levelData.collectibles.fireflies.forEach(pos => {
const firefly = document.createElement('div');
firefly.classList.add('firefly');
firefly.style.left = pos.x + 'px';
firefly.style.top = pos.y + 'px';
collectiblesLayer.appendChild(firefly);
});
}
gameOver = false; // Reset the game over status for the new level
updateScore(0); // Call updateScore to re-evaluate whether gate should be visible according to the new level score boundary
showMessage(`Starting ${levelData.name}!`); // Show the name and number of the new level
// Show the name and number of the new level, then show warnings if needed
const levelNameMsg = `Starting ${levelData.name}!`;
showMessage(levelNameMsg);
// If the level is slippery or windy, show a warning after the level name message disappears
if (levelData.slippery) {
setTimeout(() => {
showMessage("Careful! The snow is slippery!", 4000);
}, 3000); // Wait for the level name message duration (default 3000ms)
}
if (levelData.windy) {
setTimeout(() => {
showMessage("The wind is strong here!", 4000);
}, 3000); // Wait for the level name message duration (default 3000ms)
}
}
// Event listeners
document.addEventListener('keydown', (e) => {
if (gameOver) return;
const keys = ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'];
if (!keys.includes(e.key)) return;
let newX = bunnyX;
let newY = bunnyY;
// Calculate the new position based on which key is pressed
if (e.key === 'ArrowRight') newX += bunnySpeed;
if (e.key === 'ArrowLeft') newX -= bunnySpeed;
if (e.key === 'ArrowDown') newY += bunnySpeed;
if (e.key === 'ArrowUp') newY -= bunnySpeed;
const currentLevelData = levels[currentLevelIndex];
// If the level is slippery, make bunny slide
if (currentLevelData.slippery) {
if (e.key === 'ArrowRight') newX += bunnySpeed / 2;
if (e.key === 'ArrowLeft') newX -= bunnySpeed / 2;
if (e.key === 'ArrowDown') newY += bunnySpeed / 2;
if (e.key === 'ArrowUp') newY -= bunnySpeed / 2;
}
// Add the wind effect for level 5
if (currentLevelData.windy) {
newX += windDirection.x * windForce;
newY += windDirection.y * windForce;
}
// Keep the bunny within the container boundaries
newX = Math.max(0, Math.min(gameWidth - bunny.offsetWidth, newX));
newY = Math.max(0, Math.min(gameHeight - bunny.offsetHeight, newY));
// Update the bunny's position on the screen
bunnyX = newX;
bunnyY = newY;
bunny.style.left = bunnyX + 'px';
bunny.style.top = bunnyY + 'px';
// Add hop animation
bunny.style.transform = 'translateY(-10px)';
setTimeout(() => bunny.style.transform = 'translateY(0)', 100);
// Get current bounding client rect for the bunny to check for collisions with collectibles
const bunnyRect = bunny.getBoundingClientRect();
// Check collisions for all the collectibles in the current level
currentCarrots = currentCarrots.filter(carrot => {
if (checkCollision(bunnyRect, carrot.getBoundingClientRect()) && carrot.style.display !== 'none') {
carrot.style.display = 'none';
updateScore(+1);
return false;
}
return true;
});
currentCabbages = currentCabbages.filter(cabbage => {
if (checkCollision(bunnyRect, cabbage.getBoundingClientRect()) && cabbage.style.display !== 'none') {
cabbage.style.display = 'none';
updateScore(-1);
return false;
}
return true;
});
currentDandelions = currentDandelions.filter(dandelion => {
if (checkCollision(bunnyRect, dandelion.getBoundingClientRect()) && dandelion.style.display !== 'none') {
dandelion.style.display = 'none';
updateScore(+5);
return false;
}
return true;
});
currentLilies = currentLilies.filter(lily => {
if (checkCollision(bunnyRect, lily.getBoundingClientRect()) && lily.style.display !== 'none') {
lily.style.display = 'none';
updateScore(-10);
return false;
}
return true;
});
currentMushrooms = currentMushrooms.filter(mushroom => {
if (checkCollision(bunnyRect, mushroom.getBoundingClientRect()) && mushroom.style.display !== 'none') {
mushroom.style.display = 'none';
updateScore(+2);
applyMushroomEffect(); // Call the new function here!
return false;
}
return true;
});
// Check for a collision with the gate (only if the gate is visible)
if (gate.style.display === 'block') {
const gateRect = gate.getBoundingClientRect();
if (checkCollision(bunnyRect, gateRect)) {
nextLevel();
}
}
});
// Restart Button event listener
restartBtn.addEventListener('click', resetGame);
// Initialisation of the game
// Adjust the game dimensions and bunny position when the window is resized
window.addEventListener('resize', () => {
gameWidth = gameContainer.offsetWidth;
gameHeight = gameContainer.offsetHeight;
// Reposition the bunny if it goes out of bounds during the resize (optional)
bunnyX = Math.max(0, Math.min(gameWidth - bunny.offsetWidth, bunnyX));
bunnyY = Math.max(0, Math.min(gameHeight - bunny.offsetHeight, bunnyY));
bunny.style.left = bunnyX + 'px';
bunny.style.top = bunnyY + 'px';
});
// Store the original bunny dimensions and speed on initial load
bunny.onload = () => { // Ensure image is loaded before getting dimensions
originalBunnyWidth = bunny.offsetWidth;
originalBunnyHeight = bunny.offsetHeight;
originalBunnySpeed = bunnySpeed; // Store initial speed
loadLevel(currentLevelIndex); // Start the game by loading the first level
};
// Fallback in case image loads before the loading event
if (bunny.complete) {
originalBunnyWidth = bunny.offsetWidth;
originalBunnyHeight = bunny.offsetHeight;
originalBunnySpeed = bunnySpeed;
loadLevel(currentLevelIndex);
}