/**** * Classes ****/ var Boat = Container.expand(function () { var self = Container.call(this); var boatGraphics = self.attachAsset('boat', { anchorX: 0.5, anchorY: 0.5 }); self.speed = boatStartingSpeed; self.setSpeed = function () { var angle = Math.random() * 2 * Math.PI; // Random angle between 0 and 2*PI self.speedX = self.speed * Math.cos(angle); self.speedY = self.speed * Math.sin(angle); self.directionX = self.speedX >= 0 ? 1 : -1; self.directionY = self.speedY >= 0 ? 1 : -1; }; self.setSpeed(); self.contains = function (x, y) { var dx = x - self.x; var dy = y - self.y; var halfWidth = boatGraphics.width / 2; var halfHeight = boatGraphics.height / 2; return dx >= -halfWidth && dx <= halfWidth && dy >= -halfHeight && dy <= halfHeight; }; self.update = function () { // If the boat hits the edge of the screen, change direction and increase speed if (self.x - boatGraphics.width / 2 < 0) { self.x = boatGraphics.width / 2; self.speedX = -self.speedX; self.speed += 1; self.directionX = self.speedX >= 0 ? 1 : -1; } else if (self.x + boatGraphics.width / 2 > 2048) { self.x = 2048 - boatGraphics.width / 2; self.speedX = -self.speedX; self.speed += 1; self.directionX = self.speedX >= 0 ? 1 : -1; } if (self.y - boatGraphics.height / 2 < 0) { self.y = boatGraphics.height / 2; self.speedY = -self.speedY; self.speed += 1; self.directionY = self.speedY >= 0 ? 1 : -1; } else if (self.y + boatGraphics.height / 2 > 2732) { self.y = 2732 - boatGraphics.height / 2; self.speedY = -self.speedY; self.speed += 1; self.directionY = self.speedY >= 0 ? 1 : -1; } // Continue moving the boat in the current direction self.x += self.speedX; self.y += self.speedY; self.rotation = Math.atan2(self.speedY, self.speedX); }; }); //<Assets used in the game will automatically appear here> // Class for the circle objects var Circle = Container.expand(function () { var self = Container.call(this); var circleGraphics = self.attachAsset('character', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 0; self.direction = 1; self.animationTimer = 0; self.flipped = false; self.update = function () { if (self === movingCircle) { self.animationTimer++; if (self.animationTimer >= 155) { // 100 milliseconds at 60 FPS self.flipped = !self.flipped; self.animationTimer = 0; if (self.flipped) { self.removeChild(circleGraphics); circleGraphics = self.attachAsset('character_flipped', { anchorX: 0.5, anchorY: 0.5 }); } else { self.removeChild(circleGraphics); circleGraphics = self.attachAsset('character', { anchorX: 0.5, anchorY: 0.5 }); } } } if (self.direction == 1) { self.rotation -= self.speed * self.direction; } else { self.rotation += self.speed; } }; self.contains = function (x, y) { var dx = x - self.x; var dy = y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); return distance < circleGraphics.width / 2; }; }); var GO = Container.expand(function () { var self = Container.call(this); var goGraphics = self.attachAsset('GO', { anchorX: 0.5, anchorY: 0.5 }); goGraphics.width = 2048; goGraphics.height = 2732; self.x = 2048 / 2; self.y = 2732 / 2; }); // Class for the swimmer objects var Swimmer = Container.expand(function () { var self = Container.call(this); var swimmerGraphics = self.attachAsset('swimmer', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.angle = Math.random() * Math.PI * 2; self.changeAngleTimer = 0; self.animationTimer = 0; self.flipped = false; self.update = function () { // Calculate the direction to the nearest circle var dx1 = circle1.x - self.x; var dy1 = circle1.y - self.y; var dx2 = circle2.x - self.x; var dy2 = circle2.y - self.y; var dist1 = Math.sqrt(dx1 * dx1 + dy1 * dy1); var dist2 = Math.sqrt(dx2 * dx2 + dy2 * dy2); var dx, dy; if (dist1 < dist2) { dx = dx1; dy = dy1; } else { dx = dx2; dy = dy2; } // Check if the nearest circle is within the swimmer's radius if (dist1 < 600 || dist2 < 600) { // Move the swimmer away from the nearest circle self.angle = Math.atan2(dy, dx) + Math.PI; } else { // Continue moving randomly self.angle += (Math.random() - 0.5) * Math.PI / 4; } self.x += Math.cos(self.angle) * self.speed; self.y += Math.sin(self.angle) * self.speed; // Update swimmer rotation to point towards the direction it's moving // Use a lerp function to smooth out the rotation and prevent jittering // Limit the rotation speed to a certain limit var lerpFactor = 0.05; var rotationChange = (self.angle - self.rotation) * lerpFactor; var maxRotationChange = 0.05; if (Math.abs(rotationChange) > maxRotationChange) { rotationChange = maxRotationChange * Math.sign(rotationChange); } self.rotation += rotationChange; // Bounce off the edges of the screen if (self.x - swimmerGraphics.width / 2 < 0) { self.x = swimmerGraphics.width / 2; self.angle = Math.PI - self.angle; self.rotation += (self.angle - self.rotation) * lerpFactor / 10; } else if (self.x + swimmerGraphics.width / 2 > 2048) { self.x = 2048 - swimmerGraphics.width / 2; self.angle = Math.PI - self.angle; self.rotation += (self.angle - self.rotation) * lerpFactor / 10; } if (self.y - swimmerGraphics.height / 2 < 0) { self.y = swimmerGraphics.height / 2; self.angle = -self.angle; self.rotation += (self.angle - self.rotation) * lerpFactor / 10; } else if (self.y + swimmerGraphics.height / 2 > 2732) { self.y = 2732 - swimmerGraphics.height / 2; self.angle = -self.angle; self.rotation += (self.angle - self.rotation) * lerpFactor / 10; } // Change the angle slightly every 10 seconds self.changeAngleTimer++; self.animationTimer++; if (self.changeAngleTimer >= 600) { self.angle += (Math.random() - 0.5) * Math.PI / 10; self.changeAngleTimer = 0; } if (self.animationTimer >= 60) { self.flipped = !self.flipped; self.animationTimer = 0; if (self.flipped) { self.removeChild(swimmerGraphics); swimmerGraphics = self.attachAsset('swimmer_flipped', { anchorX: 0.5, anchorY: 0.5 }); } else { self.removeChild(swimmerGraphics); swimmerGraphics = self.attachAsset('swimmer', { anchorX: 0.5, anchorY: 0.5 }); } } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var boatStartingSpeed = 5; // Initialize a variable to store the value 200 var GOContainer = new Container(); game.addChild(GOContainer); GOContainer.visible = false; var BackgroundContainer = new Container(); var backgroundGraphics = BackgroundContainer.attachAsset('background', { anchorX: 0.5, anchorY: 0.5, width: 2048, height: 2732 }); backgroundGraphics.x = 2048 / 2; backgroundGraphics.y = 2732 / 2; game.addChild(BackgroundContainer); var MidgroundContainer = new Container(); game.addChild(MidgroundContainer); var ForegroundContainer = new Container(); game.addChild(ForegroundContainer); // Create a score text var scoreText = new Text2('0', { size: 180, fill: "#ffffff", stroke: "#000000", strokeThickness: 10 }); scoreText.anchor.set(0.5, 0.5); // Sets anchor to the center of the text. scoreText.x = 2048 / 2; // Position the score text in the center of the screen horizontally. scoreText.y = 2732 / 2; // Position the score text in the center of the screen vertically. MidgroundContainer.addChild(scoreText); // Attach the score text to the MidgroundContainer var circleDistance = 500; // Initialize circle objects var circle1 = new Circle(); circle1.direction = -1; circle1.x = 2048 / 2; circle1.y = 2732 / 2; var circle2 = new Circle(); circle2.x = circle1.x; circle2.y = circle1.y - circleDistance; var circleSpeed = 0.02; var movingCircle = circle2; // Initialize the moving circle circle2.speed = circleSpeed; ForegroundContainer.addChild(circle1); ForegroundContainer.addChild(circle2); // Initialize swimmer objects // Create an array to store the swimmers var swimmers = []; var swimmer1 = new Swimmer(); swimmer1.x = Math.random() * 2048; swimmer1.y = Math.random() * 2732; MidgroundContainer.addChild(swimmer1); swimmers.push(swimmer1); var boat = new Boat(); boat.setSpeed(); boat.x = 2048 / 2; boat.y = 2732 / 2 - 1000; ForegroundContainer.addChild(boat); // Create a timer to spawn swimmers every 3 seconds var spawnSwimmerTimer = 0; // Handle character movement var dragNode = null; function handleMove(x, y, obj) { if (dragNode) { dragNode.x = x; dragNode.y = y; } } game.move = handleMove; game.down = function (x, y, obj) { // Switch the movement of the circle objects if (circle1.speed == 0) { circle1.speed = circleSpeed; circle2.speed = 0; circle1.x = circle2.storedX; circle1.y = circle2.storedY; circle1.rotation = circle2.storedAngle; movingCircle = circle1; } else { movingCircle = circle2; circle1.speed = 0; circle2.speed = circleSpeed; circle2.x = circle1.storedX; circle2.y = circle1.storedY; circle2.rotation = circle1.storedAngle; } }; game.up = function (x, y, obj) { dragNode = null; }; // Update game logic game.update = function () { // Update circle objects circle1.update(); circle2.update(); // Update the timer spawnSwimmerTimer++; // If 2 seconds have passed and there are less than 5 swimmers, spawn a new swimmer at the boat's position if (spawnSwimmerTimer >= 150 && swimmers.length < 20) { // Play the Spawn sound LK.getSound('Spawn').play(); var newSwimmer = new Swimmer(); newSwimmer.x = boat.x; newSwimmer.y = boat.y; MidgroundContainer.addChild(newSwimmer); swimmers.push(newSwimmer); spawnSwimmerTimer = 0; } // Check if either circle intersects with any swimmer for (var i = swimmers.length - 1; i >= 0; i--) { if (circle1.intersects(swimmers[i]) || circle2.intersects(swimmers[i])) { // Display blood image at swimmer's position var blood = LK.getAsset('blood', { anchorX: 0.5, anchorY: 0.5, x: swimmers[i].x, y: swimmers[i].y }); MidgroundContainer.addChild(blood); // Play the Eat sound LK.getSound('Eat').play(); // Remove the swimmer from the game MidgroundContainer.removeChild(swimmers[i]); swimmers.splice(i, 1); // Increment the score LK.setScore(LK.getScore() + 1); // Play the Horror sound if the score is a multiple of 5, starting from 0 if (LK.getScore() % 5 === 0 && LK.getScore() !== 0) { LK.getSound('Horror').play(); } // Update the score text scoreText.setText(LK.getScore()); // Increase the size of the score text by 50% scoreText.scale.set(1.2, 1.2); // Set a timeout to revert the size back to normal after 100 milliseconds LK.setTimeout(function () { scoreText.scale.set(1, 1); }, 100); // Gradually fade out the blood image over 500 milliseconds var fadeOutDuration = 800; var fadeOutInterval = 80; var fadeOutSteps = fadeOutDuration / fadeOutInterval; var fadeOutStep = 1 / fadeOutSteps; var fadeOutTimer = LK.setInterval(function () { blood.alpha -= fadeOutStep; if (blood.alpha <= 0) { MidgroundContainer.removeChild(blood); LK.clearInterval(fadeOutTimer); } }, fadeOutInterval); } } // Check if the exact center of the character's asset intersects with the boat var characterCenter1 = { x: circle1.x, y: circle1.y }; var characterCenter2 = { x: circle2.x, y: circle2.y }; if (boat.contains(characterCenter1.x, characterCenter1.y) || boat.contains(characterCenter2.x, characterCenter2.y)) { // Hide all other gameplay elements BackgroundContainer.visible = false; MidgroundContainer.visible = false; ForegroundContainer.visible = false; // Disable all gameplay interactions game.move = null; game.down = null; game.up = null; game.update = null; // Display the GO asset var go = new GO(); GOContainer.addChild(go); GOContainer.visible = true; // Play the GO sound LK.getSound('GO').play(); // Set a timeout to show game over after 2 seconds LK.setTimeout(function () { // Re-enable gameplay interactions game.move = handleMove; game.down = function (x, y, obj) { // Switch the movement of the circle objects if (circle1.speed == 0) { circle1.speed = circleSpeed; circle2.speed = 0; circle1.x = circle2.storedX; circle1.y = circle2.storedY; circle1.rotation = circle2.storedAngle; movingCircle = circle1; } else { movingCircle = circle2; circle1.speed = 0; circle2.speed = circleSpeed; circle2.x = circle1.storedX; circle2.y = circle1.storedY; circle2.rotation = circle1.storedAngle; } }; game.up = function (x, y, obj) { dragNode = null; }; game.update = function () { // Update circle objects circle1.update(); circle2.update(); // Update the timer spawnSwimmerTimer++; // If 2 seconds have passed and there are less than 5 swimmers, spawn a new swimmer at the boat's position if (spawnSwimmerTimer >= 150 && swimmers.length < 20) { // Play the Spawn sound LK.getSound('Spawn').play(); var newSwimmer = new Swimmer(); newSwimmer.x = boat.x; newSwimmer.y = boat.y; MidgroundContainer.addChild(newSwimmer); swimmers.push(newSwimmer); spawnSwimmerTimer = 0; } // Check if either circle intersects with any swimmer for (var i = swimmers.length - 1; i >= 0; i--) { if (circle1.intersects(swimmers[i]) || circle2.intersects(swimmers[i])) { // Display blood image at swimmer's position var blood = LK.getAsset('blood', { anchorX: 0.5, anchorY: 0.5, x: swimmers[i].x, y: swimmers[i].y }); MidgroundContainer.addChild(blood); // Play the Eat sound LK.getSound('Eat').play(); // Remove the swimmer from the game MidgroundContainer.removeChild(swimmers[i]); swimmers.splice(i, 1); // Increment the score LK.setScore(LK.getScore() + 1); // Play the Horror sound if the score is a multiple of 5, starting from 0 if (LK.getScore() % 3 === 0 && LK.getScore() !== 0) { LK.getSound('Horror').play(); } // Update the score text scoreText.setText(LK.getScore()); // Increase the size of the score text by 50% scoreText.scale.set(1.2, 1.2); // Set a timeout to revert the size back to normal after 100 milliseconds LK.setTimeout(function () { scoreText.scale.set(1, 1); }, 100); // Gradually fade out the blood image over 500 milliseconds var fadeOutDuration = 800; var fadeOutInterval = 80; var fadeOutSteps = fadeOutDuration / fadeOutInterval; var fadeOutStep = 1 / fadeOutSteps; var fadeOutTimer = LK.setInterval(function () { blood.alpha -= fadeOutStep; if (blood.alpha <= 0) { MidgroundContainer.removeChild(blood); LK.clearInterval(fadeOutTimer); } }, fadeOutInterval); } } // Check if the exact center of the character's asset intersects with the boat var characterCenter1 = { x: circle1.x, y: circle1.y }; var characterCenter2 = { x: circle2.x, y: circle2.y }; if (boat.contains(characterCenter1.x, characterCenter1.y) || boat.contains(characterCenter2.x, characterCenter2.y)) { // Hide all other gameplay elements BackgroundContainer.visible = false; MidgroundContainer.visible = false; ForegroundContainer.visible = false; // Disable all gameplay interactions game.move = null; game.down = null; game.up = null; game.update = null; // Display the GO asset var go = new GO(); GOContainer.addChild(go); GOContainer.visible = true; // Set a timeout to show game over after 2 seconds LK.setTimeout(function () { LK.showGameOver(); }, 2000); } // Calculate the new positions of the circle objects if (circle1.speed != 0) { circle1.x = circle2.x - circleDistance * Math.cos(circle1.rotation); circle1.y = circle2.y - circleDistance * Math.sin(circle1.rotation); circle1.storedX = circle1.x; circle1.storedY = circle1.y; circle1.storedAngle = circle1.rotation; } else { circle2.x = circle1.x + circleDistance * Math.cos(circle2.rotation); circle2.y = circle1.y + circleDistance * Math.sin(circle2.rotation); circle2.storedX = circle2.x; circle2.storedY = circle2.y; circle2.storedAngle = circle2.rotation; } // Check if both circles are outside the screen if ((circle1.x < 0 || circle1.x > 2048 || circle1.y < 0 || circle1.y > 2732) && (circle2.x < 0 || circle2.x > 2048 || circle2.y < 0 || circle2.y > 2732)) { // Show game over. The game will be automatically paused while game over is showing. LK.showGameOver(); } }; LK.showGameOver(); }, 2000); } // Calculate the new positions of the circle objects if (circle1.speed != 0) { circle1.x = circle2.x - circleDistance * Math.cos(circle1.rotation); circle1.y = circle2.y - circleDistance * Math.sin(circle1.rotation); circle1.storedX = circle1.x; circle1.storedY = circle1.y; circle1.storedAngle = circle1.rotation; } else { circle2.x = circle1.x + circleDistance * Math.cos(circle2.rotation); circle2.y = circle1.y + circleDistance * Math.sin(circle2.rotation); circle2.storedX = circle2.x; circle2.storedY = circle2.y; circle2.storedAngle = circle2.rotation; } // Check if both circles are outside the screen if ((circle1.x < 0 || circle1.x > 2048 || circle1.y < 0 || circle1.y > 2732) && (circle2.x < 0 || circle2.x > 2048 || circle2.y < 0 || circle2.y > 2732)) { // Show game over. The game will be automatically paused while game over is showing. LK.showGameOver(); } };
/****
* Classes
****/
var Boat = Container.expand(function () {
var self = Container.call(this);
var boatGraphics = self.attachAsset('boat', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = boatStartingSpeed;
self.setSpeed = function () {
var angle = Math.random() * 2 * Math.PI; // Random angle between 0 and 2*PI
self.speedX = self.speed * Math.cos(angle);
self.speedY = self.speed * Math.sin(angle);
self.directionX = self.speedX >= 0 ? 1 : -1;
self.directionY = self.speedY >= 0 ? 1 : -1;
};
self.setSpeed();
self.contains = function (x, y) {
var dx = x - self.x;
var dy = y - self.y;
var halfWidth = boatGraphics.width / 2;
var halfHeight = boatGraphics.height / 2;
return dx >= -halfWidth && dx <= halfWidth && dy >= -halfHeight && dy <= halfHeight;
};
self.update = function () {
// If the boat hits the edge of the screen, change direction and increase speed
if (self.x - boatGraphics.width / 2 < 0) {
self.x = boatGraphics.width / 2;
self.speedX = -self.speedX;
self.speed += 1;
self.directionX = self.speedX >= 0 ? 1 : -1;
} else if (self.x + boatGraphics.width / 2 > 2048) {
self.x = 2048 - boatGraphics.width / 2;
self.speedX = -self.speedX;
self.speed += 1;
self.directionX = self.speedX >= 0 ? 1 : -1;
}
if (self.y - boatGraphics.height / 2 < 0) {
self.y = boatGraphics.height / 2;
self.speedY = -self.speedY;
self.speed += 1;
self.directionY = self.speedY >= 0 ? 1 : -1;
} else if (self.y + boatGraphics.height / 2 > 2732) {
self.y = 2732 - boatGraphics.height / 2;
self.speedY = -self.speedY;
self.speed += 1;
self.directionY = self.speedY >= 0 ? 1 : -1;
}
// Continue moving the boat in the current direction
self.x += self.speedX;
self.y += self.speedY;
self.rotation = Math.atan2(self.speedY, self.speedX);
};
});
//<Assets used in the game will automatically appear here>
// Class for the circle objects
var Circle = Container.expand(function () {
var self = Container.call(this);
var circleGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.direction = 1;
self.animationTimer = 0;
self.flipped = false;
self.update = function () {
if (self === movingCircle) {
self.animationTimer++;
if (self.animationTimer >= 155) {
// 100 milliseconds at 60 FPS
self.flipped = !self.flipped;
self.animationTimer = 0;
if (self.flipped) {
self.removeChild(circleGraphics);
circleGraphics = self.attachAsset('character_flipped', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
self.removeChild(circleGraphics);
circleGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
}
}
}
if (self.direction == 1) {
self.rotation -= self.speed * self.direction;
} else {
self.rotation += self.speed;
}
};
self.contains = function (x, y) {
var dx = x - self.x;
var dy = y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
return distance < circleGraphics.width / 2;
};
});
var GO = Container.expand(function () {
var self = Container.call(this);
var goGraphics = self.attachAsset('GO', {
anchorX: 0.5,
anchorY: 0.5
});
goGraphics.width = 2048;
goGraphics.height = 2732;
self.x = 2048 / 2;
self.y = 2732 / 2;
});
// Class for the swimmer objects
var Swimmer = Container.expand(function () {
var self = Container.call(this);
var swimmerGraphics = self.attachAsset('swimmer', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.angle = Math.random() * Math.PI * 2;
self.changeAngleTimer = 0;
self.animationTimer = 0;
self.flipped = false;
self.update = function () {
// Calculate the direction to the nearest circle
var dx1 = circle1.x - self.x;
var dy1 = circle1.y - self.y;
var dx2 = circle2.x - self.x;
var dy2 = circle2.y - self.y;
var dist1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
var dist2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
var dx, dy;
if (dist1 < dist2) {
dx = dx1;
dy = dy1;
} else {
dx = dx2;
dy = dy2;
}
// Check if the nearest circle is within the swimmer's radius
if (dist1 < 600 || dist2 < 600) {
// Move the swimmer away from the nearest circle
self.angle = Math.atan2(dy, dx) + Math.PI;
} else {
// Continue moving randomly
self.angle += (Math.random() - 0.5) * Math.PI / 4;
}
self.x += Math.cos(self.angle) * self.speed;
self.y += Math.sin(self.angle) * self.speed;
// Update swimmer rotation to point towards the direction it's moving
// Use a lerp function to smooth out the rotation and prevent jittering
// Limit the rotation speed to a certain limit
var lerpFactor = 0.05;
var rotationChange = (self.angle - self.rotation) * lerpFactor;
var maxRotationChange = 0.05;
if (Math.abs(rotationChange) > maxRotationChange) {
rotationChange = maxRotationChange * Math.sign(rotationChange);
}
self.rotation += rotationChange;
// Bounce off the edges of the screen
if (self.x - swimmerGraphics.width / 2 < 0) {
self.x = swimmerGraphics.width / 2;
self.angle = Math.PI - self.angle;
self.rotation += (self.angle - self.rotation) * lerpFactor / 10;
} else if (self.x + swimmerGraphics.width / 2 > 2048) {
self.x = 2048 - swimmerGraphics.width / 2;
self.angle = Math.PI - self.angle;
self.rotation += (self.angle - self.rotation) * lerpFactor / 10;
}
if (self.y - swimmerGraphics.height / 2 < 0) {
self.y = swimmerGraphics.height / 2;
self.angle = -self.angle;
self.rotation += (self.angle - self.rotation) * lerpFactor / 10;
} else if (self.y + swimmerGraphics.height / 2 > 2732) {
self.y = 2732 - swimmerGraphics.height / 2;
self.angle = -self.angle;
self.rotation += (self.angle - self.rotation) * lerpFactor / 10;
}
// Change the angle slightly every 10 seconds
self.changeAngleTimer++;
self.animationTimer++;
if (self.changeAngleTimer >= 600) {
self.angle += (Math.random() - 0.5) * Math.PI / 10;
self.changeAngleTimer = 0;
}
if (self.animationTimer >= 60) {
self.flipped = !self.flipped;
self.animationTimer = 0;
if (self.flipped) {
self.removeChild(swimmerGraphics);
swimmerGraphics = self.attachAsset('swimmer_flipped', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
self.removeChild(swimmerGraphics);
swimmerGraphics = self.attachAsset('swimmer', {
anchorX: 0.5,
anchorY: 0.5
});
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var boatStartingSpeed = 5;
// Initialize a variable to store the value 200
var GOContainer = new Container();
game.addChild(GOContainer);
GOContainer.visible = false;
var BackgroundContainer = new Container();
var backgroundGraphics = BackgroundContainer.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
width: 2048,
height: 2732
});
backgroundGraphics.x = 2048 / 2;
backgroundGraphics.y = 2732 / 2;
game.addChild(BackgroundContainer);
var MidgroundContainer = new Container();
game.addChild(MidgroundContainer);
var ForegroundContainer = new Container();
game.addChild(ForegroundContainer);
// Create a score text
var scoreText = new Text2('0', {
size: 180,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 10
});
scoreText.anchor.set(0.5, 0.5); // Sets anchor to the center of the text.
scoreText.x = 2048 / 2; // Position the score text in the center of the screen horizontally.
scoreText.y = 2732 / 2; // Position the score text in the center of the screen vertically.
MidgroundContainer.addChild(scoreText); // Attach the score text to the MidgroundContainer
var circleDistance = 500;
// Initialize circle objects
var circle1 = new Circle();
circle1.direction = -1;
circle1.x = 2048 / 2;
circle1.y = 2732 / 2;
var circle2 = new Circle();
circle2.x = circle1.x;
circle2.y = circle1.y - circleDistance;
var circleSpeed = 0.02;
var movingCircle = circle2; // Initialize the moving circle
circle2.speed = circleSpeed;
ForegroundContainer.addChild(circle1);
ForegroundContainer.addChild(circle2);
// Initialize swimmer objects
// Create an array to store the swimmers
var swimmers = [];
var swimmer1 = new Swimmer();
swimmer1.x = Math.random() * 2048;
swimmer1.y = Math.random() * 2732;
MidgroundContainer.addChild(swimmer1);
swimmers.push(swimmer1);
var boat = new Boat();
boat.setSpeed();
boat.x = 2048 / 2;
boat.y = 2732 / 2 - 1000;
ForegroundContainer.addChild(boat);
// Create a timer to spawn swimmers every 3 seconds
var spawnSwimmerTimer = 0;
// Handle character movement
var dragNode = null;
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
// Switch the movement of the circle objects
if (circle1.speed == 0) {
circle1.speed = circleSpeed;
circle2.speed = 0;
circle1.x = circle2.storedX;
circle1.y = circle2.storedY;
circle1.rotation = circle2.storedAngle;
movingCircle = circle1;
} else {
movingCircle = circle2;
circle1.speed = 0;
circle2.speed = circleSpeed;
circle2.x = circle1.storedX;
circle2.y = circle1.storedY;
circle2.rotation = circle1.storedAngle;
}
};
game.up = function (x, y, obj) {
dragNode = null;
};
// Update game logic
game.update = function () {
// Update circle objects
circle1.update();
circle2.update();
// Update the timer
spawnSwimmerTimer++;
// If 2 seconds have passed and there are less than 5 swimmers, spawn a new swimmer at the boat's position
if (spawnSwimmerTimer >= 150 && swimmers.length < 20) {
// Play the Spawn sound
LK.getSound('Spawn').play();
var newSwimmer = new Swimmer();
newSwimmer.x = boat.x;
newSwimmer.y = boat.y;
MidgroundContainer.addChild(newSwimmer);
swimmers.push(newSwimmer);
spawnSwimmerTimer = 0;
}
// Check if either circle intersects with any swimmer
for (var i = swimmers.length - 1; i >= 0; i--) {
if (circle1.intersects(swimmers[i]) || circle2.intersects(swimmers[i])) {
// Display blood image at swimmer's position
var blood = LK.getAsset('blood', {
anchorX: 0.5,
anchorY: 0.5,
x: swimmers[i].x,
y: swimmers[i].y
});
MidgroundContainer.addChild(blood);
// Play the Eat sound
LK.getSound('Eat').play();
// Remove the swimmer from the game
MidgroundContainer.removeChild(swimmers[i]);
swimmers.splice(i, 1);
// Increment the score
LK.setScore(LK.getScore() + 1);
// Play the Horror sound if the score is a multiple of 5, starting from 0
if (LK.getScore() % 5 === 0 && LK.getScore() !== 0) {
LK.getSound('Horror').play();
}
// Update the score text
scoreText.setText(LK.getScore());
// Increase the size of the score text by 50%
scoreText.scale.set(1.2, 1.2);
// Set a timeout to revert the size back to normal after 100 milliseconds
LK.setTimeout(function () {
scoreText.scale.set(1, 1);
}, 100);
// Gradually fade out the blood image over 500 milliseconds
var fadeOutDuration = 800;
var fadeOutInterval = 80;
var fadeOutSteps = fadeOutDuration / fadeOutInterval;
var fadeOutStep = 1 / fadeOutSteps;
var fadeOutTimer = LK.setInterval(function () {
blood.alpha -= fadeOutStep;
if (blood.alpha <= 0) {
MidgroundContainer.removeChild(blood);
LK.clearInterval(fadeOutTimer);
}
}, fadeOutInterval);
}
}
// Check if the exact center of the character's asset intersects with the boat
var characterCenter1 = {
x: circle1.x,
y: circle1.y
};
var characterCenter2 = {
x: circle2.x,
y: circle2.y
};
if (boat.contains(characterCenter1.x, characterCenter1.y) || boat.contains(characterCenter2.x, characterCenter2.y)) {
// Hide all other gameplay elements
BackgroundContainer.visible = false;
MidgroundContainer.visible = false;
ForegroundContainer.visible = false;
// Disable all gameplay interactions
game.move = null;
game.down = null;
game.up = null;
game.update = null;
// Display the GO asset
var go = new GO();
GOContainer.addChild(go);
GOContainer.visible = true;
// Play the GO sound
LK.getSound('GO').play();
// Set a timeout to show game over after 2 seconds
LK.setTimeout(function () {
// Re-enable gameplay interactions
game.move = handleMove;
game.down = function (x, y, obj) {
// Switch the movement of the circle objects
if (circle1.speed == 0) {
circle1.speed = circleSpeed;
circle2.speed = 0;
circle1.x = circle2.storedX;
circle1.y = circle2.storedY;
circle1.rotation = circle2.storedAngle;
movingCircle = circle1;
} else {
movingCircle = circle2;
circle1.speed = 0;
circle2.speed = circleSpeed;
circle2.x = circle1.storedX;
circle2.y = circle1.storedY;
circle2.rotation = circle1.storedAngle;
}
};
game.up = function (x, y, obj) {
dragNode = null;
};
game.update = function () {
// Update circle objects
circle1.update();
circle2.update();
// Update the timer
spawnSwimmerTimer++;
// If 2 seconds have passed and there are less than 5 swimmers, spawn a new swimmer at the boat's position
if (spawnSwimmerTimer >= 150 && swimmers.length < 20) {
// Play the Spawn sound
LK.getSound('Spawn').play();
var newSwimmer = new Swimmer();
newSwimmer.x = boat.x;
newSwimmer.y = boat.y;
MidgroundContainer.addChild(newSwimmer);
swimmers.push(newSwimmer);
spawnSwimmerTimer = 0;
}
// Check if either circle intersects with any swimmer
for (var i = swimmers.length - 1; i >= 0; i--) {
if (circle1.intersects(swimmers[i]) || circle2.intersects(swimmers[i])) {
// Display blood image at swimmer's position
var blood = LK.getAsset('blood', {
anchorX: 0.5,
anchorY: 0.5,
x: swimmers[i].x,
y: swimmers[i].y
});
MidgroundContainer.addChild(blood);
// Play the Eat sound
LK.getSound('Eat').play();
// Remove the swimmer from the game
MidgroundContainer.removeChild(swimmers[i]);
swimmers.splice(i, 1);
// Increment the score
LK.setScore(LK.getScore() + 1);
// Play the Horror sound if the score is a multiple of 5, starting from 0
if (LK.getScore() % 3 === 0 && LK.getScore() !== 0) {
LK.getSound('Horror').play();
}
// Update the score text
scoreText.setText(LK.getScore());
// Increase the size of the score text by 50%
scoreText.scale.set(1.2, 1.2);
// Set a timeout to revert the size back to normal after 100 milliseconds
LK.setTimeout(function () {
scoreText.scale.set(1, 1);
}, 100);
// Gradually fade out the blood image over 500 milliseconds
var fadeOutDuration = 800;
var fadeOutInterval = 80;
var fadeOutSteps = fadeOutDuration / fadeOutInterval;
var fadeOutStep = 1 / fadeOutSteps;
var fadeOutTimer = LK.setInterval(function () {
blood.alpha -= fadeOutStep;
if (blood.alpha <= 0) {
MidgroundContainer.removeChild(blood);
LK.clearInterval(fadeOutTimer);
}
}, fadeOutInterval);
}
}
// Check if the exact center of the character's asset intersects with the boat
var characterCenter1 = {
x: circle1.x,
y: circle1.y
};
var characterCenter2 = {
x: circle2.x,
y: circle2.y
};
if (boat.contains(characterCenter1.x, characterCenter1.y) || boat.contains(characterCenter2.x, characterCenter2.y)) {
// Hide all other gameplay elements
BackgroundContainer.visible = false;
MidgroundContainer.visible = false;
ForegroundContainer.visible = false;
// Disable all gameplay interactions
game.move = null;
game.down = null;
game.up = null;
game.update = null;
// Display the GO asset
var go = new GO();
GOContainer.addChild(go);
GOContainer.visible = true;
// Set a timeout to show game over after 2 seconds
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
}
// Calculate the new positions of the circle objects
if (circle1.speed != 0) {
circle1.x = circle2.x - circleDistance * Math.cos(circle1.rotation);
circle1.y = circle2.y - circleDistance * Math.sin(circle1.rotation);
circle1.storedX = circle1.x;
circle1.storedY = circle1.y;
circle1.storedAngle = circle1.rotation;
} else {
circle2.x = circle1.x + circleDistance * Math.cos(circle2.rotation);
circle2.y = circle1.y + circleDistance * Math.sin(circle2.rotation);
circle2.storedX = circle2.x;
circle2.storedY = circle2.y;
circle2.storedAngle = circle2.rotation;
}
// Check if both circles are outside the screen
if ((circle1.x < 0 || circle1.x > 2048 || circle1.y < 0 || circle1.y > 2732) && (circle2.x < 0 || circle2.x > 2048 || circle2.y < 0 || circle2.y > 2732)) {
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
}
};
LK.showGameOver();
}, 2000);
}
// Calculate the new positions of the circle objects
if (circle1.speed != 0) {
circle1.x = circle2.x - circleDistance * Math.cos(circle1.rotation);
circle1.y = circle2.y - circleDistance * Math.sin(circle1.rotation);
circle1.storedX = circle1.x;
circle1.storedY = circle1.y;
circle1.storedAngle = circle1.rotation;
} else {
circle2.x = circle1.x + circleDistance * Math.cos(circle2.rotation);
circle2.y = circle1.y + circleDistance * Math.sin(circle2.rotation);
circle2.storedX = circle2.x;
circle2.storedY = circle2.y;
circle2.storedAngle = circle2.rotation;
}
// Check if both circles are outside the screen
if ((circle1.x < 0 || circle1.x > 2048 || circle1.y < 0 || circle1.y > 2732) && (circle2.x < 0 || circle2.x > 2048 || circle2.y < 0 || circle2.y > 2732)) {
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
}
};
yacht seen from above. pixelated 8-bit. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
shark fin seen from above. bir-eye perspective view. pixelated 8-bit. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
blood splatter. 8-bit pixelated. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.