/**** * Classes ****/ // Define a Hero class for the player's character var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.shoot = function () { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y; game.addChild(bullet); heroBullets.push(bullet); }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Define a Bullet class for the hero's bullets var HeroBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('heroBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.update = function () { self.y += self.speed; }; }); // Define a Terrorist class for enemies var Terrorist = Container.expand(function () { var self = Container.call(this); var terroristGraphics = self.attachAsset('terrorist', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var heroBullets = []; var terrorists = []; var hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 150; // Function to spawn terrorists function spawnTerrorist() { var terrorist = new Terrorist(); terrorist.x = Math.random() * 2048; terrorist.y = -50; game.addChild(terrorist); terrorists.push(terrorist); } // Handle game updates game.update = function () { // Update hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { heroBullets[i].update(); if (heroBullets[i].y < -50) { heroBullets[i].destroy(); heroBullets.splice(i, 1); } } // Update hero position if (hero.x < 0) { hero.x = 0; } else if (hero.x > 2048) { hero.x = 2048; } // Update terrorists for (var j = terrorists.length - 1; j >= 0; j--) { terrorists[j].update(); if (terrorists[j].y > 2732) { terrorists[j].destroy(); terrorists.splice(j, 1); // End the game if a terrorist gets out of the screen LK.showGameOver(); } } // Check for collisions for (var k = heroBullets.length - 1; k >= 0; k--) { for (var l = terrorists.length - 1; l >= 0; l--) { if (heroBullets[k].intersects(terrorists[l])) { heroBullets[k].destroy(); terrorists[l].destroy(); heroBullets.splice(k, 1); terrorists.splice(l, 1); LK.setScore(LK.getScore() + 1); // Increment score by 1 for each terrorist killed scoreTxt.setText(LK.getScore()); // Update the score text break; } } } }; // Handle touch events for shooting var lastShot = 0; game.down = function (x, y, obj) { if (LK.ticks - lastShot >= 60) { hero.shoot(); lastShot = LK.ticks; } }; // Handle touch events for moving game.move = function (x, y, obj) { if (x > 0 && x < 2048) { hero.x = x; } }; // Create a Text2 object to display the score var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); // Update and display the score in the game scoreTxt.setText(LK.getScore()); // Center the score text horizontally, anchor point set at the middle of its top edge scoreTxt.anchor.set(0.5, 0); // Add the score text to the GUI overlay at the top center of the screen LK.gui.top.addChild(scoreTxt); // Spawn terrorists at intervals LK.setInterval(spawnTerrorist, 2000);
/****
* Classes
****/
// Define a Hero class for the player's character
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define a Bullet class for the hero's bullets
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.y += self.speed;
};
});
// Define a Terrorist class for enemies
var Terrorist = Container.expand(function () {
var self = Container.call(this);
var terroristGraphics = self.attachAsset('terrorist', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var heroBullets = [];
var terrorists = [];
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 150;
// Function to spawn terrorists
function spawnTerrorist() {
var terrorist = new Terrorist();
terrorist.x = Math.random() * 2048;
terrorist.y = -50;
game.addChild(terrorist);
terrorists.push(terrorist);
}
// Handle game updates
game.update = function () {
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
if (heroBullets[i].y < -50) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
// Update hero position
if (hero.x < 0) {
hero.x = 0;
} else if (hero.x > 2048) {
hero.x = 2048;
}
// Update terrorists
for (var j = terrorists.length - 1; j >= 0; j--) {
terrorists[j].update();
if (terrorists[j].y > 2732) {
terrorists[j].destroy();
terrorists.splice(j, 1);
// End the game if a terrorist gets out of the screen
LK.showGameOver();
}
}
// Check for collisions
for (var k = heroBullets.length - 1; k >= 0; k--) {
for (var l = terrorists.length - 1; l >= 0; l--) {
if (heroBullets[k].intersects(terrorists[l])) {
heroBullets[k].destroy();
terrorists[l].destroy();
heroBullets.splice(k, 1);
terrorists.splice(l, 1);
LK.setScore(LK.getScore() + 1); // Increment score by 1 for each terrorist killed
scoreTxt.setText(LK.getScore()); // Update the score text
break;
}
}
}
};
// Handle touch events for shooting
var lastShot = 0;
game.down = function (x, y, obj) {
if (LK.ticks - lastShot >= 60) {
hero.shoot();
lastShot = LK.ticks;
}
};
// Handle touch events for moving
game.move = function (x, y, obj) {
if (x > 0 && x < 2048) {
hero.x = x;
}
};
// Create a Text2 object to display the score
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
// Update and display the score in the game
scoreTxt.setText(LK.getScore());
// Center the score text horizontally, anchor point set at the middle of its top edge
scoreTxt.anchor.set(0.5, 0);
// Add the score text to the GUI overlay at the top center of the screen
LK.gui.top.addChild(scoreTxt);
// Spawn terrorists at intervals
LK.setInterval(spawnTerrorist, 2000);