/**** * Classes ****/ // 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 = 2; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); enemies.splice(enemies.indexOf(self), 1); } }; }); // HeroBullet class var HeroBullet = Container.expand(function (x, y) { var self = Container.call(this); var bulletGraphics = self.attachAsset('heroBullet', { anchorX: 0.5, anchorY: 0.5 }); self.x = x; self.y = y; self.speed = -10; self.update = function () { self.y += self.speed; if (self.y < 0) { self.destroy(); heroBullets.splice(heroBullets.indexOf(self), 1); } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Warrior class var Warrior = Container.expand(function () { var self = Container.call(this); var warriorGraphics = self.attachAsset('warrior', { anchorX: 0.5, anchorY: 0.5 }); self.strength = 1; // Initial strength self.shoot = function () { var bullet = new HeroBullet(self.x, self.y); game.addChild(bullet); heroBullets.push(bullet); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize warrior var warrior = new Warrior(); warrior.x = 2048 / 2; warrior.y = 2732 / 2; game.addChild(warrior); // Arrays to keep track of bullets and enemies var heroBullets = []; var enemies = []; // Game update loop game.update = function () { // Update all hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { heroBullets[i].update(); } // Update all enemies for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].update(); } // Check for collisions between hero bullets and enemies for (var k = heroBullets.length - 1; k >= 0; k--) { for (var l = enemies.length - 1; l >= 0; l--) { if (heroBullets[k].intersects(enemies[l])) { heroBullets[k].destroy(); enemies[l].destroy(); heroBullets.splice(k, 1); enemies.splice(l, 1); warrior.strength += 1; // Increase warrior's strength break; } } } // Spawn new enemies if (LK.ticks % 60 == 0) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = 0; game.addChild(enemy); enemies.push(enemy); } }; // Handle shooting game.down = function (x, y, obj) { warrior.shoot(); };
/****
* Classes
****/
// 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 = 2;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
enemies.splice(enemies.indexOf(self), 1);
}
};
});
// HeroBullet class
var HeroBullet = Container.expand(function (x, y) {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = x;
self.y = y;
self.speed = -10;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
heroBullets.splice(heroBullets.indexOf(self), 1);
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Warrior class
var Warrior = Container.expand(function () {
var self = Container.call(this);
var warriorGraphics = self.attachAsset('warrior', {
anchorX: 0.5,
anchorY: 0.5
});
self.strength = 1; // Initial strength
self.shoot = function () {
var bullet = new HeroBullet(self.x, self.y);
game.addChild(bullet);
heroBullets.push(bullet);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize warrior
var warrior = new Warrior();
warrior.x = 2048 / 2;
warrior.y = 2732 / 2;
game.addChild(warrior);
// Arrays to keep track of bullets and enemies
var heroBullets = [];
var enemies = [];
// Game update loop
game.update = function () {
// Update all hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
}
// Update all enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
}
// Check for collisions between hero bullets and enemies
for (var k = heroBullets.length - 1; k >= 0; k--) {
for (var l = enemies.length - 1; l >= 0; l--) {
if (heroBullets[k].intersects(enemies[l])) {
heroBullets[k].destroy();
enemies[l].destroy();
heroBullets.splice(k, 1);
enemies.splice(l, 1);
warrior.strength += 1; // Increase warrior's strength
break;
}
}
}
// Spawn new enemies
if (LK.ticks % 60 == 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = 0;
game.addChild(enemy);
enemies.push(enemy);
}
};
// Handle shooting
game.down = function (x, y, obj) {
warrior.shoot();
};