/**** * Classes ****/ var Boss = Container.expand(function () { var self = Container.call(this); var bossGraphics = self.attachAsset('obstacle3', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.health = 100; // Boss health self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; self.takeDamage = function (damage) { self.health -= damage; if (self.health <= 0) { self.destroy(); score += 1000; // Increase score when boss is defeated scoreTxt.setText('Score: ' + Math.floor(score)); } }; }); var CircleObstacleGroup = Container.expand(function () { var self = Container.call(this); var radius = 150; var numObstacles = 8; for (var i = 0; i < numObstacles; i++) { var angle = i / numObstacles * Math.PI * 2; var obstacle = new Obstacle(); obstacle.x = Math.cos(angle) * radius; obstacle.y = Math.sin(angle) * radius; self.addChild(obstacle); } self.update = function () { self.y += 5; self.rotation += 0.05; // Rotate the group if (self.y > 2732) { self.destroy(); } }; }); var DiamondObstacleGroup = Container.expand(function () { var self = Container.call(this); var size = 100; var positions = [{ x: 0, y: -size }, { x: -size, y: 0 }, { x: size, y: 0 }, { x: 0, y: size }]; positions.forEach(function (pos) { var obstacle = new Obstacle2(); obstacle.x = pos.x; obstacle.y = pos.y; self.addChild(obstacle); }); self.update = function () { self.y += 5; if (self.y > 2732) { self.destroy(); } }; }); var FreeObstacleGroup = Container.expand(function () { var self = Container.call(this); var numObstacles = 5 + Math.floor(Math.random() * 5); // Random number of obstacles between 5 and 10 for (var i = 0; i < numObstacles; i++) { var obstacle = new Obstacle(); obstacle.x = Math.random() * 2048; // Random x position obstacle.y = Math.random() * 500 - 500; // Random y position above the screen self.addChild(obstacle); } self.update = function () { self.y += 5; if (self.y > 2732) { self.destroy(); } }; }); var JavelinObstacleGroup = Container.expand(function () { var self = Container.call(this); var length = 200; for (var i = 0; i < 5; i++) { var obstacle = new Obstacle3(); obstacle.x = 0; obstacle.y = i * length / 5 - length / 2; self.addChild(obstacle); } self.update = function () { self.y += 5; if (self.y > 2732) { self.destroy(); } }; }); var Lazer = Container.expand(function () { var self = Container.call(this); var lazerGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { self.y += self.speed + Math.floor(score / 100); if (self.y > 2732) { self.destroy(); } }; }); var LazerShootingObstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.shootInterval = 100; // Interval for shooting lazers self.shootTimer = 0; self.update = function () { self.y += self.speed + Math.floor(score / 100); self.x += Math.sin(self.y / 100) * 5; // Zig-zag movement self.shootTimer++; if (self.shootTimer >= self.shootInterval) { self.shootLazer(); self.shootTimer = 0; } if (self.y > 2732) { self.destroy(); } }; self.shootLazer = function () { var lazer = new Lazer(); lazer.x = self.x; lazer.y = self.y; game.addChild(lazer); }; }); // Define a class for obstacles var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); var Obstacle2 = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle2', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); var Obstacle3 = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle3', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Assets will be automatically created and loaded by the LK engine based on their usage in the code. // Define a class for the player's car var PlayerCar = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('playerCar', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.specialAbilityActive = false; self.specialAbilityDuration = 300; // Duration in frames self.specialAbilityCooldown = 600; // Cooldown in frames self.specialAbilityTimer = 0; self.activateSpecialAbility = function () { if (!self.specialAbilityActive && self.specialAbilityTimer <= 0) { self.specialAbilityActive = true; self.specialAbilityTimer = self.specialAbilityDuration; // Visual effect for special ability activation LK.effects.flashObject(self, 0x00ff00, self.specialAbilityDuration * 16.67); } }; self.update = function () { if (self.specialAbilityActive) { // Special ability logic, e.g., temporary invincibility self.specialAbilityTimer--; if (self.specialAbilityTimer <= 0) { self.specialAbilityActive = false; self.specialAbilityTimer = -self.specialAbilityCooldown; } } else if (self.specialAbilityTimer < 0) { self.specialAbilityTimer++; } self.shootLazer = function () { var lazer = new PlayerLazer(); lazer.x = self.x; lazer.y = self.y - 50; // Position lazer in front of the car game.addChild(lazer); }; }; }); var PlayerLazer = Container.expand(function () { var self = Container.call(this); var lazerGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; // Move upwards self.update = function () { self.y += self.speed; if (self.y < 0) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize game variables var playerCar; var obstacles = []; var score = 0; var scoreTxt; var bossSpawned = false; // Track if boss has been spawned var difficulty = 'Medium'; // Default difficulty // Add event listeners for difficulty buttons LK.gui.topLeft.addChild(new Text2('Easy', { size: 50, fill: "#ffffff" }).on('down', function () { difficulty = 'Easy'; })); LK.gui.top.addChild(new Text2('Medium', { size: 50, fill: "#ffffff" }).on('down', function () { difficulty = 'Medium'; })); LK.gui.topRight.addChild(new Text2('Hard', { size: 50, fill: "#ffffff" }).on('down', function () { difficulty = 'Hard'; })); // Function to handle game updates game.update = function () { // Update player car playerCar.update(); // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { // Adjust speed increment based on score and difficulty var baseSpeed = 3; if (difficulty === 'Easy') { baseSpeed = 2; } else if (difficulty === 'Hard') { baseSpeed = 5; } obstacles[i].speed = baseSpeed + Math.floor(score / 20); obstacles[i].update(); if (playerCar.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; // Stop further processing if collision occurs } else { if (obstacles[i] instanceof LazerShootingObstacle || obstacles[i] instanceof Boss) { var playerLazers = game.children.filter(function (child) { return child instanceof PlayerLazer; }); for (var j = playerLazers.length - 1; j >= 0; j--) { playerLazers[j].update(); if (obstacles[i].intersects(playerLazers[j])) { obstacles[i].takeDamage(10); // Reduce obstacle health playerLazers[j].destroy(); // Destroy the lazer after collision } } } if (obstacles[i] instanceof LazerShootingObstacle) { var lazers = game.children.filter(function (child) { return child instanceof Lazer; }); for (var j = lazers.length - 1; j >= 0; j--) { lazers[j].update(); if (playerCar.intersects(lazers[j])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); lazers[j].destroy(); // Destroy the lazer after collision return; // Stop further processing if collision occurs } } } if (score < 5000) { score += 0.2; // Increase score at a slower rate scoreTxt.setText('Score: ' + Math.floor(score)); // Update score display with whole numbers if (score >= 2500 && score < 3000 && !bossSpawned) { for (var i = 0; i < 5; i++) { // Spawn a group of 5 LazerShootingObstacles var groupObstacle = new LazerShootingObstacle(); groupObstacle.x = i * 400 + 200; // Spread them out horizontally groupObstacle.y = -100; game.addChild(groupObstacle); obstacles.push(groupObstacle); } } else if (score >= 3000 && !bossSpawned) { var boss = new Boss(); boss.x = 2048 / 2; boss.y = -100; game.addChild(boss); obstacles.push(boss); bossSpawned = true; } } else { LK.showPopup('You Win!'); // Show 'You Win' pop-up when score reaches 5000 } } } // Spawn new obstacles // Adjust spawn frequency based on difficulty var spawnInterval = Math.max(20, 50 - Math.floor(score / 15)); if (difficulty === 'Easy') { spawnInterval += 10; } else if (difficulty === 'Hard') { spawnInterval -= 10; } if (LK.ticks % spawnInterval == 0 && obstacles.length < 1000) { // Decrease interval based on score var newObstacle; var randomValue = Math.random(); if (randomValue > 0.5) { newObstacle = new LazerShootingObstacle(); } else { newObstacle = new Obstacle(); } newObstacle.x = Math.random() * 2048; // Random initial x position newObstacle.x = playerCar.x; newObstacle.y = -50; obstacles.push(newObstacle); game.addChild(newObstacle); } }; // Function to handle player car movement game.move = function (x, y, obj) { playerCar.x = x; playerCar.y = y; // Activate special ability on double-tap if (obj.event && obj.event.detail === 2) { playerCar.activateSpecialAbility(); playerCar.shootLazer(); // Shoot lazer on tap } }; // Initialize player car playerCar = new PlayerCar(); playerCar.x = 2048 / 2; playerCar.y = 2732 - 200; game.addChild(playerCar); // Initialize score text scoreTxt = new Text2('Score: 0', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt);
/****
* Classes
****/
var Boss = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('obstacle3', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.health = 100; // Boss health
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
self.takeDamage = function (damage) {
self.health -= damage;
if (self.health <= 0) {
self.destroy();
score += 1000; // Increase score when boss is defeated
scoreTxt.setText('Score: ' + Math.floor(score));
}
};
});
var CircleObstacleGroup = Container.expand(function () {
var self = Container.call(this);
var radius = 150;
var numObstacles = 8;
for (var i = 0; i < numObstacles; i++) {
var angle = i / numObstacles * Math.PI * 2;
var obstacle = new Obstacle();
obstacle.x = Math.cos(angle) * radius;
obstacle.y = Math.sin(angle) * radius;
self.addChild(obstacle);
}
self.update = function () {
self.y += 5;
self.rotation += 0.05; // Rotate the group
if (self.y > 2732) {
self.destroy();
}
};
});
var DiamondObstacleGroup = Container.expand(function () {
var self = Container.call(this);
var size = 100;
var positions = [{
x: 0,
y: -size
}, {
x: -size,
y: 0
}, {
x: size,
y: 0
}, {
x: 0,
y: size
}];
positions.forEach(function (pos) {
var obstacle = new Obstacle2();
obstacle.x = pos.x;
obstacle.y = pos.y;
self.addChild(obstacle);
});
self.update = function () {
self.y += 5;
if (self.y > 2732) {
self.destroy();
}
};
});
var FreeObstacleGroup = Container.expand(function () {
var self = Container.call(this);
var numObstacles = 5 + Math.floor(Math.random() * 5); // Random number of obstacles between 5 and 10
for (var i = 0; i < numObstacles; i++) {
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048; // Random x position
obstacle.y = Math.random() * 500 - 500; // Random y position above the screen
self.addChild(obstacle);
}
self.update = function () {
self.y += 5;
if (self.y > 2732) {
self.destroy();
}
};
});
var JavelinObstacleGroup = Container.expand(function () {
var self = Container.call(this);
var length = 200;
for (var i = 0; i < 5; i++) {
var obstacle = new Obstacle3();
obstacle.x = 0;
obstacle.y = i * length / 5 - length / 2;
self.addChild(obstacle);
}
self.update = function () {
self.y += 5;
if (self.y > 2732) {
self.destroy();
}
};
});
var Lazer = Container.expand(function () {
var self = Container.call(this);
var lazerGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y += self.speed + Math.floor(score / 100);
if (self.y > 2732) {
self.destroy();
}
};
});
var LazerShootingObstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.shootInterval = 100; // Interval for shooting lazers
self.shootTimer = 0;
self.update = function () {
self.y += self.speed + Math.floor(score / 100);
self.x += Math.sin(self.y / 100) * 5; // Zig-zag movement
self.shootTimer++;
if (self.shootTimer >= self.shootInterval) {
self.shootLazer();
self.shootTimer = 0;
}
if (self.y > 2732) {
self.destroy();
}
};
self.shootLazer = function () {
var lazer = new Lazer();
lazer.x = self.x;
lazer.y = self.y;
game.addChild(lazer);
};
});
// Define a class for obstacles
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
var Obstacle2 = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle2', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
var Obstacle3 = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle3', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Define a class for the player's car
var PlayerCar = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('playerCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.specialAbilityActive = false;
self.specialAbilityDuration = 300; // Duration in frames
self.specialAbilityCooldown = 600; // Cooldown in frames
self.specialAbilityTimer = 0;
self.activateSpecialAbility = function () {
if (!self.specialAbilityActive && self.specialAbilityTimer <= 0) {
self.specialAbilityActive = true;
self.specialAbilityTimer = self.specialAbilityDuration;
// Visual effect for special ability activation
LK.effects.flashObject(self, 0x00ff00, self.specialAbilityDuration * 16.67);
}
};
self.update = function () {
if (self.specialAbilityActive) {
// Special ability logic, e.g., temporary invincibility
self.specialAbilityTimer--;
if (self.specialAbilityTimer <= 0) {
self.specialAbilityActive = false;
self.specialAbilityTimer = -self.specialAbilityCooldown;
}
} else if (self.specialAbilityTimer < 0) {
self.specialAbilityTimer++;
}
self.shootLazer = function () {
var lazer = new PlayerLazer();
lazer.x = self.x;
lazer.y = self.y - 50; // Position lazer in front of the car
game.addChild(lazer);
};
};
});
var PlayerLazer = Container.expand(function () {
var self = Container.call(this);
var lazerGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10; // Move upwards
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var playerCar;
var obstacles = [];
var score = 0;
var scoreTxt;
var bossSpawned = false; // Track if boss has been spawned
var difficulty = 'Medium'; // Default difficulty
// Add event listeners for difficulty buttons
LK.gui.topLeft.addChild(new Text2('Easy', {
size: 50,
fill: "#ffffff"
}).on('down', function () {
difficulty = 'Easy';
}));
LK.gui.top.addChild(new Text2('Medium', {
size: 50,
fill: "#ffffff"
}).on('down', function () {
difficulty = 'Medium';
}));
LK.gui.topRight.addChild(new Text2('Hard', {
size: 50,
fill: "#ffffff"
}).on('down', function () {
difficulty = 'Hard';
}));
// Function to handle game updates
game.update = function () {
// Update player car
playerCar.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
// Adjust speed increment based on score and difficulty
var baseSpeed = 3;
if (difficulty === 'Easy') {
baseSpeed = 2;
} else if (difficulty === 'Hard') {
baseSpeed = 5;
}
obstacles[i].speed = baseSpeed + Math.floor(score / 20);
obstacles[i].update();
if (playerCar.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return; // Stop further processing if collision occurs
} else {
if (obstacles[i] instanceof LazerShootingObstacle || obstacles[i] instanceof Boss) {
var playerLazers = game.children.filter(function (child) {
return child instanceof PlayerLazer;
});
for (var j = playerLazers.length - 1; j >= 0; j--) {
playerLazers[j].update();
if (obstacles[i].intersects(playerLazers[j])) {
obstacles[i].takeDamage(10); // Reduce obstacle health
playerLazers[j].destroy(); // Destroy the lazer after collision
}
}
}
if (obstacles[i] instanceof LazerShootingObstacle) {
var lazers = game.children.filter(function (child) {
return child instanceof Lazer;
});
for (var j = lazers.length - 1; j >= 0; j--) {
lazers[j].update();
if (playerCar.intersects(lazers[j])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
lazers[j].destroy(); // Destroy the lazer after collision
return; // Stop further processing if collision occurs
}
}
}
if (score < 5000) {
score += 0.2; // Increase score at a slower rate
scoreTxt.setText('Score: ' + Math.floor(score)); // Update score display with whole numbers
if (score >= 2500 && score < 3000 && !bossSpawned) {
for (var i = 0; i < 5; i++) {
// Spawn a group of 5 LazerShootingObstacles
var groupObstacle = new LazerShootingObstacle();
groupObstacle.x = i * 400 + 200; // Spread them out horizontally
groupObstacle.y = -100;
game.addChild(groupObstacle);
obstacles.push(groupObstacle);
}
} else if (score >= 3000 && !bossSpawned) {
var boss = new Boss();
boss.x = 2048 / 2;
boss.y = -100;
game.addChild(boss);
obstacles.push(boss);
bossSpawned = true;
}
} else {
LK.showPopup('You Win!'); // Show 'You Win' pop-up when score reaches 5000
}
}
}
// Spawn new obstacles
// Adjust spawn frequency based on difficulty
var spawnInterval = Math.max(20, 50 - Math.floor(score / 15));
if (difficulty === 'Easy') {
spawnInterval += 10;
} else if (difficulty === 'Hard') {
spawnInterval -= 10;
}
if (LK.ticks % spawnInterval == 0 && obstacles.length < 1000) {
// Decrease interval based on score
var newObstacle;
var randomValue = Math.random();
if (randomValue > 0.5) {
newObstacle = new LazerShootingObstacle();
} else {
newObstacle = new Obstacle();
}
newObstacle.x = Math.random() * 2048; // Random initial x position
newObstacle.x = playerCar.x;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
};
// Function to handle player car movement
game.move = function (x, y, obj) {
playerCar.x = x;
playerCar.y = y;
// Activate special ability on double-tap
if (obj.event && obj.event.detail === 2) {
playerCar.activateSpecialAbility();
playerCar.shootLazer(); // Shoot lazer on tap
}
};
// Initialize player car
playerCar = new PlayerCar();
playerCar.x = 2048 / 2;
playerCar.y = 2732 - 200;
game.addChild(playerCar);
// Initialize score text
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);