/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 0; self.init = function (speed) { self.speed = speed || 10; // Add rotating animation tween(coinGraphics, { rotation: Math.PI * 2 }, { duration: 1500, onFinish: function onFinish() { coinGraphics.rotation = 0; self.init(self.speed); } }); }; self.update = function () { self.x -= self.speed; // Remove if off screen if (self.x < -coinGraphics.width) { self.destroy(); } }; return self; }); var Ground = Container.expand(function () { var self = Container.call(this); var groundGraphics = self.attachAsset('ground', { anchorX: 0, anchorY: 0 }); return self; }); var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 0; self.type = "normal"; // normal, low, high self.init = function (type, speed) { self.type = type || "normal"; self.speed = speed || 10; // Adjust obstacle appearance based on type if (self.type === "low") { obstacleGraphics.scaleY = 0.5; obstacleGraphics.y = obstacleGraphics.height * 0.25; } else if (self.type === "high") { obstacleGraphics.y = -obstacleGraphics.height * 0.5; } }; self.update = function () { self.x -= self.speed; // Remove if off screen if (self.x < -obstacleGraphics.width) { self.destroy(); } }; return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.vSpeed = 0; self.gravity = 0.8; self.jumpPower = -20; self.onGround = false; self.isSliding = false; self.isDashing = false; self.slideTimer = 0; self.dashTimer = 0; self.isDoubleJumpEnabled = false; self.hasDoubleJumped = false; self.isInvincible = false; self.invincibleTimer = 0; self.jump = function () { if (self.onGround) { self.vSpeed = self.jumpPower; self.onGround = false; LK.getSound('jump').play(); } else if (self.isDoubleJumpEnabled && !self.hasDoubleJumped) { self.vSpeed = self.jumpPower * 0.8; self.hasDoubleJumped = true; LK.getSound('jump').play(); } }; self.slide = function () { if (!self.isSliding && self.onGround) { self.isSliding = true; self.slideTimer = 45; // 45 frames = 0.75 seconds at 60fps // Make player half height when sliding tween(playerGraphics, { scaleY: 0.5, y: playerGraphics.height * 0.25 }, { duration: 200, easing: tween.easeOut }); } }; self.dash = function () { if (!self.isDashing) { self.isDashing = true; self.dashTimer = 20; // 20 frames = 0.33 seconds at 60fps LK.getSound('powerup_collect').play(); } }; self.applyDoubleJump = function () { self.isDoubleJumpEnabled = true; self.hasDoubleJumped = false; }; self.applyInvincibility = function () { self.isInvincible = true; self.invincibleTimer = 300; // 5 seconds // Flash effect for invincibility var flashInterval = LK.setInterval(function () { playerGraphics.alpha = playerGraphics.alpha === 1 ? 0.4 : 1; }, 100); LK.setTimeout(function () { LK.clearInterval(flashInterval); playerGraphics.alpha = 1; }, 5000); }; self.resetSlide = function () { self.isSliding = false; tween(playerGraphics, { scaleY: 1, y: 0 }, { duration: 200, easing: tween.easeOut }); }; self.update = function () { // Apply gravity self.vSpeed += self.gravity; self.y += self.vSpeed; // Handle collision with ground if (self.y > groundY - playerGraphics.height / 2) { self.y = groundY - playerGraphics.height / 2; self.vSpeed = 0; self.onGround = true; self.hasDoubleJumped = false; } // Handle slide timer if (self.isSliding) { self.slideTimer--; if (self.slideTimer <= 0) { self.resetSlide(); } } // Handle dash timer if (self.isDashing) { self.dashTimer--; if (self.dashTimer <= 0) { self.isDashing = false; } } // Handle invincibility timer if (self.isInvincible) { self.invincibleTimer--; if (self.invincibleTimer <= 0) { self.isInvincible = false; } } // Keep player within bounds if (self.x < 200) { self.x = 200; } if (self.x > 800) { self.x = 800; } }; return self; }); var PowerUp = Container.expand(function () { var self = Container.call(this); var powerupGraphics = self.attachAsset('powerup', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 0; self.type = "doubleJump"; // doubleJump or invincibility self.init = function (type, speed) { self.type = type || "doubleJump"; self.speed = speed || 10; // Different colors for different powerups if (self.type === "doubleJump") { powerupGraphics.tint = 0x9b59b6; // Purple } else if (self.type === "invincibility") { powerupGraphics.tint = 0xf39c12; // Orange } // Add floating animation tween(self, { y: self.y - 20 }, { duration: 800, easing: tween.easeInOut, onFinish: function onFinish() { tween(self, { y: self.y + 20 }, { duration: 800, easing: tween.easeInOut, onFinish: function onFinish() { if (!self.destroyed) { self.init(self.type, self.speed); } } }); } }); }; self.update = function () { self.x -= self.speed; // Remove if off screen if (self.x < -powerupGraphics.width) { self.destroyed = true; self.destroy(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Game variables var player; var obstacles = []; var coins = []; var powerups = []; var ground; var groundY; var gameSpeed = 10; var gameStarted = false; var score = 0; var distance = 0; var coinCount = 0; var spawnTimer = 0; var speedIncreaseTimer = 0; var lastSpawnType = ""; // Setup UI var scoreTxt = new Text2('Score: 0', { size: 60, fill: 0xFFFFFF }); scoreTxt.anchor.set(0, 0); LK.gui.topRight.addChild(scoreTxt); var coinsTxt = new Text2('Coins: 0', { size: 60, fill: 0xFFFFFF }); coinsTxt.anchor.set(0, 0); coinsTxt.y = 70; LK.gui.topRight.addChild(coinsTxt); var promptTxt = new Text2('Tap to Start', { size: 100, fill: 0xFFFFFF }); promptTxt.anchor.set(0.5, 0.5); LK.gui.center.addChild(promptTxt); // Initialize game elements function initGame() { // Create ground ground = new Ground(); ground.y = 2732 - 100; // Position at bottom of screen game.addChild(ground); groundY = ground.y; // Create player player = new Player(); player.x = 400; player.y = groundY - 75; game.addChild(player); // Reset game variables gameSpeed = 10; obstacles = []; coins = []; powerups = []; score = 0; distance = 0; coinCount = 0; spawnTimer = 0; speedIncreaseTimer = 0; // Update UI updateScore(); updateCoins(); // Play background music LK.playMusic('bgmusic', { loop: true, fade: { start: 0, end: 0.4, duration: 1000 } }); } // Start the game function startGame() { gameStarted = true; promptTxt.visible = false; } // Update score display function updateScore() { scoreTxt.setText('Score: ' + Math.floor(score)); } // Update coin display function updateCoins() { coinsTxt.setText('Coins: ' + coinCount); } // Spawn obstacle function spawnObstacle() { var obstacle = new Obstacle(); // Determine obstacle type var types = ["normal", "low", "high"]; var type = types[Math.floor(Math.random() * types.length)]; // Avoid spawning same type twice in a row if (type === lastSpawnType && Math.random() > 0.3) { type = types[(types.indexOf(type) + 1) % types.length]; } lastSpawnType = type; obstacle.init(type, gameSpeed); obstacle.x = 2048 + obstacle.width / 2; obstacle.y = groundY - obstacle.height / 2; game.addChild(obstacle); obstacles.push(obstacle); } // Spawn coin function spawnCoin(x, y) { var coin = new Coin(); coin.init(gameSpeed); // If no position specified, use random position if (x === undefined || y === undefined) { coin.x = 2048 + coin.width / 2; // Position coins at different heights var heightOptions = [groundY - 200, // Above ground groundY - 400, // Higher jump groundY - 100 // Low position ]; coin.y = heightOptions[Math.floor(Math.random() * heightOptions.length)]; } else { coin.x = x; coin.y = y; } game.addChild(coin); coins.push(coin); } // Spawn coin line function spawnCoinLine() { var baseX = 2048 + 50; var baseY = groundY - 200 - Math.random() * 200; for (var i = 0; i < 5; i++) { spawnCoin(baseX + i * 100, baseY); } } // Spawn power-up function spawnPowerUp() { var powerup = new PowerUp(); // Randomly choose power-up type var type = Math.random() > 0.5 ? "doubleJump" : "invincibility"; powerup.init(type, gameSpeed); powerup.x = 2048 + powerup.width / 2; powerup.y = groundY - 300 - Math.random() * 100; game.addChild(powerup); powerups.push(powerup); } // Handle player collision with obstacles function checkCollisions() { // Check obstacle collisions for (var i = obstacles.length - 1; i >= 0; i--) { var obstacle = obstacles[i]; if (player.intersects(obstacle) && !player.isInvincible) { // Game over LK.getSound('crash').play(); LK.effects.flashScreen(0xff0000, 500); LK.setScore(Math.floor(score)); LK.showGameOver(); return; } } // Check coin collisions for (var j = coins.length - 1; j >= 0; j--) { var coin = coins[j]; if (player.intersects(coin)) { // Collect coin LK.getSound('coin_collect').play(); coinCount++; score += 10; updateCoins(); updateScore(); coin.destroy(); coins.splice(j, 1); } } // Check power-up collisions for (var k = powerups.length - 1; k >= 0; k--) { var powerup = powerups[k]; if (player.intersects(powerup)) { // Apply power-up effect LK.getSound('powerup_collect').play(); if (powerup.type === "doubleJump") { player.applyDoubleJump(); } else if (powerup.type === "invincibility") { player.applyInvincibility(); } score += 25; updateScore(); powerup.destroyed = true; powerup.destroy(); powerups.splice(k, 1); } } } // Handle player input game.down = function (x, y, obj) { if (!gameStarted) { startGame(); return; } // Detect tap position if (y < groundY - 400) { // Upper screen - Jump player.jump(); } else if (y > groundY - 200) { // Lower screen - Slide player.slide(); } else { // Middle screen - Dash player.dash(); } }; // Update game state game.update = function () { if (!gameStarted) { if (!ground) { initGame(); } return; } // Update player player.update(); // Update all obstacles for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); // Remove if destroyed if (obstacles[i].destroyed) { obstacles.splice(i, 1); } } // Update all coins for (var j = coins.length - 1; j >= 0; j--) { coins[j].update(); // Remove if destroyed if (coins[j].destroyed) { coins.splice(j, 1); } } // Update all power-ups for (var k = powerups.length - 1; k >= 0; k--) { powerups[k].update(); // Remove if destroyed if (powerups[k].destroyed) { powerups.splice(k, 1); } } // Check for collisions checkCollisions(); // Spawn logic spawnTimer++; // Spawn obstacle every 70-100 frames (depending on game speed) var spawnRate = Math.max(100 - gameSpeed, 70); if (spawnTimer >= spawnRate) { spawnTimer = 0; // Determine what to spawn var rand = Math.random(); if (rand < 0.6) { // 60% chance for obstacle spawnObstacle(); } else if (rand < 0.9) { // 30% chance for coin line spawnCoinLine(); } else { // 10% chance for power-up spawnPowerUp(); } } // Increase game speed over time speedIncreaseTimer++; if (speedIncreaseTimer >= 300) { // Every 5 seconds speedIncreaseTimer = 0; gameSpeed += 0.5; // Update speed of all moving objects for (var o = 0; o < obstacles.length; o++) { obstacles[o].speed = gameSpeed; } for (var c = 0; c < coins.length; c++) { coins[c].speed = gameSpeed; } for (var p = 0; p < powerups.length; p++) { powerups[p].speed = gameSpeed; } } // Update score based on distance distance += gameSpeed / 60; score = distance * 10; // Update score display every 10 frames if (LK.ticks % 10 === 0) { updateScore(); } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.init = function (speed) {
self.speed = speed || 10;
// Add rotating animation
tween(coinGraphics, {
rotation: Math.PI * 2
}, {
duration: 1500,
onFinish: function onFinish() {
coinGraphics.rotation = 0;
self.init(self.speed);
}
});
};
self.update = function () {
self.x -= self.speed;
// Remove if off screen
if (self.x < -coinGraphics.width) {
self.destroy();
}
};
return self;
});
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('ground', {
anchorX: 0,
anchorY: 0
});
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.type = "normal"; // normal, low, high
self.init = function (type, speed) {
self.type = type || "normal";
self.speed = speed || 10;
// Adjust obstacle appearance based on type
if (self.type === "low") {
obstacleGraphics.scaleY = 0.5;
obstacleGraphics.y = obstacleGraphics.height * 0.25;
} else if (self.type === "high") {
obstacleGraphics.y = -obstacleGraphics.height * 0.5;
}
};
self.update = function () {
self.x -= self.speed;
// Remove if off screen
if (self.x < -obstacleGraphics.width) {
self.destroy();
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.vSpeed = 0;
self.gravity = 0.8;
self.jumpPower = -20;
self.onGround = false;
self.isSliding = false;
self.isDashing = false;
self.slideTimer = 0;
self.dashTimer = 0;
self.isDoubleJumpEnabled = false;
self.hasDoubleJumped = false;
self.isInvincible = false;
self.invincibleTimer = 0;
self.jump = function () {
if (self.onGround) {
self.vSpeed = self.jumpPower;
self.onGround = false;
LK.getSound('jump').play();
} else if (self.isDoubleJumpEnabled && !self.hasDoubleJumped) {
self.vSpeed = self.jumpPower * 0.8;
self.hasDoubleJumped = true;
LK.getSound('jump').play();
}
};
self.slide = function () {
if (!self.isSliding && self.onGround) {
self.isSliding = true;
self.slideTimer = 45; // 45 frames = 0.75 seconds at 60fps
// Make player half height when sliding
tween(playerGraphics, {
scaleY: 0.5,
y: playerGraphics.height * 0.25
}, {
duration: 200,
easing: tween.easeOut
});
}
};
self.dash = function () {
if (!self.isDashing) {
self.isDashing = true;
self.dashTimer = 20; // 20 frames = 0.33 seconds at 60fps
LK.getSound('powerup_collect').play();
}
};
self.applyDoubleJump = function () {
self.isDoubleJumpEnabled = true;
self.hasDoubleJumped = false;
};
self.applyInvincibility = function () {
self.isInvincible = true;
self.invincibleTimer = 300; // 5 seconds
// Flash effect for invincibility
var flashInterval = LK.setInterval(function () {
playerGraphics.alpha = playerGraphics.alpha === 1 ? 0.4 : 1;
}, 100);
LK.setTimeout(function () {
LK.clearInterval(flashInterval);
playerGraphics.alpha = 1;
}, 5000);
};
self.resetSlide = function () {
self.isSliding = false;
tween(playerGraphics, {
scaleY: 1,
y: 0
}, {
duration: 200,
easing: tween.easeOut
});
};
self.update = function () {
// Apply gravity
self.vSpeed += self.gravity;
self.y += self.vSpeed;
// Handle collision with ground
if (self.y > groundY - playerGraphics.height / 2) {
self.y = groundY - playerGraphics.height / 2;
self.vSpeed = 0;
self.onGround = true;
self.hasDoubleJumped = false;
}
// Handle slide timer
if (self.isSliding) {
self.slideTimer--;
if (self.slideTimer <= 0) {
self.resetSlide();
}
}
// Handle dash timer
if (self.isDashing) {
self.dashTimer--;
if (self.dashTimer <= 0) {
self.isDashing = false;
}
}
// Handle invincibility timer
if (self.isInvincible) {
self.invincibleTimer--;
if (self.invincibleTimer <= 0) {
self.isInvincible = false;
}
}
// Keep player within bounds
if (self.x < 200) {
self.x = 200;
}
if (self.x > 800) {
self.x = 800;
}
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerupGraphics = self.attachAsset('powerup', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.type = "doubleJump"; // doubleJump or invincibility
self.init = function (type, speed) {
self.type = type || "doubleJump";
self.speed = speed || 10;
// Different colors for different powerups
if (self.type === "doubleJump") {
powerupGraphics.tint = 0x9b59b6; // Purple
} else if (self.type === "invincibility") {
powerupGraphics.tint = 0xf39c12; // Orange
}
// Add floating animation
tween(self, {
y: self.y - 20
}, {
duration: 800,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(self, {
y: self.y + 20
}, {
duration: 800,
easing: tween.easeInOut,
onFinish: function onFinish() {
if (!self.destroyed) {
self.init(self.type, self.speed);
}
}
});
}
});
};
self.update = function () {
self.x -= self.speed;
// Remove if off screen
if (self.x < -powerupGraphics.width) {
self.destroyed = true;
self.destroy();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Game variables
var player;
var obstacles = [];
var coins = [];
var powerups = [];
var ground;
var groundY;
var gameSpeed = 10;
var gameStarted = false;
var score = 0;
var distance = 0;
var coinCount = 0;
var spawnTimer = 0;
var speedIncreaseTimer = 0;
var lastSpawnType = "";
// Setup UI
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
var coinsTxt = new Text2('Coins: 0', {
size: 60,
fill: 0xFFFFFF
});
coinsTxt.anchor.set(0, 0);
coinsTxt.y = 70;
LK.gui.topRight.addChild(coinsTxt);
var promptTxt = new Text2('Tap to Start', {
size: 100,
fill: 0xFFFFFF
});
promptTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(promptTxt);
// Initialize game elements
function initGame() {
// Create ground
ground = new Ground();
ground.y = 2732 - 100; // Position at bottom of screen
game.addChild(ground);
groundY = ground.y;
// Create player
player = new Player();
player.x = 400;
player.y = groundY - 75;
game.addChild(player);
// Reset game variables
gameSpeed = 10;
obstacles = [];
coins = [];
powerups = [];
score = 0;
distance = 0;
coinCount = 0;
spawnTimer = 0;
speedIncreaseTimer = 0;
// Update UI
updateScore();
updateCoins();
// Play background music
LK.playMusic('bgmusic', {
loop: true,
fade: {
start: 0,
end: 0.4,
duration: 1000
}
});
}
// Start the game
function startGame() {
gameStarted = true;
promptTxt.visible = false;
}
// Update score display
function updateScore() {
scoreTxt.setText('Score: ' + Math.floor(score));
}
// Update coin display
function updateCoins() {
coinsTxt.setText('Coins: ' + coinCount);
}
// Spawn obstacle
function spawnObstacle() {
var obstacle = new Obstacle();
// Determine obstacle type
var types = ["normal", "low", "high"];
var type = types[Math.floor(Math.random() * types.length)];
// Avoid spawning same type twice in a row
if (type === lastSpawnType && Math.random() > 0.3) {
type = types[(types.indexOf(type) + 1) % types.length];
}
lastSpawnType = type;
obstacle.init(type, gameSpeed);
obstacle.x = 2048 + obstacle.width / 2;
obstacle.y = groundY - obstacle.height / 2;
game.addChild(obstacle);
obstacles.push(obstacle);
}
// Spawn coin
function spawnCoin(x, y) {
var coin = new Coin();
coin.init(gameSpeed);
// If no position specified, use random position
if (x === undefined || y === undefined) {
coin.x = 2048 + coin.width / 2;
// Position coins at different heights
var heightOptions = [groundY - 200,
// Above ground
groundY - 400,
// Higher jump
groundY - 100 // Low position
];
coin.y = heightOptions[Math.floor(Math.random() * heightOptions.length)];
} else {
coin.x = x;
coin.y = y;
}
game.addChild(coin);
coins.push(coin);
}
// Spawn coin line
function spawnCoinLine() {
var baseX = 2048 + 50;
var baseY = groundY - 200 - Math.random() * 200;
for (var i = 0; i < 5; i++) {
spawnCoin(baseX + i * 100, baseY);
}
}
// Spawn power-up
function spawnPowerUp() {
var powerup = new PowerUp();
// Randomly choose power-up type
var type = Math.random() > 0.5 ? "doubleJump" : "invincibility";
powerup.init(type, gameSpeed);
powerup.x = 2048 + powerup.width / 2;
powerup.y = groundY - 300 - Math.random() * 100;
game.addChild(powerup);
powerups.push(powerup);
}
// Handle player collision with obstacles
function checkCollisions() {
// Check obstacle collisions
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
if (player.intersects(obstacle) && !player.isInvincible) {
// Game over
LK.getSound('crash').play();
LK.effects.flashScreen(0xff0000, 500);
LK.setScore(Math.floor(score));
LK.showGameOver();
return;
}
}
// Check coin collisions
for (var j = coins.length - 1; j >= 0; j--) {
var coin = coins[j];
if (player.intersects(coin)) {
// Collect coin
LK.getSound('coin_collect').play();
coinCount++;
score += 10;
updateCoins();
updateScore();
coin.destroy();
coins.splice(j, 1);
}
}
// Check power-up collisions
for (var k = powerups.length - 1; k >= 0; k--) {
var powerup = powerups[k];
if (player.intersects(powerup)) {
// Apply power-up effect
LK.getSound('powerup_collect').play();
if (powerup.type === "doubleJump") {
player.applyDoubleJump();
} else if (powerup.type === "invincibility") {
player.applyInvincibility();
}
score += 25;
updateScore();
powerup.destroyed = true;
powerup.destroy();
powerups.splice(k, 1);
}
}
}
// Handle player input
game.down = function (x, y, obj) {
if (!gameStarted) {
startGame();
return;
}
// Detect tap position
if (y < groundY - 400) {
// Upper screen - Jump
player.jump();
} else if (y > groundY - 200) {
// Lower screen - Slide
player.slide();
} else {
// Middle screen - Dash
player.dash();
}
};
// Update game state
game.update = function () {
if (!gameStarted) {
if (!ground) {
initGame();
}
return;
}
// Update player
player.update();
// Update all obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
// Remove if destroyed
if (obstacles[i].destroyed) {
obstacles.splice(i, 1);
}
}
// Update all coins
for (var j = coins.length - 1; j >= 0; j--) {
coins[j].update();
// Remove if destroyed
if (coins[j].destroyed) {
coins.splice(j, 1);
}
}
// Update all power-ups
for (var k = powerups.length - 1; k >= 0; k--) {
powerups[k].update();
// Remove if destroyed
if (powerups[k].destroyed) {
powerups.splice(k, 1);
}
}
// Check for collisions
checkCollisions();
// Spawn logic
spawnTimer++;
// Spawn obstacle every 70-100 frames (depending on game speed)
var spawnRate = Math.max(100 - gameSpeed, 70);
if (spawnTimer >= spawnRate) {
spawnTimer = 0;
// Determine what to spawn
var rand = Math.random();
if (rand < 0.6) {
// 60% chance for obstacle
spawnObstacle();
} else if (rand < 0.9) {
// 30% chance for coin line
spawnCoinLine();
} else {
// 10% chance for power-up
spawnPowerUp();
}
}
// Increase game speed over time
speedIncreaseTimer++;
if (speedIncreaseTimer >= 300) {
// Every 5 seconds
speedIncreaseTimer = 0;
gameSpeed += 0.5;
// Update speed of all moving objects
for (var o = 0; o < obstacles.length; o++) {
obstacles[o].speed = gameSpeed;
}
for (var c = 0; c < coins.length; c++) {
coins[c].speed = gameSpeed;
}
for (var p = 0; p < powerups.length; p++) {
powerups[p].speed = gameSpeed;
}
}
// Update score based on distance
distance += gameSpeed / 60;
score = distance * 10;
// Update score display every 10 frames
if (LK.ticks % 10 === 0) {
updateScore();
}
};