/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 8; self.rotationSpeed = 0.1; self.update = function () { self.y += self.speed; coinGraphics.rotation += self.rotationSpeed; }; return self; }); var EnemyCar = Container.expand(function (carType) { var self = Container.call(this); var assetName = carType || 'enemyCar1'; var carGraphics = self.attachAsset(assetName, { anchorX: 0.5, anchorY: 0.5 }); self.speed = 6 + Math.random() * 4; self.update = function () { self.y += self.speed; }; return self; }); var PlayerCar = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('playerCar', { anchorX: 0.5, anchorY: 0.5 }); self.targetX = 0; self.speed = 8; self.update = function () { // Smooth movement towards target position var diff = self.targetX - self.x; if (Math.abs(diff) > 2) { self.x += diff * 0.15; } else { self.x = self.targetX; } }; return self; }); var RoadLine = Container.expand(function () { var self = Container.call(this); var lineGraphics = self.attachAsset('roadLine', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 12; self.update = function () { self.y += self.speed; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x2c3e50 }); /**** * Game Code ****/ // Highway lanes positions var lanePositions = [512, 768, 1024, 1280, 1536]; var currentLane = 2; // Start in middle lane // Game objects arrays var enemyCars = []; var coins = []; var roadLines = []; // Game variables var gameSpeed = 1; var spawnTimer = 0; var coinSpawnTimer = 0; var roadLineTimer = 0; var survivalTime = 0; // Create player car var playerCar = game.addChild(new PlayerCar()); playerCar.x = lanePositions[currentLane]; playerCar.y = 2400; playerCar.targetX = playerCar.x; // UI Elements var scoreTxt = new Text2('Score: 0', { size: 80, fill: '#ffffff' }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var speedTxt = new Text2('Speed: 1x', { size: 60, fill: '#ffffff' }); speedTxt.anchor.set(1, 0); LK.gui.topRight.addChild(speedTxt); // Touch/drag handling var dragStartX = 0; var isDragging = false; function handleMove(x, y, obj) { if (isDragging) { var deltaX = x - dragStartX; var newLane = Math.round((playerCar.x + deltaX - lanePositions[0]) / (lanePositions[1] - lanePositions[0])); newLane = Math.max(0, Math.min(4, newLane)); playerCar.targetX = lanePositions[newLane]; currentLane = newLane; } } game.move = handleMove; game.down = function (x, y, obj) { isDragging = true; dragStartX = x; }; game.up = function (x, y, obj) { isDragging = false; }; // Spawn enemy car function spawnEnemyCar() { var carTypes = ['enemyCar1', 'enemyCar2', 'enemyCar3']; var carType = carTypes[Math.floor(Math.random() * carTypes.length)]; var enemy = new EnemyCar(carType); var availableLanes = []; for (var i = 0; i < lanePositions.length; i++) { var tooClose = false; for (var j = 0; j < enemyCars.length; j++) { if (Math.abs(enemyCars[j].x - lanePositions[i]) < 50 && enemyCars[j].y < 300) { tooClose = true; break; } } if (!tooClose) { availableLanes.push(i); } } if (availableLanes.length > 0) { var laneIndex = availableLanes[Math.floor(Math.random() * availableLanes.length)]; enemy.x = lanePositions[laneIndex]; enemy.y = -100; enemy.speed *= gameSpeed; enemyCars.push(enemy); game.addChild(enemy); } } // Spawn coin function spawnCoin() { var coin = new Coin(); var laneIndex = Math.floor(Math.random() * lanePositions.length); coin.x = lanePositions[laneIndex]; coin.y = -50; coin.speed *= gameSpeed; coins.push(coin); game.addChild(coin); } // Spawn road line function spawnRoadLine() { for (var i = 0; i < 4; i++) { var roadLine = new RoadLine(); roadLine.x = lanePositions[i] + (lanePositions[i + 1] - lanePositions[i]) / 2; roadLine.y = -40; roadLine.speed *= gameSpeed; roadLines.push(roadLine); game.addChild(roadLine); } } game.update = function () { survivalTime += 1; // Increase game speed gradually gameSpeed = 1 + survivalTime / 3600 * 2; // Increases over 1 minute speedTxt.setText('Speed: ' + gameSpeed.toFixed(1) + 'x'); // Update score based on survival time var currentScore = Math.floor(survivalTime / 10); LK.setScore(currentScore); scoreTxt.setText('Score: ' + LK.getScore()); // Spawn timers spawnTimer++; coinSpawnTimer++; roadLineTimer++; // Spawn enemy cars var spawnRate = Math.max(60 - Math.floor(survivalTime / 100), 30); if (spawnTimer >= spawnRate) { spawnEnemyCar(); spawnTimer = 0; } // Spawn coins if (coinSpawnTimer >= 180) { if (Math.random() < 0.7) { spawnCoin(); } coinSpawnTimer = 0; } // Spawn road lines if (roadLineTimer >= 20) { spawnRoadLine(); roadLineTimer = 0; } // Check enemy car collisions and cleanup for (var i = enemyCars.length - 1; i >= 0; i--) { var enemy = enemyCars[i]; // Initialize last position tracking if (enemy.lastY === undefined) enemy.lastY = enemy.y; // Check collision with player if (enemy.intersects(playerCar)) { // Flash screen red and end game LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } // Remove if off screen if (enemy.lastY <= 2800 && enemy.y > 2800) { enemy.destroy(); enemyCars.splice(i, 1); } else { enemy.lastY = enemy.y; } } // Check coin collection and cleanup for (var i = coins.length - 1; i >= 0; i--) { var coin = coins[i]; if (coin.lastY === undefined) coin.lastY = coin.y; if (coin.lastCollected === undefined) coin.lastCollected = false; var currentCollected = coin.intersects(playerCar); if (!coin.lastCollected && currentCollected) { // Coin collected LK.setScore(LK.getScore() + 10); scoreTxt.setText('Score: ' + LK.getScore()); LK.getSound('coinCollect').play(); coin.destroy(); coins.splice(i, 1); continue; } // Remove if off screen if (coin.lastY <= 2800 && coin.y > 2800) { coin.destroy(); coins.splice(i, 1); } else { coin.lastY = coin.y; coin.lastCollected = currentCollected; } } // Cleanup road lines for (var i = roadLines.length - 1; i >= 0; i--) { var roadLine = roadLines[i]; if (roadLine.lastY === undefined) roadLine.lastY = roadLine.y; if (roadLine.lastY <= 2800 && roadLine.y > 2800) { roadLine.destroy(); roadLines.splice(i, 1); } else { roadLine.lastY = roadLine.y; } } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.rotationSpeed = 0.1;
self.update = function () {
self.y += self.speed;
coinGraphics.rotation += self.rotationSpeed;
};
return self;
});
var EnemyCar = Container.expand(function (carType) {
var self = Container.call(this);
var assetName = carType || 'enemyCar1';
var carGraphics = self.attachAsset(assetName, {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 6 + Math.random() * 4;
self.update = function () {
self.y += self.speed;
};
return self;
});
var PlayerCar = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('playerCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.speed = 8;
self.update = function () {
// Smooth movement towards target position
var diff = self.targetX - self.x;
if (Math.abs(diff) > 2) {
self.x += diff * 0.15;
} else {
self.x = self.targetX;
}
};
return self;
});
var RoadLine = Container.expand(function () {
var self = Container.call(this);
var lineGraphics = self.attachAsset('roadLine', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 12;
self.update = function () {
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Highway lanes positions
var lanePositions = [512, 768, 1024, 1280, 1536];
var currentLane = 2; // Start in middle lane
// Game objects arrays
var enemyCars = [];
var coins = [];
var roadLines = [];
// Game variables
var gameSpeed = 1;
var spawnTimer = 0;
var coinSpawnTimer = 0;
var roadLineTimer = 0;
var survivalTime = 0;
// Create player car
var playerCar = game.addChild(new PlayerCar());
playerCar.x = lanePositions[currentLane];
playerCar.y = 2400;
playerCar.targetX = playerCar.x;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: '#ffffff'
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var speedTxt = new Text2('Speed: 1x', {
size: 60,
fill: '#ffffff'
});
speedTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(speedTxt);
// Touch/drag handling
var dragStartX = 0;
var isDragging = false;
function handleMove(x, y, obj) {
if (isDragging) {
var deltaX = x - dragStartX;
var newLane = Math.round((playerCar.x + deltaX - lanePositions[0]) / (lanePositions[1] - lanePositions[0]));
newLane = Math.max(0, Math.min(4, newLane));
playerCar.targetX = lanePositions[newLane];
currentLane = newLane;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
isDragging = true;
dragStartX = x;
};
game.up = function (x, y, obj) {
isDragging = false;
};
// Spawn enemy car
function spawnEnemyCar() {
var carTypes = ['enemyCar1', 'enemyCar2', 'enemyCar3'];
var carType = carTypes[Math.floor(Math.random() * carTypes.length)];
var enemy = new EnemyCar(carType);
var availableLanes = [];
for (var i = 0; i < lanePositions.length; i++) {
var tooClose = false;
for (var j = 0; j < enemyCars.length; j++) {
if (Math.abs(enemyCars[j].x - lanePositions[i]) < 50 && enemyCars[j].y < 300) {
tooClose = true;
break;
}
}
if (!tooClose) {
availableLanes.push(i);
}
}
if (availableLanes.length > 0) {
var laneIndex = availableLanes[Math.floor(Math.random() * availableLanes.length)];
enemy.x = lanePositions[laneIndex];
enemy.y = -100;
enemy.speed *= gameSpeed;
enemyCars.push(enemy);
game.addChild(enemy);
}
}
// Spawn coin
function spawnCoin() {
var coin = new Coin();
var laneIndex = Math.floor(Math.random() * lanePositions.length);
coin.x = lanePositions[laneIndex];
coin.y = -50;
coin.speed *= gameSpeed;
coins.push(coin);
game.addChild(coin);
}
// Spawn road line
function spawnRoadLine() {
for (var i = 0; i < 4; i++) {
var roadLine = new RoadLine();
roadLine.x = lanePositions[i] + (lanePositions[i + 1] - lanePositions[i]) / 2;
roadLine.y = -40;
roadLine.speed *= gameSpeed;
roadLines.push(roadLine);
game.addChild(roadLine);
}
}
game.update = function () {
survivalTime += 1;
// Increase game speed gradually
gameSpeed = 1 + survivalTime / 3600 * 2; // Increases over 1 minute
speedTxt.setText('Speed: ' + gameSpeed.toFixed(1) + 'x');
// Update score based on survival time
var currentScore = Math.floor(survivalTime / 10);
LK.setScore(currentScore);
scoreTxt.setText('Score: ' + LK.getScore());
// Spawn timers
spawnTimer++;
coinSpawnTimer++;
roadLineTimer++;
// Spawn enemy cars
var spawnRate = Math.max(60 - Math.floor(survivalTime / 100), 30);
if (spawnTimer >= spawnRate) {
spawnEnemyCar();
spawnTimer = 0;
}
// Spawn coins
if (coinSpawnTimer >= 180) {
if (Math.random() < 0.7) {
spawnCoin();
}
coinSpawnTimer = 0;
}
// Spawn road lines
if (roadLineTimer >= 20) {
spawnRoadLine();
roadLineTimer = 0;
}
// Check enemy car collisions and cleanup
for (var i = enemyCars.length - 1; i >= 0; i--) {
var enemy = enemyCars[i];
// Initialize last position tracking
if (enemy.lastY === undefined) enemy.lastY = enemy.y;
// Check collision with player
if (enemy.intersects(playerCar)) {
// Flash screen red and end game
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
// Remove if off screen
if (enemy.lastY <= 2800 && enemy.y > 2800) {
enemy.destroy();
enemyCars.splice(i, 1);
} else {
enemy.lastY = enemy.y;
}
}
// Check coin collection and cleanup
for (var i = coins.length - 1; i >= 0; i--) {
var coin = coins[i];
if (coin.lastY === undefined) coin.lastY = coin.y;
if (coin.lastCollected === undefined) coin.lastCollected = false;
var currentCollected = coin.intersects(playerCar);
if (!coin.lastCollected && currentCollected) {
// Coin collected
LK.setScore(LK.getScore() + 10);
scoreTxt.setText('Score: ' + LK.getScore());
LK.getSound('coinCollect').play();
coin.destroy();
coins.splice(i, 1);
continue;
}
// Remove if off screen
if (coin.lastY <= 2800 && coin.y > 2800) {
coin.destroy();
coins.splice(i, 1);
} else {
coin.lastY = coin.y;
coin.lastCollected = currentCollected;
}
}
// Cleanup road lines
for (var i = roadLines.length - 1; i >= 0; i--) {
var roadLine = roadLines[i];
if (roadLine.lastY === undefined) roadLine.lastY = roadLine.y;
if (roadLine.lastY <= 2800 && roadLine.y > 2800) {
roadLine.destroy();
roadLines.splice(i, 1);
} else {
roadLine.lastY = roadLine.y;
}
}
};