/**** * Classes ****/ var Alien = Container.expand(function () { var self = Container.call(this); var alienGraphics = self.attachAsset('alien', { anchorX: 0.5, anchorY: 0.5, tint: Math.floor(Math.random() * (0xFFFFFF - 0x808080)) + 0x808080 }); self.baseSpeed = 2; self.speedMultiplier = 1; self._move_migrated = function () { var dy = earth.y - self.y; var dx = earth.x - self.x; var angle = Math.atan2(dy, dx); var speed = self.baseSpeed * self.speedMultiplier; self.x += speed * Math.cos(angle); self.y += speed * Math.sin(angle); // Emit engine particles var particleAngle = angle + Math.PI; // Opposite direction of movement if (Math.random() < 0.5) { var particleSpeed = Math.random() * 2 + 2; var particle = ParticlePool.get(self.children[0].tint, particleSpeed, particleAngle); particle.x = self.x + 50 * Math.cos(particleAngle); particle.y = self.y + 50 * Math.sin(particleAngle); game.addChild(particle); } }; }); var Explosion = Container.expand(function (colorTint) { var self = Container.call(this); self.particles = []; for (var i = 0; i < 20; i++) { var particle = self.attachAsset('alien', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.2, scaleY: 0.2, alpha: 0.8, tint: colorTint }); particle.vx = (Math.random() - 0.5) * 10; particle.vy = (Math.random() - 0.5) * 10; self.particles.push(particle); } self._update_migrated = function () { for (var i = self.particles.length - 1; i >= 0; i--) { var p = self.particles[i]; p.x += p.vx; p.y += p.vy; p.alpha -= 0.02; if (p.alpha <= 0) { p.destroy(); self.particles.splice(i, 1); } } }; }); var Particle = Container.expand(function (colorTint, speed, angle) { var self = Container.call(this); var particleGraphics = self.attachAsset('alien', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.3, scaleY: 0.3, alpha: 0.8, tint: colorTint }); self.vx = speed * Math.cos(angle); self.vy = speed * Math.sin(angle); self.lifeSpan = 60; self.reset = function (colorTint, speed, angle) { self.vx = speed * Math.cos(angle); self.vy = speed * Math.sin(angle); self.alpha = 0.8; particleGraphics.tint = colorTint; self.lifeSpan = 60; }; self._update_migrated = function () { self.x += self.vx; self.y += self.vy; self.alpha -= 1 / self.lifeSpan; if (self.alpha <= 0) { self.visible = false; } }; return self; }); // Shield class var Shield = Container.expand(function () { var self = Container.call(this); self.attachAsset('shield', { anchorX: 0.5, anchorY: 0.5 }); self.orbitRadius = 200; self.orbitSpeed = 0.04; self.orbitAngle = 0; self._update_migrated = function () { self.orbitAngle += self.orbitSpeed; self.x = earth.x + self.orbitRadius * Math.cos(self.orbitAngle); self.y = earth.y + self.orbitRadius * Math.sin(self.orbitAngle); var angleToEarth = Math.atan2(earth.y - self.y, earth.x - self.x); self.rotation = angleToEarth + Math.PI / 2; }; }); // Star class var Star = Container.expand(function () { var self = Container.call(this); self.attachAsset('star', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 1; self._update_migrated = function () { var dy = game.height / 2 - self.y; var dx = game.width / 2 - self.x; var angle = Math.atan2(dy, dx); var distanceToCenter = Math.sqrt(dx * dx + dy * dy); // Make stars move faster as they get closer to earth var speedMultiplier = 1 + (1 - distanceToCenter / (Math.sqrt(game.height * game.height + game.width * game.width) / 2)); self.x += self.speed * speedMultiplier * Math.cos(angle); self.y += self.speed * speedMultiplier * Math.sin(angle); // Make stars transparent as they get closer to earth self.alpha = distanceToCenter / (Math.sqrt(game.height * game.height + game.width * game.width) / 2); // Check if star has reached the center of the screen if (distanceToCenter <= 10) { // 10 is a small threshold to consider the star at the center // Destroy and respawn the star self.x = Math.random() * game.width; self.y = Math.random() * game.height; self.alpha = 1; // Reset transparency } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Game variables var lives = 3; // Initialize assets used in the game. // Missile class // Alien class // Explosion class // Particle class var ParticlePool = function () { var particles = []; return { get: function get(colorTint, speed, angle) { var particle = particles.find(function (p) { return !p.visible; }); if (!particle) { particle = new Particle(colorTint, speed, angle); particles.push(particle); } else { particle.reset(colorTint, speed, angle); } particle.visible = true; return particle; }, updateAll: function updateAll() { particles.forEach(function (particle) { if (particle.visible) { particle._update_migrated(); } }); } }; }(); var aliens = []; var earth; var scoreTxt; var particles = []; // Array to keep track of particles var waveNumber = 1; var aliensPerWave = 5; var aliensSpawned = 0; var spawnAlienInterval = 60; // Frames until a new alien spawns var nextAlienSpawn = spawnAlienInterval; var waveInterval = 600; // Frames between waves var nextWaveInterval = waveInterval; var alienSpeedMultiplier = 1; // Speed multiplier for aliens // Initialize Earth function initEarth() { earth = game.addChild(LK.getAsset('earth', { anchorX: 0.5, anchorY: 0.5, x: game.width / 2, y: game.height / 2 })); window.shield = game.addChild(new Shield()); window.shield.x = earth.x; window.shield.y = earth.y - window.shield.orbitRadius; } // Initialize score display function initScore() { scoreTxt = new Text2('', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); livesIcons = []; for (var i = 0; i < lives; i++) { var lifeIcon = LK.getAsset('earth', { anchorX: 1, anchorY: 0, scaleX: 0.5, scaleY: 0.5, x: -i * 60 }); LK.gui.topRight.addChild(lifeIcon); livesIcons.push(lifeIcon); } } // Spawn an alien function spawnAlien() { var alien = new Alien(); alien.baseSpeed = 2 * alienSpeedMultiplier; var side = Math.floor(Math.random() * 4); switch (side) { case 0: // top alien.x = Math.random() * game.width; alien.y = -alien.height; break; case 1: // right alien.x = game.width + alien.width; alien.y = Math.random() * game.height; break; case 2: // bottom alien.x = Math.random() * game.width; alien.y = game.height + alien.height; break; case 3: // left alien.x = -alien.width; alien.y = Math.random() * game.height; break; } aliens.push(alien); game.addChild(alien); } // Check for collisions function checkCollisions() { for (var i = 0; i < aliens.length; i++) { if (window.shield.intersects(aliens[i])) { var alienColor = aliens[i].children[0].tint; var explosion = new Explosion(alienColor); explosion.x = aliens[i].x; explosion.y = aliens[i].y; game.addChild(explosion); aliens[i].destroy(); LK.getSound('Destroy').play(); LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore().toString()); aliens.splice(i, 1); } } } // Game over check function checkGameOver() { for (var i = 0; i < aliens.length; i++) { if (aliens[i].intersects(earth)) { var alienColor = aliens[i].children[0].tint; LK.effects.flashScreen(alienColor, 2000); lives--; livesIcons[lives].destroy(); livesIcons.pop(); if (lives <= 0) { LK.showGameOver(); } else { aliens[i].destroy(); aliens.splice(i, 1); LK.getSound('Hit').play(); } break; } } } // Touch event to move shield game.on('down', function (x, y, obj) { window.shield.orbitSpeed *= -1; LK.getSound('Move').play(); }); // Game tick event LK.on('tick', function () { // Update explosions and particles game.children.forEach(function (child) { if (child instanceof Explosion) { child._update_migrated(); } }); ParticlePool.updateAll(); // Move aliens for (var j = aliens.length - 1; j >= 0; j--) { aliens[j]._move_migrated(); if (aliens[j].y > game.height + aliens[j].height) { aliens[j].destroy(); aliens.splice(j, 1); } } // Spawn aliens and update spawn interval if (aliensSpawned < aliensPerWave) { nextAlienSpawn--; if (nextAlienSpawn <= 0) { spawnAlien(); aliensSpawned++; nextAlienSpawn = spawnAlienInterval; } } else { nextWaveInterval--; if (nextWaveInterval <= 0) { waveNumber++; aliensPerWave += 2; // Increase the number of aliens per wave aliensSpawned = 0; nextWaveInterval = waveInterval; alienSpeedMultiplier *= 1.1; // Increase alien speed by 10% } } // Shield speed remains constant // Update shield position window.shield._update_migrated(); // Update stars game.children.forEach(function (child) { if (child instanceof Star) { child._update_migrated(); } }); // Check for collisions and game over checkCollisions(); checkGameOver(); }); // Initialize game elements initScore(); // Create stars for (var i = 0; i < 50; i++) { var star = new Star(); star.x = Math.random() * game.width; star.y = Math.random() * game.height; game.addChild(star); } initEarth(); ;
/****
* Classes
****/
var Alien = Container.expand(function () {
var self = Container.call(this);
var alienGraphics = self.attachAsset('alien', {
anchorX: 0.5,
anchorY: 0.5,
tint: Math.floor(Math.random() * (0xFFFFFF - 0x808080)) + 0x808080
});
self.baseSpeed = 2;
self.speedMultiplier = 1;
self._move_migrated = function () {
var dy = earth.y - self.y;
var dx = earth.x - self.x;
var angle = Math.atan2(dy, dx);
var speed = self.baseSpeed * self.speedMultiplier;
self.x += speed * Math.cos(angle);
self.y += speed * Math.sin(angle);
// Emit engine particles
var particleAngle = angle + Math.PI; // Opposite direction of movement
if (Math.random() < 0.5) {
var particleSpeed = Math.random() * 2 + 2;
var particle = ParticlePool.get(self.children[0].tint, particleSpeed, particleAngle);
particle.x = self.x + 50 * Math.cos(particleAngle);
particle.y = self.y + 50 * Math.sin(particleAngle);
game.addChild(particle);
}
};
});
var Explosion = Container.expand(function (colorTint) {
var self = Container.call(this);
self.particles = [];
for (var i = 0; i < 20; i++) {
var particle = self.attachAsset('alien', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.2,
scaleY: 0.2,
alpha: 0.8,
tint: colorTint
});
particle.vx = (Math.random() - 0.5) * 10;
particle.vy = (Math.random() - 0.5) * 10;
self.particles.push(particle);
}
self._update_migrated = function () {
for (var i = self.particles.length - 1; i >= 0; i--) {
var p = self.particles[i];
p.x += p.vx;
p.y += p.vy;
p.alpha -= 0.02;
if (p.alpha <= 0) {
p.destroy();
self.particles.splice(i, 1);
}
}
};
});
var Particle = Container.expand(function (colorTint, speed, angle) {
var self = Container.call(this);
var particleGraphics = self.attachAsset('alien', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.3,
scaleY: 0.3,
alpha: 0.8,
tint: colorTint
});
self.vx = speed * Math.cos(angle);
self.vy = speed * Math.sin(angle);
self.lifeSpan = 60;
self.reset = function (colorTint, speed, angle) {
self.vx = speed * Math.cos(angle);
self.vy = speed * Math.sin(angle);
self.alpha = 0.8;
particleGraphics.tint = colorTint;
self.lifeSpan = 60;
};
self._update_migrated = function () {
self.x += self.vx;
self.y += self.vy;
self.alpha -= 1 / self.lifeSpan;
if (self.alpha <= 0) {
self.visible = false;
}
};
return self;
});
// Shield class
var Shield = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('shield', {
anchorX: 0.5,
anchorY: 0.5
});
self.orbitRadius = 200;
self.orbitSpeed = 0.04;
self.orbitAngle = 0;
self._update_migrated = function () {
self.orbitAngle += self.orbitSpeed;
self.x = earth.x + self.orbitRadius * Math.cos(self.orbitAngle);
self.y = earth.y + self.orbitRadius * Math.sin(self.orbitAngle);
var angleToEarth = Math.atan2(earth.y - self.y, earth.x - self.x);
self.rotation = angleToEarth + Math.PI / 2;
};
});
// Star class
var Star = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1;
self._update_migrated = function () {
var dy = game.height / 2 - self.y;
var dx = game.width / 2 - self.x;
var angle = Math.atan2(dy, dx);
var distanceToCenter = Math.sqrt(dx * dx + dy * dy);
// Make stars move faster as they get closer to earth
var speedMultiplier = 1 + (1 - distanceToCenter / (Math.sqrt(game.height * game.height + game.width * game.width) / 2));
self.x += self.speed * speedMultiplier * Math.cos(angle);
self.y += self.speed * speedMultiplier * Math.sin(angle);
// Make stars transparent as they get closer to earth
self.alpha = distanceToCenter / (Math.sqrt(game.height * game.height + game.width * game.width) / 2);
// Check if star has reached the center of the screen
if (distanceToCenter <= 10) {
// 10 is a small threshold to consider the star at the center
// Destroy and respawn the star
self.x = Math.random() * game.width;
self.y = Math.random() * game.height;
self.alpha = 1; // Reset transparency
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Game variables
var lives = 3;
// Initialize assets used in the game.
// Missile class
// Alien class
// Explosion class
// Particle class
var ParticlePool = function () {
var particles = [];
return {
get: function get(colorTint, speed, angle) {
var particle = particles.find(function (p) {
return !p.visible;
});
if (!particle) {
particle = new Particle(colorTint, speed, angle);
particles.push(particle);
} else {
particle.reset(colorTint, speed, angle);
}
particle.visible = true;
return particle;
},
updateAll: function updateAll() {
particles.forEach(function (particle) {
if (particle.visible) {
particle._update_migrated();
}
});
}
};
}();
var aliens = [];
var earth;
var scoreTxt;
var particles = []; // Array to keep track of particles
var waveNumber = 1;
var aliensPerWave = 5;
var aliensSpawned = 0;
var spawnAlienInterval = 60; // Frames until a new alien spawns
var nextAlienSpawn = spawnAlienInterval;
var waveInterval = 600; // Frames between waves
var nextWaveInterval = waveInterval;
var alienSpeedMultiplier = 1; // Speed multiplier for aliens
// Initialize Earth
function initEarth() {
earth = game.addChild(LK.getAsset('earth', {
anchorX: 0.5,
anchorY: 0.5,
x: game.width / 2,
y: game.height / 2
}));
window.shield = game.addChild(new Shield());
window.shield.x = earth.x;
window.shield.y = earth.y - window.shield.orbitRadius;
}
// Initialize score display
function initScore() {
scoreTxt = new Text2('', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
livesIcons = [];
for (var i = 0; i < lives; i++) {
var lifeIcon = LK.getAsset('earth', {
anchorX: 1,
anchorY: 0,
scaleX: 0.5,
scaleY: 0.5,
x: -i * 60
});
LK.gui.topRight.addChild(lifeIcon);
livesIcons.push(lifeIcon);
}
}
// Spawn an alien
function spawnAlien() {
var alien = new Alien();
alien.baseSpeed = 2 * alienSpeedMultiplier;
var side = Math.floor(Math.random() * 4);
switch (side) {
case 0:
// top
alien.x = Math.random() * game.width;
alien.y = -alien.height;
break;
case 1:
// right
alien.x = game.width + alien.width;
alien.y = Math.random() * game.height;
break;
case 2:
// bottom
alien.x = Math.random() * game.width;
alien.y = game.height + alien.height;
break;
case 3:
// left
alien.x = -alien.width;
alien.y = Math.random() * game.height;
break;
}
aliens.push(alien);
game.addChild(alien);
}
// Check for collisions
function checkCollisions() {
for (var i = 0; i < aliens.length; i++) {
if (window.shield.intersects(aliens[i])) {
var alienColor = aliens[i].children[0].tint;
var explosion = new Explosion(alienColor);
explosion.x = aliens[i].x;
explosion.y = aliens[i].y;
game.addChild(explosion);
aliens[i].destroy();
LK.getSound('Destroy').play();
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore().toString());
aliens.splice(i, 1);
}
}
}
// Game over check
function checkGameOver() {
for (var i = 0; i < aliens.length; i++) {
if (aliens[i].intersects(earth)) {
var alienColor = aliens[i].children[0].tint;
LK.effects.flashScreen(alienColor, 2000);
lives--;
livesIcons[lives].destroy();
livesIcons.pop();
if (lives <= 0) {
LK.showGameOver();
} else {
aliens[i].destroy();
aliens.splice(i, 1);
LK.getSound('Hit').play();
}
break;
}
}
}
// Touch event to move shield
game.on('down', function (x, y, obj) {
window.shield.orbitSpeed *= -1;
LK.getSound('Move').play();
});
// Game tick event
LK.on('tick', function () {
// Update explosions and particles
game.children.forEach(function (child) {
if (child instanceof Explosion) {
child._update_migrated();
}
});
ParticlePool.updateAll();
// Move aliens
for (var j = aliens.length - 1; j >= 0; j--) {
aliens[j]._move_migrated();
if (aliens[j].y > game.height + aliens[j].height) {
aliens[j].destroy();
aliens.splice(j, 1);
}
}
// Spawn aliens and update spawn interval
if (aliensSpawned < aliensPerWave) {
nextAlienSpawn--;
if (nextAlienSpawn <= 0) {
spawnAlien();
aliensSpawned++;
nextAlienSpawn = spawnAlienInterval;
}
} else {
nextWaveInterval--;
if (nextWaveInterval <= 0) {
waveNumber++;
aliensPerWave += 2; // Increase the number of aliens per wave
aliensSpawned = 0;
nextWaveInterval = waveInterval;
alienSpeedMultiplier *= 1.1; // Increase alien speed by 10%
}
}
// Shield speed remains constant
// Update shield position
window.shield._update_migrated();
// Update stars
game.children.forEach(function (child) {
if (child instanceof Star) {
child._update_migrated();
}
});
// Check for collisions and game over
checkCollisions();
checkGameOver();
});
// Initialize game elements
initScore();
// Create stars
for (var i = 0; i < 50; i++) {
var star = new Star();
star.x = Math.random() * game.width;
star.y = Math.random() * game.height;
game.addChild(star);
}
initEarth();
;