/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Mother = Container.expand(function () {
var self = Container.call(this);
var motherGraphics = self.attachAsset('mother', {
anchorX: 0.5,
anchorY: 1.0
});
self.targetDistance = 400;
self.currentDistance = 400;
self.catchUpSpeed = 3.5;
self.fallBackSpeed = 1;
self.isJumping = false;
self.jumpSpeed = 0;
self.groundY = 0;
self.gravity = 1.2;
self.jumpPower = -22;
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = self.jumpPower;
}
};
self.update = function () {
var diff = self.targetDistance - self.currentDistance;
if (diff < 0) {
self.currentDistance += Math.max(diff, -self.catchUpSpeed);
}
// Handle jumping physics
if (self.isJumping) {
self.jumpSpeed += self.gravity;
self.y += self.jumpSpeed;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.isJumping = false;
self.jumpSpeed = 0;
}
}
// Check for obstacles and jump over them
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
var distanceToObstacle = obstacle.x - self.x;
// If obstacle is approaching (within jumping distance) and mother is on ground
if (distanceToObstacle > 0 && distanceToObstacle < 120 && !self.isJumping) {
self.jump();
break;
}
}
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = 8;
self.passed = false;
self.update = function () {
self.x -= self.speed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.isJumping = false;
self.jumpSpeed = 0;
self.groundY = 0;
self.gravity = 1.2;
self.jumpPower = -24;
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = self.jumpPower;
LK.getSound('jump').play();
}
};
self.update = function () {
if (self.isJumping) {
self.jumpSpeed += self.gravity;
self.y += self.jumpSpeed;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.isJumping = false;
self.jumpSpeed = 0;
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var groundY = 2732 - 200;
var gameSpeed = 8;
var baseGameSpeed = 8;
var obstacles = [];
var obstacleSpawnTimer = 0;
var obstacleSpawnDelay = 90;
var minObstacleSpawnDelay = 45;
var speedIncreaseTimer = 0;
var isGameRunning = true;
var gameWon = false;
var house = null;
var grandmother = null;
// Create ground
var ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0,
x: 0,
y: groundY
}));
// Create player
var player = game.addChild(new Player());
player.x = 400;
player.y = groundY;
player.groundY = groundY;
// Create mother
var mother = game.addChild(new Mother());
mother.x = player.x - mother.currentDistance;
mother.y = groundY;
mother.groundY = groundY;
// Create score display
var scoreTxt = new Text2('Distance: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create distance indicator
var distanceTxt = new Text2('Safe Distance', {
size: 60,
fill: 0x4CAF50
});
distanceTxt.anchor.set(0.5, 0);
distanceTxt.y = 120;
LK.gui.top.addChild(distanceTxt);
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048 + 100;
obstacle.y = groundY;
obstacle.speed = gameSpeed;
obstacles.push(obstacle);
game.addChild(obstacle);
// Randomly spawn consecutive obstacles
if (Math.random() < 0.3) {
// 30% chance for consecutive obstacles
var consecutiveObstacle = new Obstacle();
consecutiveObstacle.x = 2048 + 150; // Reduced spacing between consecutive obstacles
consecutiveObstacle.y = groundY;
consecutiveObstacle.speed = gameSpeed;
obstacles.push(consecutiveObstacle);
game.addChild(consecutiveObstacle);
}
}
function updateDistanceIndicator() {
var distance = mother.currentDistance;
var color = 0x4CAF50; // Green - safe
var text = "Safe Distance";
if (distance < 150) {
color = 0xF44336; // Red - danger
text = "DANGER!";
} else if (distance < 250) {
color = 0xFF9800; // Orange - warning
text = "Getting Close!";
}
distanceTxt.tint = color;
distanceTxt.setText(text);
}
// Touch controls
game.down = function (x, y, obj) {
if (isGameRunning) {
player.jump();
}
};
game.update = function () {
if (!isGameRunning) return;
// Update score based on distance traveled
LK.setScore(LK.getScore() + 1);
scoreTxt.setText('Distance: ' + LK.getScore());
// Increase game speed every 100 distance points
if (LK.getScore() > 0 && LK.getScore() % 100 === 0) {
// Only increase if we haven't already increased at this score
if (!speedIncreaseTimer || speedIncreaseTimer !== LK.getScore()) {
speedIncreaseTimer = LK.getScore(); // Track last speed increase score
gameSpeed += 0.5;
obstacleSpawnDelay = Math.max(minObstacleSpawnDelay, obstacleSpawnDelay - 2);
}
}
// Spawn obstacles
obstacleSpawnTimer++;
if (obstacleSpawnTimer >= obstacleSpawnDelay) {
obstacleSpawnTimer = 0;
spawnObstacle();
}
// Update obstacles and check collisions
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.speed = gameSpeed;
// Check if obstacle passed player (score bonus)
if (!obstacle.passed && obstacle.x < player.x - 50) {
obstacle.passed = true;
mother.targetDistance = Math.min(500, mother.targetDistance + 15);
}
// Check collision with player
if (player.intersects(obstacle) && !player.isJumping) {
// Player hit obstacle
LK.getSound('hit').play();
LK.effects.flashObject(player, 0xFF0000, 300);
mother.targetDistance = Math.max(50, mother.targetDistance - 40);
// Make mother faster after taking damage
mother.catchUpSpeed += 0.5;
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
// Remove off-screen obstacles
if (obstacle.x < -100) {
obstacle.destroy();
obstacles.splice(i, 1);
}
}
// Update mother position
mother.x = player.x - mother.currentDistance;
// Keep mother within screen bounds (never let her go off-screen)
mother.x = Math.max(50, Math.min(mother.x, 2048 - 50));
// Update distance indicator
updateDistanceIndicator();
// Check win condition - distance 6000
if (LK.getScore() >= 6000 && !gameWon) {
gameWon = true;
// Create house at the end
house = game.addChild(LK.getAsset('house', {
anchorX: 0.5,
anchorY: 1.0,
x: 2048 + 200,
y: groundY
}));
// Create grandmother inside the house
grandmother = game.addChild(LK.getAsset('grandmother', {
anchorX: 0.5,
anchorY: 1.0,
x: 2048 + 200,
y: groundY
}));
// Move house and grandmother into view
tween(house, {
x: 1800
}, {
duration: 2000
});
tween(grandmother, {
x: 1800
}, {
duration: 2000
});
// Show victory after house appears
LK.setTimeout(function () {
LK.showYouWin();
}, 2500);
}
// Check if mother caught player
if (mother.currentDistance <= 80 && !gameWon) {
isGameRunning = false;
LK.effects.flashScreen(0xFF0000, 1000);
LK.setTimeout(function () {
LK.showGameOver();
}, 500);
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Mother = Container.expand(function () {
var self = Container.call(this);
var motherGraphics = self.attachAsset('mother', {
anchorX: 0.5,
anchorY: 1.0
});
self.targetDistance = 400;
self.currentDistance = 400;
self.catchUpSpeed = 3.5;
self.fallBackSpeed = 1;
self.isJumping = false;
self.jumpSpeed = 0;
self.groundY = 0;
self.gravity = 1.2;
self.jumpPower = -22;
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = self.jumpPower;
}
};
self.update = function () {
var diff = self.targetDistance - self.currentDistance;
if (diff < 0) {
self.currentDistance += Math.max(diff, -self.catchUpSpeed);
}
// Handle jumping physics
if (self.isJumping) {
self.jumpSpeed += self.gravity;
self.y += self.jumpSpeed;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.isJumping = false;
self.jumpSpeed = 0;
}
}
// Check for obstacles and jump over them
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
var distanceToObstacle = obstacle.x - self.x;
// If obstacle is approaching (within jumping distance) and mother is on ground
if (distanceToObstacle > 0 && distanceToObstacle < 120 && !self.isJumping) {
self.jump();
break;
}
}
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = 8;
self.passed = false;
self.update = function () {
self.x -= self.speed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.isJumping = false;
self.jumpSpeed = 0;
self.groundY = 0;
self.gravity = 1.2;
self.jumpPower = -24;
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = self.jumpPower;
LK.getSound('jump').play();
}
};
self.update = function () {
if (self.isJumping) {
self.jumpSpeed += self.gravity;
self.y += self.jumpSpeed;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.isJumping = false;
self.jumpSpeed = 0;
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var groundY = 2732 - 200;
var gameSpeed = 8;
var baseGameSpeed = 8;
var obstacles = [];
var obstacleSpawnTimer = 0;
var obstacleSpawnDelay = 90;
var minObstacleSpawnDelay = 45;
var speedIncreaseTimer = 0;
var isGameRunning = true;
var gameWon = false;
var house = null;
var grandmother = null;
// Create ground
var ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0,
x: 0,
y: groundY
}));
// Create player
var player = game.addChild(new Player());
player.x = 400;
player.y = groundY;
player.groundY = groundY;
// Create mother
var mother = game.addChild(new Mother());
mother.x = player.x - mother.currentDistance;
mother.y = groundY;
mother.groundY = groundY;
// Create score display
var scoreTxt = new Text2('Distance: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create distance indicator
var distanceTxt = new Text2('Safe Distance', {
size: 60,
fill: 0x4CAF50
});
distanceTxt.anchor.set(0.5, 0);
distanceTxt.y = 120;
LK.gui.top.addChild(distanceTxt);
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048 + 100;
obstacle.y = groundY;
obstacle.speed = gameSpeed;
obstacles.push(obstacle);
game.addChild(obstacle);
// Randomly spawn consecutive obstacles
if (Math.random() < 0.3) {
// 30% chance for consecutive obstacles
var consecutiveObstacle = new Obstacle();
consecutiveObstacle.x = 2048 + 150; // Reduced spacing between consecutive obstacles
consecutiveObstacle.y = groundY;
consecutiveObstacle.speed = gameSpeed;
obstacles.push(consecutiveObstacle);
game.addChild(consecutiveObstacle);
}
}
function updateDistanceIndicator() {
var distance = mother.currentDistance;
var color = 0x4CAF50; // Green - safe
var text = "Safe Distance";
if (distance < 150) {
color = 0xF44336; // Red - danger
text = "DANGER!";
} else if (distance < 250) {
color = 0xFF9800; // Orange - warning
text = "Getting Close!";
}
distanceTxt.tint = color;
distanceTxt.setText(text);
}
// Touch controls
game.down = function (x, y, obj) {
if (isGameRunning) {
player.jump();
}
};
game.update = function () {
if (!isGameRunning) return;
// Update score based on distance traveled
LK.setScore(LK.getScore() + 1);
scoreTxt.setText('Distance: ' + LK.getScore());
// Increase game speed every 100 distance points
if (LK.getScore() > 0 && LK.getScore() % 100 === 0) {
// Only increase if we haven't already increased at this score
if (!speedIncreaseTimer || speedIncreaseTimer !== LK.getScore()) {
speedIncreaseTimer = LK.getScore(); // Track last speed increase score
gameSpeed += 0.5;
obstacleSpawnDelay = Math.max(minObstacleSpawnDelay, obstacleSpawnDelay - 2);
}
}
// Spawn obstacles
obstacleSpawnTimer++;
if (obstacleSpawnTimer >= obstacleSpawnDelay) {
obstacleSpawnTimer = 0;
spawnObstacle();
}
// Update obstacles and check collisions
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.speed = gameSpeed;
// Check if obstacle passed player (score bonus)
if (!obstacle.passed && obstacle.x < player.x - 50) {
obstacle.passed = true;
mother.targetDistance = Math.min(500, mother.targetDistance + 15);
}
// Check collision with player
if (player.intersects(obstacle) && !player.isJumping) {
// Player hit obstacle
LK.getSound('hit').play();
LK.effects.flashObject(player, 0xFF0000, 300);
mother.targetDistance = Math.max(50, mother.targetDistance - 40);
// Make mother faster after taking damage
mother.catchUpSpeed += 0.5;
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
// Remove off-screen obstacles
if (obstacle.x < -100) {
obstacle.destroy();
obstacles.splice(i, 1);
}
}
// Update mother position
mother.x = player.x - mother.currentDistance;
// Keep mother within screen bounds (never let her go off-screen)
mother.x = Math.max(50, Math.min(mother.x, 2048 - 50));
// Update distance indicator
updateDistanceIndicator();
// Check win condition - distance 6000
if (LK.getScore() >= 6000 && !gameWon) {
gameWon = true;
// Create house at the end
house = game.addChild(LK.getAsset('house', {
anchorX: 0.5,
anchorY: 1.0,
x: 2048 + 200,
y: groundY
}));
// Create grandmother inside the house
grandmother = game.addChild(LK.getAsset('grandmother', {
anchorX: 0.5,
anchorY: 1.0,
x: 2048 + 200,
y: groundY
}));
// Move house and grandmother into view
tween(house, {
x: 1800
}, {
duration: 2000
});
tween(grandmother, {
x: 1800
}, {
duration: 2000
});
// Show victory after house appears
LK.setTimeout(function () {
LK.showYouWin();
}, 2500);
}
// Check if mother caught player
if (mother.currentDistance <= 80 && !gameWon) {
isGameRunning = false;
LK.effects.flashScreen(0xFF0000, 1000);
LK.setTimeout(function () {
LK.showGameOver();
}, 500);
}
};