/****
* 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 = 0;
self.rotationSpeed = 0.1;
self.update = function () {
self.y += self.speed;
self.rotation += self.rotationSpeed;
};
return self;
});
var EnemyCar = Container.expand(function () {
var self = Container.call(this);
var carTypes = ['enemyCar1', 'enemyCar2', 'enemyCar3'];
var randomType = carTypes[Math.floor(Math.random() * carTypes.length)];
var carGraphics = self.attachAsset(randomType, {
anchorX: 0.5,
anchorY: 0.5
});
// Removed tint to disable shadows/coloring
self.speed = 0;
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
});
// Removed tint to disable shadows/coloring
self.lane = 1; // 0 = left, 1 = center, 2 = right
self.targetX = 0;
self.moveSpeed = 12;
self.update = function () {
// Smooth lane changing
if (Math.abs(self.x - self.targetX) > 5) {
if (self.x < self.targetX) {
self.x += self.moveSpeed;
} else {
self.x -= self.moveSpeed;
}
} else {
self.x = self.targetX;
}
};
self.changeLane = function (direction) {
if (direction === 'left' && self.lane > 0) {
self.lane--;
} else if (direction === 'right' && self.lane < 2) {
self.lane++;
}
self.targetX = lanePositions[self.lane];
};
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 = 0;
self.update = function () {
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game constants
var lanePositions = [2048 * 0.25, 2048 * 0.5, 2048 * 0.75];
var gameSpeed = 8;
var baseGameSpeed = 8;
var minGameSpeed = 4;
var maxGameSpeed = 20;
var baseSpeedIncrement = 0.002;
var spawnTimer = 0;
var coinSpawnTimer = 0;
var roadLineTimer = 0;
var distance = 0;
var coins = 0;
var isGameOver = false;
var isGasPressed = false;
var isBrakePressed = false;
var gasAcceleration = 0.3;
var brakeDeceleration = 0.5;
// Game arrays
var enemyCars = [];
var gameCoins = [];
var roadLines = [];
// Create player car
var playerCar = game.addChild(new PlayerCar());
playerCar.x = lanePositions[1];
playerCar.y = 2732 * 0.8;
playerCar.targetX = lanePositions[1];
// Create UI
var distanceText = new Text2('Distance: 0', {
size: 60,
fill: 0xFFFFFF
});
distanceText.anchor.set(0, 0);
distanceText.x = 150;
distanceText.y = 50;
LK.gui.topLeft.addChild(distanceText);
var coinText = new Text2('Coins: 0', {
size: 60,
fill: 0xF1C40F
});
coinText.anchor.set(1, 0);
coinText.x = -50;
coinText.y = 50;
LK.gui.topRight.addChild(coinText);
var speedText = new Text2('Speed: 8', {
size: 50,
fill: 0xFFFFFF
});
speedText.anchor.set(0.5, 0);
speedText.x = 0;
speedText.y = 120;
LK.gui.top.addChild(speedText);
// Gas button
var gasButton = LK.getAsset('gasButton', {
anchorX: 0.5,
anchorY: 1
});
gasButton.x = -200;
gasButton.y = -50;
LK.gui.bottom.addChild(gasButton);
var gasButtonText = new Text2('GAS', {
size: 40,
fill: 0xFFFFFF
});
gasButtonText.anchor.set(0.5, 0.5);
gasButton.addChild(gasButtonText);
// Brake button
var brakeButton = LK.getAsset('brakeButton', {
anchorX: 0.5,
anchorY: 1
});
brakeButton.x = 200;
brakeButton.y = -50;
LK.gui.bottom.addChild(brakeButton);
var brakeButtonText = new Text2('BRAKE', {
size: 40,
fill: 0xFFFFFF
});
brakeButtonText.anchor.set(0.5, 0.5);
brakeButton.addChild(brakeButtonText);
// Input handling
game.down = function (x, y, obj) {
if (isGameOver) return;
// Check if touching gas button area (bottom left)
if (y > 2732 * 0.8 && x < 2048 * 0.4) {
isGasPressed = true;
gasButton.tint = 0x1E8449; // Darker green when pressed
return;
}
// Check if touching brake button area (bottom right)
if (y > 2732 * 0.8 && x > 2048 * 0.6) {
isBrakePressed = true;
brakeButton.tint = 0xC0392B; // Darker red when pressed
return;
}
// Lane changing (upper area)
if (y < 2732 * 0.8) {
if (x < 2048 / 2) {
playerCar.changeLane('left');
} else {
playerCar.changeLane('right');
}
}
};
game.up = function (x, y, obj) {
if (isGameOver) return;
// Release gas button
if (isGasPressed) {
isGasPressed = false;
gasButton.tint = 0xFFFFFF; // Reset to normal color
}
// Release brake button
if (isBrakePressed) {
isBrakePressed = false;
brakeButton.tint = 0xFFFFFF; // Reset to normal color
}
};
// Spawn functions
function spawnEnemyCar() {
var enemy = new EnemyCar();
var lane = Math.floor(Math.random() * 3);
enemy.x = lanePositions[lane];
enemy.y = -200;
enemy.speed = gameSpeed * 0.5; // Make enemy cars move slower
enemy.lastY = enemy.y;
enemy.lastIntersecting = false;
enemy.currentLane = lane;
enemy.isMoving = false;
enemyCars.push(enemy);
game.addChild(enemy);
}
function spawnCoin() {
var coin = new Coin();
var lane = Math.floor(Math.random() * 3);
coin.x = lanePositions[lane];
coin.y = -100;
coin.speed = gameSpeed;
coin.lastY = coin.y;
coin.lastIntersecting = false;
gameCoins.push(coin);
game.addChild(coin);
}
function spawnRoadLine() {
for (var i = 0; i < 2; i++) {
var roadLine = new RoadLine();
roadLine.x = lanePositions[i] + (lanePositions[i + 1] - lanePositions[i]) / 2;
roadLine.y = -100;
roadLine.speed = gameSpeed;
roadLine.lastY = roadLine.y;
roadLines.push(roadLine);
game.addChild(roadLine);
}
}
// Game update loop
game.update = function () {
if (isGameOver) return;
// Update distance and speed
distance += 0.1;
baseGameSpeed += baseSpeedIncrement;
// Handle gas and brake input
if (isGasPressed && !isBrakePressed) {
gameSpeed += gasAcceleration;
if (gameSpeed > maxGameSpeed) gameSpeed = maxGameSpeed;
} else if (isBrakePressed && !isGasPressed) {
gameSpeed -= brakeDeceleration;
if (gameSpeed < minGameSpeed) gameSpeed = minGameSpeed;
} else {
// Return to base speed gradually when neither is pressed
if (gameSpeed > baseGameSpeed) {
gameSpeed -= 0.1;
if (gameSpeed < baseGameSpeed) gameSpeed = baseGameSpeed;
} else if (gameSpeed < baseGameSpeed) {
gameSpeed += 0.1;
if (gameSpeed > baseGameSpeed) gameSpeed = baseGameSpeed;
}
}
// Update UI
distanceText.setText('Distance: ' + Math.floor(distance));
coinText.setText('Coins: ' + coins);
speedText.setText('Speed: ' + Math.floor(gameSpeed));
// Spawn timers
spawnTimer++;
coinSpawnTimer++;
roadLineTimer++;
// Spawn enemy cars
if (spawnTimer >= Math.max(60 - Math.floor(distance / 10), 30)) {
spawnEnemyCar();
spawnTimer = 0;
}
// Spawn coins
if (coinSpawnTimer >= 120 + Math.floor(Math.random() * 120)) {
spawnCoin();
coinSpawnTimer = 0;
}
// Spawn road lines
if (roadLineTimer >= 40) {
spawnRoadLine();
roadLineTimer = 0;
}
// Update and check enemy cars
for (var i = enemyCars.length - 1; i >= 0; i--) {
var enemy = enemyCars[i];
// Initialize tracking variables
if (enemy.lastY === undefined) enemy.lastY = enemy.y;
if (enemy.lastIntersecting === undefined) enemy.lastIntersecting = false;
// Check if enemy went off screen
if (enemy.lastY <= 2800 && enemy.y > 2800) {
enemy.destroy();
enemyCars.splice(i, 1);
continue;
}
// Check collision with player
var currentIntersecting = enemy.intersects(playerCar);
if (!enemy.lastIntersecting && currentIntersecting) {
// Collision detected
LK.getSound('crash').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.setScore(Math.floor(distance) + coins * 10);
isGameOver = true;
LK.showGameOver();
return;
}
// Update last known states
enemy.lastY = enemy.y;
enemy.lastIntersecting = currentIntersecting;
}
// Update and check coins
for (var j = gameCoins.length - 1; j >= 0; j--) {
var coin = gameCoins[j];
// Initialize tracking variables
if (coin.lastY === undefined) coin.lastY = coin.y;
if (coin.lastIntersecting === undefined) coin.lastIntersecting = false;
// Check if coin went off screen
if (coin.lastY <= 2800 && coin.y > 2800) {
coin.destroy();
gameCoins.splice(j, 1);
continue;
}
// Check collection by player
var currentIntersecting = coin.intersects(playerCar);
if (!coin.lastIntersecting && currentIntersecting) {
// Coin collected
LK.getSound('coinCollect').play();
coins++;
// Speed boost platform - increase base speed when collecting coins
baseGameSpeed += 0.1;
if (baseGameSpeed > maxGameSpeed * 0.8) baseGameSpeed = maxGameSpeed * 0.8;
coin.destroy();
gameCoins.splice(j, 1);
continue;
}
// Update last known states
coin.lastY = coin.y;
coin.lastIntersecting = currentIntersecting;
}
// Update road lines
for (var k = roadLines.length - 1; k >= 0; k--) {
var roadLine = roadLines[k];
// Initialize tracking variables
if (roadLine.lastY === undefined) roadLine.lastY = roadLine.y;
// Check if road line went off screen
if (roadLine.lastY <= 2800 && roadLine.y > 2800) {
roadLine.destroy();
roadLines.splice(k, 1);
continue;
}
// Update last known state
roadLine.lastY = roadLine.y;
}
// Update all objects' speed
for (var m = 0; m < enemyCars.length; m++) {
enemyCars[m].speed = gameSpeed * 0.5; // Keep enemy cars slower
}
for (var n = 0; n < gameCoins.length; n++) {
gameCoins[n].speed = gameSpeed;
}
for (var o = 0; o < roadLines.length; o++) {
roadLines[o].speed = gameSpeed;
}
}; /****
* 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 = 0;
self.rotationSpeed = 0.1;
self.update = function () {
self.y += self.speed;
self.rotation += self.rotationSpeed;
};
return self;
});
var EnemyCar = Container.expand(function () {
var self = Container.call(this);
var carTypes = ['enemyCar1', 'enemyCar2', 'enemyCar3'];
var randomType = carTypes[Math.floor(Math.random() * carTypes.length)];
var carGraphics = self.attachAsset(randomType, {
anchorX: 0.5,
anchorY: 0.5
});
// Removed tint to disable shadows/coloring
self.speed = 0;
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
});
// Removed tint to disable shadows/coloring
self.lane = 1; // 0 = left, 1 = center, 2 = right
self.targetX = 0;
self.moveSpeed = 12;
self.update = function () {
// Smooth lane changing
if (Math.abs(self.x - self.targetX) > 5) {
if (self.x < self.targetX) {
self.x += self.moveSpeed;
} else {
self.x -= self.moveSpeed;
}
} else {
self.x = self.targetX;
}
};
self.changeLane = function (direction) {
if (direction === 'left' && self.lane > 0) {
self.lane--;
} else if (direction === 'right' && self.lane < 2) {
self.lane++;
}
self.targetX = lanePositions[self.lane];
};
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 = 0;
self.update = function () {
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game constants
var lanePositions = [2048 * 0.25, 2048 * 0.5, 2048 * 0.75];
var gameSpeed = 8;
var baseGameSpeed = 8;
var minGameSpeed = 4;
var maxGameSpeed = 20;
var baseSpeedIncrement = 0.002;
var spawnTimer = 0;
var coinSpawnTimer = 0;
var roadLineTimer = 0;
var distance = 0;
var coins = 0;
var isGameOver = false;
var isGasPressed = false;
var isBrakePressed = false;
var gasAcceleration = 0.3;
var brakeDeceleration = 0.5;
// Game arrays
var enemyCars = [];
var gameCoins = [];
var roadLines = [];
// Create player car
var playerCar = game.addChild(new PlayerCar());
playerCar.x = lanePositions[1];
playerCar.y = 2732 * 0.8;
playerCar.targetX = lanePositions[1];
// Create UI
var distanceText = new Text2('Distance: 0', {
size: 60,
fill: 0xFFFFFF
});
distanceText.anchor.set(0, 0);
distanceText.x = 150;
distanceText.y = 50;
LK.gui.topLeft.addChild(distanceText);
var coinText = new Text2('Coins: 0', {
size: 60,
fill: 0xF1C40F
});
coinText.anchor.set(1, 0);
coinText.x = -50;
coinText.y = 50;
LK.gui.topRight.addChild(coinText);
var speedText = new Text2('Speed: 8', {
size: 50,
fill: 0xFFFFFF
});
speedText.anchor.set(0.5, 0);
speedText.x = 0;
speedText.y = 120;
LK.gui.top.addChild(speedText);
// Gas button
var gasButton = LK.getAsset('gasButton', {
anchorX: 0.5,
anchorY: 1
});
gasButton.x = -200;
gasButton.y = -50;
LK.gui.bottom.addChild(gasButton);
var gasButtonText = new Text2('GAS', {
size: 40,
fill: 0xFFFFFF
});
gasButtonText.anchor.set(0.5, 0.5);
gasButton.addChild(gasButtonText);
// Brake button
var brakeButton = LK.getAsset('brakeButton', {
anchorX: 0.5,
anchorY: 1
});
brakeButton.x = 200;
brakeButton.y = -50;
LK.gui.bottom.addChild(brakeButton);
var brakeButtonText = new Text2('BRAKE', {
size: 40,
fill: 0xFFFFFF
});
brakeButtonText.anchor.set(0.5, 0.5);
brakeButton.addChild(brakeButtonText);
// Input handling
game.down = function (x, y, obj) {
if (isGameOver) return;
// Check if touching gas button area (bottom left)
if (y > 2732 * 0.8 && x < 2048 * 0.4) {
isGasPressed = true;
gasButton.tint = 0x1E8449; // Darker green when pressed
return;
}
// Check if touching brake button area (bottom right)
if (y > 2732 * 0.8 && x > 2048 * 0.6) {
isBrakePressed = true;
brakeButton.tint = 0xC0392B; // Darker red when pressed
return;
}
// Lane changing (upper area)
if (y < 2732 * 0.8) {
if (x < 2048 / 2) {
playerCar.changeLane('left');
} else {
playerCar.changeLane('right');
}
}
};
game.up = function (x, y, obj) {
if (isGameOver) return;
// Release gas button
if (isGasPressed) {
isGasPressed = false;
gasButton.tint = 0xFFFFFF; // Reset to normal color
}
// Release brake button
if (isBrakePressed) {
isBrakePressed = false;
brakeButton.tint = 0xFFFFFF; // Reset to normal color
}
};
// Spawn functions
function spawnEnemyCar() {
var enemy = new EnemyCar();
var lane = Math.floor(Math.random() * 3);
enemy.x = lanePositions[lane];
enemy.y = -200;
enemy.speed = gameSpeed * 0.5; // Make enemy cars move slower
enemy.lastY = enemy.y;
enemy.lastIntersecting = false;
enemy.currentLane = lane;
enemy.isMoving = false;
enemyCars.push(enemy);
game.addChild(enemy);
}
function spawnCoin() {
var coin = new Coin();
var lane = Math.floor(Math.random() * 3);
coin.x = lanePositions[lane];
coin.y = -100;
coin.speed = gameSpeed;
coin.lastY = coin.y;
coin.lastIntersecting = false;
gameCoins.push(coin);
game.addChild(coin);
}
function spawnRoadLine() {
for (var i = 0; i < 2; i++) {
var roadLine = new RoadLine();
roadLine.x = lanePositions[i] + (lanePositions[i + 1] - lanePositions[i]) / 2;
roadLine.y = -100;
roadLine.speed = gameSpeed;
roadLine.lastY = roadLine.y;
roadLines.push(roadLine);
game.addChild(roadLine);
}
}
// Game update loop
game.update = function () {
if (isGameOver) return;
// Update distance and speed
distance += 0.1;
baseGameSpeed += baseSpeedIncrement;
// Handle gas and brake input
if (isGasPressed && !isBrakePressed) {
gameSpeed += gasAcceleration;
if (gameSpeed > maxGameSpeed) gameSpeed = maxGameSpeed;
} else if (isBrakePressed && !isGasPressed) {
gameSpeed -= brakeDeceleration;
if (gameSpeed < minGameSpeed) gameSpeed = minGameSpeed;
} else {
// Return to base speed gradually when neither is pressed
if (gameSpeed > baseGameSpeed) {
gameSpeed -= 0.1;
if (gameSpeed < baseGameSpeed) gameSpeed = baseGameSpeed;
} else if (gameSpeed < baseGameSpeed) {
gameSpeed += 0.1;
if (gameSpeed > baseGameSpeed) gameSpeed = baseGameSpeed;
}
}
// Update UI
distanceText.setText('Distance: ' + Math.floor(distance));
coinText.setText('Coins: ' + coins);
speedText.setText('Speed: ' + Math.floor(gameSpeed));
// Spawn timers
spawnTimer++;
coinSpawnTimer++;
roadLineTimer++;
// Spawn enemy cars
if (spawnTimer >= Math.max(60 - Math.floor(distance / 10), 30)) {
spawnEnemyCar();
spawnTimer = 0;
}
// Spawn coins
if (coinSpawnTimer >= 120 + Math.floor(Math.random() * 120)) {
spawnCoin();
coinSpawnTimer = 0;
}
// Spawn road lines
if (roadLineTimer >= 40) {
spawnRoadLine();
roadLineTimer = 0;
}
// Update and check enemy cars
for (var i = enemyCars.length - 1; i >= 0; i--) {
var enemy = enemyCars[i];
// Initialize tracking variables
if (enemy.lastY === undefined) enemy.lastY = enemy.y;
if (enemy.lastIntersecting === undefined) enemy.lastIntersecting = false;
// Check if enemy went off screen
if (enemy.lastY <= 2800 && enemy.y > 2800) {
enemy.destroy();
enemyCars.splice(i, 1);
continue;
}
// Check collision with player
var currentIntersecting = enemy.intersects(playerCar);
if (!enemy.lastIntersecting && currentIntersecting) {
// Collision detected
LK.getSound('crash').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.setScore(Math.floor(distance) + coins * 10);
isGameOver = true;
LK.showGameOver();
return;
}
// Update last known states
enemy.lastY = enemy.y;
enemy.lastIntersecting = currentIntersecting;
}
// Update and check coins
for (var j = gameCoins.length - 1; j >= 0; j--) {
var coin = gameCoins[j];
// Initialize tracking variables
if (coin.lastY === undefined) coin.lastY = coin.y;
if (coin.lastIntersecting === undefined) coin.lastIntersecting = false;
// Check if coin went off screen
if (coin.lastY <= 2800 && coin.y > 2800) {
coin.destroy();
gameCoins.splice(j, 1);
continue;
}
// Check collection by player
var currentIntersecting = coin.intersects(playerCar);
if (!coin.lastIntersecting && currentIntersecting) {
// Coin collected
LK.getSound('coinCollect').play();
coins++;
// Speed boost platform - increase base speed when collecting coins
baseGameSpeed += 0.1;
if (baseGameSpeed > maxGameSpeed * 0.8) baseGameSpeed = maxGameSpeed * 0.8;
coin.destroy();
gameCoins.splice(j, 1);
continue;
}
// Update last known states
coin.lastY = coin.y;
coin.lastIntersecting = currentIntersecting;
}
// Update road lines
for (var k = roadLines.length - 1; k >= 0; k--) {
var roadLine = roadLines[k];
// Initialize tracking variables
if (roadLine.lastY === undefined) roadLine.lastY = roadLine.y;
// Check if road line went off screen
if (roadLine.lastY <= 2800 && roadLine.y > 2800) {
roadLine.destroy();
roadLines.splice(k, 1);
continue;
}
// Update last known state
roadLine.lastY = roadLine.y;
}
// Update all objects' speed
for (var m = 0; m < enemyCars.length; m++) {
enemyCars[m].speed = gameSpeed * 0.5; // Keep enemy cars slower
}
for (var n = 0; n < gameCoins.length; n++) {
gameCoins[n].speed = gameSpeed;
}
for (var o = 0; o < roadLines.length; o++) {
roadLines[o].speed = gameSpeed;
}
};