/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { highScore: 0 }); /**** * Classes ****/ var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.collected = false; self.rotationSpeed = 0.05; self.update = function () { if (!self.collected) { coinGraphics.rotation += self.rotationSpeed; } else { self.alpha -= 0.1; self.y -= 5; self.scale.x *= 0.9; self.scale.y *= 0.9; } }; self.collect = function () { if (!self.collected) { self.collected = true; LK.getSound('coin').play(); tween(self, { alpha: 0 }, { duration: 500, onFinish: function onFinish() { self.destroy(); } }); } }; return self; }); var Lane = Container.expand(function (y) { var self = Container.call(this); var laneGraphics = self.attachAsset('lane', { anchorX: 0.5, anchorY: 0.5 }); self.y = y; self.x = 2048 / 2; // Create lane dividers self.dividers = []; var dividerCount = 15; var spacing = 2048 / dividerCount; for (var i = 0; i < dividerCount; i++) { var divider = self.attachAsset('laneDivider', { anchorX: 0.5, anchorY: 0.5, x: i * spacing, y: 0 }); self.dividers.push(divider); } self.update = function () { for (var i = 0; i < self.dividers.length; i++) { self.dividers[i].x -= 10; if (self.dividers[i].x < -75) { self.dividers[i].x += 2048; } } }; return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 1.0 }); self.vx = 0; self.vy = 0; self.gravity = 1; self.jumpForce = -25; self.isJumping = false; self.isOnVehicle = false; self.currentVehicle = null; self.jump = function () { if (!self.isJumping || self.isOnVehicle) { self.vy = self.jumpForce; self.isJumping = true; self.isOnVehicle = false; self.currentVehicle = null; LK.getSound('jump').play(); } }; self.update = function () { if (self.currentVehicle && self.isOnVehicle) { self.x += self.currentVehicle.speed; } if (self.isJumping && !self.isOnVehicle) { self.vy += self.gravity; self.y += self.vy; } // Keep player within bounds if (self.x < 100) { self.x = 100; } if (self.x > 2048 - 100) { self.x = 2048 - 100; } }; self.down = function (x, y, obj) { self.jump(); }; return self; }); var Vehicle = Container.expand(function (type) { var self = Container.call(this); self.type = type || 'car'; var vehicleGraphics = self.attachAsset(self.type, { anchorX: 0.5, anchorY: 1.0 }); self.width = vehicleGraphics.width; self.height = vehicleGraphics.height; self.speed = self.type === 'car' ? -8 - Math.random() * 5 : -5 - Math.random() * 3; self.update = function () { self.x += self.speed; // Ensure vehicle stays within its lane // Ensure vehicle stays within its lane self.y = baseY + self.laneIndex * laneSpacing; // Reset vehicle position if off-screen if (self.x < -self.width) { self.x = 2048 + self.width; } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ // Game state variables var score = 0; var highScore = storage.highScore || 0; var gameSpeed = 1; var coinCount = 0; var lanes = []; var vehicles = []; var coins = []; var spawnTimer = 0; var coinSpawnTimer = 0; var gameRunning = true; // Create background var sky = game.addChild(LK.getAsset('sky', { anchorX: 0.5, anchorY: 0, x: 2048 / 2, y: 0 })); var city = game.addChild(LK.getAsset('city', { anchorX: 0.5, anchorY: 0, x: 2048 / 2, y: 1000 })); // Create lanes var laneCount = 4; var laneSpacing = 250; var baseY = 1800; for (var i = 0; i < laneCount; i++) { var lane = new Lane(baseY + i * laneSpacing); game.addChild(lane); lanes.push(lane); } // Create player var player = new Player(); player.x = 300; player.y = baseY - 10; // Position slightly above the lane game.addChild(player); // Create score display var scoreTxt = new Text2('SCORE: 0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0, 0); LK.gui.topLeft.addChild(scoreTxt); // Create high score display var highScoreTxt = new Text2('HIGH SCORE: ' + highScore, { size: 60, fill: 0xFFFFFF }); highScoreTxt.anchor.set(1, 0); highScoreTxt.x = -20; highScoreTxt.y = 90; LK.gui.topRight.addChild(highScoreTxt); // Spawn a vehicle on a random lane function spawnVehicle() { var type = Math.random() > 0.3 ? 'car' : 'truck'; var vehicle = new Vehicle(type); var laneIndex = Math.floor(Math.random() * laneCount); vehicle.y = baseY + laneIndex * laneSpacing; vehicle.laneIndex = laneIndex; // Track the lane index for the vehicle vehicle.x = 2048 + vehicle.width; game.addChild(vehicle); vehicles.push(vehicle); return vehicle; } // Spawn a coin at a random position, possibly on top of a vehicle function spawnCoin() { var coin = new Coin(); var onVehicle = Math.random() > 0.5 && vehicles.length > 0; if (onVehicle) { var vehicleIndex = Math.floor(Math.random() * vehicles.length); var vehicle = vehicles[vehicleIndex]; coin.x = vehicle.x; coin.y = vehicle.y - vehicle.height - 50; } else { var laneIndex = Math.floor(Math.random() * laneCount); coin.x = 2048 + 100; coin.y = baseY + laneIndex * laneSpacing - 150; } game.addChild(coin); coins.push(coin); } // Check collision between player and vehicles function checkCollisions() { for (var i = 0; i < vehicles.length; i++) { var vehicle = vehicles[i]; if (player.intersects(vehicle)) { // Check if player is on top of the vehicle if (player.y <= vehicle.y - vehicle.height + 20 && player.vy >= 0) { player.isJumping = false; player.isOnVehicle = true; player.currentVehicle = vehicle; player.y = vehicle.y - vehicle.height; player.vy = 0; } else if (!player.isOnVehicle || player.currentVehicle !== vehicle) { // Collision from the side or bottom gameOver(); } } } // Check ground collision for (var j = 0; j < laneCount; j++) { var laneY = baseY + j * laneSpacing; if (player.y >= laneY && player.y - player.vy < laneY) { player.y = laneY; player.vy = 0; player.isJumping = false; } } // Check coin collection for (var k = coins.length - 1; k >= 0; k--) { if (player.intersects(coins[k]) && !coins[k].collected) { coins[k].collect(); coinCount++; score += 2; updateScore(); } if (coins[k].collected && coins[k].alpha <= 0) { coins.splice(k, 1); } } // Check if player fell off screen if (player.y > 2732 || player.y < 0) { gameOver(); } } // Update score display function updateScore() { score = coinCount * 2; scoreTxt.setText('SCORE: ' + score); if (score > highScore) { highScore = score; storage.highScore = highScore; highScoreTxt.setText('HIGH SCORE: ' + highScore); } } // Game over function function gameOver() { LK.getSound('crash').play(); LK.effects.flashScreen(0xFF0000, 500); LK.showGameOver(); } // Game input handlers game.down = function (x, y, obj) { player.jump(); }; // Main game update loop game.update = function () { if (!gameRunning) { return; } // Update score updateScore(); // Update game entities player.update(); for (var i = 0; i < lanes.length; i++) { lanes[i].update(); } for (var j = vehicles.length - 1; j >= 0; j--) { vehicles[j].update(); // Remove vehicles that are far off screen if (vehicles[j].x < -500) { vehicles[j].destroy(); vehicles.splice(j, 1); } } for (var k = 0; k < coins.length; k++) { coins[k].update(); // Move coins with the game speed if they're not on a vehicle if (coins[k].x > -100) { coins[k].x -= 10; } else { coins[k].destroy(); coins.splice(k, 1); k--; } } // Check for collisions checkCollisions(); // Spawn vehicles spawnTimer++; if (spawnTimer >= 120) { spawnVehicle(); spawnTimer = 0; } // Spawn coins coinSpawnTimer++; if (coinSpawnTimer >= 180) { spawnCoin(); coinSpawnTimer = 0; } // Increase game difficulty over time if (LK.ticks % 600 === 0) { gameSpeed += 0.1; } }; // Start background music LK.playMusic('bgmusic', { fade: { start: 0, end: 0.4, duration: 1000 } });
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.rotationSpeed = 0.05;
self.update = function () {
if (!self.collected) {
coinGraphics.rotation += self.rotationSpeed;
} else {
self.alpha -= 0.1;
self.y -= 5;
self.scale.x *= 0.9;
self.scale.y *= 0.9;
}
};
self.collect = function () {
if (!self.collected) {
self.collected = true;
LK.getSound('coin').play();
tween(self, {
alpha: 0
}, {
duration: 500,
onFinish: function onFinish() {
self.destroy();
}
});
}
};
return self;
});
var Lane = Container.expand(function (y) {
var self = Container.call(this);
var laneGraphics = self.attachAsset('lane', {
anchorX: 0.5,
anchorY: 0.5
});
self.y = y;
self.x = 2048 / 2;
// Create lane dividers
self.dividers = [];
var dividerCount = 15;
var spacing = 2048 / dividerCount;
for (var i = 0; i < dividerCount; i++) {
var divider = self.attachAsset('laneDivider', {
anchorX: 0.5,
anchorY: 0.5,
x: i * spacing,
y: 0
});
self.dividers.push(divider);
}
self.update = function () {
for (var i = 0; i < self.dividers.length; i++) {
self.dividers[i].x -= 10;
if (self.dividers[i].x < -75) {
self.dividers[i].x += 2048;
}
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.vx = 0;
self.vy = 0;
self.gravity = 1;
self.jumpForce = -25;
self.isJumping = false;
self.isOnVehicle = false;
self.currentVehicle = null;
self.jump = function () {
if (!self.isJumping || self.isOnVehicle) {
self.vy = self.jumpForce;
self.isJumping = true;
self.isOnVehicle = false;
self.currentVehicle = null;
LK.getSound('jump').play();
}
};
self.update = function () {
if (self.currentVehicle && self.isOnVehicle) {
self.x += self.currentVehicle.speed;
}
if (self.isJumping && !self.isOnVehicle) {
self.vy += self.gravity;
self.y += self.vy;
}
// Keep player within bounds
if (self.x < 100) {
self.x = 100;
}
if (self.x > 2048 - 100) {
self.x = 2048 - 100;
}
};
self.down = function (x, y, obj) {
self.jump();
};
return self;
});
var Vehicle = Container.expand(function (type) {
var self = Container.call(this);
self.type = type || 'car';
var vehicleGraphics = self.attachAsset(self.type, {
anchorX: 0.5,
anchorY: 1.0
});
self.width = vehicleGraphics.width;
self.height = vehicleGraphics.height;
self.speed = self.type === 'car' ? -8 - Math.random() * 5 : -5 - Math.random() * 3;
self.update = function () {
self.x += self.speed;
// Ensure vehicle stays within its lane
// Ensure vehicle stays within its lane
self.y = baseY + self.laneIndex * laneSpacing;
// Reset vehicle position if off-screen
if (self.x < -self.width) {
self.x = 2048 + self.width;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game state variables
var score = 0;
var highScore = storage.highScore || 0;
var gameSpeed = 1;
var coinCount = 0;
var lanes = [];
var vehicles = [];
var coins = [];
var spawnTimer = 0;
var coinSpawnTimer = 0;
var gameRunning = true;
// Create background
var sky = game.addChild(LK.getAsset('sky', {
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
y: 0
}));
var city = game.addChild(LK.getAsset('city', {
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
y: 1000
}));
// Create lanes
var laneCount = 4;
var laneSpacing = 250;
var baseY = 1800;
for (var i = 0; i < laneCount; i++) {
var lane = new Lane(baseY + i * laneSpacing);
game.addChild(lane);
lanes.push(lane);
}
// Create player
var player = new Player();
player.x = 300;
player.y = baseY - 10; // Position slightly above the lane
game.addChild(player);
// Create score display
var scoreTxt = new Text2('SCORE: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topLeft.addChild(scoreTxt);
// Create high score display
var highScoreTxt = new Text2('HIGH SCORE: ' + highScore, {
size: 60,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(1, 0);
highScoreTxt.x = -20;
highScoreTxt.y = 90;
LK.gui.topRight.addChild(highScoreTxt);
// Spawn a vehicle on a random lane
function spawnVehicle() {
var type = Math.random() > 0.3 ? 'car' : 'truck';
var vehicle = new Vehicle(type);
var laneIndex = Math.floor(Math.random() * laneCount);
vehicle.y = baseY + laneIndex * laneSpacing;
vehicle.laneIndex = laneIndex; // Track the lane index for the vehicle
vehicle.x = 2048 + vehicle.width;
game.addChild(vehicle);
vehicles.push(vehicle);
return vehicle;
}
// Spawn a coin at a random position, possibly on top of a vehicle
function spawnCoin() {
var coin = new Coin();
var onVehicle = Math.random() > 0.5 && vehicles.length > 0;
if (onVehicle) {
var vehicleIndex = Math.floor(Math.random() * vehicles.length);
var vehicle = vehicles[vehicleIndex];
coin.x = vehicle.x;
coin.y = vehicle.y - vehicle.height - 50;
} else {
var laneIndex = Math.floor(Math.random() * laneCount);
coin.x = 2048 + 100;
coin.y = baseY + laneIndex * laneSpacing - 150;
}
game.addChild(coin);
coins.push(coin);
}
// Check collision between player and vehicles
function checkCollisions() {
for (var i = 0; i < vehicles.length; i++) {
var vehicle = vehicles[i];
if (player.intersects(vehicle)) {
// Check if player is on top of the vehicle
if (player.y <= vehicle.y - vehicle.height + 20 && player.vy >= 0) {
player.isJumping = false;
player.isOnVehicle = true;
player.currentVehicle = vehicle;
player.y = vehicle.y - vehicle.height;
player.vy = 0;
} else if (!player.isOnVehicle || player.currentVehicle !== vehicle) {
// Collision from the side or bottom
gameOver();
}
}
}
// Check ground collision
for (var j = 0; j < laneCount; j++) {
var laneY = baseY + j * laneSpacing;
if (player.y >= laneY && player.y - player.vy < laneY) {
player.y = laneY;
player.vy = 0;
player.isJumping = false;
}
}
// Check coin collection
for (var k = coins.length - 1; k >= 0; k--) {
if (player.intersects(coins[k]) && !coins[k].collected) {
coins[k].collect();
coinCount++;
score += 2;
updateScore();
}
if (coins[k].collected && coins[k].alpha <= 0) {
coins.splice(k, 1);
}
}
// Check if player fell off screen
if (player.y > 2732 || player.y < 0) {
gameOver();
}
}
// Update score display
function updateScore() {
score = coinCount * 2;
scoreTxt.setText('SCORE: ' + score);
if (score > highScore) {
highScore = score;
storage.highScore = highScore;
highScoreTxt.setText('HIGH SCORE: ' + highScore);
}
}
// Game over function
function gameOver() {
LK.getSound('crash').play();
LK.effects.flashScreen(0xFF0000, 500);
LK.showGameOver();
}
// Game input handlers
game.down = function (x, y, obj) {
player.jump();
};
// Main game update loop
game.update = function () {
if (!gameRunning) {
return;
}
// Update score
updateScore();
// Update game entities
player.update();
for (var i = 0; i < lanes.length; i++) {
lanes[i].update();
}
for (var j = vehicles.length - 1; j >= 0; j--) {
vehicles[j].update();
// Remove vehicles that are far off screen
if (vehicles[j].x < -500) {
vehicles[j].destroy();
vehicles.splice(j, 1);
}
}
for (var k = 0; k < coins.length; k++) {
coins[k].update();
// Move coins with the game speed if they're not on a vehicle
if (coins[k].x > -100) {
coins[k].x -= 10;
} else {
coins[k].destroy();
coins.splice(k, 1);
k--;
}
}
// Check for collisions
checkCollisions();
// Spawn vehicles
spawnTimer++;
if (spawnTimer >= 120) {
spawnVehicle();
spawnTimer = 0;
}
// Spawn coins
coinSpawnTimer++;
if (coinSpawnTimer >= 180) {
spawnCoin();
coinSpawnTimer = 0;
}
// Increase game difficulty over time
if (LK.ticks % 600 === 0) {
gameSpeed += 0.1;
}
};
// Start background music
LK.playMusic('bgmusic', {
fade: {
start: 0,
end: 0.4,
duration: 1000
}
});
create a monkey. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create banana. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a sky with moon and starts. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a city with buidlings. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a black color road. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a truck. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows