/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highScore: 0
});
/**** 
* Classes
****/ 
var BoostButton = Container.expand(function () {
	var self = Container.call(this);
	var buttonGraphics = self.attachAsset('boostButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.down = function () {
		buttonGraphics.alpha = 0.8;
		if (player && !player.boosting) {
			player.boost();
		}
	};
	self.up = function () {
		buttonGraphics.alpha = 0.5;
	};
	return self;
});
var BrakeButton = Container.expand(function () {
	var self = Container.call(this);
	var buttonGraphics = self.attachAsset('brakeButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.down = function () {
		if (player) {
			player.speed = baseSpeed * 0.2; // Decrease speed rapidly
		}
	};
	self.up = function () {
		if (player) {
			player.speed = baseSpeed; // Restore normal speed
		}
	};
});
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = coinGraphics.width;
	self.height = coinGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
		// Add a spinning effect to the coin
		coinGraphics.rotation += 0.05;
	};
	return self;
});
var EnemyCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('enemyCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
var FastCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('fastCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
var GreenCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('greenCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	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.lane = 1; // 0=left, 1=middle, 2=right
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.moving = false;
	self.boosting = false;
	self.boostTime = 0;
	self.boostDuration = 120; // 2 seconds at 60 fps
	self.switchLane = function (targetLane) {
		if (self.moving || targetLane < 0 || targetLane > 2) {
			return;
		}
		self.moving = true;
		var targetX = getLanePosition(targetLane);
		tween(self, {
			x: targetX
		}, {
			duration: 300,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				self.lane = targetLane;
				self.moving = false;
			}
		});
	};
	self.boost = function () {
		if (self.boosting) {
			return;
		}
		self.boosting = true;
		self.boostTime = self.boostDuration;
		LK.getSound('boost').play();
	};
	self.update = function () {
		if (self.boosting) {
			self.boostTime--;
			if (self.boostTime <= 0) {
				self.boosting = false;
			}
		}
		// Play car movement sound
		LK.getSound('carMove').play();
	};
	return self;
});
var PoliceCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('policeCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.followTime = 180; // Follow for 3 seconds at 60 fps
	self.following = false;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
		self.following = true;
	};
	self.update = function () {
		if (self.following) {
			self.followTime--;
			if (self.followTime <= 0) {
				self.following = false;
			} else {
				// Follow the player's lane
				self.x = getLanePosition(player.lane);
			}
		}
		self.y += self.speed;
	};
	return self;
});
var Road = Container.expand(function () {
	var self = Container.call(this);
	var roadGraphics = self.attachAsset('road', {
		anchorX: 0.5,
		anchorY: 0
	});
	self.laneWidth = roadGraphics.width;
	// Create road lines
	self.lines = [];
	var lineCount = 15;
	var lineSpacing = 2732 / lineCount;
	for (var i = 0; i < lineCount; i++) {
		var line = self.addChild(LK.getAsset('roadLine', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 0,
			y: i * lineSpacing
		}));
		self.lines.push(line);
	}
	self.speed = 10;
	self.update = function () {
		// Animate the road lines
		for (var i = 0; i < self.lines.length; i++) {
			self.lines[i].y += self.speed;
			// Reset line position when it goes off screen
			if (self.lines[i].y > 2732) {
				self.lines[i].y = 0;
			}
		}
	};
	return self;
});
var SlowTruck = Container.expand(function () {
	var self = Container.call(this);
	var truckGraphics = self.attachAsset('slowTruck', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = truckGraphics.width;
	self.height = truckGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane) + (roadWidth - self.width) / 2;
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
var ZigZagCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('zigZagCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.zigzagInterval = 120; // Switch lanes every 2 seconds at 60 fps
	self.zigzagTimer = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
		self.zigzagTimer++;
		if (self.zigzagTimer >= self.zigzagInterval) {
			self.zigzagTimer = 0;
			// Switch lanes randomly
			var newLane = (self.lane + 1) % 3;
			self.lane = newLane;
			self.x = getLanePosition(newLane);
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000,
	title: "Highway Hero"
});
/**** 
* Game Code
****/ 
// Game variables
var player;
var score = 0; // Initialize score tracking
var roads = [];
var enemies = [];
var coins = [];
var boostButton;
var baseSpeed = 10;
var score = 0;
var startX, direction;
var spawnTimer = 0;
var spawnInterval = 60; // Spawn an object every second (60 frames)
var difficultyTimer = 0;
var difficultyInterval = 600; // Increase difficulty every 10 seconds
var roadWidth = 240;
var lanePositions = [];
var gameRunning = false;
var scoreText;
var highScoreText;
// Helper function to get the x-position for a lane
function getLanePosition(lane) {
	return lanePositions[lane];
}
// Initialize game elements
function initGame() {
	// Set background
	game.setBackgroundColor(0x85C1E9);
	// Add background assets 
	var background = LK.getAsset('background', {
		anchorX: 0.5,
		anchorY: 0
	});
	background.x = 2048 / 2;
	background.y = 0;
	game.addChild(background);
	// Initialize lane positions
	var screenCenter = 2048 / 2;
	lanePositions = [screenCenter - roadWidth, screenCenter, screenCenter + roadWidth];
	// Create roads
	for (var i = 0; i < 3; i++) {
		var road = new Road();
		road.x = lanePositions[i];
		roads.push(road);
		game.addChild(road);
	}
	// Create player car
	player = new PlayerCar();
	player.x = lanePositions[1]; // Start in middle lane
	player.y = 2732 - 500; // Position near bottom of screen
	game.addChild(player);
	// Create boost button
	boostButton = new BoostButton();
	boostButton.x = 2048 - 200;
	boostButton.y = 2732 - 200;
	game.addChild(boostButton);
	// Create brake button
	var brakeButton = new BrakeButton();
	brakeButton.x = 200; // Position on the left side of the screen
	brakeButton.y = 2732 - 200;
	game.addChild(brakeButton);
	// Initialize score
	score = 0;
	// Setup score text
	scoreText = new Text2('Score: 0', {
		size: 80,
		fill: 0xFFFFFF
	});
	scoreText.anchor.set(0.5, 0); // Center the score text horizontally
	LK.gui.top.addChild(scoreText); // Position the score text at the top-center
	// Setup high score text
	highScoreText = new Text2('Best: ' + storage.highScore, {
		size: 60,
		fill: 0xFFFFFF
	});
	highScoreText.anchor.set(0, 0);
	highScoreText.y = 90;
	LK.gui.topRight.addChild(highScoreText);
	// Reset game state
	enemies = [];
	coins = [];
	baseSpeed = 10;
	spawnTimer = 0;
	difficultyTimer = 0;
	gameRunning = true;
	// Start background music
	LK.playMusic('bgmusic', {
		fade: {
			start: 0,
			end: 0.4,
			duration: 1000
		}
	});
}
// Spawn objects (enemies or coins)
function spawnObject() {
	var lane = Math.floor(Math.random() * 3);
	// 30% chance to spawn a coin, 20% chance for enemy car, 20% chance for normal car, 10% chance for police car, 10% chance for slow truck, 10% chance for fast car
	var randomValue = Math.random();
	if (randomValue < 0.3) {
		var coin = new Coin();
		coin.setup(lane, baseSpeed);
		coins.push(coin);
		game.addChild(coin);
	} else if (randomValue < 0.7) {
		var enemy = new EnemyCar();
		enemy.setup(lane, baseSpeed * 0.8); // Enemies move a bit slower than the road
		enemies.push(enemy);
		game.addChild(enemy);
	} else if (randomValue < 0.9) {
		var policeCar = new PoliceCar();
		policeCar.setup(lane, baseSpeed * 0.8);
		enemies.push(policeCar);
		game.addChild(policeCar);
		LK.getSound('siren').play();
	} else if (randomValue < 0.95) {
		var zigZagCar = new ZigZagCar();
		zigZagCar.setup(lane, baseSpeed * 1.2); // ZigZag cars move slightly faster
		enemies.push(zigZagCar);
		game.addChild(zigZagCar);
	} else if (randomValue < 0.975) {
		var slowTruck = new SlowTruck();
		slowTruck.setup(lane, baseSpeed * 0.5); // Slow trucks move slower than other vehicles
		enemies.push(slowTruck);
		game.addChild(slowTruck);
	} else if (randomValue < 0.99) {
		var greenCar = new GreenCar();
		greenCar.setup(lane, baseSpeed * 1.1); // Green cars move slightly faster than normal
		enemies.push(greenCar);
		game.addChild(greenCar);
	} else {
		var fastCar = new FastCar();
		fastCar.setup(lane, baseSpeed * 1.5); // Fast cars move quicker than other vehicles
		enemies.push(fastCar);
		game.addChild(fastCar);
	}
}
// Check for collisions
function checkCollisions() {
	if (!player) {
		return;
	}
	// Check for collision with enemies
	for (var i = enemies.length - 1; i >= 0; i--) {
		if (player.intersects(enemies[i])) {
			// Game over on collision
			LK.getSound('crash').play();
			LK.effects.flashScreen(0xFF0000, 500);
			// Update high score if needed
			if (score > storage.highScore) {
				storage.highScore = score;
				highScoreText.setText('Best: ' + storage.highScore);
			}
			// Show game over
			LK.stopMusic();
			LK.showGameOver();
			gameRunning = false;
			return;
		}
	}
	// Check for collision with coins
	for (var j = coins.length - 1; j >= 0; j--) {
		if (player.intersects(coins[j])) {
			// Collect coin
			score += 10;
			scoreText.setText('Score: ' + score);
			LK.getSound('coinCollect').play();
			LK.effects.flashObject(coins[j], 0xFFFFFF, 200);
			coins[j].destroy();
			coins.splice(j, 1);
		}
	}
}
// Update function for game logic
game.update = function () {
	if (!gameRunning) {
		return;
	}
	// Update road and player
	for (var i = 0; i < roads.length; i++) {
		var speedMultiplier = player.boosting ? 2 : 1;
		roads[i].speed = baseSpeed * speedMultiplier;
		roads[i].update();
	}
	player.update();
	// Spawn objects
	spawnTimer++;
	if (spawnTimer >= spawnInterval) {
		spawnObject();
		spawnTimer = 0;
	}
	// Increase difficulty over time
	difficultyTimer++;
	if (difficultyTimer >= difficultyInterval) {
		baseSpeed += 1;
		spawnInterval = Math.max(30, spawnInterval - 3); // Speed up spawning but not below 0.5 seconds
		difficultyTimer = 0;
		// Add points for surviving longer
		score += 5;
		scoreText.setText('Score: ' + score);
	}
	// Update enemies
	for (var j = enemies.length - 1; j >= 0; j--) {
		var speedMultiplier = player.boosting ? 2 : 1;
		enemies[j].speed = baseSpeed * 0.8 * speedMultiplier;
		enemies[j].update();
		// Remove enemies that are off screen
		if (enemies[j].y > 2732 + enemies[j].height) {
			enemies[j].destroy();
			enemies.splice(j, 1);
			// Add points for each car you pass
			score += 1;
			scoreText.setText('Score: ' + score);
		}
	}
	// Update coins
	for (var k = coins.length - 1; k >= 0; k--) {
		var speedMultiplier = player.boosting ? 2 : 1;
		coins[k].speed = baseSpeed * 0.8 * speedMultiplier;
		coins[k].update();
		// Remove coins that are off screen
		if (coins[k].y > 2732 + coins[k].height) {
			coins[k].destroy();
			coins.splice(k, 1);
		}
	}
	// Check for collisions
	checkCollisions();
};
// Touch handling for lane changes
game.down = function (x, y) {
	if (!gameRunning) {
		return;
	}
	// Both sides of the screen are for lane switching
	startX = x;
	direction = null;
};
game.move = function (x, y) {
	if (!gameRunning || !startX) {
		return;
	}
	// Determine swipe direction if it's significant enough
	var deltaX = x - startX;
	if (Math.abs(deltaX) > 50) {
		direction = deltaX > 0 ? 'right' : 'left';
	}
};
game.up = function (x, y) {
	if (!gameRunning) {
		return;
	}
	// Execute lane change based on swipe direction
	if (direction) {
		if (direction === 'left' && player.lane > 0) {
			player.switchLane(player.lane - 1);
		} else if (direction === 'right' && player.lane < 2) {
			player.switchLane(player.lane + 1);
		}
	}
	startX = null;
	direction = null;
};
// Initialize the game
initGame(); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highScore: 0
});
/**** 
* Classes
****/ 
var BoostButton = Container.expand(function () {
	var self = Container.call(this);
	var buttonGraphics = self.attachAsset('boostButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.down = function () {
		buttonGraphics.alpha = 0.8;
		if (player && !player.boosting) {
			player.boost();
		}
	};
	self.up = function () {
		buttonGraphics.alpha = 0.5;
	};
	return self;
});
var BrakeButton = Container.expand(function () {
	var self = Container.call(this);
	var buttonGraphics = self.attachAsset('brakeButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.down = function () {
		if (player) {
			player.speed = baseSpeed * 0.2; // Decrease speed rapidly
		}
	};
	self.up = function () {
		if (player) {
			player.speed = baseSpeed; // Restore normal speed
		}
	};
});
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = coinGraphics.width;
	self.height = coinGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
		// Add a spinning effect to the coin
		coinGraphics.rotation += 0.05;
	};
	return self;
});
var EnemyCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('enemyCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
var FastCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('fastCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
var GreenCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('greenCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	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.lane = 1; // 0=left, 1=middle, 2=right
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.moving = false;
	self.boosting = false;
	self.boostTime = 0;
	self.boostDuration = 120; // 2 seconds at 60 fps
	self.switchLane = function (targetLane) {
		if (self.moving || targetLane < 0 || targetLane > 2) {
			return;
		}
		self.moving = true;
		var targetX = getLanePosition(targetLane);
		tween(self, {
			x: targetX
		}, {
			duration: 300,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				self.lane = targetLane;
				self.moving = false;
			}
		});
	};
	self.boost = function () {
		if (self.boosting) {
			return;
		}
		self.boosting = true;
		self.boostTime = self.boostDuration;
		LK.getSound('boost').play();
	};
	self.update = function () {
		if (self.boosting) {
			self.boostTime--;
			if (self.boostTime <= 0) {
				self.boosting = false;
			}
		}
		// Play car movement sound
		LK.getSound('carMove').play();
	};
	return self;
});
var PoliceCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('policeCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.followTime = 180; // Follow for 3 seconds at 60 fps
	self.following = false;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
		self.following = true;
	};
	self.update = function () {
		if (self.following) {
			self.followTime--;
			if (self.followTime <= 0) {
				self.following = false;
			} else {
				// Follow the player's lane
				self.x = getLanePosition(player.lane);
			}
		}
		self.y += self.speed;
	};
	return self;
});
var Road = Container.expand(function () {
	var self = Container.call(this);
	var roadGraphics = self.attachAsset('road', {
		anchorX: 0.5,
		anchorY: 0
	});
	self.laneWidth = roadGraphics.width;
	// Create road lines
	self.lines = [];
	var lineCount = 15;
	var lineSpacing = 2732 / lineCount;
	for (var i = 0; i < lineCount; i++) {
		var line = self.addChild(LK.getAsset('roadLine', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 0,
			y: i * lineSpacing
		}));
		self.lines.push(line);
	}
	self.speed = 10;
	self.update = function () {
		// Animate the road lines
		for (var i = 0; i < self.lines.length; i++) {
			self.lines[i].y += self.speed;
			// Reset line position when it goes off screen
			if (self.lines[i].y > 2732) {
				self.lines[i].y = 0;
			}
		}
	};
	return self;
});
var SlowTruck = Container.expand(function () {
	var self = Container.call(this);
	var truckGraphics = self.attachAsset('slowTruck', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = truckGraphics.width;
	self.height = truckGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane) + (roadWidth - self.width) / 2;
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
var ZigZagCar = Container.expand(function () {
	var self = Container.call(this);
	var carGraphics = self.attachAsset('zigZagCar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = carGraphics.width;
	self.height = carGraphics.height;
	self.lane = 0;
	self.speed = 0;
	self.zigzagInterval = 120; // Switch lanes every 2 seconds at 60 fps
	self.zigzagTimer = 0;
	self.setup = function (lane, speed) {
		self.lane = lane;
		self.speed = speed;
		self.x = getLanePosition(lane);
		self.y = -self.height;
	};
	self.update = function () {
		self.y += self.speed;
		self.zigzagTimer++;
		if (self.zigzagTimer >= self.zigzagInterval) {
			self.zigzagTimer = 0;
			// Switch lanes randomly
			var newLane = (self.lane + 1) % 3;
			self.lane = newLane;
			self.x = getLanePosition(newLane);
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000,
	title: "Highway Hero"
});
/**** 
* Game Code
****/ 
// Game variables
var player;
var score = 0; // Initialize score tracking
var roads = [];
var enemies = [];
var coins = [];
var boostButton;
var baseSpeed = 10;
var score = 0;
var startX, direction;
var spawnTimer = 0;
var spawnInterval = 60; // Spawn an object every second (60 frames)
var difficultyTimer = 0;
var difficultyInterval = 600; // Increase difficulty every 10 seconds
var roadWidth = 240;
var lanePositions = [];
var gameRunning = false;
var scoreText;
var highScoreText;
// Helper function to get the x-position for a lane
function getLanePosition(lane) {
	return lanePositions[lane];
}
// Initialize game elements
function initGame() {
	// Set background
	game.setBackgroundColor(0x85C1E9);
	// Add background assets 
	var background = LK.getAsset('background', {
		anchorX: 0.5,
		anchorY: 0
	});
	background.x = 2048 / 2;
	background.y = 0;
	game.addChild(background);
	// Initialize lane positions
	var screenCenter = 2048 / 2;
	lanePositions = [screenCenter - roadWidth, screenCenter, screenCenter + roadWidth];
	// Create roads
	for (var i = 0; i < 3; i++) {
		var road = new Road();
		road.x = lanePositions[i];
		roads.push(road);
		game.addChild(road);
	}
	// Create player car
	player = new PlayerCar();
	player.x = lanePositions[1]; // Start in middle lane
	player.y = 2732 - 500; // Position near bottom of screen
	game.addChild(player);
	// Create boost button
	boostButton = new BoostButton();
	boostButton.x = 2048 - 200;
	boostButton.y = 2732 - 200;
	game.addChild(boostButton);
	// Create brake button
	var brakeButton = new BrakeButton();
	brakeButton.x = 200; // Position on the left side of the screen
	brakeButton.y = 2732 - 200;
	game.addChild(brakeButton);
	// Initialize score
	score = 0;
	// Setup score text
	scoreText = new Text2('Score: 0', {
		size: 80,
		fill: 0xFFFFFF
	});
	scoreText.anchor.set(0.5, 0); // Center the score text horizontally
	LK.gui.top.addChild(scoreText); // Position the score text at the top-center
	// Setup high score text
	highScoreText = new Text2('Best: ' + storage.highScore, {
		size: 60,
		fill: 0xFFFFFF
	});
	highScoreText.anchor.set(0, 0);
	highScoreText.y = 90;
	LK.gui.topRight.addChild(highScoreText);
	// Reset game state
	enemies = [];
	coins = [];
	baseSpeed = 10;
	spawnTimer = 0;
	difficultyTimer = 0;
	gameRunning = true;
	// Start background music
	LK.playMusic('bgmusic', {
		fade: {
			start: 0,
			end: 0.4,
			duration: 1000
		}
	});
}
// Spawn objects (enemies or coins)
function spawnObject() {
	var lane = Math.floor(Math.random() * 3);
	// 30% chance to spawn a coin, 20% chance for enemy car, 20% chance for normal car, 10% chance for police car, 10% chance for slow truck, 10% chance for fast car
	var randomValue = Math.random();
	if (randomValue < 0.3) {
		var coin = new Coin();
		coin.setup(lane, baseSpeed);
		coins.push(coin);
		game.addChild(coin);
	} else if (randomValue < 0.7) {
		var enemy = new EnemyCar();
		enemy.setup(lane, baseSpeed * 0.8); // Enemies move a bit slower than the road
		enemies.push(enemy);
		game.addChild(enemy);
	} else if (randomValue < 0.9) {
		var policeCar = new PoliceCar();
		policeCar.setup(lane, baseSpeed * 0.8);
		enemies.push(policeCar);
		game.addChild(policeCar);
		LK.getSound('siren').play();
	} else if (randomValue < 0.95) {
		var zigZagCar = new ZigZagCar();
		zigZagCar.setup(lane, baseSpeed * 1.2); // ZigZag cars move slightly faster
		enemies.push(zigZagCar);
		game.addChild(zigZagCar);
	} else if (randomValue < 0.975) {
		var slowTruck = new SlowTruck();
		slowTruck.setup(lane, baseSpeed * 0.5); // Slow trucks move slower than other vehicles
		enemies.push(slowTruck);
		game.addChild(slowTruck);
	} else if (randomValue < 0.99) {
		var greenCar = new GreenCar();
		greenCar.setup(lane, baseSpeed * 1.1); // Green cars move slightly faster than normal
		enemies.push(greenCar);
		game.addChild(greenCar);
	} else {
		var fastCar = new FastCar();
		fastCar.setup(lane, baseSpeed * 1.5); // Fast cars move quicker than other vehicles
		enemies.push(fastCar);
		game.addChild(fastCar);
	}
}
// Check for collisions
function checkCollisions() {
	if (!player) {
		return;
	}
	// Check for collision with enemies
	for (var i = enemies.length - 1; i >= 0; i--) {
		if (player.intersects(enemies[i])) {
			// Game over on collision
			LK.getSound('crash').play();
			LK.effects.flashScreen(0xFF0000, 500);
			// Update high score if needed
			if (score > storage.highScore) {
				storage.highScore = score;
				highScoreText.setText('Best: ' + storage.highScore);
			}
			// Show game over
			LK.stopMusic();
			LK.showGameOver();
			gameRunning = false;
			return;
		}
	}
	// Check for collision with coins
	for (var j = coins.length - 1; j >= 0; j--) {
		if (player.intersects(coins[j])) {
			// Collect coin
			score += 10;
			scoreText.setText('Score: ' + score);
			LK.getSound('coinCollect').play();
			LK.effects.flashObject(coins[j], 0xFFFFFF, 200);
			coins[j].destroy();
			coins.splice(j, 1);
		}
	}
}
// Update function for game logic
game.update = function () {
	if (!gameRunning) {
		return;
	}
	// Update road and player
	for (var i = 0; i < roads.length; i++) {
		var speedMultiplier = player.boosting ? 2 : 1;
		roads[i].speed = baseSpeed * speedMultiplier;
		roads[i].update();
	}
	player.update();
	// Spawn objects
	spawnTimer++;
	if (spawnTimer >= spawnInterval) {
		spawnObject();
		spawnTimer = 0;
	}
	// Increase difficulty over time
	difficultyTimer++;
	if (difficultyTimer >= difficultyInterval) {
		baseSpeed += 1;
		spawnInterval = Math.max(30, spawnInterval - 3); // Speed up spawning but not below 0.5 seconds
		difficultyTimer = 0;
		// Add points for surviving longer
		score += 5;
		scoreText.setText('Score: ' + score);
	}
	// Update enemies
	for (var j = enemies.length - 1; j >= 0; j--) {
		var speedMultiplier = player.boosting ? 2 : 1;
		enemies[j].speed = baseSpeed * 0.8 * speedMultiplier;
		enemies[j].update();
		// Remove enemies that are off screen
		if (enemies[j].y > 2732 + enemies[j].height) {
			enemies[j].destroy();
			enemies.splice(j, 1);
			// Add points for each car you pass
			score += 1;
			scoreText.setText('Score: ' + score);
		}
	}
	// Update coins
	for (var k = coins.length - 1; k >= 0; k--) {
		var speedMultiplier = player.boosting ? 2 : 1;
		coins[k].speed = baseSpeed * 0.8 * speedMultiplier;
		coins[k].update();
		// Remove coins that are off screen
		if (coins[k].y > 2732 + coins[k].height) {
			coins[k].destroy();
			coins.splice(k, 1);
		}
	}
	// Check for collisions
	checkCollisions();
};
// Touch handling for lane changes
game.down = function (x, y) {
	if (!gameRunning) {
		return;
	}
	// Both sides of the screen are for lane switching
	startX = x;
	direction = null;
};
game.move = function (x, y) {
	if (!gameRunning || !startX) {
		return;
	}
	// Determine swipe direction if it's significant enough
	var deltaX = x - startX;
	if (Math.abs(deltaX) > 50) {
		direction = deltaX > 0 ? 'right' : 'left';
	}
};
game.up = function (x, y) {
	if (!gameRunning) {
		return;
	}
	// Execute lane change based on swipe direction
	if (direction) {
		if (direction === 'left' && player.lane > 0) {
			player.switchLane(player.lane - 1);
		} else if (direction === 'right' && player.lane < 2) {
			player.switchLane(player.lane + 1);
		}
	}
	startX = null;
	direction = null;
};
// Initialize the game
initGame();
:quality(85)/https://cdn.frvr.ai/67f429cff9699d20fcacbf29.png%3F3) 
 Top-down view of a pixel art police car with flashing red and blue lights, 64x64, arcade retro style". Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f42cf5f9699d20fcacbf82.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67f42d4cf9699d20fcacbf93.png%3F3) 
 Top-down view of a sleek red sports car, clean and minimal pixel art style, 64x64 size, suitable for a mobile racing game". Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f42dc5f9699d20fcacbfa4.png%3F3) 
 Top-down view of a simple blue car in pixel art style, 64x64 size, designed for an endless driving game, minimal detail, arcade style". Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f43077c19c3cd96fa498c2.png%3F3) 
 Top-down view of a simple pink car in pixel art style, 64x64 size, designed for an endless driving game, minimal detail, arcade style". straight top. View Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f43364c19c3cd96fa4992e.png%3F3) 
 Pixel art style gold coin with shine, top-down view, 32x32, designed for arcade coin collection in a racing game". Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f4e222ecc5949d0e6be99b.png%3F3) 
 Top-down seamless vertically scrolling background for an endless driving game, central road with 3 lanes, side grass, trees, and fences, pixel art style, 512x1024 resolution". Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f4e671ecc5949d0e6be9ed.png%3F3) 
 "Pixel art style accelerate button icon, 64x64 size, glowing red or white, with an upward arrow or speed meter symbol, circular or rounded rectangle, designed for mobile racing game UI, clean and minimal arcade look". button shoudld be straight up and accelerate text hd images Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f4ee05ecc5949d0e6bea85.png%3F3) 
 High-quality brake button for mobile racing game, 64x64 pixels, glowing red, with a brake or stop symbol in the center, 3D UI style with soft shadows and clean edges". Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67f5115892e67804b44cbd88.png%3F3)