User prompt
Analyse the code to match with the game!
User prompt
if level 2 start change the background to lava_planet background and start respawn the lavarocks, lavarocks1 randomly
User prompt
make the objects of level 2 going upward not downward!
User prompt
make lavarocks first object respawn when level 2 is start or reached by score
User prompt
make lavarocks1 image object 2 in level 2
User prompt
make the lavarocks image object for level 2 only
User prompt
delete lavarock and lavarock1 from the code
Code edit (1 edits merged)
Please save this source code
User prompt
If level 2 start no trees respawn and start respawning the 2 new objects rocklava and rocklava1
User prompt
Make tree objects respawn in level 1 only
User prompt
Add 2 object to the game lavarock and lavarock1 for level 2 only
User prompt
Make the lava_planet behind all objects
User prompt
Rename background2 to lava_planet
User prompt
make the background in level 2 inside the screen and remove shaking
User prompt
if score reach 200 go to level 2
User prompt
fix the bug of the background2 in level 2 not it's outside not inside the screen
User prompt
Delete the shaking from background2 in level 2 and make it fit to the screen not out the screen
User prompt
ada more tree object
User prompt
fit the background2 to screen
User prompt
fix bug when reach score 200 or above start lagging delete any lagge after that amount
User prompt
change diraction to upward
User prompt
fix the movment of tree object
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'now')' in or related to this line: 'var lastFrameTime = performance.now();' Line Number: 151
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'now')' in or related to this line: 'var lastFrameTime = performance.now();' Line Number: 151
User prompt
detect lagge when i play and fix it auto
/**** * Classes ****/ //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Dragon class to represent the player character var Dragon = Container.expand(function () { var self = Container.call(this); var dragonGraphics = self.attachAsset('dragon', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5, smooth: true, filterQuality: 2 }); self.speed = 15; // Increase the speed of the dragon self.update = function () { // Update logic for the dragon to flip based on movement direction if (self.x < 2048 / 2) { dragonGraphics.scaleX = -1; // Flip horizontally when on the left side of the screen } else { dragonGraphics.scaleX = 1; // Normal orientation when on the right side of the screen } // Removed unnecessary variable to optimize performance }; }); // Fireball class to represent the fireballs that the dragon shoots var Fireball = Container.expand(function () { var self = Container.call(this); var fireballGraphics = self.attachAsset('Shout', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.0, scaleY: 1.0, smooth: true, filterQuality: 1 }); self.speed = 30; // Further increase the speed of the fireball self.update = function () { // Move the fireball straight down self.y += self.speed; }; }); // HealthBar class to represent the player's health var HealthBar = Container.expand(function () { var self = Container.call(this); var healthBarGraphics = self.attachAsset('healthBar', { anchorX: 0.0, anchorY: 0.0, scaleX: 2530 / 1000, // Scale to fit the game width scaleY: 0.1 }); self.maxHealth = 1000; self.currentHealth = self.maxHealth; self.update = function () { healthBarGraphics.scaleX = self.currentHealth / self.maxHealth; healthBarGraphics.scaleY = 0.05; // Resize the health bar to fit the top of the screen }; }); // Tree class to represent the first object var Tree = Container.expand(function () { var self = Container.call(this); var treeGraphics = self.attachAsset('Tree01', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.8, scaleY: 0.8, smooth: true, filterQuality: 1 }); self.speed = 10; // Further increase the speed of the tree self.update = function () { // Move the tree straight up self.y -= self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000, //Init game with black background height: 3000 // Increase the game screen height }); /**** * Game Code ****/ // Initialize game variables var dragon; var spheres = []; var fireballs = []; // Initialize fireballs array var score = 0; // Initialize score to 0 var scoreTxt; var level = 1; // Initialize level to 1 // Add background image to the game for level 1 if (level === 1) { var background = LK.getAsset('Background', { anchorX: 0.0, anchorY: 0.0 }); background = game.addChild(background); background.width = game.width; background.height = game.height; background.smooth = true; // Enable smoothing to remove pixelation background.filterQuality = 5; // Reduce the filter quality to optimize performance background.scale.set(game.width / background.width, game.height / background.height); // Scale the background to fit the screen // Refresh the background to be clearer & fit to screen every 60 ticks game.update = function () { background.x = 2048 / 2 + Math.sin(LK.ticks * 0.2) * 20; // Increase shake intensity left and right around the center background.y = 2732 / 2 + Math.sin(LK.ticks * 0.2) * 20; // Increase shake intensity up and down around the center if (background.y >= 2732) { background.y = -2732; } }; } // Add background2 image to the game for level 2 if (level === 2) { var background2 = LK.getAsset('Background2', { anchorX: 0.0, anchorY: 0.0 }); background2 = game.addChild(background2); background2.width = game.width; background2.height = game.height; background2.smooth = true; // Enable smoothing to remove pixelation background2.filterQuality = 5; // Reduce the filter quality to optimize performance background2.scale.set(game.width / background2.width, game.height / background2.height); // Scale the background to fit the screen // Refresh the background2 to be clearer & fit to screen every 60 ticks game.update = function () { background2.y += 2; if (background2.y >= 2732) { background2.y = -2732; } }; } // Function to handle game updates var lastFrameTime = performance.now(); var frameTimes = []; var maxFrameTime = 16.67; // Target frame time for 60 FPS game.update = function () { var currentFrameTime = performance.now(); var deltaTime = currentFrameTime - lastFrameTime; lastFrameTime = currentFrameTime; frameTimes.push(deltaTime); if (frameTimes.length > 60) { frameTimes.shift(); } var averageFrameTime = frameTimes.reduce(function (a, b) { return a + b; }, 0) / frameTimes.length; if (averageFrameTime > maxFrameTime) { // If the average frame time is greater than the target, reduce game speed dragon.speed *= 0.95; fireballs.forEach(function (fireball) { return fireball.speed *= 0.95; }); spheres.forEach(function (tree) { return tree.speed *= 0.95; }); } else { // If the average frame time is less than the target, increase game speed dragon.speed *= 1.05; fireballs.forEach(function (fireball) { return fireball.speed *= 1.05; }); spheres.forEach(function (tree) { return tree.speed *= 1.05; }); } // Update dragon position based on touch input if (!LK.gameOver && dragNode && dragNode.global) { dragNode.x = game.toLocal(dragNode.global).x; } // Reuse off-screen trees or spawn new ones if necessary if (!LK.gameOver && LK.ticks % 100 == 0) { // Reduce the interval for spawning trees var newTree; for (var i = 0; i < spheres.length; i++) { if (spheres[i].y < -200) { // Check if the tree is off-screen newTree = spheres[i]; break; } } if (!newTree) { newTree = new Tree(); spheres.push(newTree); game.addChild(newTree); } newTree.x = Math.random() * (2048 - newTree.width) + newTree.width / 2; newTree.y = 2732; } // Check for collisions between fireballs and rocks for (var i = fireballs.length - 1; i >= 0; i--) { for (var j = spheres.length - 1; j >= 0; j--) { if (fireballs[i].intersects(spheres[j])) { // Add random points from 0-40 when the dragon shoots the trees var points = Math.floor(Math.random() * 41); score += points; // Remove the fireball and tree from the game fireballs[i].destroy(); fireballs.splice(i, 1); spheres[j].destroy(); spheres.splice(j, 1); break; } } } // Check for collisions between dragon and rocks for (var j = spheres.length - 1; j >= 0; j--) { if (dragon.intersects(spheres[j])) { // Decrease the dragon's health healthBar.currentHealth -= 20; // Decrease the damage taken by the dragon from rock01 // Remove the rock from the game spheres[j].destroy(); spheres.splice(j, 1); break; } // Remove off-screen trees if (spheres[j].y < -200) { spheres[j].destroy(); spheres.splice(j, 1); } } // Update the score display when the dragon gets points scoreTxt.setText('Score: ' + score); // Regenerate player's health to max when score reaches 1000 if (score >= 1000) { healthBar.currentHealth = healthBar.maxHealth; } // Transition to level 2 when score reaches 1234 if (score >= 1234 && level === 1) { level = 2; levelTxt.setText('Level: 2'); // Remove current background game.removeChild(background); // Add background2 for level 2 var background2 = LK.getAsset('Background2', { anchorX: 0.0, anchorY: 0.0 }); background2 = game.addChild(background2); background2.width = game.width; background2.height = game.height; background2.smooth = true; background2.filterQuality = 5; background2.scale.set(game.width / background2.width, game.height / background2.height); // Refresh the background2 to be clearer & fit to screen every 60 ticks game.update = function () { background2.x = 2048 / 2 + Math.sin(LK.ticks * 0.2) * 20; // Increase shake intensity left and right around the center background2.y = 2732 / 2 + Math.sin(LK.ticks * 0.2) * 20; // Increase shake intensity up and down around the center if (background2.y >= 2732) { background2.y = -2732; } }; } }; // Initialize dragon dragon = game.addChild(new Dragon()); dragon.x = 2048 / 2; dragon.y = 200; // Initialize level display var levelTxt = new Text2('Level: 1', { size: 100, fill: 0x800080, // Purple color font: "'Time new roman',Impact,'Arial Black',Tahoma" }); levelTxt.anchor.set(1, 1); LK.gui.bottomRight.addChild(levelTxt); levelTxt.x = -50; // Move the level text to the right side of the screen levelTxt.y = -50; // Position the level text at the bottom of the screen // Initialize score display scoreTxt = new Text2('Score: 0', { size: 100, fill: 0x800080, // Purple color font: "'Time new roman',Impact,'Arial Black',Tahoma" }); LK.gui.top.addChild(scoreTxt); scoreTxt.anchor.set(0, 0); // Sets anchor to the left of the top edge of the text. scoreTxt.x = 50; // Position the score text a little bit to the right scoreTxt.y = levelTxt.y - scoreTxt.height - 100; // Position the score text 100 pixels above the level text // Initialize health bar var healthBar = game.addChild(new HealthBar()); healthBar.x = 2000 / 2 - healthBar.width / 2 + 100; // Move health bar to the right a little bit healthBar.y = 2710 - healthBar.height / 2; // Move health bar to the middle of the bottom side scoreTxt.anchor.set(0, 1); LK.gui.bottomLeft.addChild(scoreTxt); scoreTxt.x = 50; // Position the score text a little bit to the right scoreTxt.y = -50; // Position the score text above the health bar // Handle touch input for dragging the dragon var dragNode = null; game.down = function (x, y, obj) { dragNode = dragon; }; game.up = function (x, y, obj) { dragNode = null; }; game.move = function (x, y, obj) { if (dragNode) { // Limit the dragon's movement to the game area var newX = Math.max(dragon.width / 2, Math.min(2048 - dragon.width / 2, x)); var newY = Math.max(dragon.height / 2, Math.min(2732 - dragon.height / 2, y)); dragNode.x = newX; dragNode.y = newY; // Shoot a fireball at regular intervals if (LK.ticks % 10 == 0) { // Reduce the interval for shooting fireballs for (var i = 0; i < 1; i++) { var fireball = new Fireball(); fireball.x = dragon.x; fireball.y = dragon.y; fireballs.push(fireball); game.addChild(fireball); } } } };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Dragon class to represent the player character
var Dragon = Container.expand(function () {
var self = Container.call(this);
var dragonGraphics = self.attachAsset('dragon', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5,
smooth: true,
filterQuality: 2
});
self.speed = 15; // Increase the speed of the dragon
self.update = function () {
// Update logic for the dragon to flip based on movement direction
if (self.x < 2048 / 2) {
dragonGraphics.scaleX = -1; // Flip horizontally when on the left side of the screen
} else {
dragonGraphics.scaleX = 1; // Normal orientation when on the right side of the screen
}
// Removed unnecessary variable to optimize performance
};
});
// Fireball class to represent the fireballs that the dragon shoots
var Fireball = Container.expand(function () {
var self = Container.call(this);
var fireballGraphics = self.attachAsset('Shout', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.0,
scaleY: 1.0,
smooth: true,
filterQuality: 1
});
self.speed = 30; // Further increase the speed of the fireball
self.update = function () {
// Move the fireball straight down
self.y += self.speed;
};
});
// HealthBar class to represent the player's health
var HealthBar = Container.expand(function () {
var self = Container.call(this);
var healthBarGraphics = self.attachAsset('healthBar', {
anchorX: 0.0,
anchorY: 0.0,
scaleX: 2530 / 1000,
// Scale to fit the game width
scaleY: 0.1
});
self.maxHealth = 1000;
self.currentHealth = self.maxHealth;
self.update = function () {
healthBarGraphics.scaleX = self.currentHealth / self.maxHealth;
healthBarGraphics.scaleY = 0.05; // Resize the health bar to fit the top of the screen
};
});
// Tree class to represent the first object
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('Tree01', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.8,
scaleY: 0.8,
smooth: true,
filterQuality: 1
});
self.speed = 10; // Further increase the speed of the tree
self.update = function () {
// Move the tree straight up
self.y -= self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000,
//Init game with black background
height: 3000 // Increase the game screen height
});
/****
* Game Code
****/
// Initialize game variables
var dragon;
var spheres = [];
var fireballs = []; // Initialize fireballs array
var score = 0; // Initialize score to 0
var scoreTxt;
var level = 1; // Initialize level to 1
// Add background image to the game for level 1
if (level === 1) {
var background = LK.getAsset('Background', {
anchorX: 0.0,
anchorY: 0.0
});
background = game.addChild(background);
background.width = game.width;
background.height = game.height;
background.smooth = true; // Enable smoothing to remove pixelation
background.filterQuality = 5; // Reduce the filter quality to optimize performance
background.scale.set(game.width / background.width, game.height / background.height); // Scale the background to fit the screen
// Refresh the background to be clearer & fit to screen every 60 ticks
game.update = function () {
background.x = 2048 / 2 + Math.sin(LK.ticks * 0.2) * 20; // Increase shake intensity left and right around the center
background.y = 2732 / 2 + Math.sin(LK.ticks * 0.2) * 20; // Increase shake intensity up and down around the center
if (background.y >= 2732) {
background.y = -2732;
}
};
}
// Add background2 image to the game for level 2
if (level === 2) {
var background2 = LK.getAsset('Background2', {
anchorX: 0.0,
anchorY: 0.0
});
background2 = game.addChild(background2);
background2.width = game.width;
background2.height = game.height;
background2.smooth = true; // Enable smoothing to remove pixelation
background2.filterQuality = 5; // Reduce the filter quality to optimize performance
background2.scale.set(game.width / background2.width, game.height / background2.height); // Scale the background to fit the screen
// Refresh the background2 to be clearer & fit to screen every 60 ticks
game.update = function () {
background2.y += 2;
if (background2.y >= 2732) {
background2.y = -2732;
}
};
}
// Function to handle game updates
var lastFrameTime = performance.now();
var frameTimes = [];
var maxFrameTime = 16.67; // Target frame time for 60 FPS
game.update = function () {
var currentFrameTime = performance.now();
var deltaTime = currentFrameTime - lastFrameTime;
lastFrameTime = currentFrameTime;
frameTimes.push(deltaTime);
if (frameTimes.length > 60) {
frameTimes.shift();
}
var averageFrameTime = frameTimes.reduce(function (a, b) {
return a + b;
}, 0) / frameTimes.length;
if (averageFrameTime > maxFrameTime) {
// If the average frame time is greater than the target, reduce game speed
dragon.speed *= 0.95;
fireballs.forEach(function (fireball) {
return fireball.speed *= 0.95;
});
spheres.forEach(function (tree) {
return tree.speed *= 0.95;
});
} else {
// If the average frame time is less than the target, increase game speed
dragon.speed *= 1.05;
fireballs.forEach(function (fireball) {
return fireball.speed *= 1.05;
});
spheres.forEach(function (tree) {
return tree.speed *= 1.05;
});
}
// Update dragon position based on touch input
if (!LK.gameOver && dragNode && dragNode.global) {
dragNode.x = game.toLocal(dragNode.global).x;
}
// Reuse off-screen trees or spawn new ones if necessary
if (!LK.gameOver && LK.ticks % 100 == 0) {
// Reduce the interval for spawning trees
var newTree;
for (var i = 0; i < spheres.length; i++) {
if (spheres[i].y < -200) {
// Check if the tree is off-screen
newTree = spheres[i];
break;
}
}
if (!newTree) {
newTree = new Tree();
spheres.push(newTree);
game.addChild(newTree);
}
newTree.x = Math.random() * (2048 - newTree.width) + newTree.width / 2;
newTree.y = 2732;
}
// Check for collisions between fireballs and rocks
for (var i = fireballs.length - 1; i >= 0; i--) {
for (var j = spheres.length - 1; j >= 0; j--) {
if (fireballs[i].intersects(spheres[j])) {
// Add random points from 0-40 when the dragon shoots the trees
var points = Math.floor(Math.random() * 41);
score += points;
// Remove the fireball and tree from the game
fireballs[i].destroy();
fireballs.splice(i, 1);
spheres[j].destroy();
spheres.splice(j, 1);
break;
}
}
}
// Check for collisions between dragon and rocks
for (var j = spheres.length - 1; j >= 0; j--) {
if (dragon.intersects(spheres[j])) {
// Decrease the dragon's health
healthBar.currentHealth -= 20; // Decrease the damage taken by the dragon from rock01
// Remove the rock from the game
spheres[j].destroy();
spheres.splice(j, 1);
break;
}
// Remove off-screen trees
if (spheres[j].y < -200) {
spheres[j].destroy();
spheres.splice(j, 1);
}
}
// Update the score display when the dragon gets points
scoreTxt.setText('Score: ' + score);
// Regenerate player's health to max when score reaches 1000
if (score >= 1000) {
healthBar.currentHealth = healthBar.maxHealth;
}
// Transition to level 2 when score reaches 1234
if (score >= 1234 && level === 1) {
level = 2;
levelTxt.setText('Level: 2');
// Remove current background
game.removeChild(background);
// Add background2 for level 2
var background2 = LK.getAsset('Background2', {
anchorX: 0.0,
anchorY: 0.0
});
background2 = game.addChild(background2);
background2.width = game.width;
background2.height = game.height;
background2.smooth = true;
background2.filterQuality = 5;
background2.scale.set(game.width / background2.width, game.height / background2.height);
// Refresh the background2 to be clearer & fit to screen every 60 ticks
game.update = function () {
background2.x = 2048 / 2 + Math.sin(LK.ticks * 0.2) * 20; // Increase shake intensity left and right around the center
background2.y = 2732 / 2 + Math.sin(LK.ticks * 0.2) * 20; // Increase shake intensity up and down around the center
if (background2.y >= 2732) {
background2.y = -2732;
}
};
}
};
// Initialize dragon
dragon = game.addChild(new Dragon());
dragon.x = 2048 / 2;
dragon.y = 200;
// Initialize level display
var levelTxt = new Text2('Level: 1', {
size: 100,
fill: 0x800080,
// Purple color
font: "'Time new roman',Impact,'Arial Black',Tahoma"
});
levelTxt.anchor.set(1, 1);
LK.gui.bottomRight.addChild(levelTxt);
levelTxt.x = -50; // Move the level text to the right side of the screen
levelTxt.y = -50; // Position the level text at the bottom of the screen
// Initialize score display
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0x800080,
// Purple color
font: "'Time new roman',Impact,'Arial Black',Tahoma"
});
LK.gui.top.addChild(scoreTxt);
scoreTxt.anchor.set(0, 0); // Sets anchor to the left of the top edge of the text.
scoreTxt.x = 50; // Position the score text a little bit to the right
scoreTxt.y = levelTxt.y - scoreTxt.height - 100; // Position the score text 100 pixels above the level text
// Initialize health bar
var healthBar = game.addChild(new HealthBar());
healthBar.x = 2000 / 2 - healthBar.width / 2 + 100; // Move health bar to the right a little bit
healthBar.y = 2710 - healthBar.height / 2; // Move health bar to the middle of the bottom side
scoreTxt.anchor.set(0, 1);
LK.gui.bottomLeft.addChild(scoreTxt);
scoreTxt.x = 50; // Position the score text a little bit to the right
scoreTxt.y = -50; // Position the score text above the health bar
// Handle touch input for dragging the dragon
var dragNode = null;
game.down = function (x, y, obj) {
dragNode = dragon;
};
game.up = function (x, y, obj) {
dragNode = null;
};
game.move = function (x, y, obj) {
if (dragNode) {
// Limit the dragon's movement to the game area
var newX = Math.max(dragon.width / 2, Math.min(2048 - dragon.width / 2, x));
var newY = Math.max(dragon.height / 2, Math.min(2732 - dragon.height / 2, y));
dragNode.x = newX;
dragNode.y = newY;
// Shoot a fireball at regular intervals
if (LK.ticks % 10 == 0) {
// Reduce the interval for shooting fireballs
for (var i = 0; i < 1; i++) {
var fireball = new Fireball();
fireball.x = dragon.x;
fireball.y = dragon.y;
fireballs.push(fireball);
game.addChild(fireball);
}
}
}
};