User prompt
Show lives
User prompt
Don't show lives
User prompt
Don't show lives
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'setText')' in or related to this line: 'livesTxt.setText('Lives: ' + playerLives);' Line Number: 94
User prompt
Fix lives
User prompt
Fix
Code edit (1 edits merged)
Please save this source code
User prompt
Make increasing level difficult
User prompt
Show level at left
User prompt
Make different levels
User prompt
Make bullets despawn after 4 seconds
User prompt
lives at right
User prompt
Give player 4 lives
User prompt
Remove lag from game
User prompt
Destroy ammos after 8 seconds
User prompt
Make bots disappear after a line
User prompt
My game is lagging
User prompt
Decrease player firing rate
User prompt
Decrease firing rate
User prompt
Decrease frequency of bots
User prompt
Fix the game
User prompt
Gives Player 3 lives
/**** * Classes ****/ // Bot class var Bot = Container.expand(function () { var self = Container.call(this); var botGraphics = self.attachAsset('bot', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { self.y += self.speed; // Check if the bot has crossed the line and remove it if (self.y > 2732) { self.destroy(); var index = bots.indexOf(self); if (index > -1) { bots.splice(index, 1); } } // Bot firing logic if (LK.ticks % 120 == 0) { // Adjust the firing rate by changing the modulus value var botBullet = new BotBullet(); botBullet.x = self.x; botBullet.y = self.y; game.addChild(botBullet); botBullets.push(botBullet); } }; }); // BotBullet class var BotBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; }; }); // 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 = 10; self.update = function () { self.y -= self.speed; }; }); // The assets will be automatically created and loaded by the LK engine // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { // Player movement logic goes here }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 100; // Initialize player lives var playerLives = 4; // Initialize score display var score = 0; var scoreTxt = new Text2('Score: 0', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Initialize lives display var livesTxt = new Text2('Lives: 4', { size: 100, fill: "#ffffff" }); livesTxt.anchor.set(0.5, 0); LK.gui.top.addChild(livesTxt); // Initialize bots and bullets var bots = []; var bullets = []; var botBullets = []; // Game update function game.update = function () { // Update player player.update(); // Update all game objects and handle collisions for (var i = bots.length - 1; i >= 0; i--) { bots[i].update(); if (bots[i].intersects(player)) { playerLives--; if (playerLives <= 0) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } else { livesTxt.setText('Lives: ' + playerLives); } } // Update bot bullets within the same loop for (var j = botBullets.length - 1; j >= 0; j--) { botBullets[j].update(); if (botBullets[j].intersects(player)) { botBullets[j].destroy(); botBullets.splice(j, 1); for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); for (var j = bots.length - 1; j >= 0; j--) { if (bullets[i].intersects(bots[j])) { bullets[i].destroy(); bullets.splice(i, 1); bots[j].destroy(); bots.splice(j, 1); score += 10; scoreTxt.setText('Score: ' + score); break; } } } LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } } // Autofire bullets if (LK.ticks % 90 == 0) { // Reduced firing frequency var bullet = new Bullet(); bullet.x = player.x; bullet.y = player.y - player.height / 2; bullets.push(bullet); game.addChild(bullet); // Set a timeout to destroy the bullet after 8 seconds LK.setTimeout(function () { if (bullets.includes(bullet)) { bullet.destroy(); bullets.splice(bullets.indexOf(bullet), 1); } }, 8000); } // Collision detection for (var i = bots.length - 1; i >= 0; i--) { for (var j = bullets.length - 1; j >= 0; j--) { if (bots[i].intersects(bullets[j])) { bullets[j].destroy(); bullets.splice(j, 1); bots[i].destroy(); bots.splice(i, 1); // Update score score += 10; scoreTxt.setText('Score: ' + score); break; } } } // Spawn bots if (LK.ticks % 240 == 0) { // Reduced spawning frequency var bot = new Bot(); bot.x = Math.random() * 2048; bot.y = 0; bots.push(bot); game.addChild(bot); } }; // Player control game.down = function (x, y, obj) { player.x = x; player.y = y; }; game.move = function (x, y, obj) { player.x = x; player.y = y; }; game.up = function (x, y, obj) { var bullet = new Bullet(); bullet.x = player.x; bullet.y = player.y; bullets.push(bullet); game.addChild(bullet); };
===================================================================
--- original.js
+++ change.js
@@ -81,16 +81,25 @@
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 100;
+// Initialize player lives
+var playerLives = 4;
// Initialize score display
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
+// Initialize lives display
+var livesTxt = new Text2('Lives: 4', {
+ size: 100,
+ fill: "#ffffff"
+});
+livesTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(livesTxt);
// Initialize bots and bullets
var bots = [];
var bullets = [];
var botBullets = [];
@@ -101,10 +110,15 @@
// Update all game objects and handle collisions
for (var i = bots.length - 1; i >= 0; i--) {
bots[i].update();
if (bots[i].intersects(player)) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
+ playerLives--;
+ if (playerLives <= 0) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ } else {
+ livesTxt.setText('Lives: ' + playerLives);
+ }
}
// Update bot bullets within the same loop
for (var j = botBullets.length - 1; j >= 0; j--) {
botBullets[j].update();