/****
* Classes
****/
// Asteroid class
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 7;
self.scale.set(0.5 + Math.random() * 1.5);
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + asteroidGraphics.height) {
self.destroy();
asteroids.splice(asteroids.indexOf(self), 1);
}
};
});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
if (self.y < 2732 / 2) {
self.y += self.speed;
} else {
self.x += Math.sin(LK.ticks / 10) * 5;
self.y += Math.cos(LK.ticks / 10) * 5;
// Shoot bullet towards hero
if (LK.ticks % 100 == 0) {
var bullet = new EnemyBullet();
bullet.x = self.x;
bullet.y = self.y + enemyGraphics.height / 2;
// Calculate angle towards hero
var angle = Math.atan2(hero.y - bullet.y, hero.x - bullet.x);
bullet.rotation = angle; // Rotate bullet towards hero
bullet.vx = Math.cos(angle) * bullet.speed;
bullet.vy = Math.sin(angle) * bullet.speed;
game.addChild(bullet);
enemyBullets.push(bullet);
}
}
if (self.y > 2732 + enemyGraphics.height) {
self.destroy();
enemies.splice(enemies.indexOf(self), 1);
}
};
});
// EnemyBullet class
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.x += self.vx;
self.y += self.vy;
if (self.y > 2732 + bulletGraphics.height) {
self.destroy();
enemyBullets.splice(enemyBullets.indexOf(self), 1);
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Hero update logic
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
// Play shooting sound
LK.getSound('shoot').play();
};
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < -bulletGraphics.height) {
self.destroy();
heroBullets.splice(heroBullets.indexOf(self), 1);
}
};
});
var StarBackground = Container.expand(function () {
var self = Container.call(this);
var background = self.attachAsset('starBackground', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize star background
var starBackground = new StarBackground();
starBackground.x = 2048 / 2;
starBackground.y = 0;
game.addChild(starBackground);
// Initialize arrays and variables
var hero;
var heroBullets = [];
var enemies = [];
var asteroids = [];
var enemyBullets = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Handle game events
game.down = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.update = function () {
// Update star background
starBackground.update();
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
// Check for collision between hero bullets and enemies
for (var i = heroBullets.length - 1; i >= 0; i--) {
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
enemies[j].destroy();
enemies.splice(j, 1);
heroBullets[i].destroy();
heroBullets.splice(i, 1);
score += 10; // Increase score by 10
break;
}
}
}
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (hero.intersects(enemies[j])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
enemyBullets[i].update();
if (hero.intersects(enemyBullets[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
for (var j = heroBullets.length - 1; j >= 0; j--) {
if (enemyBullets[i] && heroBullets[j] && enemyBullets[i].intersects(heroBullets[j])) {
enemyBullets[i].destroy();
heroBullets[j].destroy();
enemyBullets.splice(i, 1);
heroBullets.splice(j, 1);
break;
}
}
}
for (var k = asteroids.length - 1; k >= 0; k--) {
if (asteroids[k]) {
asteroids[k].update();
for (var i = heroBullets.length - 1; i >= 0; i--) {
if (heroBullets[i].intersects(asteroids[k])) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
break;
}
}
if (hero.intersects(asteroids[k])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
}
// Spawn enemies
if (LK.ticks % 30 == 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -enemy.height;
game.addChild(enemy);
enemies.push(enemy);
}
// Spawn asteroids
if (LK.ticks % 280 == 0) {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -asteroid.height;
// Set random scale for the asteroid
var scale = 0.5 + Math.random() * 1.5;
asteroid.scale.set(scale, scale);
game.addChild(asteroid);
asteroids.push(asteroid);
}
// Update score
scoreTxt.setText(score);
};
// Shoot bullets
game.down = function (x, y, obj) {
hero.shoot();
};