/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Bird = Container.expand(function () {
	var self = Container.call(this);
	var birdGraphics = self.attachAsset('bird', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.xSpeed = 6;
	self.ySpeed = -15;
	self.gravity = 0.5;
	self.lift = -10;
	self.flap = function () {
		self.ySpeed = self.lift * 1.5;
		LK.getSound('flap').play();
	};
	self._update_migrated = function () {
		if (game.isMouseDown) {
			self.ySpeed += self.gravity / 3;
		} else {
			self.ySpeed += self.gravity;
		}
		self.y += self.ySpeed;
		self.x += self.xSpeed;
		if (self.y <= 0 || self.y >= 2732) {
			self.speed = -self.speed;
		}
		var targetRotation = Math.atan2(self.ySpeed, self.xSpeed * self.scale.x) / 2;
		birdGraphics.rotation += (targetRotation - birdGraphics.rotation) / 10;
	};
	self.flip = function () {
		self.scale.x *= -1;
	};
});
var Jetpack = Container.expand(function () {
	var self = Container.call(this);
	var jetpackGraphics = self.attachAsset('jetpack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 3;
	self.collected = false;
	self._move_migrated = function () {
		self.y += self.speed;
	};
});
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var obstacleShadow = self.attachAsset('obstacleShadow', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	obstacleShadow.rotation = Math.PI / 4;
	var obstacleShadow2 = self.attachAsset('obstacleShadow2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	obstacleShadow2.rotation = Math.PI / 4;
	obstacleShadow2.y = -7;
	var obstacleGraphics = self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	obstacleGraphics.rotation = Math.PI / 4;
	self.speed = 5;
	self._move_migrated = function (speed) {
		self.y += speed;
	};
});
var Particle = Container.expand(function () {
	var self = Container.call(this);
	var particleGraphics = self.attachAsset('particle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.xSpeed = (Math.random() - 0.5) * 10;
	self.ySpeed = (Math.random() - 0.5) * 10;
	self.life = 30;
	self.maxLife = 30;
	self.update = function () {
		self.x += self.xSpeed;
		self.y += self.ySpeed;
		self.life--;
		var alpha = self.life / self.maxLife;
		particleGraphics.alpha = alpha;
		if (self.life <= 0) {
			self.destroy();
		}
	};
	return self;
});
var PowerUp = Container.expand(function () {
	var self = Container.call(this);
	var powerUpGraphics = self.attachAsset('particle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	powerUpGraphics.tint = 0xffd700;
	powerUpGraphics.scale.set(3, 3);
	self.speed = 3;
	self.collected = false;
	self.bobOffset = Math.random() * Math.PI * 2;
	self.update = function () {
		self.y += self.speed;
		self.x += Math.sin(LK.ticks * 0.1 + self.bobOffset) * 2;
		powerUpGraphics.rotation += 0.1;
	};
	return self;
});
var Wall = Container.expand(function () {
	var self = Container.call(this);
	var wallGraphics = self.attachAsset('wall', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
game.gameStarted = false;
var startMenuTitle = new Text2('FLAPPY BIRD', {
	size: 200,
	fill: '#3a84f7',
	font: 'Impact',
	dropShadow: true,
	dropShadowColor: '#222a9a',
	dropShadowBlur: 5,
	dropShadowDistance: 7,
	dropShadowAngle: 0,
	align: 'center'
});
startMenuTitle.anchor.set(.5, .5);
LK.gui.center.addChild(startMenuTitle);
var startMenuText = new Text2('TAP TO START', {
	size: 120,
	fill: '#ffffff',
	font: 'Impact',
	align: 'center'
});
startMenuText.anchor.set(.5, .5);
startMenuText.y = 150;
LK.gui.center.addChild(startMenuText);
var tutorialTextWhite = new Text2('Tap to Flap\nHold to Float', {
	size: 150,
	fill: '#ffffff',
	font: 'Impact',
	align: 'center'
});
tutorialTextWhite.anchor.set(.5, 1);
tutorialTextWhite.x = -4;
tutorialTextWhite.y = -62;
tutorialTextWhite.visible = false;
LK.gui.bottom.addChild(tutorialTextWhite);
var tutorialText = new Text2('Tap to Flap\nHold to Float', {
	size: 150,
	fill: '#3a84f7',
	font: 'Impact',
	dropShadow: true,
	dropShadowColor: '#222a9a',
	dropShadowBlur: 5,
	dropShadowDistance: 7,
	dropShadowAngle: 0,
	align: 'center'
});
tutorialText.anchor.set(.5, 1);
tutorialText.y = -50;
tutorialText.visible = false;
LK.gui.bottom.addChild(tutorialText);
game.score = 0;
game.obstacleSpeed = 2;
game.obstacleSpeedIncrease = 0.0001;
var jetpacks = [];
var jetpackSpawnTime = 500;
var lastJetpackSpawn = 0;
var particles = [];
var powerUps = [];
var powerUpSpawnTime = 800;
var lastPowerUpSpawn = 0;
var comboMultiplier = 1;
var lastScoreTime = 0;
game.createParticles = function (x, y, count, color) {
	for (var i = 0; i < count; i++) {
		var particle = game.addChild(new Particle());
		particle.x = x;
		particle.y = y;
		if (color) {
			particle.children[0].tint = color;
		}
		particles.push(particle);
	}
};
game.checkObstacleCollision = function (obstacles) {
	for (var i = 0; i < obstacles.length; i++) {
		obstacles[i]._move_migrated();
		var dist = Math.sqrt(Math.pow(bird.x - obstacles[i].x, 2) + Math.pow(bird.y - obstacles[i].y, 2));
		if (dist < 280) {
			game.createParticles(bird.x, bird.y, 15, 0xff0000);
			LK.setScore(game.score);
			LK.getSound('gameOverJingle').play();
			LK.playMusic('defeatMusic', {
				loop: false
			});
			LK.showGameOver();
		}
	}
};
game.setBackgroundColor(0xadd8e6);
var scoreText = new Text2('0', {
	size: 150,
	fill: '#3a84f7',
	font: 'Impact',
	dropShadow: true,
	dropShadowColor: '#222a9a',
	dropShadowBlur: 5,
	dropShadowDistance: 7,
	dropShadowAngle: 0
});
scoreText.anchor.set(.5, 0);
LK.gui.top.addChild(scoreText);
var scoreText2 = new Text2('0', {
	size: 150,
	fill: '#ffffff',
	font: 'Impact'
});
scoreText2.anchor.set(.5, 0);
scoreText2.x = -4;
scoreText2.y = -5;
LK.gui.top.addChild(scoreText2);
LK.gui.top.addChild(scoreText);
var bird = game.addChild(new Bird());
var leftWall = game.addChild(new Wall());
leftWall.x = 0;
leftWall.y = 1366;
var rightWall = game.addChild(new Wall());
rightWall.x = 2048;
rightWall.y = 1366;
var leftObstacles = [],
	rightObstacles = [];
var obstacleSpawnRandomness = 300;
var obstacleSpawnRandomnessDecrease = 0.0001;
var obstacleSpawnY = -500;
var leftObstacleSpawnTime = Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
var rightObstacleSpawnTime = Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
bird.x = 1024;
bird.y = 1366;
game.isMouseDown = false;
game.down = function (x, y, obj) {
	if (!game.gameStarted) {
		game.gameStarted = true;
		startMenuTitle.visible = false;
		startMenuText.visible = false;
		tutorialText.visible = true;
		tutorialTextWhite.visible = true;
		LK.playMusic('backgroundMusic', {
			loop: true
		});
		return;
	}
	bird.flap();
	game.isMouseDown = true;
};
game.up = function (x, y, obj) {
	game.isMouseDown = false;
};
game.update = function () {
	if (!game.gameStarted) {
		return;
	}
	bird._update_migrated();
	if (game.score > 2) {
		tutorialText.y += 5;
		tutorialTextWhite.y += 5;
	}
	// Update combo multiplier based on scoring frequency
	if (LK.ticks - lastScoreTime > 300) {
		comboMultiplier = 1;
	}
	var displayScore = game.score;
	if (comboMultiplier > 1) {
		displayScore += ' x' + comboMultiplier;
	}
	scoreText.setText(displayScore);
	scoreText2.setText(displayScore);
	// Dynamic difficulty scaling - slightly faster progression
	var difficultyLevel = Math.floor(game.score / 20);
	game.obstacleSpeed = 2 + difficultyLevel * 0.15;
	obstacleSpawnRandomness = Math.max(180, 300 - difficultyLevel * 3);
	// Update particles
	for (var i = particles.length - 1; i >= 0; i--) {
		particles[i].update();
		if (particles[i].life <= 0) {
			particles.splice(i, 1);
		}
	}
	// Spawn power-ups
	if (LK.ticks - lastPowerUpSpawn >= powerUpSpawnTime && Math.random() < 0.005) {
		var newPowerUp = game.addChild(new PowerUp());
		newPowerUp.x = Math.random() * 1600 + 224;
		newPowerUp.y = -50;
		powerUps.push(newPowerUp);
		lastPowerUpSpawn = LK.ticks;
		powerUpSpawnTime = 600 + Math.random() * 800;
	}
	// Update power-ups
	for (var i = powerUps.length - 1; i >= 0; i--) {
		powerUps[i].update();
		if (bird.intersects(powerUps[i]) && !powerUps[i].collected) {
			powerUps[i].collected = true;
			game.createParticles(powerUps[i].x, powerUps[i].y, 8, 0xffd700);
			comboMultiplier++;
			game.score += 5 * comboMultiplier;
			lastScoreTime = LK.ticks;
			LK.setScore(game.score);
			LK.getSound('U').play();
			var powerUpToDestroy = powerUps[i];
			tween(powerUps[i], {
				alpha: 0,
				scaleX: 6,
				scaleY: 6
			}, {
				duration: 300,
				onFinish: function onFinish() {
					powerUpToDestroy.destroy();
				}
			});
			powerUps.splice(i, 1);
			continue;
		}
		if (powerUps[i].y > 3000) {
			powerUps[i].destroy();
			powerUps.splice(i, 1);
		}
	}
	if (LK.ticks - lastJetpackSpawn >= jetpackSpawnTime && Math.random() < 0.006) {
		var newJetpack = game.addChild(new Jetpack());
		newJetpack.x = Math.random() * 1600 + 224;
		newJetpack.y = -100;
		jetpacks.push(newJetpack);
		lastJetpackSpawn = LK.ticks;
		jetpackSpawnTime = 400 + Math.random() * 600;
	}
	if (LK.ticks >= leftObstacleSpawnTime) {
		var newObstacle = game.addChildAt(new Obstacle(), 0);
		newObstacle.x = 0;
		newObstacle.y = obstacleSpawnY;
		leftObstacles.push(newObstacle);
		leftObstacleSpawnTime += Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
	}
	if (LK.ticks >= rightObstacleSpawnTime) {
		var newObstacle = game.addChildAt(new Obstacle(), 0);
		newObstacle.x = 2048;
		newObstacle.y = -newObstacle.height;
		rightObstacles.push(newObstacle);
		rightObstacleSpawnTime += Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
	}
	for (var i = jetpacks.length - 1; i >= 0; i--) {
		jetpacks[i]._move_migrated();
		if (bird.intersects(jetpacks[i]) && !jetpacks[i].collected) {
			jetpacks[i].collected = true;
			bird.ySpeed = -25;
			game.score += 10;
			LK.setScore(game.score);
			LK.getSound('jetpackSound').play();
			var currentDirection = bird.scale.x > 0 ? 1 : -1;
			tween(bird, {
				y: bird.y - 200
			}, {
				duration: 500,
				easing: tween.easeOut
			});
			tween(bird, {
				scaleX: 1.3 * currentDirection,
				scaleY: 1.3
			}, {
				duration: 200,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					tween(bird, {
						scaleX: 1 * currentDirection,
						scaleY: 1
					}, {
						duration: 200,
						easing: tween.easeIn
					});
				}
			});
			var jetpackToDestroy = jetpacks[i];
			tween(jetpacks[i], {
				alpha: 0,
				scaleX: 2,
				scaleY: 2
			}, {
				duration: 300,
				onFinish: function onFinish() {
					jetpackToDestroy.destroy();
				}
			});
			jetpacks.splice(i, 1);
			continue;
		}
		if (jetpacks[i].y > 3232) {
			jetpacks[i].destroy();
			jetpacks.splice(i, 1);
		}
	}
	if (bird.intersects(leftWall) && bird.xSpeed < 0 || bird.intersects(rightWall) && bird.xSpeed > 0) {
		bird.xSpeed = -bird.xSpeed;
		bird.flip();
		// Combo scoring system
		if (LK.ticks - lastScoreTime < 300) {
			comboMultiplier++;
		} else {
			comboMultiplier = 1;
		}
		var bounceScore = 1 * comboMultiplier;
		game.score += bounceScore;
		lastScoreTime = LK.ticks;
		// Create bounce particles
		var wallX = bird.intersects(leftWall) ? leftWall.x + 50 : rightWall.x - 50;
		game.createParticles(wallX, bird.y, 5, 0x3a84f7);
		LK.setScore(game.score);
		LK.getSound('bounce').play();
	}
	for (var i = leftObstacles.length - 1; i >= 0; i--) {
		leftObstacles[i]._move_migrated(game.obstacleSpeed);
		if (leftObstacles[i].y > 3232) {
			leftObstacles[i].destroy();
			leftObstacles.splice(i, 1);
		}
	}
	for (var i = rightObstacles.length - 1; i >= 0; i--) {
		rightObstacles[i]._move_migrated(game.obstacleSpeed);
		if (rightObstacles[i].y > 3232) {
			rightObstacles[i].destroy();
			rightObstacles.splice(i, 1);
		}
	}
	game.checkObstacleCollision(leftObstacles);
	game.checkObstacleCollision(rightObstacles);
	if (bird.y < 0 || bird.y > 2732) {
		game.createParticles(bird.x, bird.y < 0 ? 0 : 2732, 20, 0xff0000);
		// Screen shake effect
		tween(game, {
			x: 10
		}, {
			duration: 50,
			onFinish: function onFinish() {
				tween(game, {
					x: -10
				}, {
					duration: 50,
					onFinish: function onFinish() {
						tween(game, {
							x: 0
						}, {
							duration: 50
						});
					}
				});
			}
		});
		LK.setScore(game.score);
		LK.getSound('gameOverJingle').play();
		LK.playMusic('defeatMusic', {
			loop: false
		});
		LK.showGameOver();
	}
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Bird = Container.expand(function () {
	var self = Container.call(this);
	var birdGraphics = self.attachAsset('bird', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.xSpeed = 6;
	self.ySpeed = -15;
	self.gravity = 0.5;
	self.lift = -10;
	self.flap = function () {
		self.ySpeed = self.lift * 1.5;
		LK.getSound('flap').play();
	};
	self._update_migrated = function () {
		if (game.isMouseDown) {
			self.ySpeed += self.gravity / 3;
		} else {
			self.ySpeed += self.gravity;
		}
		self.y += self.ySpeed;
		self.x += self.xSpeed;
		if (self.y <= 0 || self.y >= 2732) {
			self.speed = -self.speed;
		}
		var targetRotation = Math.atan2(self.ySpeed, self.xSpeed * self.scale.x) / 2;
		birdGraphics.rotation += (targetRotation - birdGraphics.rotation) / 10;
	};
	self.flip = function () {
		self.scale.x *= -1;
	};
});
var Jetpack = Container.expand(function () {
	var self = Container.call(this);
	var jetpackGraphics = self.attachAsset('jetpack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 3;
	self.collected = false;
	self._move_migrated = function () {
		self.y += self.speed;
	};
});
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var obstacleShadow = self.attachAsset('obstacleShadow', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	obstacleShadow.rotation = Math.PI / 4;
	var obstacleShadow2 = self.attachAsset('obstacleShadow2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	obstacleShadow2.rotation = Math.PI / 4;
	obstacleShadow2.y = -7;
	var obstacleGraphics = self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	obstacleGraphics.rotation = Math.PI / 4;
	self.speed = 5;
	self._move_migrated = function (speed) {
		self.y += speed;
	};
});
var Particle = Container.expand(function () {
	var self = Container.call(this);
	var particleGraphics = self.attachAsset('particle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.xSpeed = (Math.random() - 0.5) * 10;
	self.ySpeed = (Math.random() - 0.5) * 10;
	self.life = 30;
	self.maxLife = 30;
	self.update = function () {
		self.x += self.xSpeed;
		self.y += self.ySpeed;
		self.life--;
		var alpha = self.life / self.maxLife;
		particleGraphics.alpha = alpha;
		if (self.life <= 0) {
			self.destroy();
		}
	};
	return self;
});
var PowerUp = Container.expand(function () {
	var self = Container.call(this);
	var powerUpGraphics = self.attachAsset('particle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	powerUpGraphics.tint = 0xffd700;
	powerUpGraphics.scale.set(3, 3);
	self.speed = 3;
	self.collected = false;
	self.bobOffset = Math.random() * Math.PI * 2;
	self.update = function () {
		self.y += self.speed;
		self.x += Math.sin(LK.ticks * 0.1 + self.bobOffset) * 2;
		powerUpGraphics.rotation += 0.1;
	};
	return self;
});
var Wall = Container.expand(function () {
	var self = Container.call(this);
	var wallGraphics = self.attachAsset('wall', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
game.gameStarted = false;
var startMenuTitle = new Text2('FLAPPY BIRD', {
	size: 200,
	fill: '#3a84f7',
	font: 'Impact',
	dropShadow: true,
	dropShadowColor: '#222a9a',
	dropShadowBlur: 5,
	dropShadowDistance: 7,
	dropShadowAngle: 0,
	align: 'center'
});
startMenuTitle.anchor.set(.5, .5);
LK.gui.center.addChild(startMenuTitle);
var startMenuText = new Text2('TAP TO START', {
	size: 120,
	fill: '#ffffff',
	font: 'Impact',
	align: 'center'
});
startMenuText.anchor.set(.5, .5);
startMenuText.y = 150;
LK.gui.center.addChild(startMenuText);
var tutorialTextWhite = new Text2('Tap to Flap\nHold to Float', {
	size: 150,
	fill: '#ffffff',
	font: 'Impact',
	align: 'center'
});
tutorialTextWhite.anchor.set(.5, 1);
tutorialTextWhite.x = -4;
tutorialTextWhite.y = -62;
tutorialTextWhite.visible = false;
LK.gui.bottom.addChild(tutorialTextWhite);
var tutorialText = new Text2('Tap to Flap\nHold to Float', {
	size: 150,
	fill: '#3a84f7',
	font: 'Impact',
	dropShadow: true,
	dropShadowColor: '#222a9a',
	dropShadowBlur: 5,
	dropShadowDistance: 7,
	dropShadowAngle: 0,
	align: 'center'
});
tutorialText.anchor.set(.5, 1);
tutorialText.y = -50;
tutorialText.visible = false;
LK.gui.bottom.addChild(tutorialText);
game.score = 0;
game.obstacleSpeed = 2;
game.obstacleSpeedIncrease = 0.0001;
var jetpacks = [];
var jetpackSpawnTime = 500;
var lastJetpackSpawn = 0;
var particles = [];
var powerUps = [];
var powerUpSpawnTime = 800;
var lastPowerUpSpawn = 0;
var comboMultiplier = 1;
var lastScoreTime = 0;
game.createParticles = function (x, y, count, color) {
	for (var i = 0; i < count; i++) {
		var particle = game.addChild(new Particle());
		particle.x = x;
		particle.y = y;
		if (color) {
			particle.children[0].tint = color;
		}
		particles.push(particle);
	}
};
game.checkObstacleCollision = function (obstacles) {
	for (var i = 0; i < obstacles.length; i++) {
		obstacles[i]._move_migrated();
		var dist = Math.sqrt(Math.pow(bird.x - obstacles[i].x, 2) + Math.pow(bird.y - obstacles[i].y, 2));
		if (dist < 280) {
			game.createParticles(bird.x, bird.y, 15, 0xff0000);
			LK.setScore(game.score);
			LK.getSound('gameOverJingle').play();
			LK.playMusic('defeatMusic', {
				loop: false
			});
			LK.showGameOver();
		}
	}
};
game.setBackgroundColor(0xadd8e6);
var scoreText = new Text2('0', {
	size: 150,
	fill: '#3a84f7',
	font: 'Impact',
	dropShadow: true,
	dropShadowColor: '#222a9a',
	dropShadowBlur: 5,
	dropShadowDistance: 7,
	dropShadowAngle: 0
});
scoreText.anchor.set(.5, 0);
LK.gui.top.addChild(scoreText);
var scoreText2 = new Text2('0', {
	size: 150,
	fill: '#ffffff',
	font: 'Impact'
});
scoreText2.anchor.set(.5, 0);
scoreText2.x = -4;
scoreText2.y = -5;
LK.gui.top.addChild(scoreText2);
LK.gui.top.addChild(scoreText);
var bird = game.addChild(new Bird());
var leftWall = game.addChild(new Wall());
leftWall.x = 0;
leftWall.y = 1366;
var rightWall = game.addChild(new Wall());
rightWall.x = 2048;
rightWall.y = 1366;
var leftObstacles = [],
	rightObstacles = [];
var obstacleSpawnRandomness = 300;
var obstacleSpawnRandomnessDecrease = 0.0001;
var obstacleSpawnY = -500;
var leftObstacleSpawnTime = Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
var rightObstacleSpawnTime = Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
bird.x = 1024;
bird.y = 1366;
game.isMouseDown = false;
game.down = function (x, y, obj) {
	if (!game.gameStarted) {
		game.gameStarted = true;
		startMenuTitle.visible = false;
		startMenuText.visible = false;
		tutorialText.visible = true;
		tutorialTextWhite.visible = true;
		LK.playMusic('backgroundMusic', {
			loop: true
		});
		return;
	}
	bird.flap();
	game.isMouseDown = true;
};
game.up = function (x, y, obj) {
	game.isMouseDown = false;
};
game.update = function () {
	if (!game.gameStarted) {
		return;
	}
	bird._update_migrated();
	if (game.score > 2) {
		tutorialText.y += 5;
		tutorialTextWhite.y += 5;
	}
	// Update combo multiplier based on scoring frequency
	if (LK.ticks - lastScoreTime > 300) {
		comboMultiplier = 1;
	}
	var displayScore = game.score;
	if (comboMultiplier > 1) {
		displayScore += ' x' + comboMultiplier;
	}
	scoreText.setText(displayScore);
	scoreText2.setText(displayScore);
	// Dynamic difficulty scaling - slightly faster progression
	var difficultyLevel = Math.floor(game.score / 20);
	game.obstacleSpeed = 2 + difficultyLevel * 0.15;
	obstacleSpawnRandomness = Math.max(180, 300 - difficultyLevel * 3);
	// Update particles
	for (var i = particles.length - 1; i >= 0; i--) {
		particles[i].update();
		if (particles[i].life <= 0) {
			particles.splice(i, 1);
		}
	}
	// Spawn power-ups
	if (LK.ticks - lastPowerUpSpawn >= powerUpSpawnTime && Math.random() < 0.005) {
		var newPowerUp = game.addChild(new PowerUp());
		newPowerUp.x = Math.random() * 1600 + 224;
		newPowerUp.y = -50;
		powerUps.push(newPowerUp);
		lastPowerUpSpawn = LK.ticks;
		powerUpSpawnTime = 600 + Math.random() * 800;
	}
	// Update power-ups
	for (var i = powerUps.length - 1; i >= 0; i--) {
		powerUps[i].update();
		if (bird.intersects(powerUps[i]) && !powerUps[i].collected) {
			powerUps[i].collected = true;
			game.createParticles(powerUps[i].x, powerUps[i].y, 8, 0xffd700);
			comboMultiplier++;
			game.score += 5 * comboMultiplier;
			lastScoreTime = LK.ticks;
			LK.setScore(game.score);
			LK.getSound('U').play();
			var powerUpToDestroy = powerUps[i];
			tween(powerUps[i], {
				alpha: 0,
				scaleX: 6,
				scaleY: 6
			}, {
				duration: 300,
				onFinish: function onFinish() {
					powerUpToDestroy.destroy();
				}
			});
			powerUps.splice(i, 1);
			continue;
		}
		if (powerUps[i].y > 3000) {
			powerUps[i].destroy();
			powerUps.splice(i, 1);
		}
	}
	if (LK.ticks - lastJetpackSpawn >= jetpackSpawnTime && Math.random() < 0.006) {
		var newJetpack = game.addChild(new Jetpack());
		newJetpack.x = Math.random() * 1600 + 224;
		newJetpack.y = -100;
		jetpacks.push(newJetpack);
		lastJetpackSpawn = LK.ticks;
		jetpackSpawnTime = 400 + Math.random() * 600;
	}
	if (LK.ticks >= leftObstacleSpawnTime) {
		var newObstacle = game.addChildAt(new Obstacle(), 0);
		newObstacle.x = 0;
		newObstacle.y = obstacleSpawnY;
		leftObstacles.push(newObstacle);
		leftObstacleSpawnTime += Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
	}
	if (LK.ticks >= rightObstacleSpawnTime) {
		var newObstacle = game.addChildAt(new Obstacle(), 0);
		newObstacle.x = 2048;
		newObstacle.y = -newObstacle.height;
		rightObstacles.push(newObstacle);
		rightObstacleSpawnTime += Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
	}
	for (var i = jetpacks.length - 1; i >= 0; i--) {
		jetpacks[i]._move_migrated();
		if (bird.intersects(jetpacks[i]) && !jetpacks[i].collected) {
			jetpacks[i].collected = true;
			bird.ySpeed = -25;
			game.score += 10;
			LK.setScore(game.score);
			LK.getSound('jetpackSound').play();
			var currentDirection = bird.scale.x > 0 ? 1 : -1;
			tween(bird, {
				y: bird.y - 200
			}, {
				duration: 500,
				easing: tween.easeOut
			});
			tween(bird, {
				scaleX: 1.3 * currentDirection,
				scaleY: 1.3
			}, {
				duration: 200,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					tween(bird, {
						scaleX: 1 * currentDirection,
						scaleY: 1
					}, {
						duration: 200,
						easing: tween.easeIn
					});
				}
			});
			var jetpackToDestroy = jetpacks[i];
			tween(jetpacks[i], {
				alpha: 0,
				scaleX: 2,
				scaleY: 2
			}, {
				duration: 300,
				onFinish: function onFinish() {
					jetpackToDestroy.destroy();
				}
			});
			jetpacks.splice(i, 1);
			continue;
		}
		if (jetpacks[i].y > 3232) {
			jetpacks[i].destroy();
			jetpacks.splice(i, 1);
		}
	}
	if (bird.intersects(leftWall) && bird.xSpeed < 0 || bird.intersects(rightWall) && bird.xSpeed > 0) {
		bird.xSpeed = -bird.xSpeed;
		bird.flip();
		// Combo scoring system
		if (LK.ticks - lastScoreTime < 300) {
			comboMultiplier++;
		} else {
			comboMultiplier = 1;
		}
		var bounceScore = 1 * comboMultiplier;
		game.score += bounceScore;
		lastScoreTime = LK.ticks;
		// Create bounce particles
		var wallX = bird.intersects(leftWall) ? leftWall.x + 50 : rightWall.x - 50;
		game.createParticles(wallX, bird.y, 5, 0x3a84f7);
		LK.setScore(game.score);
		LK.getSound('bounce').play();
	}
	for (var i = leftObstacles.length - 1; i >= 0; i--) {
		leftObstacles[i]._move_migrated(game.obstacleSpeed);
		if (leftObstacles[i].y > 3232) {
			leftObstacles[i].destroy();
			leftObstacles.splice(i, 1);
		}
	}
	for (var i = rightObstacles.length - 1; i >= 0; i--) {
		rightObstacles[i]._move_migrated(game.obstacleSpeed);
		if (rightObstacles[i].y > 3232) {
			rightObstacles[i].destroy();
			rightObstacles.splice(i, 1);
		}
	}
	game.checkObstacleCollision(leftObstacles);
	game.checkObstacleCollision(rightObstacles);
	if (bird.y < 0 || bird.y > 2732) {
		game.createParticles(bird.x, bird.y < 0 ? 0 : 2732, 20, 0xff0000);
		// Screen shake effect
		tween(game, {
			x: 10
		}, {
			duration: 50,
			onFinish: function onFinish() {
				tween(game, {
					x: -10
				}, {
					duration: 50,
					onFinish: function onFinish() {
						tween(game, {
							x: 0
						}, {
							duration: 50
						});
					}
				});
			}
		});
		LK.setScore(game.score);
		LK.getSound('gameOverJingle').play();
		LK.playMusic('defeatMusic', {
			loop: false
		});
		LK.showGameOver();
	}
};