/****
* Classes
****/
var RoadMarking = Container.expand(function () {
var self = Container.call(this);
var markingGraphics = self.attachAsset('roadLane', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.update = function () {
self.y += self.speed;
};
return self;
});
var TrafficCar = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('trafficCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.passed = false;
self.update = function () {
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game dimensions: 2048x2732
var LANE_WIDTH = 400;
var LANE_COUNT = 3;
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
// Lane positions (center of each lane)
var lanePositions = [GAME_WIDTH / 2 - LANE_WIDTH,
// Left lane
GAME_WIDTH / 2,
// Center lane
GAME_WIDTH / 2 + LANE_WIDTH // Right lane
];
// Player car
var playerCar = game.addChild(LK.getAsset('playerCar', {
anchorX: 0.5,
anchorY: 0.5
}));
playerCar.x = lanePositions[1]; // Start in center lane
playerCar.y = GAME_HEIGHT - 300;
var currentLane = 1;
// Game variables
var trafficCars = [];
var roadMarkings = [];
var gameSpeed = 8;
var spawnTimer = 0;
var spawnInterval = 90;
var markingTimer = 0;
var markingInterval = 30;
var musicChanged = false;
// Score display
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 100;
// Speed display
var speedTxt = new Text2('Speed: 3x', {
size: 50,
fill: 0xFFFFFF
});
speedTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(speedTxt);
speedTxt.x = -50;
speedTxt.y = 200;
function updateSpeedDisplay() {
var baseSpeedLevel = selectedDifficulty === 'impossible' ? 10 : selectedDifficulty === 'hard' ? 6 : 3;
var speedIncrease;
if (selectedDifficulty === 'impossible') {
speedIncrease = Math.floor(LK.getScore() / 20);
} else if (selectedDifficulty === 'hard') {
speedIncrease = Math.floor(LK.getScore() / 30);
} else {
speedIncrease = Math.floor(LK.getScore() / 50);
}
var speedLevel = speedIncrease + baseSpeedLevel;
speedTxt.setText('Speed: ' + speedLevel + 'x');
}
function spawnTrafficCar() {
// After 650 points, spawn 3 cars (2 in one lane, 1 in adjacent) for maximum difficulty
if (LK.getScore() >= 650) {
// Spawn 2 cars in a random lane with some vertical spacing
var randomLane = Math.floor(Math.random() * LANE_COUNT);
// First car
var car1 = new TrafficCar();
car1.x = lanePositions[randomLane];
car1.y = -100;
car1.speed = gameSpeed;
trafficCars.push(car1);
game.addChild(car1);
// Second car with spacing
var car2 = new TrafficCar();
car2.x = lanePositions[randomLane];
car2.y = -350; // Spawn second car further up with spacing
car2.speed = gameSpeed;
trafficCars.push(car2);
game.addChild(car2);
// Third car in adjacent lane (50% chance)
if (Math.random() < 0.5) {
var adjacentLanes = [];
if (randomLane > 0) adjacentLanes.push(randomLane - 1);
if (randomLane < LANE_COUNT - 1) adjacentLanes.push(randomLane + 1);
if (adjacentLanes.length > 0) {
var adjacentLane = adjacentLanes[Math.floor(Math.random() * adjacentLanes.length)];
var car3 = new TrafficCar();
car3.x = lanePositions[adjacentLane];
car3.y = -225; // Position between the two cars vertically
car3.speed = gameSpeed;
trafficCars.push(car3);
game.addChild(car3);
}
}
} else if (LK.getScore() >= 450) {
// Spawn 2 cars in a random lane with some vertical spacing
var randomLane = Math.floor(Math.random() * LANE_COUNT);
// First car
var car1 = new TrafficCar();
car1.x = lanePositions[randomLane];
car1.y = -100;
car1.speed = gameSpeed;
trafficCars.push(car1);
game.addChild(car1);
// Second car with spacing
var car2 = new TrafficCar();
car2.x = lanePositions[randomLane];
car2.y = -350; // Spawn second car further up with spacing
car2.speed = gameSpeed;
trafficCars.push(car2);
game.addChild(car2);
} else {
// Original behavior - spawn single car
var car = new TrafficCar();
var randomLane = Math.floor(Math.random() * LANE_COUNT);
car.x = lanePositions[randomLane];
car.y = -100;
car.speed = gameSpeed;
trafficCars.push(car);
game.addChild(car);
}
}
function spawnRoadMarking() {
for (var i = 0; i < LANE_COUNT - 1; i++) {
var marking = new RoadMarking();
marking.x = GAME_WIDTH / 2 - LANE_WIDTH / 2 + i * LANE_WIDTH;
marking.y = -50;
marking.speed = gameSpeed;
roadMarkings.push(marking);
game.addChild(marking);
}
}
function changeLane(direction) {
var targetLane = currentLane + direction;
if (targetLane >= 0 && targetLane < LANE_COUNT) {
currentLane = targetLane;
playerCar.x = lanePositions[currentLane];
}
}
// Touch controls
game.down = function (x, y, obj) {
// Only allow lane changes if game has started
if (!gameStarted) return;
if (x < GAME_WIDTH / 2) {
// Left side tap - move left
changeLane(-1);
} else {
// Right side tap - move right
changeLane(1);
}
};
// Game state variables
var gameStarted = false;
var selectedDifficulty = 'easy'; // Default difficulty
// Difficulty selection buttons
var easyButton = new Text2('EASY', {
size: 120,
fill: 0x00ff00
});
easyButton.anchor.set(0.5, 0.5);
easyButton.x = GAME_WIDTH / 2;
easyButton.y = GAME_HEIGHT / 2 - 150;
game.addChild(easyButton);
var hardButton = new Text2('HARD', {
size: 120,
fill: 0xff0000
});
hardButton.anchor.set(0.5, 0.5);
hardButton.x = GAME_WIDTH / 2;
hardButton.y = GAME_HEIGHT / 2;
game.addChild(hardButton);
var impossibleButton = new Text2('IMPOSSIBLE', {
size: 120,
fill: 0xff00ff
});
impossibleButton.anchor.set(0.5, 0.5);
impossibleButton.x = GAME_WIDTH / 2;
impossibleButton.y = GAME_HEIGHT / 2 + 200;
game.addChild(impossibleButton);
// Button interaction handlers
easyButton.down = function (x, y, obj) {
selectedDifficulty = 'easy';
startGame();
};
hardButton.down = function (x, y, obj) {
selectedDifficulty = 'hard';
startGame();
};
impossibleButton.down = function (x, y, obj) {
selectedDifficulty = 'impossible';
startGame();
};
function startGame() {
gameStarted = true;
// Remove difficulty buttons
easyButton.destroy();
hardButton.destroy();
impossibleButton.destroy();
// Start background music
LK.playMusic('highway_music');
// Set initial speed display based on difficulty
if (selectedDifficulty === 'impossible') {
speedTxt.setText('Speed: 10x');
} else if (selectedDifficulty === 'hard') {
speedTxt.setText('Speed: 6x');
} else {
speedTxt.setText('Speed: 3x');
}
}
// Main game update
game.update = function () {
// Only update game if started
if (!gameStarted) return;
// Update speed based on score and difficulty
var baseSpeedLevel = selectedDifficulty === 'impossible' ? 10 : selectedDifficulty === 'hard' ? 6 : 3;
var speedIncrease;
if (selectedDifficulty === 'impossible') {
speedIncrease = Math.floor(LK.getScore() / 20);
} else if (selectedDifficulty === 'hard') {
speedIncrease = Math.floor(LK.getScore() / 30);
} else {
speedIncrease = Math.floor(LK.getScore() / 50);
}
var speedLevel = speedIncrease + baseSpeedLevel;
gameSpeed = 8 + (speedLevel - 1) * 2;
// Change music at 450 points
if (LK.getScore() >= 450 && !musicChanged) {
musicChanged = true;
LK.playMusic('highway_music_fast');
}
// Spawn traffic cars
spawnTimer++;
if (spawnTimer >= spawnInterval) {
spawnTrafficCar();
spawnTimer = 0;
spawnInterval = Math.max(30, 90 - speedLevel * 5); // Spawn more frequently as speed increases
}
// Spawn road markings
markingTimer++;
if (markingTimer >= markingInterval) {
spawnRoadMarking();
markingTimer = 0;
}
// Update traffic cars
for (var i = trafficCars.length - 1; i >= 0; i--) {
var car = trafficCars[i];
car.speed = gameSpeed;
// Check if car passed player (award points)
if (!car.passed && car.y > playerCar.y + 150) {
car.passed = true;
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
updateSpeedDisplay();
LK.getSound('pass').play();
}
// Check collision with player
if (car.intersects(playerCar)) {
LK.getSound('crash').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
// Remove cars that are off screen
if (car.y > GAME_HEIGHT + 100) {
car.destroy();
trafficCars.splice(i, 1);
}
}
// Update road markings
for (var j = roadMarkings.length - 1; j >= 0; j--) {
var marking = roadMarkings[j];
marking.speed = gameSpeed;
// Remove markings that are off screen
if (marking.y > GAME_HEIGHT + 50) {
marking.destroy();
roadMarkings.splice(j, 1);
}
}
}; /****
* Classes
****/
var RoadMarking = Container.expand(function () {
var self = Container.call(this);
var markingGraphics = self.attachAsset('roadLane', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.update = function () {
self.y += self.speed;
};
return self;
});
var TrafficCar = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('trafficCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.passed = false;
self.update = function () {
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game dimensions: 2048x2732
var LANE_WIDTH = 400;
var LANE_COUNT = 3;
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
// Lane positions (center of each lane)
var lanePositions = [GAME_WIDTH / 2 - LANE_WIDTH,
// Left lane
GAME_WIDTH / 2,
// Center lane
GAME_WIDTH / 2 + LANE_WIDTH // Right lane
];
// Player car
var playerCar = game.addChild(LK.getAsset('playerCar', {
anchorX: 0.5,
anchorY: 0.5
}));
playerCar.x = lanePositions[1]; // Start in center lane
playerCar.y = GAME_HEIGHT - 300;
var currentLane = 1;
// Game variables
var trafficCars = [];
var roadMarkings = [];
var gameSpeed = 8;
var spawnTimer = 0;
var spawnInterval = 90;
var markingTimer = 0;
var markingInterval = 30;
var musicChanged = false;
// Score display
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 100;
// Speed display
var speedTxt = new Text2('Speed: 3x', {
size: 50,
fill: 0xFFFFFF
});
speedTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(speedTxt);
speedTxt.x = -50;
speedTxt.y = 200;
function updateSpeedDisplay() {
var baseSpeedLevel = selectedDifficulty === 'impossible' ? 10 : selectedDifficulty === 'hard' ? 6 : 3;
var speedIncrease;
if (selectedDifficulty === 'impossible') {
speedIncrease = Math.floor(LK.getScore() / 20);
} else if (selectedDifficulty === 'hard') {
speedIncrease = Math.floor(LK.getScore() / 30);
} else {
speedIncrease = Math.floor(LK.getScore() / 50);
}
var speedLevel = speedIncrease + baseSpeedLevel;
speedTxt.setText('Speed: ' + speedLevel + 'x');
}
function spawnTrafficCar() {
// After 650 points, spawn 3 cars (2 in one lane, 1 in adjacent) for maximum difficulty
if (LK.getScore() >= 650) {
// Spawn 2 cars in a random lane with some vertical spacing
var randomLane = Math.floor(Math.random() * LANE_COUNT);
// First car
var car1 = new TrafficCar();
car1.x = lanePositions[randomLane];
car1.y = -100;
car1.speed = gameSpeed;
trafficCars.push(car1);
game.addChild(car1);
// Second car with spacing
var car2 = new TrafficCar();
car2.x = lanePositions[randomLane];
car2.y = -350; // Spawn second car further up with spacing
car2.speed = gameSpeed;
trafficCars.push(car2);
game.addChild(car2);
// Third car in adjacent lane (50% chance)
if (Math.random() < 0.5) {
var adjacentLanes = [];
if (randomLane > 0) adjacentLanes.push(randomLane - 1);
if (randomLane < LANE_COUNT - 1) adjacentLanes.push(randomLane + 1);
if (adjacentLanes.length > 0) {
var adjacentLane = adjacentLanes[Math.floor(Math.random() * adjacentLanes.length)];
var car3 = new TrafficCar();
car3.x = lanePositions[adjacentLane];
car3.y = -225; // Position between the two cars vertically
car3.speed = gameSpeed;
trafficCars.push(car3);
game.addChild(car3);
}
}
} else if (LK.getScore() >= 450) {
// Spawn 2 cars in a random lane with some vertical spacing
var randomLane = Math.floor(Math.random() * LANE_COUNT);
// First car
var car1 = new TrafficCar();
car1.x = lanePositions[randomLane];
car1.y = -100;
car1.speed = gameSpeed;
trafficCars.push(car1);
game.addChild(car1);
// Second car with spacing
var car2 = new TrafficCar();
car2.x = lanePositions[randomLane];
car2.y = -350; // Spawn second car further up with spacing
car2.speed = gameSpeed;
trafficCars.push(car2);
game.addChild(car2);
} else {
// Original behavior - spawn single car
var car = new TrafficCar();
var randomLane = Math.floor(Math.random() * LANE_COUNT);
car.x = lanePositions[randomLane];
car.y = -100;
car.speed = gameSpeed;
trafficCars.push(car);
game.addChild(car);
}
}
function spawnRoadMarking() {
for (var i = 0; i < LANE_COUNT - 1; i++) {
var marking = new RoadMarking();
marking.x = GAME_WIDTH / 2 - LANE_WIDTH / 2 + i * LANE_WIDTH;
marking.y = -50;
marking.speed = gameSpeed;
roadMarkings.push(marking);
game.addChild(marking);
}
}
function changeLane(direction) {
var targetLane = currentLane + direction;
if (targetLane >= 0 && targetLane < LANE_COUNT) {
currentLane = targetLane;
playerCar.x = lanePositions[currentLane];
}
}
// Touch controls
game.down = function (x, y, obj) {
// Only allow lane changes if game has started
if (!gameStarted) return;
if (x < GAME_WIDTH / 2) {
// Left side tap - move left
changeLane(-1);
} else {
// Right side tap - move right
changeLane(1);
}
};
// Game state variables
var gameStarted = false;
var selectedDifficulty = 'easy'; // Default difficulty
// Difficulty selection buttons
var easyButton = new Text2('EASY', {
size: 120,
fill: 0x00ff00
});
easyButton.anchor.set(0.5, 0.5);
easyButton.x = GAME_WIDTH / 2;
easyButton.y = GAME_HEIGHT / 2 - 150;
game.addChild(easyButton);
var hardButton = new Text2('HARD', {
size: 120,
fill: 0xff0000
});
hardButton.anchor.set(0.5, 0.5);
hardButton.x = GAME_WIDTH / 2;
hardButton.y = GAME_HEIGHT / 2;
game.addChild(hardButton);
var impossibleButton = new Text2('IMPOSSIBLE', {
size: 120,
fill: 0xff00ff
});
impossibleButton.anchor.set(0.5, 0.5);
impossibleButton.x = GAME_WIDTH / 2;
impossibleButton.y = GAME_HEIGHT / 2 + 200;
game.addChild(impossibleButton);
// Button interaction handlers
easyButton.down = function (x, y, obj) {
selectedDifficulty = 'easy';
startGame();
};
hardButton.down = function (x, y, obj) {
selectedDifficulty = 'hard';
startGame();
};
impossibleButton.down = function (x, y, obj) {
selectedDifficulty = 'impossible';
startGame();
};
function startGame() {
gameStarted = true;
// Remove difficulty buttons
easyButton.destroy();
hardButton.destroy();
impossibleButton.destroy();
// Start background music
LK.playMusic('highway_music');
// Set initial speed display based on difficulty
if (selectedDifficulty === 'impossible') {
speedTxt.setText('Speed: 10x');
} else if (selectedDifficulty === 'hard') {
speedTxt.setText('Speed: 6x');
} else {
speedTxt.setText('Speed: 3x');
}
}
// Main game update
game.update = function () {
// Only update game if started
if (!gameStarted) return;
// Update speed based on score and difficulty
var baseSpeedLevel = selectedDifficulty === 'impossible' ? 10 : selectedDifficulty === 'hard' ? 6 : 3;
var speedIncrease;
if (selectedDifficulty === 'impossible') {
speedIncrease = Math.floor(LK.getScore() / 20);
} else if (selectedDifficulty === 'hard') {
speedIncrease = Math.floor(LK.getScore() / 30);
} else {
speedIncrease = Math.floor(LK.getScore() / 50);
}
var speedLevel = speedIncrease + baseSpeedLevel;
gameSpeed = 8 + (speedLevel - 1) * 2;
// Change music at 450 points
if (LK.getScore() >= 450 && !musicChanged) {
musicChanged = true;
LK.playMusic('highway_music_fast');
}
// Spawn traffic cars
spawnTimer++;
if (spawnTimer >= spawnInterval) {
spawnTrafficCar();
spawnTimer = 0;
spawnInterval = Math.max(30, 90 - speedLevel * 5); // Spawn more frequently as speed increases
}
// Spawn road markings
markingTimer++;
if (markingTimer >= markingInterval) {
spawnRoadMarking();
markingTimer = 0;
}
// Update traffic cars
for (var i = trafficCars.length - 1; i >= 0; i--) {
var car = trafficCars[i];
car.speed = gameSpeed;
// Check if car passed player (award points)
if (!car.passed && car.y > playerCar.y + 150) {
car.passed = true;
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
updateSpeedDisplay();
LK.getSound('pass').play();
}
// Check collision with player
if (car.intersects(playerCar)) {
LK.getSound('crash').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
// Remove cars that are off screen
if (car.y > GAME_HEIGHT + 100) {
car.destroy();
trafficCars.splice(i, 1);
}
}
// Update road markings
for (var j = roadMarkings.length - 1; j >= 0; j--) {
var marking = roadMarkings[j];
marking.speed = gameSpeed;
// Remove markings that are off screen
if (marking.y > GAME_HEIGHT + 50) {
marking.destroy();
roadMarkings.splice(j, 1);
}
}
};