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 % Math.max(30, 120 - level * 5) == 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 + level * 0.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 = 3;
// Initialize level
var level = 1;
// 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 level display
var levelTxt = new Text2('Level: 1', {
size: 100,
fill: "#ffffff"
});
levelTxt.anchor.set(0, 0); // Set anchor to the left edge
LK.gui.topLeft.addChild(levelTxt); // Position level display at the top-left corner
// Initialize lives display
var livesTxt = new Text2('Lives: 4', {
size: 100,
fill: "#ffffff"
});
livesTxt.anchor.set(1, 0); // Set anchor to the right edge
LK.gui.topRight.addChild(livesTxt); // Position lives display at the top-right corner
// 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 <= 3) {
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);
playerLives--;
if (playerLives <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
livesTxt.setText('Lives: ' + playerLives);
LK.showGameOver();
} else {
livesTxt.setText('Lives: ' + playerLives);
}
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);
// Increase level and difficulty
if (score % 100 === 0) {
level++;
levelTxt.setText('Level: ' + level);
// Increase bot speed
for (var k = 0; k < bots.length; k++) {
bots[k].speed += level * 0.5;
}
}
break;
}
}
}
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
}
// Autofire bullets
if (LK.ticks % 90 == 0) {
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 4 seconds
LK.setTimeout(function () {
if (bullets.includes(bullet)) {
bullet.destroy();
bullets.splice(bullets.indexOf(bullet), 1);
}
}, 4000);
}
// 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);
// Increase level and difficulty
if (score % 100 === 0) {
level++;
levelTxt.setText('Level: ' + level);
// Increase bot speed
for (var i = 0; i < bots.length; i++) {
bots[i].speed += level * 0.5;
}
// Increase bot spawn rate
if (LK.ticks % Math.max(60, 240 - level * 10) == 0) {
var bot = new Bot();
bot.x = Math.random() * 2048;
bot.y = 0;
bots.push(bot);
game.addChild(bot);
}
}
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
@@ -83,14 +83,8 @@
player.x = 2048 / 2;
player.y = 2732 - 100;
// Initialize player lives
var playerLives = 3;
-var livesTxt = new Text2('Lives: ' + playerLives, {
- size: 100,
- fill: "#ffffff"
-});
-livesTxt.anchor.set(1, 0); // Set anchor to the right edge
-LK.gui.topRight.addChild(livesTxt); // Position lives display at the top-right corner
// Initialize level
var level = 1;
// Initialize score display
var score = 0;
@@ -128,9 +122,8 @@
if (bots[i].intersects(player)) {
playerLives--;
if (playerLives <= 3) {
LK.effects.flashScreen(0xff0000, 1000);
- livesTxt.setText('Lives: ' + playerLives);
LK.showGameOver();
} else {
livesTxt.setText('Lives: ' + playerLives);
}