/**** 
* Classes
****/ 
// Define the BackgroundContainer class
var BackgroundContainer = Container.expand(function () {
	var self = Container.call(this);
	var backgroundGraphics1 = self.attachAsset('background', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 2048 / 2732,
		scaleY: 2732 / 2732
	});
	backgroundGraphics1.x = 2048 / 2;
	backgroundGraphics1.y = 2732 / 2;
	var backgroundGraphics2 = self.attachAsset('background', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 2048 / 2732,
		scaleY: 2732 / 2732
	});
	backgroundGraphics2.x = 2048 / 2;
	backgroundGraphics2.y = -2732 / 2;
	self.backgroundGraphics1 = backgroundGraphics1;
	self.backgroundGraphics2 = backgroundGraphics2;
	self.update = function () {
		if (gameStarted) {
			self.backgroundGraphics1.y -= backgroundSpeed;
			self.backgroundGraphics2.y -= backgroundSpeed;
			backgroundSpeed += backgroundAcceleration;
		}
		// Ensure backgrounds stay within bounds after jump movement
		if (self.backgroundGraphics1.y > 2732 / 2) {
			self.backgroundGraphics1.y = self.backgroundGraphics2.y - 2732;
		}
		if (self.backgroundGraphics2.y > 2732 / 2) {
			self.backgroundGraphics2.y = self.backgroundGraphics1.y - 2732;
		}
		if (self.backgroundGraphics1.y <= -2732 / 2) {
			self.backgroundGraphics1.y = self.backgroundGraphics2.y + 2732;
		}
		if (self.backgroundGraphics2.y <= -2732 / 2) {
			self.backgroundGraphics2.y = self.backgroundGraphics1.y + 2732;
		}
	};
	return self;
});
var Booster = Container.expand(function () {
	var self = Container.call(this);
	var boosterGraphics = self.attachAsset('booster', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = boosterGraphics.width;
	self.height = boosterGraphics.height;
	self.speed = 2;
	self.update = function () {
		self.speed += 0.05; // Acceleration
		self.y += self.speed;
		if (self.y > 2732 + self.height / 2) {
			self.destroy();
		}
	};
	return self;
});
// Define the Coin class
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.speed = 2;
	self.animationFrames = [LK.getAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	}), LK.getAsset('coin_2', {
		anchorX: 0.5,
		anchorY: 0.5
	}), LK.getAsset('coin_3', {
		anchorX: 0.5,
		anchorY: 0.5
	}), LK.getAsset('coin_2', {
		anchorX: 0.5,
		anchorY: 0.5
	})];
	self.currentFrame = 0;
	self.frameDuration = 144; // 300 milliseconds per frame
	self.lastFrameChange = Date.now();
	self.update = function () {
		self.speed += 0.05; // Acceleration
		self.y += self.speed;
		// Handle animation frame change
		if (Date.now() - self.lastFrameChange >= self.frameDuration) {
			self.currentFrame = (self.currentFrame + 1) % self.animationFrames.length;
			self.removeChildAt(0);
			self.addChild(self.animationFrames[self.currentFrame]);
			self.lastFrameChange = Date.now();
		}
		if (self.y > 2732 + self.height / 2) {
			self.destroy();
		}
	};
	return self;
});
// Define the ForegroundContainer class
var ForegroundContainer = Container.expand(function () {
	var self = Container.call(this);
	return self;
});
// Define the MidgroundContainer class
var MidgroundContainer = Container.expand(function () {
	var self = Container.call(this);
	return self;
});
// Define the Obstacle class
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var obstacleGraphics = self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = obstacleGraphics.width;
	self.height = obstacleGraphics.height;
	self.speed = 5;
	self.rotationSpeed = Math.random() < 0.5 ? 0.03 : -0.03; // Randomly decide rotation direction
	self.update = function () {
		self.speed += 0.2; // Acceleration
		self.y += self.speed;
		self.rotation += self.rotationSpeed; // Add rotation
		if (self.y > 2732 + self.height / 2) {
			self.destroy();
		}
	};
	return self;
});
var Platform = Container.expand(function () {
	var self = Container.call(this);
	var platformGraphics = self.attachAsset('platform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = platformGraphics.width;
	self.height = platformGraphics.height;
	self.update = function () {
		if (gameStarted) {
			self.y += 30;
			if (self.y > 2732 + self.height / 2) {
				self.destroy();
			}
		}
	};
	return self;
});
//<Assets used in the game will automatically appear here>
// Define the Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	playerGraphics.visible = false;
	var playerIdleGraphics = self.attachAsset('player_idle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	playerIdleGraphics.visible = true;
	self.playerWidth = playerGraphics.width;
	self.playerHeight = playerGraphics.height;
	self.playerIdleWidth = playerIdleGraphics.width;
	self.playerIdleHeight = playerIdleGraphics.height;
	self.speed = playerHorizontalSpeed;
	self.slidingSpeed = playerSlidingSpeed; // Add sliding speed property
	self.direction = 1; // 1: right, -1: left
	self.isTouchingWall = true; // true: player is touching a wall, false: player is in mid air
	self.update = function () {
		if (gameStarted) {
			if (!self.isBlinking) {
				self.alpha = self.immune > 0 ? 0.5 : 1;
			}
			if (self.direction === -1 && self.x > self.playerWidth / 2 - 60) {
				self.x -= self.speed;
				if (self.x <= self.playerWidth / 2 - 60) {
					self.x = self.playerWidth / 2 - 60; // Prevent the player from going past the left edge of the screen
					self.isTouchingWall = true;
					playerGraphics.scale.x = -1; // Flip the player's x axis
				}
			} else if (self.direction === 1 && self.x < 2048 - self.playerWidth / 2 + 60) {
				self.x += self.speed;
				if (self.x >= 2048 - self.playerWidth / 2 + 60) {
					self.x = 2048 - self.playerWidth / 2 + 60; // Prevent the player from going past the right edge of the screen
					self.isTouchingWall = true;
					playerGraphics.scale.x = 1; // Flip the player's x axis back to its original state
				}
			}
			self.y += self.verticalSpeed; // Apply the vertical speed to the player's position
			if (self.y < 0) {
				// Prevent the player from going off the top of the screen
				self.y = 0;
				self.verticalSpeed = 0;
			}
			// Removed game over check in the player update method
			if (self.isTouchingWall) {
				// Reset the vertical speed and apply the sliding speed when the player touches a wall
				self.verticalSpeed = 0;
				self.slidingSpeed += playerSlidingAcceleration;
				self.y += self.slidingSpeed;
			}
		}
	};
	self.immune = false; // Add a new property to the player to make it immune to spikes
	self.isBlinking = false; // Add a new property to manage the blinking state
	self.jump = function () {
		if (self.isTouchingWall) {
			if (!gameStarted) {
				gameStarted = true;
				firstJump = true; // Set the firstJump flag to true after the first jump
			}
			self.direction *= -1;
			self.verticalSpeed = -playerVerticalJumpSpeed; // Set the vertical speed when jumping
			self.slidingSpeed = playerSlidingSpeed; // Reset the sliding speed
			self.isTouchingWall = false;
			playerGraphics.visible = true;
			playerIdleGraphics.visible = false;
			// Move backgrounds down by 200 pixels
			backgroundContainer.backgroundGraphics1.y += 300;
			backgroundContainer.backgroundGraphics2.y += 300;
			// Move forest down by 100 pixels
			forest.y += 300;
			// Reset background speed
			backgroundSpeed = initialBackgroundSpeed;
			// Move all spikes down by 500 pixels
			for (var i = 0; i < spikes.length; i++) {
				spikes[i].y += spikeMovement;
			}
			// Reset the spikes' upwards movement speed
			spikeUpwardsMovement = spikeSpeed;
			self.immune++; // Increment immunity to spikes after jumping
			LK.setTimeout(function () {
				// Set a timeout to remove the immunity after half a second
				self.immune--;
			}, 100);
			spawnObstacle(); // Spawn a new obstacle whenever the player jumps
		}
	};
	return self;
});
// Define the Spike class
var Spike = Container.expand(function () {
	var self = Container.call(this);
	var spikeGraphics = self.attachAsset('spikes', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = spikeGraphics.width;
	self.height = spikeGraphics.height;
	self.hasMoved = false; // Add hasMoved flag
	self.update = function () {
		if (self.y > 2732 + self.height / 2 + 500) {
			self.destroy();
		}
		if (player.isTouchingWall) {
			self.y -= spikeSpeed;
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x7abde6 //Init game with new background color
});
/**** 
* Game Code
****/ 
// Function to check score and start spike intervals
function checkAndStartSpikes() {
	if (LK.getScore() >= scoreThreshold && !spikeInterval && gameStarted) {
		spikeInterval = LK.setInterval(spawnSpike, spikeIntervalTime);
	}
}
// Declare global variables for background speed and acceleration
var initialBackgroundSpeed = 1;
var backgroundSpeed = initialBackgroundSpeed;
var backgroundAcceleration = 0.02;
var firstJump = false; // Flag to track the first jump
// Declare a global variable to track if the game has started
var gameStarted = false;
// Initialize BackgroundContainer, MidgroundContainer, and ForegroundContainer
var backgroundContainer = game.addChild(new BackgroundContainer());
var midgroundContainer = game.addChild(new MidgroundContainer());
var foregroundContainer = game.addChild(new ForegroundContainer());
var playerVerticalJumpSpeed = 20;
// Declare global variables for player's horizontal, vertical and sliding speed
var playerHorizontalSpeed = 80;
var playerSlidingSpeed = 1;
var playerSlidingAcceleration = 0.02;
// Declare a global variable to track if the game has started
var gameStarted = false;
// Declare global variables for the spikes movement, their upwards movement and their acceleration
var spikeMovement = 500;
var spikeUpwardsMovement = 10;
var spikeAcceleration = 0.02;
// Declare global variables for spike interval and score threshold
var spikeIntervalTime = 15000;
var scoreThreshold = 10;
// Initialize player
var player = foregroundContainer.addChild(new Player());
player.immune = 0; // Add a new property to the player to make it immune to spikes
// Add forest asset to the midground container
var forest = LK.getAsset('forest', {
	anchorX: 0.5,
	anchorY: 0,
	scaleX: 1,
	scaleY: 1,
	x: 2048 / 2,
	y: 2732 / 2
});
midgroundContainer.addChild(forest);
// Initialize platform and place it below the player_idle
var platform = midgroundContainer.addChild(new Platform());
platform.x = 2048 / 2;
platform.y = 2732 - 500 + player.playerIdleHeight / 2 + platform.height / 2;
// Initialize coins array
var coins = [];
// Track the number of coin arrays generated
var coinArrayCounter = 0;
// Initialize spikes array
var spikes = [];
// Set the direction at the start of the game
var spikePlacementFlag = Math.random() < 0.5 ? true : false;
// Initialize the first spike (Removed initial spike generation)
player.verticalSpeed = 0;
player.x = 2048 / 2;
player.y = 2732 - 430;
// Initialize obstacles array
var obstacles = [];
// Function to spawn coins instead of obstacles
function spawnObstacle() {
	if (gameStarted) {
		var coin = new Coin();
		coin.x = Math.random() * (2048 - 800) + 400; // Add padding to the coin spawner
		coin.y = -coin.height / 2;
		coins.push(coin);
		game.addChild(coin);
	}
}
// Function to spawn coins
function spawnCoins() {
	// Define the 18 available spots
	var spots = [];
	for (var i = 0; i < 3; i++) {
		for (var j = 0; j < 6; j++) {
			spots.push({
				x: j * (150 + 100) + 400,
				y: i * (150 + 50) - 500
			});
		}
	}
	// Shuffle the spots array
	for (var i = spots.length - 1; i > 0; i--) {
		var j = Math.floor(Math.random() * (i + 1));
		var temp = spots[i];
		spots[i] = spots[j];
		spots[j] = temp;
	}
	// Generate a random number of coins (between 4 and 9) in random spots
	var numCoins = Math.floor(Math.random() * 8) + 5;
	for (var i = 0; i < numCoins; i++) {
		var coin = new Coin();
		coin.x = spots[i].x;
		coin.y = spots[i].y;
		coins.push(coin);
		game.addChild(coin);
	}
	// Increment the coin array counter
	coinArrayCounter++;
	// Add extra coin to a random spot every 5 coin array generations
	if (coinArrayCounter % 5 === 0) {
		var extraCoin = new Coin();
		// Find an empty spot for the extra coin
		var emptySpots = spots.slice(numCoins); // Get the remaining spots after placing coins
		var randomEmptySpot = Math.floor(Math.random() * emptySpots.length);
		extraCoin.x = emptySpots[randomEmptySpot].x;
		extraCoin.y = emptySpots[randomEmptySpot].y;
		coins.push(extraCoin);
		game.addChild(extraCoin);
	}
}
// Function to spawn coins instead of spikes
function spawnSpike() {
	var coin = new Coin();
	coin.x = spikePlacementFlag ? 150 : 2048 - 150; // Place coin near walls
	coin.y = -coin.height / 2;
	coins.push(coin);
	game.addChild(coin);
	spikePlacementFlag = !spikePlacementFlag; // Flip the flag after each coin placement
}
// Initialize score text
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF,
	stroke: 0x000000,
	strokeThickness: 15
});
scoreTxt.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text.
scoreTxt.y -= 950; // Move the score 500 pixels higher
LK.gui.center.addChild(scoreTxt); // Add the score text to the GUI overlay.
// Set interval to spawn spikes and coins
var coinInterval;
var spikeInterval;
checkAndStartSpikes();
// Handle touch events
game.down = function (x, y, obj) {
	if (!gameStarted) {
		gameStarted = true;
		player.direction = Math.random() < 0.5 ? -1 : 1;
		LK.playMusic('background_music');
		// Spawn coins immediately after the first jump
		spawnCoins();
		// Start generating coins after the set interval
		coinInterval = LK.setInterval(spawnCoins, 3500);
		// Start generating spikes after the score threshold
		checkAndStartSpikes();
	}
	player.jump();
	// Play a sound effect when the player jumps
	LK.getSound('jump').play();
	LK.getSound('jumping').play();
};
game.up = function (x, y, obj) {
	// No operations needed here
};
// Update game logic
game.update = function () {
	if (gameStarted) {
		backgroundContainer.update();
		player.update();
		if (player.y > 2732 + player.height / 2) {
			// Move player much higher up when falling off screen
			player.y -= 1500;
			// Reset vertical speed to prevent continuous falling
			player.verticalSpeed = 0;
			// Reset sliding speed
			player.slidingSpeed = playerSlidingSpeed;
		}
		platform.update();
		forest.y -= 2; // Move the forest upwards by 1 pixel
		if (forest.y < -forest.height) {
			forest.destroy();
		}
		// Obstacles converted to coins - no collision detection needed
		for (var i = coins.length - 1; i >= 0; i--) {
			coins[i].update();
			if (coins[i].y > 2732 + coins[i].height / 2) {
				coins[i].destroy();
				coins.splice(i, 1);
			}
			if (player.intersects(coins[i])) {
				LK.setScore(LK.getScore() + 10000000000);
				scoreTxt.setText(LK.getScore());
				LK.getSound('coin').play();
				coins[i].destroy();
				coins.splice(i, 1);
				spawnObstacle();
				checkAndStartSpikes();
			}
		}
		// Spikes converted to coins - no collision detection needed
	}
};
var spikeSpeed = 3; /**** 
* Classes
****/ 
// Define the BackgroundContainer class
var BackgroundContainer = Container.expand(function () {
	var self = Container.call(this);
	var backgroundGraphics1 = self.attachAsset('background', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 2048 / 2732,
		scaleY: 2732 / 2732
	});
	backgroundGraphics1.x = 2048 / 2;
	backgroundGraphics1.y = 2732 / 2;
	var backgroundGraphics2 = self.attachAsset('background', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 2048 / 2732,
		scaleY: 2732 / 2732
	});
	backgroundGraphics2.x = 2048 / 2;
	backgroundGraphics2.y = -2732 / 2;
	self.backgroundGraphics1 = backgroundGraphics1;
	self.backgroundGraphics2 = backgroundGraphics2;
	self.update = function () {
		if (gameStarted) {
			self.backgroundGraphics1.y -= backgroundSpeed;
			self.backgroundGraphics2.y -= backgroundSpeed;
			backgroundSpeed += backgroundAcceleration;
		}
		// Ensure backgrounds stay within bounds after jump movement
		if (self.backgroundGraphics1.y > 2732 / 2) {
			self.backgroundGraphics1.y = self.backgroundGraphics2.y - 2732;
		}
		if (self.backgroundGraphics2.y > 2732 / 2) {
			self.backgroundGraphics2.y = self.backgroundGraphics1.y - 2732;
		}
		if (self.backgroundGraphics1.y <= -2732 / 2) {
			self.backgroundGraphics1.y = self.backgroundGraphics2.y + 2732;
		}
		if (self.backgroundGraphics2.y <= -2732 / 2) {
			self.backgroundGraphics2.y = self.backgroundGraphics1.y + 2732;
		}
	};
	return self;
});
var Booster = Container.expand(function () {
	var self = Container.call(this);
	var boosterGraphics = self.attachAsset('booster', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = boosterGraphics.width;
	self.height = boosterGraphics.height;
	self.speed = 2;
	self.update = function () {
		self.speed += 0.05; // Acceleration
		self.y += self.speed;
		if (self.y > 2732 + self.height / 2) {
			self.destroy();
		}
	};
	return self;
});
// Define the Coin class
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.speed = 2;
	self.animationFrames = [LK.getAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	}), LK.getAsset('coin_2', {
		anchorX: 0.5,
		anchorY: 0.5
	}), LK.getAsset('coin_3', {
		anchorX: 0.5,
		anchorY: 0.5
	}), LK.getAsset('coin_2', {
		anchorX: 0.5,
		anchorY: 0.5
	})];
	self.currentFrame = 0;
	self.frameDuration = 144; // 300 milliseconds per frame
	self.lastFrameChange = Date.now();
	self.update = function () {
		self.speed += 0.05; // Acceleration
		self.y += self.speed;
		// Handle animation frame change
		if (Date.now() - self.lastFrameChange >= self.frameDuration) {
			self.currentFrame = (self.currentFrame + 1) % self.animationFrames.length;
			self.removeChildAt(0);
			self.addChild(self.animationFrames[self.currentFrame]);
			self.lastFrameChange = Date.now();
		}
		if (self.y > 2732 + self.height / 2) {
			self.destroy();
		}
	};
	return self;
});
// Define the ForegroundContainer class
var ForegroundContainer = Container.expand(function () {
	var self = Container.call(this);
	return self;
});
// Define the MidgroundContainer class
var MidgroundContainer = Container.expand(function () {
	var self = Container.call(this);
	return self;
});
// Define the Obstacle class
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var obstacleGraphics = self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = obstacleGraphics.width;
	self.height = obstacleGraphics.height;
	self.speed = 5;
	self.rotationSpeed = Math.random() < 0.5 ? 0.03 : -0.03; // Randomly decide rotation direction
	self.update = function () {
		self.speed += 0.2; // Acceleration
		self.y += self.speed;
		self.rotation += self.rotationSpeed; // Add rotation
		if (self.y > 2732 + self.height / 2) {
			self.destroy();
		}
	};
	return self;
});
var Platform = Container.expand(function () {
	var self = Container.call(this);
	var platformGraphics = self.attachAsset('platform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = platformGraphics.width;
	self.height = platformGraphics.height;
	self.update = function () {
		if (gameStarted) {
			self.y += 30;
			if (self.y > 2732 + self.height / 2) {
				self.destroy();
			}
		}
	};
	return self;
});
//<Assets used in the game will automatically appear here>
// Define the Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	playerGraphics.visible = false;
	var playerIdleGraphics = self.attachAsset('player_idle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	playerIdleGraphics.visible = true;
	self.playerWidth = playerGraphics.width;
	self.playerHeight = playerGraphics.height;
	self.playerIdleWidth = playerIdleGraphics.width;
	self.playerIdleHeight = playerIdleGraphics.height;
	self.speed = playerHorizontalSpeed;
	self.slidingSpeed = playerSlidingSpeed; // Add sliding speed property
	self.direction = 1; // 1: right, -1: left
	self.isTouchingWall = true; // true: player is touching a wall, false: player is in mid air
	self.update = function () {
		if (gameStarted) {
			if (!self.isBlinking) {
				self.alpha = self.immune > 0 ? 0.5 : 1;
			}
			if (self.direction === -1 && self.x > self.playerWidth / 2 - 60) {
				self.x -= self.speed;
				if (self.x <= self.playerWidth / 2 - 60) {
					self.x = self.playerWidth / 2 - 60; // Prevent the player from going past the left edge of the screen
					self.isTouchingWall = true;
					playerGraphics.scale.x = -1; // Flip the player's x axis
				}
			} else if (self.direction === 1 && self.x < 2048 - self.playerWidth / 2 + 60) {
				self.x += self.speed;
				if (self.x >= 2048 - self.playerWidth / 2 + 60) {
					self.x = 2048 - self.playerWidth / 2 + 60; // Prevent the player from going past the right edge of the screen
					self.isTouchingWall = true;
					playerGraphics.scale.x = 1; // Flip the player's x axis back to its original state
				}
			}
			self.y += self.verticalSpeed; // Apply the vertical speed to the player's position
			if (self.y < 0) {
				// Prevent the player from going off the top of the screen
				self.y = 0;
				self.verticalSpeed = 0;
			}
			// Removed game over check in the player update method
			if (self.isTouchingWall) {
				// Reset the vertical speed and apply the sliding speed when the player touches a wall
				self.verticalSpeed = 0;
				self.slidingSpeed += playerSlidingAcceleration;
				self.y += self.slidingSpeed;
			}
		}
	};
	self.immune = false; // Add a new property to the player to make it immune to spikes
	self.isBlinking = false; // Add a new property to manage the blinking state
	self.jump = function () {
		if (self.isTouchingWall) {
			if (!gameStarted) {
				gameStarted = true;
				firstJump = true; // Set the firstJump flag to true after the first jump
			}
			self.direction *= -1;
			self.verticalSpeed = -playerVerticalJumpSpeed; // Set the vertical speed when jumping
			self.slidingSpeed = playerSlidingSpeed; // Reset the sliding speed
			self.isTouchingWall = false;
			playerGraphics.visible = true;
			playerIdleGraphics.visible = false;
			// Move backgrounds down by 200 pixels
			backgroundContainer.backgroundGraphics1.y += 300;
			backgroundContainer.backgroundGraphics2.y += 300;
			// Move forest down by 100 pixels
			forest.y += 300;
			// Reset background speed
			backgroundSpeed = initialBackgroundSpeed;
			// Move all spikes down by 500 pixels
			for (var i = 0; i < spikes.length; i++) {
				spikes[i].y += spikeMovement;
			}
			// Reset the spikes' upwards movement speed
			spikeUpwardsMovement = spikeSpeed;
			self.immune++; // Increment immunity to spikes after jumping
			LK.setTimeout(function () {
				// Set a timeout to remove the immunity after half a second
				self.immune--;
			}, 100);
			spawnObstacle(); // Spawn a new obstacle whenever the player jumps
		}
	};
	return self;
});
// Define the Spike class
var Spike = Container.expand(function () {
	var self = Container.call(this);
	var spikeGraphics = self.attachAsset('spikes', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = spikeGraphics.width;
	self.height = spikeGraphics.height;
	self.hasMoved = false; // Add hasMoved flag
	self.update = function () {
		if (self.y > 2732 + self.height / 2 + 500) {
			self.destroy();
		}
		if (player.isTouchingWall) {
			self.y -= spikeSpeed;
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x7abde6 //Init game with new background color
});
/**** 
* Game Code
****/ 
// Function to check score and start spike intervals
function checkAndStartSpikes() {
	if (LK.getScore() >= scoreThreshold && !spikeInterval && gameStarted) {
		spikeInterval = LK.setInterval(spawnSpike, spikeIntervalTime);
	}
}
// Declare global variables for background speed and acceleration
var initialBackgroundSpeed = 1;
var backgroundSpeed = initialBackgroundSpeed;
var backgroundAcceleration = 0.02;
var firstJump = false; // Flag to track the first jump
// Declare a global variable to track if the game has started
var gameStarted = false;
// Initialize BackgroundContainer, MidgroundContainer, and ForegroundContainer
var backgroundContainer = game.addChild(new BackgroundContainer());
var midgroundContainer = game.addChild(new MidgroundContainer());
var foregroundContainer = game.addChild(new ForegroundContainer());
var playerVerticalJumpSpeed = 20;
// Declare global variables for player's horizontal, vertical and sliding speed
var playerHorizontalSpeed = 80;
var playerSlidingSpeed = 1;
var playerSlidingAcceleration = 0.02;
// Declare a global variable to track if the game has started
var gameStarted = false;
// Declare global variables for the spikes movement, their upwards movement and their acceleration
var spikeMovement = 500;
var spikeUpwardsMovement = 10;
var spikeAcceleration = 0.02;
// Declare global variables for spike interval and score threshold
var spikeIntervalTime = 15000;
var scoreThreshold = 10;
// Initialize player
var player = foregroundContainer.addChild(new Player());
player.immune = 0; // Add a new property to the player to make it immune to spikes
// Add forest asset to the midground container
var forest = LK.getAsset('forest', {
	anchorX: 0.5,
	anchorY: 0,
	scaleX: 1,
	scaleY: 1,
	x: 2048 / 2,
	y: 2732 / 2
});
midgroundContainer.addChild(forest);
// Initialize platform and place it below the player_idle
var platform = midgroundContainer.addChild(new Platform());
platform.x = 2048 / 2;
platform.y = 2732 - 500 + player.playerIdleHeight / 2 + platform.height / 2;
// Initialize coins array
var coins = [];
// Track the number of coin arrays generated
var coinArrayCounter = 0;
// Initialize spikes array
var spikes = [];
// Set the direction at the start of the game
var spikePlacementFlag = Math.random() < 0.5 ? true : false;
// Initialize the first spike (Removed initial spike generation)
player.verticalSpeed = 0;
player.x = 2048 / 2;
player.y = 2732 - 430;
// Initialize obstacles array
var obstacles = [];
// Function to spawn coins instead of obstacles
function spawnObstacle() {
	if (gameStarted) {
		var coin = new Coin();
		coin.x = Math.random() * (2048 - 800) + 400; // Add padding to the coin spawner
		coin.y = -coin.height / 2;
		coins.push(coin);
		game.addChild(coin);
	}
}
// Function to spawn coins
function spawnCoins() {
	// Define the 18 available spots
	var spots = [];
	for (var i = 0; i < 3; i++) {
		for (var j = 0; j < 6; j++) {
			spots.push({
				x: j * (150 + 100) + 400,
				y: i * (150 + 50) - 500
			});
		}
	}
	// Shuffle the spots array
	for (var i = spots.length - 1; i > 0; i--) {
		var j = Math.floor(Math.random() * (i + 1));
		var temp = spots[i];
		spots[i] = spots[j];
		spots[j] = temp;
	}
	// Generate a random number of coins (between 4 and 9) in random spots
	var numCoins = Math.floor(Math.random() * 8) + 5;
	for (var i = 0; i < numCoins; i++) {
		var coin = new Coin();
		coin.x = spots[i].x;
		coin.y = spots[i].y;
		coins.push(coin);
		game.addChild(coin);
	}
	// Increment the coin array counter
	coinArrayCounter++;
	// Add extra coin to a random spot every 5 coin array generations
	if (coinArrayCounter % 5 === 0) {
		var extraCoin = new Coin();
		// Find an empty spot for the extra coin
		var emptySpots = spots.slice(numCoins); // Get the remaining spots after placing coins
		var randomEmptySpot = Math.floor(Math.random() * emptySpots.length);
		extraCoin.x = emptySpots[randomEmptySpot].x;
		extraCoin.y = emptySpots[randomEmptySpot].y;
		coins.push(extraCoin);
		game.addChild(extraCoin);
	}
}
// Function to spawn coins instead of spikes
function spawnSpike() {
	var coin = new Coin();
	coin.x = spikePlacementFlag ? 150 : 2048 - 150; // Place coin near walls
	coin.y = -coin.height / 2;
	coins.push(coin);
	game.addChild(coin);
	spikePlacementFlag = !spikePlacementFlag; // Flip the flag after each coin placement
}
// Initialize score text
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF,
	stroke: 0x000000,
	strokeThickness: 15
});
scoreTxt.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text.
scoreTxt.y -= 950; // Move the score 500 pixels higher
LK.gui.center.addChild(scoreTxt); // Add the score text to the GUI overlay.
// Set interval to spawn spikes and coins
var coinInterval;
var spikeInterval;
checkAndStartSpikes();
// Handle touch events
game.down = function (x, y, obj) {
	if (!gameStarted) {
		gameStarted = true;
		player.direction = Math.random() < 0.5 ? -1 : 1;
		LK.playMusic('background_music');
		// Spawn coins immediately after the first jump
		spawnCoins();
		// Start generating coins after the set interval
		coinInterval = LK.setInterval(spawnCoins, 3500);
		// Start generating spikes after the score threshold
		checkAndStartSpikes();
	}
	player.jump();
	// Play a sound effect when the player jumps
	LK.getSound('jump').play();
	LK.getSound('jumping').play();
};
game.up = function (x, y, obj) {
	// No operations needed here
};
// Update game logic
game.update = function () {
	if (gameStarted) {
		backgroundContainer.update();
		player.update();
		if (player.y > 2732 + player.height / 2) {
			// Move player much higher up when falling off screen
			player.y -= 1500;
			// Reset vertical speed to prevent continuous falling
			player.verticalSpeed = 0;
			// Reset sliding speed
			player.slidingSpeed = playerSlidingSpeed;
		}
		platform.update();
		forest.y -= 2; // Move the forest upwards by 1 pixel
		if (forest.y < -forest.height) {
			forest.destroy();
		}
		// Obstacles converted to coins - no collision detection needed
		for (var i = coins.length - 1; i >= 0; i--) {
			coins[i].update();
			if (coins[i].y > 2732 + coins[i].height / 2) {
				coins[i].destroy();
				coins.splice(i, 1);
			}
			if (player.intersects(coins[i])) {
				LK.setScore(LK.getScore() + 10000000000);
				scoreTxt.setText(LK.getScore());
				LK.getSound('coin').play();
				coins[i].destroy();
				coins.splice(i, 1);
				spawnObstacle();
				checkAndStartSpikes();
			}
		}
		// Spikes converted to coins - no collision detection needed
	}
};
var spikeSpeed = 3;
 8-bit pixel shuriken. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 
 8-bit pixelated background of a minimalist cloudy sky. keep it simple with a light blue sky of a single color and a few pixelated clouds scattered around. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 
 
 8-bit pixelated wooden stump. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 
 8-bit pixelated invisibility potion powerup. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.