/****
* Classes
****/
// 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 = -15;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
// 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 () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// 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 = 10;
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
****/
var player;
var enemies = [];
var bullets = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -50;
enemies.push(enemy);
game.addChild(enemy);
}
function shootBullet() {
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
bullets.push(bullet);
game.addChild(bullet);
}
game.down = function (x, y, obj) {
player.move(x, y);
};
game.move = function (x, y, obj) {
player.move(x, y);
};
game.update = function () {
player.update();
enemies.forEach(function (enemy) {
enemy.update();
if (enemy.intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
});
bullets.forEach(function (bullet) {
bullet.update();
enemies.forEach(function (enemy, index) {
if (bullet.intersects(enemy)) {
bullet.destroy();
enemy.destroy();
bullets.splice(bullets.indexOf(bullet), 1);
enemies.splice(index, 1);
score += 1;
scoreTxt.setText(score);
}
});
});
if (LK.ticks % 60 == 0) {
spawnEnemy();
}
if (LK.ticks % 15 == 0) {
shootBullet();
}
};
player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;