/**** * 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> // 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.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 ****/ // Initialize variables var hero; var enemies = []; var bullets = []; var score = 0; var scoreTxt; // Initialize hero hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 200; // Initialize score text scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Handle game move event game.move = function (x, y, obj) { hero.move(x, y); }; // Handle game update event game.update = function () { // Update hero hero.update(); // Update enemies for (var i = enemies.length - 1; i >= 0; i--) { enemies[i].update(); if (enemies[i].intersects(hero)) { 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); score++; scoreTxt.setText(score); break; } } } // Spawn enemies if (LK.ticks % 60 == 0) { var newEnemy = new Enemy(); newEnemy.x = Math.random() * 2048; newEnemy.y = -50; enemies.push(newEnemy); game.addChild(newEnemy); } // Fire bullets if (LK.ticks % 15 == 0) { var newBullet = new Bullet(); newBullet.x = hero.x; newBullet.y = hero.y; bullets.push(newBullet); game.addChild(newBullet); } };
/****
* 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>
// 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.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
****/
// Initialize variables
var hero;
var enemies = [];
var bullets = [];
var score = 0;
var scoreTxt;
// Initialize hero
hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Initialize score text
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle game move event
game.move = function (x, y, obj) {
hero.move(x, y);
};
// Handle game update event
game.update = function () {
// Update hero
hero.update();
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].intersects(hero)) {
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);
score++;
scoreTxt.setText(score);
break;
}
}
}
// Spawn enemies
if (LK.ticks % 60 == 0) {
var newEnemy = new Enemy();
newEnemy.x = Math.random() * 2048;
newEnemy.y = -50;
enemies.push(newEnemy);
game.addChild(newEnemy);
}
// Fire bullets
if (LK.ticks % 15 == 0) {
var newBullet = new Bullet();
newBullet.x = hero.x;
newBullet.y = hero.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
};