/****
* Classes
****/
var Anime = Container.expand(function () {
var self = Container.call(this);
var animeGraphics = self.attachAsset('anime1', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Define the Bullet class
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -75;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
// Define the 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 = 25;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Define the Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 50;
self.update = function () {
// Player update logic
};
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Add space background image
var spaceBackground = LK.getAsset('spaceBackground', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(spaceBackground);
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Initialize score text
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize enemies array
var enemies = [];
// Initialize anime array
var animes = [];
// Initialize bullets array
var bullets = [];
// Handle player movement
game.move = function (x, y, obj) {
player.move(x, y);
};
// Handle shooting
game.down = function (x, y, obj) {
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
};
// Update game state
game.update = function () {
// Update player
player.update();
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
bullets[j].update();
for (var k = enemies.length - 1; k >= 0; k--) {
if (bullets[j].intersects(enemies[k])) {
bullets[j].destroy();
bullets.splice(j, 1);
enemies[k].destroy();
enemies.splice(k, 1);
// Update score
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
if (LK.getScore() >= 1) {
LK.effects.flashScreen(0x00ff00, 1000);
LK.showGameOver();
}
if (LK.getScore() >= 1) {
LK.effects.flashScreen(0x00ff00, 1000);
LK.showGameOver();
}
break;
}
}
for (var l = animes.length - 1; l >= 0; l--) {
if (bullets[j] && bullets[j].intersects(animes[l])) {
bullets[j].destroy();
bullets.splice(j, 1);
animes[l].destroy();
animes.splice(l, 1);
// Update score
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
break;
}
}
}
// Spawn enemies
if (LK.ticks % 60 == 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -100;
enemies.push(enemy);
game.addChild(enemy);
}
// Spawn animes
if (LK.ticks % 120 == 0) {
var anime = new Anime();
anime.x = Math.random() * 2048;
anime.y = -100;
animes.push(anime);
game.addChild(anime);
}
};