/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var AmmoBox = Container.expand(function () {
var self = Container.call(this);
var ammoBoxGraphics = self.attachAsset('ammoBox', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 4;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.rotationSpeed = 0.02;
self.update = function () {
self.y += self.speed;
asteroidGraphics.rotation += self.rotationSpeed;
};
return self;
});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -12;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Explosion = Container.expand(function () {
var self = Container.call(this);
var explosionGraphics = self.attachAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5
});
self.lifetime = 30;
self.age = 0;
self.update = function () {
self.age++;
explosionGraphics.alpha = 1 - self.age / self.lifetime;
explosionGraphics.scaleX = explosionGraphics.scaleY = 1 + self.age / self.lifetime * 0.5;
};
return self;
});
var Ship = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('ship', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000A1A
});
/****
* Game Code
****/
var ship = new Ship();
var bullets = [];
var asteroids = [];
var explosions = [];
var ammoBoxes = [];
var gameTime = 0;
var asteroidSpawnTimer = 0;
var bulletFireTimer = 0;
var difficultyLevel = 1;
var powerUpActive = false;
var powerUpTimer = 0;
var powerUpDuration = 600; // 10 seconds at 60fps
var normalFireRate = 15;
var powerUpFireRate = 7;
var normalBulletCount = 1;
var powerUpBulletCount = 2;
var normalBulletSpeed = -12;
var powerUpBulletSpeed = -24;
// Initialize ship position
ship.x = 2048 / 2;
ship.y = 2732 - 150;
game.addChild(ship);
// Score display
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Dragging variables
var dragNode = null;
var dragOffset = 0;
function handleMove(x, y, obj) {
if (dragNode) {
var newX = x + dragOffset;
// Keep ship within screen bounds
newX = Math.max(90, Math.min(2048 - 90, newX));
dragNode.x = newX;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
// Calculate offset from ship center to touch point
dragOffset = ship.x - x;
dragNode = ship;
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragNode = null;
dragOffset = 0;
};
function spawnAsteroid() {
var asteroid = new Asteroid();
asteroid.x = Math.random() * (2048 - 180) + 90;
asteroid.y = -40;
asteroid.speed = 3 + Math.random() * 2 + difficultyLevel * 0.5;
asteroid.rotationSpeed = (Math.random() - 0.5) * 0.1;
asteroids.push(asteroid);
game.addChild(asteroid);
}
function spawnBullet() {
var bulletCount = powerUpActive ? powerUpBulletCount : normalBulletCount;
var spacing = 40; // Space between bullets when firing multiple
for (var i = 0; i < bulletCount; i++) {
var bullet = new Bullet();
if (bulletCount === 1) {
bullet.x = ship.x;
} else {
// Spread bullets evenly around ship center
var offset = (i - (bulletCount - 1) / 2) * spacing;
bullet.x = ship.x + offset;
}
bullet.y = ship.y - 40;
bullet.speed = powerUpActive ? powerUpBulletSpeed : normalBulletSpeed;
bullets.push(bullet);
game.addChild(bullet);
}
LK.getSound('shoot').play();
}
function spawnAmmoBox(x, y) {
var ammoBox = new AmmoBox();
ammoBox.x = x;
ammoBox.y = y;
ammoBoxes.push(ammoBox);
game.addChild(ammoBox);
}
function spawnExplosion(x, y) {
var explosion = new Explosion();
explosion.x = x;
explosion.y = y;
explosions.push(explosion);
game.addChild(explosion);
}
function updateScore() {
scoreTxt.setText('Score: ' + LK.getScore());
}
game.update = function () {
gameTime++;
// Increase difficulty over time
difficultyLevel = Math.floor(gameTime / 1800) + 1;
// Handle power-up timer
if (powerUpActive) {
powerUpTimer--;
if (powerUpTimer <= 0) {
powerUpActive = false;
}
}
// Spawn bullets
bulletFireTimer++;
var currentFireRate = powerUpActive ? powerUpFireRate : normalFireRate;
if (bulletFireTimer >= currentFireRate) {
spawnBullet();
bulletFireTimer = 0;
}
// Spawn asteroids
asteroidSpawnTimer++;
var spawnRate = Math.max(60 - difficultyLevel * 5, 20);
if (asteroidSpawnTimer >= spawnRate) {
spawnAsteroid();
asteroidSpawnTimer = 0;
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
if (bullet.lastY === undefined) bullet.lastY = bullet.y;
// Remove bullets that go off screen
if (bullet.lastY >= -10 && bullet.y < -10) {
bullet.destroy();
bullets.splice(i, 1);
continue;
}
bullet.lastY = bullet.y;
}
// Update asteroids
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
if (asteroid.lastY === undefined) asteroid.lastY = asteroid.y;
// Remove asteroids that go off screen
if (asteroid.lastY <= 2732 + 40 && asteroid.y > 2732 + 40) {
asteroid.destroy();
asteroids.splice(i, 1);
continue;
}
// Check collision with ship
if (asteroid.intersects(ship)) {
LK.getSound('hit').play();
LK.effects.flashScreen(0xFF0000, 1000);
LK.showGameOver();
return;
}
asteroid.lastY = asteroid.y;
}
// Update ammo boxes
for (var i = ammoBoxes.length - 1; i >= 0; i--) {
var ammoBox = ammoBoxes[i];
if (ammoBox.lastY === undefined) ammoBox.lastY = ammoBox.y;
// Remove ammo boxes that go off screen
if (ammoBox.lastY <= 2732 + 30 && ammoBox.y > 2732 + 30) {
ammoBox.destroy();
ammoBoxes.splice(i, 1);
continue;
}
// Check collision with ship
if (ammoBox.intersects(ship)) {
LK.getSound('powerup').play();
powerUpActive = true;
powerUpTimer = powerUpDuration;
ammoBox.destroy();
ammoBoxes.splice(i, 1);
}
ammoBox.lastY = ammoBox.y;
}
// Update explosions
for (var i = explosions.length - 1; i >= 0; i--) {
var explosion = explosions[i];
if (explosion.age >= explosion.lifetime) {
explosion.destroy();
explosions.splice(i, 1);
}
}
// Check bullet-asteroid collisions
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
var bulletHit = false;
for (var j = asteroids.length - 1; j >= 0; j--) {
var asteroid = asteroids[j];
if (bullet.intersects(asteroid)) {
// Create explosion
spawnExplosion(asteroid.x, asteroid.y);
// 30% chance to spawn ammo box
if (Math.random() < 0.3) {
spawnAmmoBox(asteroid.x, asteroid.y);
}
// Play sound
LK.getSound('explode').play();
// Update score
LK.setScore(LK.getScore() + 10);
updateScore();
// Remove bullet and asteroid
bullet.destroy();
bullets.splice(i, 1);
asteroid.destroy();
asteroids.splice(j, 1);
bulletHit = true;
break;
}
}
if (bulletHit) break;
}
};
// Initialize score display
updateScore(); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var AmmoBox = Container.expand(function () {
var self = Container.call(this);
var ammoBoxGraphics = self.attachAsset('ammoBox', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 4;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.rotationSpeed = 0.02;
self.update = function () {
self.y += self.speed;
asteroidGraphics.rotation += self.rotationSpeed;
};
return self;
});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -12;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Explosion = Container.expand(function () {
var self = Container.call(this);
var explosionGraphics = self.attachAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5
});
self.lifetime = 30;
self.age = 0;
self.update = function () {
self.age++;
explosionGraphics.alpha = 1 - self.age / self.lifetime;
explosionGraphics.scaleX = explosionGraphics.scaleY = 1 + self.age / self.lifetime * 0.5;
};
return self;
});
var Ship = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('ship', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000A1A
});
/****
* Game Code
****/
var ship = new Ship();
var bullets = [];
var asteroids = [];
var explosions = [];
var ammoBoxes = [];
var gameTime = 0;
var asteroidSpawnTimer = 0;
var bulletFireTimer = 0;
var difficultyLevel = 1;
var powerUpActive = false;
var powerUpTimer = 0;
var powerUpDuration = 600; // 10 seconds at 60fps
var normalFireRate = 15;
var powerUpFireRate = 7;
var normalBulletCount = 1;
var powerUpBulletCount = 2;
var normalBulletSpeed = -12;
var powerUpBulletSpeed = -24;
// Initialize ship position
ship.x = 2048 / 2;
ship.y = 2732 - 150;
game.addChild(ship);
// Score display
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Dragging variables
var dragNode = null;
var dragOffset = 0;
function handleMove(x, y, obj) {
if (dragNode) {
var newX = x + dragOffset;
// Keep ship within screen bounds
newX = Math.max(90, Math.min(2048 - 90, newX));
dragNode.x = newX;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
// Calculate offset from ship center to touch point
dragOffset = ship.x - x;
dragNode = ship;
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragNode = null;
dragOffset = 0;
};
function spawnAsteroid() {
var asteroid = new Asteroid();
asteroid.x = Math.random() * (2048 - 180) + 90;
asteroid.y = -40;
asteroid.speed = 3 + Math.random() * 2 + difficultyLevel * 0.5;
asteroid.rotationSpeed = (Math.random() - 0.5) * 0.1;
asteroids.push(asteroid);
game.addChild(asteroid);
}
function spawnBullet() {
var bulletCount = powerUpActive ? powerUpBulletCount : normalBulletCount;
var spacing = 40; // Space between bullets when firing multiple
for (var i = 0; i < bulletCount; i++) {
var bullet = new Bullet();
if (bulletCount === 1) {
bullet.x = ship.x;
} else {
// Spread bullets evenly around ship center
var offset = (i - (bulletCount - 1) / 2) * spacing;
bullet.x = ship.x + offset;
}
bullet.y = ship.y - 40;
bullet.speed = powerUpActive ? powerUpBulletSpeed : normalBulletSpeed;
bullets.push(bullet);
game.addChild(bullet);
}
LK.getSound('shoot').play();
}
function spawnAmmoBox(x, y) {
var ammoBox = new AmmoBox();
ammoBox.x = x;
ammoBox.y = y;
ammoBoxes.push(ammoBox);
game.addChild(ammoBox);
}
function spawnExplosion(x, y) {
var explosion = new Explosion();
explosion.x = x;
explosion.y = y;
explosions.push(explosion);
game.addChild(explosion);
}
function updateScore() {
scoreTxt.setText('Score: ' + LK.getScore());
}
game.update = function () {
gameTime++;
// Increase difficulty over time
difficultyLevel = Math.floor(gameTime / 1800) + 1;
// Handle power-up timer
if (powerUpActive) {
powerUpTimer--;
if (powerUpTimer <= 0) {
powerUpActive = false;
}
}
// Spawn bullets
bulletFireTimer++;
var currentFireRate = powerUpActive ? powerUpFireRate : normalFireRate;
if (bulletFireTimer >= currentFireRate) {
spawnBullet();
bulletFireTimer = 0;
}
// Spawn asteroids
asteroidSpawnTimer++;
var spawnRate = Math.max(60 - difficultyLevel * 5, 20);
if (asteroidSpawnTimer >= spawnRate) {
spawnAsteroid();
asteroidSpawnTimer = 0;
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
if (bullet.lastY === undefined) bullet.lastY = bullet.y;
// Remove bullets that go off screen
if (bullet.lastY >= -10 && bullet.y < -10) {
bullet.destroy();
bullets.splice(i, 1);
continue;
}
bullet.lastY = bullet.y;
}
// Update asteroids
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
if (asteroid.lastY === undefined) asteroid.lastY = asteroid.y;
// Remove asteroids that go off screen
if (asteroid.lastY <= 2732 + 40 && asteroid.y > 2732 + 40) {
asteroid.destroy();
asteroids.splice(i, 1);
continue;
}
// Check collision with ship
if (asteroid.intersects(ship)) {
LK.getSound('hit').play();
LK.effects.flashScreen(0xFF0000, 1000);
LK.showGameOver();
return;
}
asteroid.lastY = asteroid.y;
}
// Update ammo boxes
for (var i = ammoBoxes.length - 1; i >= 0; i--) {
var ammoBox = ammoBoxes[i];
if (ammoBox.lastY === undefined) ammoBox.lastY = ammoBox.y;
// Remove ammo boxes that go off screen
if (ammoBox.lastY <= 2732 + 30 && ammoBox.y > 2732 + 30) {
ammoBox.destroy();
ammoBoxes.splice(i, 1);
continue;
}
// Check collision with ship
if (ammoBox.intersects(ship)) {
LK.getSound('powerup').play();
powerUpActive = true;
powerUpTimer = powerUpDuration;
ammoBox.destroy();
ammoBoxes.splice(i, 1);
}
ammoBox.lastY = ammoBox.y;
}
// Update explosions
for (var i = explosions.length - 1; i >= 0; i--) {
var explosion = explosions[i];
if (explosion.age >= explosion.lifetime) {
explosion.destroy();
explosions.splice(i, 1);
}
}
// Check bullet-asteroid collisions
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
var bulletHit = false;
for (var j = asteroids.length - 1; j >= 0; j--) {
var asteroid = asteroids[j];
if (bullet.intersects(asteroid)) {
// Create explosion
spawnExplosion(asteroid.x, asteroid.y);
// 30% chance to spawn ammo box
if (Math.random() < 0.3) {
spawnAmmoBox(asteroid.x, asteroid.y);
}
// Play sound
LK.getSound('explode').play();
// Update score
LK.setScore(LK.getScore() + 10);
updateScore();
// Remove bullet and asteroid
bullet.destroy();
bullets.splice(i, 1);
asteroid.destroy();
asteroids.splice(j, 1);
bulletHit = true;
break;
}
}
if (bulletHit) break;
}
};
// Initialize score display
updateScore();