User prompt
Speed of terrorist changes to 5
User prompt
Fix the big of hero not moving left and right
User prompt
If a terrorist got out of the screen, the game ends
User prompt
Change the bullet cooldown to 1 second
User prompt
Shooting bullet has 2 second cooldown
User prompt
Show score in top middle of the screen
User prompt
Every kill of hero gets 1 score
User prompt
Hero moves left and right
Initial prompt
Terrorist Killer
/**** * 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 = 2; 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); } } // 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 game.down = function (x, y, obj) { hero.shoot(); hero.x = x; }; // Handle touch events for moving game.move = function (x, y, obj) { 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);
===================================================================
--- original.js
+++ change.js
@@ -99,8 +99,9 @@
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;
}
}
}
@@ -113,6 +114,17 @@
// Handle touch events for moving
game.move = function (x, y, obj) {
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);
\ No newline at end of file