/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obsSprite = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 1 }); self.width = obsSprite.width; self.height = obsSprite.height; // Update method self.update = function () { self.x -= gameSpeed; }; return self; }); // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerSprite = self.attachAsset('player', { anchorX: 0.5, anchorY: 1 }); self.width = playerSprite.width; self.height = playerSprite.height; // Physics self.vy = 0; self.isOnGround = false; // Jump method self.jump = function () { if (self.isOnGround) { self.vy = -48; // Jump velocity self.isOnGround = false; } }; // Update method self.update = function () { // Gravity self.vy += 4; // Gravity acceleration self.y += self.vy; // Prevent falling below ground if (self.y > groundY) { self.y = groundY; self.vy = 0; self.isOnGround = true; } }; return self; }); // Point token class var PointToken = Container.expand(function () { var self = Container.call(this); var pointSprite = self.attachAsset('point', { anchorX: 0.5, anchorY: 0.5 }); self.width = pointSprite.width; self.height = pointSprite.height; // Update method self.update = function () { self.x -= gameSpeed; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb // Sky blue }); /**** * Game Code ****/ // Point token: yellow ellipse // Obstacle: dark gray box // Ground: green box // Player character: red box // Constants var groundHeight = 120; var groundY = 2732 - groundHeight; var playerStartX = 400; var playerStartY = groundY; var gameSpeed = 24; // pixels per frame var obstacleMinGap = 600; var obstacleMaxGap = 1100; var obstacleMinY = groundY - 160; var obstacleMaxY = groundY; var pointMinGap = 400; var pointMaxGap = 900; // Game state var player; var ground; var obstacles = []; var points = []; var score = 0; var scoreTxt; var nextObstacleX = 1200; var nextPointX = 900; var isGameOver = false; // Add ground ground = LK.getAsset('ground', { anchorX: 0, anchorY: 0 }); ground.x = 0; ground.y = groundY; game.addChild(ground); // Add player player = new Player(); player.x = playerStartX; player.y = playerStartY; game.addChild(player); // Score text scoreTxt = new Text2('0', { size: 120, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Helper: spawn obstacle function spawnObstacle(x) { var obs = new Obstacle(); obs.x = x; obs.y = groundY; obstacles.push(obs); game.addChild(obs); } // Helper: spawn point token function spawnPoint(x) { var pt = new PointToken(); pt.x = x; pt.y = groundY - 200 - Math.floor(Math.random() * 200); // Random height above ground points.push(pt); game.addChild(pt); } // Initial obstacles and points spawnObstacle(1200); spawnPoint(900); // Touch/tap to jump game.down = function (x, y, obj) { if (!isGameOver) { player.jump(); } }; // Main update loop game.update = function () { if (isGameOver) return; // Update player player.update(); // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { var obs = obstacles[i]; obs.update(); // Remove if off screen if (obs.x < -obs.width) { obs.destroy(); obstacles.splice(i, 1); continue; } // Collision with player if (player.intersects(obs)) { // Flash screen red and show game over LK.effects.flashScreen(0xff0000, 800); isGameOver = true; LK.setTimeout(function () { LK.showGameOver(); }, 800); return; } } // Update points for (var j = points.length - 1; j >= 0; j--) { var pt = points[j]; pt.update(); // Remove if off screen if (pt.x < -pt.width) { pt.destroy(); points.splice(j, 1); continue; } // Collect point if (player.intersects(pt)) { score += 1; LK.setScore(score); scoreTxt.setText(score); // Animate point collection tween(pt, { scaleX: 1.8, scaleY: 1.8, alpha: 0 }, { duration: 200, easing: tween.easeOut, onFinish: function onFinish() { pt.destroy(); } }); points.splice(j, 1); continue; } } // Spawn new obstacles if (obstacles.length === 0 || obstacles[obstacles.length - 1].x < 2048 - obstacleMinGap) { var gap = obstacleMinGap + Math.floor(Math.random() * (obstacleMaxGap - obstacleMinGap)); var newX = (obstacles.length > 0 ? obstacles[obstacles.length - 1].x : 1200) + gap; spawnObstacle(newX); } // Spawn new points if (points.length === 0 || points[points.length - 1].x < 2048 - pointMinGap) { var gapP = pointMinGap + Math.floor(Math.random() * (pointMaxGap - pointMinGap)); var newXP = (points.length > 0 ? points[points.length - 1].x : 900) + gapP; spawnPoint(newXP); } }; // Reset score on game start LK.setScore(0); score = 0; scoreTxt.setText(score);
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obsSprite = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1
});
self.width = obsSprite.width;
self.height = obsSprite.height;
// Update method
self.update = function () {
self.x -= gameSpeed;
};
return self;
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerSprite = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1
});
self.width = playerSprite.width;
self.height = playerSprite.height;
// Physics
self.vy = 0;
self.isOnGround = false;
// Jump method
self.jump = function () {
if (self.isOnGround) {
self.vy = -48; // Jump velocity
self.isOnGround = false;
}
};
// Update method
self.update = function () {
// Gravity
self.vy += 4; // Gravity acceleration
self.y += self.vy;
// Prevent falling below ground
if (self.y > groundY) {
self.y = groundY;
self.vy = 0;
self.isOnGround = true;
}
};
return self;
});
// Point token class
var PointToken = Container.expand(function () {
var self = Container.call(this);
var pointSprite = self.attachAsset('point', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = pointSprite.width;
self.height = pointSprite.height;
// Update method
self.update = function () {
self.x -= gameSpeed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb // Sky blue
});
/****
* Game Code
****/
// Point token: yellow ellipse
// Obstacle: dark gray box
// Ground: green box
// Player character: red box
// Constants
var groundHeight = 120;
var groundY = 2732 - groundHeight;
var playerStartX = 400;
var playerStartY = groundY;
var gameSpeed = 24; // pixels per frame
var obstacleMinGap = 600;
var obstacleMaxGap = 1100;
var obstacleMinY = groundY - 160;
var obstacleMaxY = groundY;
var pointMinGap = 400;
var pointMaxGap = 900;
// Game state
var player;
var ground;
var obstacles = [];
var points = [];
var score = 0;
var scoreTxt;
var nextObstacleX = 1200;
var nextPointX = 900;
var isGameOver = false;
// Add ground
ground = LK.getAsset('ground', {
anchorX: 0,
anchorY: 0
});
ground.x = 0;
ground.y = groundY;
game.addChild(ground);
// Add player
player = new Player();
player.x = playerStartX;
player.y = playerStartY;
game.addChild(player);
// Score text
scoreTxt = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Helper: spawn obstacle
function spawnObstacle(x) {
var obs = new Obstacle();
obs.x = x;
obs.y = groundY;
obstacles.push(obs);
game.addChild(obs);
}
// Helper: spawn point token
function spawnPoint(x) {
var pt = new PointToken();
pt.x = x;
pt.y = groundY - 200 - Math.floor(Math.random() * 200); // Random height above ground
points.push(pt);
game.addChild(pt);
}
// Initial obstacles and points
spawnObstacle(1200);
spawnPoint(900);
// Touch/tap to jump
game.down = function (x, y, obj) {
if (!isGameOver) {
player.jump();
}
};
// Main update loop
game.update = function () {
if (isGameOver) return;
// Update player
player.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
var obs = obstacles[i];
obs.update();
// Remove if off screen
if (obs.x < -obs.width) {
obs.destroy();
obstacles.splice(i, 1);
continue;
}
// Collision with player
if (player.intersects(obs)) {
// Flash screen red and show game over
LK.effects.flashScreen(0xff0000, 800);
isGameOver = true;
LK.setTimeout(function () {
LK.showGameOver();
}, 800);
return;
}
}
// Update points
for (var j = points.length - 1; j >= 0; j--) {
var pt = points[j];
pt.update();
// Remove if off screen
if (pt.x < -pt.width) {
pt.destroy();
points.splice(j, 1);
continue;
}
// Collect point
if (player.intersects(pt)) {
score += 1;
LK.setScore(score);
scoreTxt.setText(score);
// Animate point collection
tween(pt, {
scaleX: 1.8,
scaleY: 1.8,
alpha: 0
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
pt.destroy();
}
});
points.splice(j, 1);
continue;
}
}
// Spawn new obstacles
if (obstacles.length === 0 || obstacles[obstacles.length - 1].x < 2048 - obstacleMinGap) {
var gap = obstacleMinGap + Math.floor(Math.random() * (obstacleMaxGap - obstacleMinGap));
var newX = (obstacles.length > 0 ? obstacles[obstacles.length - 1].x : 1200) + gap;
spawnObstacle(newX);
}
// Spawn new points
if (points.length === 0 || points[points.length - 1].x < 2048 - pointMinGap) {
var gapP = pointMinGap + Math.floor(Math.random() * (pointMaxGap - pointMinGap));
var newXP = (points.length > 0 ? points[points.length - 1].x : 900) + gapP;
spawnPoint(newXP);
}
};
// Reset score on game start
LK.setScore(0);
score = 0;
scoreTxt.setText(score);