/**** * Classes ****/ // AI Tool class var AITool = Container.expand(function () { var self = Container.call(this); var toolGraphics = self.attachAsset('aiTool', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // AI Tool update logic }; self.activate = function () { // AI Tool activation logic }; }); // 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.shoot = function () { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - heroGraphics.height / 2; game.addChild(bullet); heroBullets.push(bullet); }; }); // HeroBullet class 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; if (self.y < 0) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var hero; var enemies = []; var heroBullets = []; var score = 0; var scoreTxt; // Initialize hero hero = new Hero(); hero.x = 2048 / 2; hero.y = 2732 - 200; game.addChild(hero); // Initialize AI tool var aiTool = new AITool(); aiTool.x = 2048 / 2; aiTool.y = 2732 - 400; game.addChild(aiTool); // Initialize score text scoreTxt = new Text2('Score: 0', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Handle game move event game.move = function (x, y, obj) { hero.x = x; hero.y = y; }; // Handle game down event game.down = function (x, y, obj) { hero.shoot(); }; // Update game logic game.update = function () { // Update hero hero.update(); // Update AI tool aiTool.update(); // Update hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { heroBullets[i].update(); if (heroBullets[i].destroyed) { heroBullets.splice(i, 1); } } // Update enemies for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].update(); if (enemies[j].destroyed) { enemies.splice(j, 1); } } // Spawn enemies if (LK.ticks % 60 == 0) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = -100; game.addChild(enemy); enemies.push(enemy); } // Check collisions 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); score += 10; scoreTxt.setText('Score: ' + score); break; } } } };
/****
* Classes
****/
// AI Tool class
var AITool = Container.expand(function () {
var self = Container.call(this);
var toolGraphics = self.attachAsset('aiTool', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// AI Tool update logic
};
self.activate = function () {
// AI Tool activation logic
};
});
// 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.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
// HeroBullet class
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;
if (self.y < 0) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero;
var enemies = [];
var heroBullets = [];
var score = 0;
var scoreTxt;
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Initialize AI tool
var aiTool = new AITool();
aiTool.x = 2048 / 2;
aiTool.y = 2732 - 400;
game.addChild(aiTool);
// Initialize score text
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle game move event
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
// Handle game down event
game.down = function (x, y, obj) {
hero.shoot();
};
// Update game logic
game.update = function () {
// Update hero
hero.update();
// Update AI tool
aiTool.update();
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
if (heroBullets[i].destroyed) {
heroBullets.splice(i, 1);
}
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (enemies[j].destroyed) {
enemies.splice(j, 1);
}
}
// Spawn enemies
if (LK.ticks % 60 == 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -100;
game.addChild(enemy);
enemies.push(enemy);
}
// Check collisions
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);
score += 10;
scoreTxt.setText('Score: ' + score);
break;
}
}
}
};