/**** * Classes ****/ // Bird class to be created when the dolphin jumps var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }); self.vx = 5; // Horizontal velocity self.update = function () { // Make the bird move horizontally self.x += self.vx; if (self.x > 2048 || self.x < 0) { self.vx *= -1; // Change direction when reaching the screen borders } }; }); // Cloud class to be created when the dolphin jumps var Cloud = Container.expand(function () { var self = Container.call(this); var cloudGraphics = self.attachAsset('cloud', { anchorX: 0.5, anchorY: 0.5 }); self.alpha = 0.5; // Make the cloud transparent self.update = function () { // Make the cloud disappear after a while self.alpha -= 0.01; if (self.alpha <= 0) { self.destroy(); } }; }); // Dolphin class to be thrown var Dolphin = Container.expand(function () { var self = Container.call(this); var dolphinGraphics = self.attachAsset('dolphin', { anchorX: 0.5, anchorY: 0.5 }); self.vy = 0; // Vertical velocity self.update = function () { self.y += self.vy; self.vy += 1; // Gravity effect if (self.y > 2732) { // Reset position if it falls below the screen self.y = 2732; self.vy = 0; } // Make the dolphin spin as it jumps self.rotation += 0.1; }; }); // Mar class to be placed on the ground var Mar = Container.expand(function () { var self = Container.call(this); var marGraphics = self.attachAsset('Mar', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // The mar stays still on the ground }; }); // NormalCloud class to be created in the background var NormalCloud = Container.expand(function () { var self = Container.call(this); var normalCloudGraphics = self.attachAsset('normalcloud', { anchorX: 0.5, anchorY: 0.5 }); self.alpha = 0.5; // Make the normal cloud a little transparent self.vx = -5; // Horizontal velocity self.update = function () { // Make the normal cloud move horizontally self.x += self.vx; if (self.x < 0) { self.x = 2048; // Reset position when reaching the left border } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Whale class to support the dolphin var Whale = Container.expand(function () { var self = Container.call(this); var whaleGraphics = self.attachAsset('whale', { anchorX: 0.5, anchorY: 0.5 }); self.vy = 0; // Vertical velocity self.update = function () { // Make the whale follow the dolphin horizontally self.x = dolphin.x; // Apply gravity to the whale self.y += self.vy; self.vy += 1; // Gravity effect if (self.y > 2500) { // Reset position if it falls below the initial position self.y = 2500; self.vy = 0; } }; self.flatten = function () { // Play a toy sound LK.getSound('toy').play(); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Create NormalCloud instances and distribute them across the sky var normalClouds = []; for (var i = 0; i < 5; i++) { var normalCloud = game.addChild(new NormalCloud()); normalCloud.x = Math.random() * 2048; // Random horizontal position normalCloud.y = Math.random() * 1366; // Random vertical position (only in the upper half of the screen) normalClouds.push(normalCloud); } var whale = game.addChild(new Whale()); whale.x = 1024; // Center horizontally whale.y = 2500; // Near the bottom var dolphin = game.addChild(new Dolphin()); dolphin.x = whale.x; dolphin.y = whale.y - 100; // Position above the whale // Create 10 Mar instances and distribute them across the ground var mars = []; for (var i = 0; i < 10; i++) { var mar = game.addChild(new Mar()); mar.x = i * 204.8; // Distribute horizontally mar.y = 2732; // At the bottom mar.scaleX = 10; // Make them smaller mar.scaleY = 10; // Make them smaller mars.push(mar); } // Event listener for throwing the dolphin game.down = function (x, y, obj) { if (dolphin.y >= whale.y - 100 && Math.abs(dolphin.x - whale.x) < whale.width / 2) { dolphin.vy = -50; // Increase upward velocity if jumping on the whale whale.vy = -20; // Make the whale jump a little LK.getSound('toy').play(); // Play a toy sound } else { dolphin.vy = -20; // Initial upward velocity // Create a cloud at the dolphin's position var cloud = game.addChild(new Cloud()); cloud.x = dolphin.x; cloud.y = dolphin.y; } }; // Create a variable to store the number of birds to be created var numBirds = Math.floor(Math.random() * 2) + 1; // Create Bird instances and distribute them a little more to the middle var birds = []; for (var i = 0; i < numBirds; i++) { var bird = game.addChild(new Bird()); bird.x = Math.random() * (2048 - 500) + 500; // Random position from the left to the right, a little more to the middle bird.y = Math.random() * (2732 - 500 - 500) + 500; // Random position from the top to the bottom, a little more to the middle but not underwater birds.push(bird); } // Play background music LK.playMusic('Jumping_song'); // Update game state game.update = function () { dolphin.update(); whale.update(); birds.forEach(function (bird) { bird.update(); if (dolphin.intersects(bird)) { normalClouds.forEach(function (normalCloud) { normalCloud.update(); }); LK.showGameOver("Oh no! You've hit a bird. Try again!"); } }); if (dolphin.y <= 0) { // Dolphin reached the top LK.showYouWin("Congratulations! You've reached the top! Play again for a new difficulty"); } };
/****
* Classes
****/
// Bird class to be created when the dolphin jumps
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.vx = 5; // Horizontal velocity
self.update = function () {
// Make the bird move horizontally
self.x += self.vx;
if (self.x > 2048 || self.x < 0) {
self.vx *= -1; // Change direction when reaching the screen borders
}
};
});
// Cloud class to be created when the dolphin jumps
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.attachAsset('cloud', {
anchorX: 0.5,
anchorY: 0.5
});
self.alpha = 0.5; // Make the cloud transparent
self.update = function () {
// Make the cloud disappear after a while
self.alpha -= 0.01;
if (self.alpha <= 0) {
self.destroy();
}
};
});
// Dolphin class to be thrown
var Dolphin = Container.expand(function () {
var self = Container.call(this);
var dolphinGraphics = self.attachAsset('dolphin', {
anchorX: 0.5,
anchorY: 0.5
});
self.vy = 0; // Vertical velocity
self.update = function () {
self.y += self.vy;
self.vy += 1; // Gravity effect
if (self.y > 2732) {
// Reset position if it falls below the screen
self.y = 2732;
self.vy = 0;
}
// Make the dolphin spin as it jumps
self.rotation += 0.1;
};
});
// Mar class to be placed on the ground
var Mar = Container.expand(function () {
var self = Container.call(this);
var marGraphics = self.attachAsset('Mar', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// The mar stays still on the ground
};
});
// NormalCloud class to be created in the background
var NormalCloud = Container.expand(function () {
var self = Container.call(this);
var normalCloudGraphics = self.attachAsset('normalcloud', {
anchorX: 0.5,
anchorY: 0.5
});
self.alpha = 0.5; // Make the normal cloud a little transparent
self.vx = -5; // Horizontal velocity
self.update = function () {
// Make the normal cloud move horizontally
self.x += self.vx;
if (self.x < 0) {
self.x = 2048; // Reset position when reaching the left border
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Whale class to support the dolphin
var Whale = Container.expand(function () {
var self = Container.call(this);
var whaleGraphics = self.attachAsset('whale', {
anchorX: 0.5,
anchorY: 0.5
});
self.vy = 0; // Vertical velocity
self.update = function () {
// Make the whale follow the dolphin horizontally
self.x = dolphin.x;
// Apply gravity to the whale
self.y += self.vy;
self.vy += 1; // Gravity effect
if (self.y > 2500) {
// Reset position if it falls below the initial position
self.y = 2500;
self.vy = 0;
}
};
self.flatten = function () {
// Play a toy sound
LK.getSound('toy').play();
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Create NormalCloud instances and distribute them across the sky
var normalClouds = [];
for (var i = 0; i < 5; i++) {
var normalCloud = game.addChild(new NormalCloud());
normalCloud.x = Math.random() * 2048; // Random horizontal position
normalCloud.y = Math.random() * 1366; // Random vertical position (only in the upper half of the screen)
normalClouds.push(normalCloud);
}
var whale = game.addChild(new Whale());
whale.x = 1024; // Center horizontally
whale.y = 2500; // Near the bottom
var dolphin = game.addChild(new Dolphin());
dolphin.x = whale.x;
dolphin.y = whale.y - 100; // Position above the whale
// Create 10 Mar instances and distribute them across the ground
var mars = [];
for (var i = 0; i < 10; i++) {
var mar = game.addChild(new Mar());
mar.x = i * 204.8; // Distribute horizontally
mar.y = 2732; // At the bottom
mar.scaleX = 10; // Make them smaller
mar.scaleY = 10; // Make them smaller
mars.push(mar);
}
// Event listener for throwing the dolphin
game.down = function (x, y, obj) {
if (dolphin.y >= whale.y - 100 && Math.abs(dolphin.x - whale.x) < whale.width / 2) {
dolphin.vy = -50; // Increase upward velocity if jumping on the whale
whale.vy = -20; // Make the whale jump a little
LK.getSound('toy').play(); // Play a toy sound
} else {
dolphin.vy = -20; // Initial upward velocity
// Create a cloud at the dolphin's position
var cloud = game.addChild(new Cloud());
cloud.x = dolphin.x;
cloud.y = dolphin.y;
}
};
// Create a variable to store the number of birds to be created
var numBirds = Math.floor(Math.random() * 2) + 1;
// Create Bird instances and distribute them a little more to the middle
var birds = [];
for (var i = 0; i < numBirds; i++) {
var bird = game.addChild(new Bird());
bird.x = Math.random() * (2048 - 500) + 500; // Random position from the left to the right, a little more to the middle
bird.y = Math.random() * (2732 - 500 - 500) + 500; // Random position from the top to the bottom, a little more to the middle but not underwater
birds.push(bird);
}
// Play background music
LK.playMusic('Jumping_song');
// Update game state
game.update = function () {
dolphin.update();
whale.update();
birds.forEach(function (bird) {
bird.update();
if (dolphin.intersects(bird)) {
normalClouds.forEach(function (normalCloud) {
normalCloud.update();
});
LK.showGameOver("Oh no! You've hit a bird. Try again!");
}
});
if (dolphin.y <= 0) {
// Dolphin reached the top
LK.showYouWin("Congratulations! You've reached the top! Play again for a new difficulty");
}
};