/****
* Classes
****/
// Enemy class representing the creatures
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.y = 0;
self.x = Math.random() * 2048;
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class representing the player character
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 () {
// Update logic for hero
};
});
// Bullet class for hero's weapon
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;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize hero
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 150;
// Initialize enemies
var enemies = [];
for (var i = 0; i < 5; i++) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = Math.random() * 1000;
enemies.push(enemy);
game.addChild(enemy);
}
// Initialize bullets
var heroBullets = [];
// Handle game updates
game.update = function () {
// Update hero
hero.update();
// Update enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].update();
if (hero.intersects(enemies[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update bullets
for (var j = heroBullets.length - 1; j >= 0; j--) {
heroBullets[j].update();
for (var k = enemies.length - 1; k >= 0; k--) {
if (heroBullets[j].intersects(enemies[k])) {
enemies[k].destroy();
enemies.splice(k, 1);
heroBullets[j].destroy();
heroBullets.splice(j, 1);
break;
}
}
if (heroBullets[j] && heroBullets[j].y < -50) {
heroBullets[j].destroy();
heroBullets.splice(j, 1);
}
}
// Fire bullets
if (LK.ticks % 20 == 0) {
var newBullet = new HeroBullet();
newBullet.x = hero.x;
newBullet.y = hero.y;
heroBullets.push(newBullet);
game.addChild(newBullet);
}
};
// Handle touch events for hero movement
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.up = function (x, y, obj) {
// Stop hero movement
};