/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Bomb (hazard, instant game over) class var Bomb = Container.expand(function () { var self = Container.call(this); var bomb = self.attachAsset('Bomb', { anchorX: 0.5, anchorY: 0.5 }); // Randomize spawn position self.x = 200 + Math.random() * (2048 - 400); self.y = 300 + Math.random() * (2732 - 600); // Track spawn time for lifetime removal self.spawnTime = Date.now(); return self; }); // Catval (player) class var Catval = Container.expand(function () { var self = Container.call(this); var cat = self.attachAsset('catval', { anchorX: 0.5, anchorY: 0.5 }); // For possible future animation return self; }); // Dog (hazard) class var Dog = Container.expand(function () { var self = Container.call(this); var dog = self.attachAsset('dog', { anchorX: 0.5, anchorY: 0.5 }); // Randomize spawn position self.x = 200 + Math.random() * (2048 - 400); self.y = 300 + Math.random() * (2732 - 600); // Track spawn time for lifetime removal self.spawnTime = Date.now(); // For possible future animation return self; }); // Mouse (collectible) class var Mouse = Container.expand(function () { var self = Container.call(this); var mouse = self.attachAsset('mouse', { anchorX: 0.5, anchorY: 0.5 }); // Randomize spawn position self.x = 200 + Math.random() * (2048 - 400); self.y = 300 + Math.random() * (2732 - 600); // Track spawn time for lifetime removal self.spawnTime = Date.now(); // For possible future animation return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x222244 }); /**** * Game Code ****/ // Score // Catval (the player cat) // Mouse (collectible, gives points) // Dog (hazard, costs points) var score = 0; // Score display var scoreTxt = new Text2('0', { size: 150, fill: "#fff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Catval (player) var catval = new Catval(); game.addChild(catval); // Start at center catval.x = 2048 / 2; catval.y = 2732 / 2; // Arrays for collectibles/hazards var mice = []; var dogs = []; var bombs = []; // Bombs array // Dragging logic var dragNode = null; // Helper: spawn a mouse function spawnMouse() { var mouse = new Mouse(); mice.push(mouse); game.addChild(mouse); } // Helper: spawn a dog function spawnDog() { var dog = new Dog(); dogs.push(dog); game.addChild(dog); } // Helper: spawn a bomb function spawnBomb() { var bomb = new Bomb(); bombs.push(bomb); game.addChild(bomb); } // Initial spawn spawnMouse(); // Timers for spawning var mouseTimer = LK.setInterval(function () { // Only spawn if less than 3 mice if (mice.length < 3) { spawnMouse(); } }, 1200); var dogTimer = LK.setInterval(function () { // Dogs are rare, max 2 at a time if (dogs.length < 2 && Math.random() < 0.5) { spawnDog(); } }, 2200); // Bombs are rare, max 1 at a time var bombTimer = LK.setInterval(function () { if (bombs.length < 1 && Math.random() < 0.33) { spawnBomb(); } }, 3500); // Move handler (drag Catval) function handleMove(x, y, obj) { if (dragNode) { // Clamp to screen var halfW = catval.width / 2; var halfH = catval.height / 2; var nx = Math.max(halfW, Math.min(2048 - halfW, x)); var ny = Math.max(halfH, Math.min(2732 - halfH, y)); dragNode.x = nx; dragNode.y = ny; } } game.move = handleMove; game.down = function (x, y, obj) { // Start dragging Catval dragNode = catval; handleMove(x, y, obj); }; game.up = function (x, y, obj) { dragNode = null; }; // Main update loop game.update = function () { // Check for Catval-mouse collisions for (var i = mice.length - 1; i >= 0; i--) { var mouse = mice[i]; if (catval.intersects(mouse)) { // Collect mouse score += 1; LK.setScore(score); scoreTxt.setText(score); // Animate mouse fade out tween(mouse, { alpha: 0 }, { duration: 300, onFinish: function onFinish() { mouse.destroy(); } }); mice.splice(i, 1); // Flash Catval green LK.effects.flashObject(catval, 0x00ff00, 300); } } // Check for Catval-dog collisions for (var j = dogs.length - 1; j >= 0; j--) { var dog = dogs[j]; if (catval.intersects(dog)) { // Hit dog: lose 5 points score -= 5; LK.setScore(score); scoreTxt.setText(score); // Animate dog fade out tween(dog, { alpha: 0 }, { duration: 300, onFinish: function onFinish() { dog.destroy(); } }); dogs.splice(j, 1); // Flash Catval red LK.effects.flashObject(catval, 0xff0000, 400); // Game over if score < 1 if (score < 1) { LK.effects.flashScreen(0xff0000, 800); LK.showGameOver(); return; } } } // Check for Catval-bomb collisions for (var b = bombs.length - 1; b >= 0; b--) { var bomb = bombs[b]; if (catval.intersects(bomb)) { // Bomb hit: instant game over tween(bomb, { alpha: 0 }, { duration: 300, onFinish: function onFinish() { bomb.destroy(); } }); bombs.splice(b, 1); LK.effects.flashObject(catval, 0xffff00, 400); LK.effects.flashScreen(0xff0000, 800); LK.showGameOver(); return; } } // Remove offscreen mice/dogs (shouldn't happen, but safety) // Also remove if on screen for more than 15 seconds (15000 ms) var now = Date.now(); for (var k = mice.length - 1; k >= 0; k--) { var m = mice[k]; if (m.x < -200 || m.x > 2248 || m.y < -200 || m.y > 2932 || now - m.spawnTime > 15000) { m.destroy(); mice.splice(k, 1); } } for (var l = dogs.length - 1; l >= 0; l--) { var d = dogs[l]; if (d.x < -200 || d.x > 2248 || d.y < -200 || d.y > 2932 || now - d.spawnTime > 15000) { d.destroy(); dogs.splice(l, 1); } } // Remove offscreen bombs for (var n = bombs.length - 1; n >= 0; n--) { var bomb = bombs[n]; if (bomb.x < -200 || bomb.x > 2248 || bomb.y < -200 || bomb.y > 2932 || now - bomb.spawnTime > 15000) { bomb.destroy(); bombs.splice(n, 1); } } }; // Reset score on game start LK.setScore(0); score = 0; scoreTxt.setText(score); // Play background music LK.playMusic('Music');
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Bomb (hazard, instant game over) class
var Bomb = Container.expand(function () {
var self = Container.call(this);
var bomb = self.attachAsset('Bomb', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomize spawn position
self.x = 200 + Math.random() * (2048 - 400);
self.y = 300 + Math.random() * (2732 - 600);
// Track spawn time for lifetime removal
self.spawnTime = Date.now();
return self;
});
// Catval (player) class
var Catval = Container.expand(function () {
var self = Container.call(this);
var cat = self.attachAsset('catval', {
anchorX: 0.5,
anchorY: 0.5
});
// For possible future animation
return self;
});
// Dog (hazard) class
var Dog = Container.expand(function () {
var self = Container.call(this);
var dog = self.attachAsset('dog', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomize spawn position
self.x = 200 + Math.random() * (2048 - 400);
self.y = 300 + Math.random() * (2732 - 600);
// Track spawn time for lifetime removal
self.spawnTime = Date.now();
// For possible future animation
return self;
});
// Mouse (collectible) class
var Mouse = Container.expand(function () {
var self = Container.call(this);
var mouse = self.attachAsset('mouse', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomize spawn position
self.x = 200 + Math.random() * (2048 - 400);
self.y = 300 + Math.random() * (2732 - 600);
// Track spawn time for lifetime removal
self.spawnTime = Date.now();
// For possible future animation
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x222244
});
/****
* Game Code
****/
// Score
// Catval (the player cat)
// Mouse (collectible, gives points)
// Dog (hazard, costs points)
var score = 0;
// Score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#fff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Catval (player)
var catval = new Catval();
game.addChild(catval);
// Start at center
catval.x = 2048 / 2;
catval.y = 2732 / 2;
// Arrays for collectibles/hazards
var mice = [];
var dogs = [];
var bombs = []; // Bombs array
// Dragging logic
var dragNode = null;
// Helper: spawn a mouse
function spawnMouse() {
var mouse = new Mouse();
mice.push(mouse);
game.addChild(mouse);
}
// Helper: spawn a dog
function spawnDog() {
var dog = new Dog();
dogs.push(dog);
game.addChild(dog);
}
// Helper: spawn a bomb
function spawnBomb() {
var bomb = new Bomb();
bombs.push(bomb);
game.addChild(bomb);
}
// Initial spawn
spawnMouse();
// Timers for spawning
var mouseTimer = LK.setInterval(function () {
// Only spawn if less than 3 mice
if (mice.length < 3) {
spawnMouse();
}
}, 1200);
var dogTimer = LK.setInterval(function () {
// Dogs are rare, max 2 at a time
if (dogs.length < 2 && Math.random() < 0.5) {
spawnDog();
}
}, 2200);
// Bombs are rare, max 1 at a time
var bombTimer = LK.setInterval(function () {
if (bombs.length < 1 && Math.random() < 0.33) {
spawnBomb();
}
}, 3500);
// Move handler (drag Catval)
function handleMove(x, y, obj) {
if (dragNode) {
// Clamp to screen
var halfW = catval.width / 2;
var halfH = catval.height / 2;
var nx = Math.max(halfW, Math.min(2048 - halfW, x));
var ny = Math.max(halfH, Math.min(2732 - halfH, y));
dragNode.x = nx;
dragNode.y = ny;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
// Start dragging Catval
dragNode = catval;
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragNode = null;
};
// Main update loop
game.update = function () {
// Check for Catval-mouse collisions
for (var i = mice.length - 1; i >= 0; i--) {
var mouse = mice[i];
if (catval.intersects(mouse)) {
// Collect mouse
score += 1;
LK.setScore(score);
scoreTxt.setText(score);
// Animate mouse fade out
tween(mouse, {
alpha: 0
}, {
duration: 300,
onFinish: function onFinish() {
mouse.destroy();
}
});
mice.splice(i, 1);
// Flash Catval green
LK.effects.flashObject(catval, 0x00ff00, 300);
}
}
// Check for Catval-dog collisions
for (var j = dogs.length - 1; j >= 0; j--) {
var dog = dogs[j];
if (catval.intersects(dog)) {
// Hit dog: lose 5 points
score -= 5;
LK.setScore(score);
scoreTxt.setText(score);
// Animate dog fade out
tween(dog, {
alpha: 0
}, {
duration: 300,
onFinish: function onFinish() {
dog.destroy();
}
});
dogs.splice(j, 1);
// Flash Catval red
LK.effects.flashObject(catval, 0xff0000, 400);
// Game over if score < 1
if (score < 1) {
LK.effects.flashScreen(0xff0000, 800);
LK.showGameOver();
return;
}
}
}
// Check for Catval-bomb collisions
for (var b = bombs.length - 1; b >= 0; b--) {
var bomb = bombs[b];
if (catval.intersects(bomb)) {
// Bomb hit: instant game over
tween(bomb, {
alpha: 0
}, {
duration: 300,
onFinish: function onFinish() {
bomb.destroy();
}
});
bombs.splice(b, 1);
LK.effects.flashObject(catval, 0xffff00, 400);
LK.effects.flashScreen(0xff0000, 800);
LK.showGameOver();
return;
}
}
// Remove offscreen mice/dogs (shouldn't happen, but safety)
// Also remove if on screen for more than 15 seconds (15000 ms)
var now = Date.now();
for (var k = mice.length - 1; k >= 0; k--) {
var m = mice[k];
if (m.x < -200 || m.x > 2248 || m.y < -200 || m.y > 2932 || now - m.spawnTime > 15000) {
m.destroy();
mice.splice(k, 1);
}
}
for (var l = dogs.length - 1; l >= 0; l--) {
var d = dogs[l];
if (d.x < -200 || d.x > 2248 || d.y < -200 || d.y > 2932 || now - d.spawnTime > 15000) {
d.destroy();
dogs.splice(l, 1);
}
}
// Remove offscreen bombs
for (var n = bombs.length - 1; n >= 0; n--) {
var bomb = bombs[n];
if (bomb.x < -200 || bomb.x > 2248 || bomb.y < -200 || bomb.y > 2932 || now - bomb.spawnTime > 15000) {
bomb.destroy();
bombs.splice(n, 1);
}
}
};
// Reset score on game start
LK.setScore(0);
score = 0;
scoreTxt.setText(score);
// Play background music
LK.playMusic('Music');