/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Coin = Container.expand(function (startX, startY) { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.collected = false; self.x = startX || 400; self.y = startY || 2300; self.update = function () { if (!self.collected) { self.rotation += 0.1; } }; self.collect = function () { if (!self.collected) { self.collected = true; self.alpha = 0; LK.setScore(LK.getScore() + 10); LK.getSound('coin_collect').play(); } }; return self; }); var Enemy = Container.expand(function (startX, startY) { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 1 }); self.velocityX = -1; self.velocityY = 0; self.gravity = 0.5; self.alive = true; self.x = startX || 800; self.y = startY || 2400; self.update = function () { if (!self.alive) return; // Apply gravity self.velocityY += self.gravity; // Update position self.x += self.velocityX; self.y += self.velocityY; // Ground collision if (self.y >= 2400) { self.y = 2400; self.velocityY = 0; } // Platform collision for (var i = 0; i < platforms.length; i++) { var platform = platforms[i]; if (self.intersects(platform) && self.velocityY > 0) { if (self.y - 40 <= platform.y - 20) { self.y = platform.y - 20; self.velocityY = 0; break; } } } // Reverse direction at edges if (self.x < 50 || self.x > levelWidth - 50) { self.velocityX = -self.velocityX; } }; self.defeat = function () { self.alive = false; self.alpha = 0.5; LK.getSound('enemy_hit').play(); LK.setTimeout(function () { self.destroy(); }, 500); }; return self; }); var Platform = Container.expand(function (startX, startY, width) { var self = Container.call(this); self.platformWidth = width || 200; var platformGraphics = self.attachAsset('platform', { anchorX: 0.5, anchorY: 0.5, width: self.platformWidth }); self.x = startX || 400; self.y = startY || 2000; return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 1 }); self.velocityX = 0; self.velocityY = 0; self.gravity = 0.5; self.jumpPower = -12; self.speed = 4; self.onGround = false; self.lastOnGround = false; self.x = 200; self.y = 2400; self.update = function () { // Auto-move forward for runner game self.velocityX = self.speed; // Apply gravity self.velocityY += self.gravity; // Update position self.x += self.velocityX; self.y += self.velocityY; // Add walking animation with subtle scaling if (self.onGround) { // Walking animation - scale slightly up and down var walkCycle = Math.sin(LK.ticks * 0.3) * 0.1; self.scaleX = 1 + walkCycle * 0.1; self.scaleY = 1 - walkCycle * 0.05; } // Ground collision if (self.y >= 2400) { self.y = 2400; self.velocityY = 0; // Landing animation - only trigger when transitioning from air to ground if (!self.lastOnGround && !self.onGround) { tween(self, { scaleX: 1.3, scaleY: 0.7 }, { duration: 150, easing: tween.easeOut, onFinish: function onFinish() { tween(self, { scaleX: 1, scaleY: 1 }, { duration: 200, easing: tween.bounceOut }); } }); } self.onGround = true; } else { self.onGround = false; } // Platform collision for (var i = 0; i < platforms.length; i++) { var platform = platforms[i]; if (self.intersects(platform) && self.velocityY > 0) { if (self.y - 60 <= platform.y - 20) { self.y = platform.y - 20; self.velocityY = 0; // Landing animation - only trigger when transitioning from air to ground if (!self.lastOnGround && !self.onGround) { tween(self, { scaleX: 1.3, scaleY: 0.7 }, { duration: 150, easing: tween.easeOut, onFinish: function onFinish() { tween(self, { scaleX: 1, scaleY: 1 }, { duration: 200, easing: tween.bounceOut }); } }); } self.onGround = true; break; } } } // Update camera to follow player cameraX = self.x - 1024; if (cameraX < 0) cameraX = 0; if (cameraX > levelWidth - 2048) cameraX = levelWidth - 2048; self.lastOnGround = self.onGround; }; self.jump = function () { if (self.onGround) { self.velocityY = self.jumpPower; self.onGround = false; LK.getSound('jump').play(); // Jump animation - quick rotation and scale tween(self, { rotation: Math.PI * 0.1, scaleX: 1.2, scaleY: 0.8 }, { duration: 200, easing: tween.easeOut, onFinish: function onFinish() { tween(self, { rotation: 0, scaleX: 1, scaleY: 1 }, { duration: 300, easing: tween.easeInOut }); } }); } }; self.moveLeft = function () { self.velocityX = -self.speed; }; self.moveRight = function () { self.velocityX = self.speed; }; self.stop = function () { self.velocityX = 0; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb }); /**** * Game Code ****/ // Game variables var levelWidth = 4000; var cameraX = 0; var platforms = []; var enemies = []; var coins = []; var player = new Player(); var gameContainer = new Container(); // Touch controls - tap anywhere to jump for runner game game.addChild(gameContainer); gameContainer.addChild(player); // Add tap-to-jump functionality for entire game screen game.down = function (x, y, obj) { player.jump(); }; // Create ground var ground = gameContainer.attachAsset('ground', { x: 0, y: 2460, width: levelWidth, anchorX: 0, anchorY: 0 }); // Create platforms var platformData = [{ x: 600, y: 2300, width: 200 }, { x: 1000, y: 2250, width: 200 }, { x: 1400, y: 2200, width: 200 }, { x: 1800, y: 2280, width: 200 }, { x: 2200, y: 2240, width: 200 }, { x: 2600, y: 2180, width: 200 }, { x: 3000, y: 2150, width: 200 }, { x: 3400, y: 2220, width: 200 }]; for (var i = 0; i < platformData.length; i++) { var platData = platformData[i]; var platform = new Platform(platData.x, platData.y, platData.width); platforms.push(platform); gameContainer.addChild(platform); } // Create enemies var enemyData = [{ x: 900, y: 2400 }, { x: 1500, y: 2400 }, { x: 2300, y: 2400 }, { x: 3200, y: 2400 }]; for (var i = 0; i < enemyData.length; i++) { var enemyPos = enemyData[i]; var enemy = new Enemy(enemyPos.x, enemyPos.y); enemies.push(enemy); gameContainer.addChild(enemy); } // Create coins var coinData = [{ x: 400, y: 2350 }, { x: 700, y: 2250 }, { x: 1100, y: 2200 }, { x: 1500, y: 2150 }, { x: 1900, y: 2230 }, { x: 2300, y: 2190 }, { x: 2700, y: 2130 }, { x: 3100, y: 2100 }, { x: 3500, y: 2170 }]; for (var i = 0; i < coinData.length; i++) { var coinPos = coinData[i]; var coin = new Coin(coinPos.x, coinPos.y); coins.push(coin); gameContainer.addChild(coin); } // Create finish flag var flag = gameContainer.attachAsset('flag', { x: 3800, y: 2320, anchorX: 0.5, anchorY: 1 }); // Create UI var scoreText = new Text2('Score: 0', { size: 48, fill: '#ffffff' }); scoreText.anchor.set(0, 0); scoreText.x = 50; scoreText.y = 50; LK.gui.topLeft.addChild(scoreText); var instructionText = new Text2('Tap anywhere to jump! Avoid enemies and collect coins!', { size: 32, fill: '#ffffff' }); instructionText.anchor.set(0.5, 0); instructionText.x = 1024; instructionText.y = 100; LK.gui.top.addChild(instructionText); // Removed jump control button - tap anywhere on screen to jump game.update = function () { // Update player (auto-running) player.update(); // Update enemies for (var i = enemies.length - 1; i >= 0; i--) { var enemy = enemies[i]; enemy.update(); } // Update coins for (var i = 0; i < coins.length; i++) { var coin = coins[i]; coin.update(); } // Check collisions with enemies for (var i = enemies.length - 1; i >= 0; i--) { var enemy = enemies[i]; if (enemy.alive && player.intersects(enemy)) { if (player.velocityY > 0 && player.y < enemy.y - 45) { // Player jumped on enemy enemy.defeat(); enemies.splice(i, 1); player.velocityY = -10; // Bounce LK.setScore(LK.getScore() + 50); } else { // Player hit enemy LK.getSound('game_over').play(); LK.showGameOver(); return; } } } // Check collisions with coins for (var i = coins.length - 1; i >= 0; i--) { var coin = coins[i]; if (!coin.collected && player.intersects(coin)) { coin.collect(); coins.splice(i, 1); } } // Check if player reached flag if (player.intersects(flag)) { LK.showYouWin(); return; } // Check if player fell off screen if (player.y > 2800) { LK.getSound('game_over').play(); LK.showGameOver(); return; } // Update camera gameContainer.x = -cameraX; // Update score display scoreText.setText('Score: ' + LK.getScore()); };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Coin = Container.expand(function (startX, startY) {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.x = startX || 400;
self.y = startY || 2300;
self.update = function () {
if (!self.collected) {
self.rotation += 0.1;
}
};
self.collect = function () {
if (!self.collected) {
self.collected = true;
self.alpha = 0;
LK.setScore(LK.getScore() + 10);
LK.getSound('coin_collect').play();
}
};
return self;
});
var Enemy = Container.expand(function (startX, startY) {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 1
});
self.velocityX = -1;
self.velocityY = 0;
self.gravity = 0.5;
self.alive = true;
self.x = startX || 800;
self.y = startY || 2400;
self.update = function () {
if (!self.alive) return;
// Apply gravity
self.velocityY += self.gravity;
// Update position
self.x += self.velocityX;
self.y += self.velocityY;
// Ground collision
if (self.y >= 2400) {
self.y = 2400;
self.velocityY = 0;
}
// Platform collision
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (self.intersects(platform) && self.velocityY > 0) {
if (self.y - 40 <= platform.y - 20) {
self.y = platform.y - 20;
self.velocityY = 0;
break;
}
}
}
// Reverse direction at edges
if (self.x < 50 || self.x > levelWidth - 50) {
self.velocityX = -self.velocityX;
}
};
self.defeat = function () {
self.alive = false;
self.alpha = 0.5;
LK.getSound('enemy_hit').play();
LK.setTimeout(function () {
self.destroy();
}, 500);
};
return self;
});
var Platform = Container.expand(function (startX, startY, width) {
var self = Container.call(this);
self.platformWidth = width || 200;
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5,
width: self.platformWidth
});
self.x = startX || 400;
self.y = startY || 2000;
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1
});
self.velocityX = 0;
self.velocityY = 0;
self.gravity = 0.5;
self.jumpPower = -12;
self.speed = 4;
self.onGround = false;
self.lastOnGround = false;
self.x = 200;
self.y = 2400;
self.update = function () {
// Auto-move forward for runner game
self.velocityX = self.speed;
// Apply gravity
self.velocityY += self.gravity;
// Update position
self.x += self.velocityX;
self.y += self.velocityY;
// Add walking animation with subtle scaling
if (self.onGround) {
// Walking animation - scale slightly up and down
var walkCycle = Math.sin(LK.ticks * 0.3) * 0.1;
self.scaleX = 1 + walkCycle * 0.1;
self.scaleY = 1 - walkCycle * 0.05;
}
// Ground collision
if (self.y >= 2400) {
self.y = 2400;
self.velocityY = 0;
// Landing animation - only trigger when transitioning from air to ground
if (!self.lastOnGround && !self.onGround) {
tween(self, {
scaleX: 1.3,
scaleY: 0.7
}, {
duration: 150,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 200,
easing: tween.bounceOut
});
}
});
}
self.onGround = true;
} else {
self.onGround = false;
}
// Platform collision
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (self.intersects(platform) && self.velocityY > 0) {
if (self.y - 60 <= platform.y - 20) {
self.y = platform.y - 20;
self.velocityY = 0;
// Landing animation - only trigger when transitioning from air to ground
if (!self.lastOnGround && !self.onGround) {
tween(self, {
scaleX: 1.3,
scaleY: 0.7
}, {
duration: 150,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 200,
easing: tween.bounceOut
});
}
});
}
self.onGround = true;
break;
}
}
}
// Update camera to follow player
cameraX = self.x - 1024;
if (cameraX < 0) cameraX = 0;
if (cameraX > levelWidth - 2048) cameraX = levelWidth - 2048;
self.lastOnGround = self.onGround;
};
self.jump = function () {
if (self.onGround) {
self.velocityY = self.jumpPower;
self.onGround = false;
LK.getSound('jump').play();
// Jump animation - quick rotation and scale
tween(self, {
rotation: Math.PI * 0.1,
scaleX: 1.2,
scaleY: 0.8
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
rotation: 0,
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeInOut
});
}
});
}
};
self.moveLeft = function () {
self.velocityX = -self.speed;
};
self.moveRight = function () {
self.velocityX = self.speed;
};
self.stop = function () {
self.velocityX = 0;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
// Game variables
var levelWidth = 4000;
var cameraX = 0;
var platforms = [];
var enemies = [];
var coins = [];
var player = new Player();
var gameContainer = new Container();
// Touch controls - tap anywhere to jump for runner game
game.addChild(gameContainer);
gameContainer.addChild(player);
// Add tap-to-jump functionality for entire game screen
game.down = function (x, y, obj) {
player.jump();
};
// Create ground
var ground = gameContainer.attachAsset('ground', {
x: 0,
y: 2460,
width: levelWidth,
anchorX: 0,
anchorY: 0
});
// Create platforms
var platformData = [{
x: 600,
y: 2300,
width: 200
}, {
x: 1000,
y: 2250,
width: 200
}, {
x: 1400,
y: 2200,
width: 200
}, {
x: 1800,
y: 2280,
width: 200
}, {
x: 2200,
y: 2240,
width: 200
}, {
x: 2600,
y: 2180,
width: 200
}, {
x: 3000,
y: 2150,
width: 200
}, {
x: 3400,
y: 2220,
width: 200
}];
for (var i = 0; i < platformData.length; i++) {
var platData = platformData[i];
var platform = new Platform(platData.x, platData.y, platData.width);
platforms.push(platform);
gameContainer.addChild(platform);
}
// Create enemies
var enemyData = [{
x: 900,
y: 2400
}, {
x: 1500,
y: 2400
}, {
x: 2300,
y: 2400
}, {
x: 3200,
y: 2400
}];
for (var i = 0; i < enemyData.length; i++) {
var enemyPos = enemyData[i];
var enemy = new Enemy(enemyPos.x, enemyPos.y);
enemies.push(enemy);
gameContainer.addChild(enemy);
}
// Create coins
var coinData = [{
x: 400,
y: 2350
}, {
x: 700,
y: 2250
}, {
x: 1100,
y: 2200
}, {
x: 1500,
y: 2150
}, {
x: 1900,
y: 2230
}, {
x: 2300,
y: 2190
}, {
x: 2700,
y: 2130
}, {
x: 3100,
y: 2100
}, {
x: 3500,
y: 2170
}];
for (var i = 0; i < coinData.length; i++) {
var coinPos = coinData[i];
var coin = new Coin(coinPos.x, coinPos.y);
coins.push(coin);
gameContainer.addChild(coin);
}
// Create finish flag
var flag = gameContainer.attachAsset('flag', {
x: 3800,
y: 2320,
anchorX: 0.5,
anchorY: 1
});
// Create UI
var scoreText = new Text2('Score: 0', {
size: 48,
fill: '#ffffff'
});
scoreText.anchor.set(0, 0);
scoreText.x = 50;
scoreText.y = 50;
LK.gui.topLeft.addChild(scoreText);
var instructionText = new Text2('Tap anywhere to jump! Avoid enemies and collect coins!', {
size: 32,
fill: '#ffffff'
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 1024;
instructionText.y = 100;
LK.gui.top.addChild(instructionText);
// Removed jump control button - tap anywhere on screen to jump
game.update = function () {
// Update player (auto-running)
player.update();
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
var enemy = enemies[i];
enemy.update();
}
// Update coins
for (var i = 0; i < coins.length; i++) {
var coin = coins[i];
coin.update();
}
// Check collisions with enemies
for (var i = enemies.length - 1; i >= 0; i--) {
var enemy = enemies[i];
if (enemy.alive && player.intersects(enemy)) {
if (player.velocityY > 0 && player.y < enemy.y - 45) {
// Player jumped on enemy
enemy.defeat();
enemies.splice(i, 1);
player.velocityY = -10; // Bounce
LK.setScore(LK.getScore() + 50);
} else {
// Player hit enemy
LK.getSound('game_over').play();
LK.showGameOver();
return;
}
}
}
// Check collisions with coins
for (var i = coins.length - 1; i >= 0; i--) {
var coin = coins[i];
if (!coin.collected && player.intersects(coin)) {
coin.collect();
coins.splice(i, 1);
}
}
// Check if player reached flag
if (player.intersects(flag)) {
LK.showYouWin();
return;
}
// Check if player fell off screen
if (player.y > 2800) {
LK.getSound('game_over').play();
LK.showGameOver();
return;
}
// Update camera
gameContainer.x = -cameraX;
// Update score display
scoreText.setText('Score: ' + LK.getScore());
};