/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * 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: 2.0, scaleY: 2.0, smooth: true, filterQuality: 2 }); self.speed = 20; // Further 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 (assetId) { var self = Container.call(this); var fireballGraphics = self.attachAsset(assetId, { anchorX: 0.5, anchorY: 0.5, scaleX: 0.6, scaleY: 0.9, smooth: true, filterQuality: 1 }); self.speed = 40; // Increase the speed of the fireballs self.update = function () { 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 }; }); var Icerocks = Container.expand(function () { var self = Container.call(this); var icerocksGraphics = self.attachAsset('Icerocks', { anchorX: 0.5, anchorY: 0.5, scaleX: level === 3 ? 1.5 : 1.0, scaleY: level === 3 ? 1.5 : 1.0, smooth: true, filterQuality: 1 }); self.speed = level === 3 ? 10 : 5; // Increase speed for level 3 self.update = function () { self.y -= self.speed; }; }); var Icerocks1 = Container.expand(function () { var self = Container.call(this); var icerocks1Graphics = self.attachAsset('Icerocks1', { anchorX: 0.5, anchorY: 0.5, scaleX: level === 3 ? 1.5 : 1.0, scaleY: level === 3 ? 1.5 : 1.0, smooth: true, filterQuality: 1 }); self.speed = level === 3 ? 10 : 5; // Increase speed for level 3 self.update = function () { // Move the icerocks upwards self.y -= self.speed; }; }); var Icerocks2 = Container.expand(function () { var self = Container.call(this); var icerocks2Graphics = self.attachAsset('Icerocks2', { anchorX: 0.5, anchorY: 0.5, scaleX: level === 3 ? 1.5 : 1.0, scaleY: level === 3 ? 1.5 : 1.0, smooth: true, filterQuality: 1 }); self.speed = level === 3 ? 10 : 5; // Increase speed for level 3 self.update = function () { // Move the icerocks upwards self.y -= self.speed; }; }); var Lavarocks = Container.expand(function () { var self = Container.call(this); var lavarocksGraphics = self.attachAsset('Lavarocks', { anchorX: 0.5, anchorY: 0.5, scaleX: level === 2 ? 1.5 : 1.0, scaleY: level === 2 ? 1.5 : 1.0, smooth: true, filterQuality: 1 }); self.speed = level === 2 ? 15 : 5; // Further increase speed for level 2 self.update = function () { self.y -= self.speed; }; }); var Lavarocks1 = Container.expand(function () { var self = Container.call(this); var lavarocks1Graphics = self.attachAsset('Lavarocks1', { anchorX: 0.5, anchorY: 0.5, scaleX: level === 2 ? 1.5 : 1.0, scaleY: level === 2 ? 1.5 : 1.0, smooth: true, filterQuality: 1 }); self.speed = level === 2 ? 15 : 5; // Further increase speed for level 2 self.update = function () { // Move the lavarocks1 upwards self.y -= self.speed; }; }); var Lavarocks2 = Container.expand(function () { var self = Container.call(this); var lavarocks2Graphics = self.attachAsset('Lavarocks2', { anchorX: 0.5, anchorY: 0.5, scaleX: level === 2 ? 1.5 : 1.0, scaleY: level === 2 ? 1.5 : 1.0, smooth: true, filterQuality: 1 }); self.speed = level === 2 ? 15 : 5; // Further increase speed for level 2 self.update = function () { // Move the lavarocks2 upwards self.y -= self.speed; }; }); var Stone = Container.expand(function () { var self = Container.call(this); var stoneGraphics = self.attachAsset('Stone', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.3, scaleY: 0.3, smooth: true, filterQuality: 1 }); self.speed = 10; // Increase speed for Stone objects self.update = function () { self.y -= self.speed; }; }); var StoneCircle = Container.expand(function () { var self = Container.call(this); var stoneCircleGraphics = self.attachAsset('StoneCircle', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.5, scaleY: 0.5, smooth: true, filterQuality: 1 }); self.speed = 10; // Increase speed for StoneCircle objects self.update = function () { // Move the stones upwards self.y -= self.speed; }; }); var StoneSquare = Container.expand(function () { var self = Container.call(this); var stoneSquareGraphics = self.attachAsset('StoneSquare', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.5, scaleY: 0.5, smooth: true, filterQuality: 1 }); self.speed = 10; // Increase speed for StoneSquare objects self.update = function () { // Move the stones upwards self.y -= self.speed; }; }); var Tree = Container.expand(function () { var self = Container.call(this); var treeGraphics = self.attachAsset('Tree01', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.0, scaleY: 1.0, smooth: true, filterQuality: 1 }); self.speed = 15; // Further increase the speed of the tree self.update = function () { 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 ****/ // or via static code analysis based on their usage in the code. function startRespawningObjects() { // Logic to start respawning objects based on the current level if (level === 1) { // Respawn Explosions randomly in level 1 if (!LK.gameOver && LK.ticks % 50 == 0 && levelTxtOrange.visible === false) { var newExplosion; for (var i = 0; i < spheres.length; i++) { if (spheres[i] instanceof Explosion && spheres[i].y < -200) { newExplosion = spheres[i]; break; } } if (!newExplosion) { newExplosion = new Explosion(); spheres.push(newExplosion); game.addChild(newExplosion); } newExplosion.x = Math.random() * (2048 - newExplosion.width) + newExplosion.width / 2; newExplosion.y = 2732; } } else if (level === 2) { // Respawn Lavarocks, Lavarocks1, and Lavarocks2 randomly in level 2 if (!LK.gameOver && LK.ticks % 50 == 0) { var newLavarock; for (var i = 0; i < spheres.length; i++) { if ((spheres[i] instanceof Lavarocks || spheres[i] instanceof Lavarocks1 || spheres[i] instanceof Lavarocks2) && spheres[i].y < -200) { newLavarock = spheres[i]; break; } } if (!newLavarock) { var randomType = Math.floor(Math.random() * 5); if (randomType === 0) { newLavarock = new Lavarocks(); } else if (randomType === 1) { newLavarock = new Lavarocks1(); } else { newLavarock = new Lavarocks2(); } spheres.push(newLavarock); game.addChild(newLavarock); } newLavarock.x = Math.random() * (2048 - newLavarock.width) + newLavarock.width / 2; newLavarock.y = 2732; var randomScale = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0 newLavarock.scaleX = randomScale; newLavarock.scaleY = randomScale; } } else if (level === 3) { // Respawn Icerocks randomly in level 3 if (!LK.gameOver && LK.ticks % 50 == 0) { var newIcerock; for (var i = 0; i < spheres.length; i++) { if ((spheres[i] instanceof Icerocks || spheres[i] instanceof Icerocks1 || spheres[i] instanceof Icerocks2) && spheres[i].y < -200) { newIcerock = spheres[i]; break; } } if (!newIcerock) { var randomType = Math.floor(Math.random() * 3); if (randomType === 0) { newIcerock = new Icerocks(); } else if (randomType === 1) { newIcerock = new Icerocks1(); } else { newIcerock = new Icerocks2(); } spheres.push(newIcerock); game.addChild(newIcerock); } newIcerock.x = Math.random() * (2048 - newIcerock.width) + newIcerock.width / 2; newIcerock.y = 2732; var randomScaleX = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0 for width var randomScaleY = 0.5 + Math.random() * 2.0; // Random scale between 0.5 and 2.5 for height newIcerock.scaleX = randomScaleX; newIcerock.scaleY = randomScaleY; } } } // Function to show the level text twice function showLevelTextTwice() { levelTxtOrange.fill = 0x0000ff; // Blue color levelTxtOrange.visible = true; LK.setTimeout(function () { levelTxtOrange.visible = false; LK.setTimeout(function () { levelTxtOrange.visible = true; LK.setTimeout(function () { levelTxtOrange.visible = false; // Start respawning objects after the text has shown and hidden for the last time startRespawningObjects(); }, 500); }, 500); }, 500); } // Add background image for level 4 var backgroundLevel4 = LK.getAsset('Stone_lands', { anchorX: 0.0, anchorY: 0.0 }); backgroundLevel4.smooth = true; backgroundLevel4.filterQuality = 0; // Further increase the filter quality for better clarity backgroundLevel4.scale.set(2048 / 800, 2732 / 800); backgroundLevel4.smooth = true; // Enable smoothing to remove pixelation backgroundLevel4.filterQuality = 0; // Adjust filter quality for better clarity backgroundLevel4.visible = false; // Initially hidden game.addChild(backgroundLevel4); // 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 var backgroundLevel1 = LK.getAsset('Green_lands', { anchorX: 0.0, anchorY: 0.0 }); backgroundLevel1.smooth = true; // Enable smoothing to remove pixelation backgroundLevel1.filterQuality = 0; // Adjust filter quality for better clarity backgroundLevel1.scale.set(2048 / 800, 2732 / 800); // Scale the background to fit the screen backgroundLevel1.smooth = true; // Enable smoothing to remove pixelation backgroundLevel1.filterQuality = 0; // Further increase the filter quality for better clarity backgroundLevel1.visible = true; // Initially visible game.addChild(backgroundLevel1); // Add background image for level 2 var backgroundLevel2 = LK.getAsset('Lava_lands', { anchorX: 0.0, anchorY: 0.0 }); backgroundLevel2.smooth = true; backgroundLevel2.filterQuality = 0; // Further increase the filter quality for better clarity backgroundLevel2.scale.set(2048 / 800, 2732 / 800); backgroundLevel2.smooth = true; // Enable smoothing to remove pixelation backgroundLevel2.filterQuality = 2; // Increase filter quality for better clarity backgroundLevel2.visible = false; // Initially hidden game.addChild(backgroundLevel2); // Add background image for level 3 var backgroundLevel3 = LK.getAsset('Ice_lands', { anchorX: 0.0, anchorY: 0.0 }); backgroundLevel3.smooth = true; backgroundLevel3.filterQuality = 2; // Increase filter quality for better clarity backgroundLevel3.scale.set(2048 / 800, 2732 / 800); // Scale the background to fit the screen backgroundLevel3.smooth = true; // Enable smoothing to remove pixelation backgroundLevel3.filterQuality = 2; // Further increase the filter quality for better clarity backgroundLevel3.visible = false; // Initially hidden game.addChild(backgroundLevel3); // Refresh the background to be clearer & fit to screen every 60 ticks game.update = function () { backgroundLevel1.x = 2048 / 2; // Center the background horizontally backgroundLevel1.y = 2732 / 2; // Center the background vertically if (backgroundLevel1.y >= 2732) { backgroundLevel1.y = -2732; } }; // Function to handle game updates game.update = function () { // Reuse off-screen stones or spawn new ones if necessary if (level === 4 && !LK.gameOver && LK.ticks % 50 == 0) { var newStone; for (var i = 0; i < spheres.length; i++) { if ((spheres[i] instanceof Stone || spheres[i] instanceof StoneCircle || spheres[i] instanceof StoneSquare) && spheres[i].y < -200) { newStone = spheres[i]; break; } } if (!newStone) { var randomType = Math.floor(Math.random() * 3); if (randomType === 0) { newStone = new Stone(); } else if (randomType === 1) { newStone = new StoneCircle(); } else { newStone = new StoneSquare(); } spheres.push(newStone); game.addChild(newStone); } var randomScale = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0 newStone.scaleX = randomScale; newStone.scaleY = randomScale; newStone.x = Math.random() * (2048 - newStone.width) + newStone.width / 2; newStone.y = 2732; } // Check for collisions between fireballs and stones for (var i = fireballs.length - 1; i >= 0; i--) { if (fireballs[i].y > 2732) { fireballs[i].destroy(); fireballs.splice(i, 1); continue; } for (var j = spheres.length - 1; j >= 0; j--) { if (fireballs[i].intersects(spheres[j]) && (spheres[j] instanceof Stone || spheres[j] instanceof StoneCircle || spheres[j] instanceof StoneSquare)) { var explosion4 = LK.getAsset('Explosion4', { anchorX: 0.5, anchorY: 0.5, scaleX: spheres[j].scaleX, scaleY: spheres[j].scaleY }); explosion4.x = spheres[j].x; explosion4.y = spheres[j].y; game.addChild(explosion4); tween(explosion4, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { explosion4.destroy(); } }); // Add random points from 0-40 when the dragon shoots the stones var points = Math.floor(Math.random() * 41); score += points; // Remove the fireball and the object from the game fireballs[i].destroy(); fireballs.splice(i, 1); spheres[j].destroy(); spheres.splice(j, 1); break; } } } // Check for collisions between dragon and stones for (var j = spheres.length - 1; j >= 0; j--) { if (dragon.intersects(spheres[j]) && (spheres[j] instanceof Stone || spheres[j] instanceof StoneCircle || spheres[j] instanceof StoneSquare)) { var explosion4 = LK.getAsset('Explosion4', { anchorX: 0.5, anchorY: 0.5, scaleX: spheres[j].scaleX, scaleY: spheres[j].scaleY }); explosion4.x = spheres[j].x; explosion4.y = spheres[j].y; game.addChild(explosion4); tween(explosion4, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { explosion4.destroy(); } }); // Decrease the dragon's health healthBar.currentHealth -= 20; // Consistent damage taken by the dragon from all objects in level 4 // Remove the stone from the game spheres[j].destroy(); spheres.splice(j, 1); break; } } if (!LK.gameOver && dragNode && dragNode.global) { dragNode.x = game.toLocal(dragNode.global).x; } // Reuse off-screen trees or spawn new ones if necessary if (level === 1 && !LK.gameOver && LK.ticks % 50 == 0) { // Further reduce the interval for spawning trees to add more tree objects // Reduce the interval for spawning trees var newTree; for (var i = 0; i < spheres.length; i++) { if (spheres[i] instanceof Tree && 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); } var randomScale = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0 newTree.scaleX = randomScale; newTree.scaleY = randomScale; newTree.x = Math.random() * (2048 - newTree.width) + newTree.width / 2; newTree.y = 2732; } // Check for collisions between fireballs and trees or Lavarocks for (var i = fireballs.length - 1; i >= 0; i--) { if (fireballs[i].y > 2732) { fireballs[i].destroy(); fireballs.splice(i, 1); continue; } for (var j = spheres.length - 1; j >= 0; j--) { if (fireballs[i].intersects(spheres[j])) { if (spheres[j] instanceof Tree) { var explosion = LK.getAsset('explosion', { anchorX: 0.5, anchorY: 0.5, scaleX: spheres[j].scaleX, // Match the scale of the Tree object scaleY: spheres[j].scaleY // Match the scale of the Tree object }); explosion.x = spheres[j].x; explosion.y = spheres[j].y; game.addChild(explosion); tween(explosion, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { explosion.destroy(); } }); } else if (spheres[j] instanceof Lavarocks || spheres[j] instanceof Lavarocks1 || spheres[j] instanceof Lavarocks2) { var explosion2 = LK.getAsset('Explosion2', { anchorX: 0.5, anchorY: 0.5, scaleX: spheres[j].scaleX, // Match the scale of the Lavarocks object scaleY: spheres[j].scaleY // Match the scale of the Lavarocks object }); explosion2.x = spheres[j].x; explosion2.y = spheres[j].y; game.addChild(explosion2); tween(explosion2, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { explosion2.destroy(); } }); } else if (spheres[j] instanceof Icerocks || spheres[j] instanceof Icerocks1 || spheres[j] instanceof Icerocks2) { var explosion3 = LK.getAsset('Explosion3', { anchorX: 0.5, anchorY: 0.5, scaleX: spheres[j].scaleX, // Match the scale of the Icerocks object scaleY: spheres[j].scaleY // Match the scale of the Icerocks object }); explosion3.x = spheres[j].x; explosion3.y = spheres[j].y; game.addChild(explosion3); tween(explosion3, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { explosion3.destroy(); } }); } // Add random points from 0-40 when the dragon shoots the trees or Lavarocks var points = Math.floor(Math.random() * 41); score += points; // Remove the fireball and the object 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])) { if (spheres[j] instanceof Tree) { var explosion = LK.getAsset('explosion', { anchorX: 0.5, anchorY: 0.5, scaleX: spheres[j].scaleX, // Match the scale of the Tree object scaleY: spheres[j].scaleY // Match the scale of the Tree object }); explosion.x = spheres[j].x; explosion.y = spheres[j].y; game.addChild(explosion); tween(explosion, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { explosion.destroy(); } }); } else if (spheres[j] instanceof Lavarocks || spheres[j] instanceof Lavarocks1 || spheres[j] instanceof Lavarocks2) { var explosion2 = LK.getAsset('Explosion2', { anchorX: 0.5, anchorY: 0.5, scaleX: spheres[j].scaleX, // Match the scale of the Lavarocks object scaleY: spheres[j].scaleY // Match the scale of the Lavarocks object }); explosion2.x = spheres[j].x; explosion2.y = spheres[j].y; game.addChild(explosion2); tween(explosion2, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { explosion2.destroy(); } }); } else if (spheres[j] instanceof Icerocks || spheres[j] instanceof Icerocks1 || spheres[j] instanceof Icerocks2) { var explosion3 = LK.getAsset('Explosion3', { anchorX: 0.5, anchorY: 0.5, scaleX: spheres[j].scaleX, // Match the scale of the Icerocks object scaleY: spheres[j].scaleY // Match the scale of the Icerocks object }); explosion3.x = spheres[j].x; explosion3.y = spheres[j].y; game.addChild(explosion3); tween(explosion3, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { explosion3.destroy(); } }); } // Decrease the dragon's health healthBar.currentHealth -= 20; // Consistent damage taken by the dragon from all objects in level 1 // Remove the rock from the game spheres[j].destroy(); spheres.splice(j, 1); break; } // Decrease dragon's health if any object passes the top of the screen if (spheres[j].y < -200) { healthBar.currentHealth -= 10; // Decrease health by 10 for each object spheres[j].destroy(); spheres.splice(j, 1); } // Reuse off-screen lavarocks or spawn new ones if necessary } // Update the score display when the dragon gets points scoreTxt.setText('Score: ' + score); // Check if health bar reaches 0 to trigger game over if (healthBar.currentHealth <= 0 && !LK.gameOver) { LK.effects.flashScreen(0xff0000, 1000); // Flash screen red for 1 second LK.showGameOver(); // Show game over screen } // Regenerate player's health to max when score reaches 1000 if (score >= 1000) { healthBar.currentHealth = healthBar.maxHealth; } // Transition to the next level when score reaches 532, 1064, and 1596 if (score >= 532 && level === 1 || score >= 1064 && level === 2 || score >= 1596 && level === 3) { level++; levelTxt.setText('Level: ' + level); levelTxtOrange.setText('Level: ' + level); showLevelTextOnce(); // Hide all backgrounds backgroundLevel1.visible = false; backgroundLevel2.visible = false; backgroundLevel3.visible = false; backgroundLevel4.visible = false; // Show the current level background if (level === 2) { backgroundLevel2.visible = true; var newLavarocks = new Lavarocks(); spheres.push(newLavarocks); game.addChild(newLavarocks); newLavarocks.x = Math.random() * (2048 - newLavarocks.width) + newLavarocks.width / 2; newLavarocks.y = -100; } else if (level === 3) { backgroundLevel3.visible = true; var newIcerocks = new Icerocks(); spheres.push(newIcerocks); game.addChild(newIcerocks); newIcerocks.x = Math.random() * (2048 - newIcerocks.width) + newIcerocks.width / 2; newIcerocks.y = -100; } else if (level === 4) { backgroundLevel4.visible = true; var newStone = new Stone(); spheres.push(newStone); game.addChild(newStone); newStone.x = Math.random() * (2048 - newStone.width) + newStone.width / 2; newStone.y = -100; } } // Respawn Lavarocks, Lavarocks1, and Lavarocks2 randomly in level 2 if (level === 2 && !LK.gameOver && LK.ticks % 50 == 0) { var newLavarock; for (var i = 0; i < spheres.length; i++) { if ((spheres[i] instanceof Lavarocks || spheres[i] instanceof Lavarocks1 || spheres[i] instanceof Lavarocks2) && spheres[i].y < -200) { newLavarock = spheres[i]; break; } } if (!newLavarock) { var randomType = Math.floor(Math.random() * 3); if (randomType === 0) { newLavarock = new Lavarocks(); } else if (randomType === 1) { newLavarock = new Lavarocks1(); } else { newLavarock = new Lavarocks2(); } spheres.push(newLavarock); game.addChild(newLavarock); } var randomScale = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0 newLavarock.scaleX = randomScale; newLavarock.scaleY = randomScale; newLavarock.x = Math.random() * (2048 - newLavarock.width) + newLavarock.width / 2; newLavarock.y = 2732; } // Respawn Icerocks randomly in level 3 if (level === 3 && !LK.gameOver && LK.ticks % 50 == 0) { var newIcerock; for (var i = 0; i < spheres.length; i++) { if ((spheres[i] instanceof Icerocks || spheres[i] instanceof Icerocks1 || spheres[i] instanceof Icerocks2) && spheres[i].y < -200) { newIcerock = spheres[i]; break; } } if (!newIcerock) { var randomType = Math.floor(Math.random() * 3); if (randomType === 0) { newIcerock = new Icerocks(); } else if (randomType === 1) { newIcerock = new Icerocks1(); } else { newIcerock = new Icerocks2(); } spheres.push(newIcerock); game.addChild(newIcerock); } var randomScaleX = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0 for width var randomScaleY = 0.5 + Math.random() * 2.0; // Random scale between 0.5 and 2.5 for height newIcerock.scaleX = randomScaleX; newIcerock.scaleY = randomScaleY; newIcerock.x = Math.random() * (2048 - newIcerock.width) + newIcerock.width / 2; newIcerock.y = 2732; } }; // Initialize dragon dragon = game.addChild(new Dragon()); dragon.x = 2048 / 2; dragon.y = 200; // Initialize start text var startTxt = new Text2('Start', { size: 150, fill: 0xffa500, // Orange color font: "'Time new roman',Impact,'Arial Black',Tahoma" }); startTxt.anchor.set(0.5, 0.5); LK.gui.center.addChild(startTxt); // Function to show start text with alternating colors function showStartText() { startTxt.visible = true; LK.setTimeout(function () { startTxt.visible = false; showLevelTextTwice(); // Show level status text before start text }, 2000); } // 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 // Call the function to show start text at the beginning of the game showStartText(); // Initialize second level display in orange var levelTxtOrange = new Text2('Level: ' + level, { size: 100, fill: 0xffff00, // Yellow color font: "'Time new roman',Impact,'Arial Black',Tahoma" }); levelTxtOrange.anchor.set(0.5, 0.5); LK.gui.center.addChild(levelTxtOrange); levelTxtOrange.visible = false; // Initially hidden // Function to show the orange level text twice function showLevelTextOnce() { levelTxtOrange.fill = 0xffff00; // Yellow color levelTxtOrange.visible = true; tween(levelTxtOrange, { alpha: 0 }, { duration: 500, easing: tween.easeInOut, onFinish: function onFinish() { tween(levelTxtOrange, { alpha: 1 }, { duration: 500, easing: tween.easeInOut, onFinish: function onFinish() { tween(levelTxtOrange, { alpha: 0 }, { duration: 500, easing: tween.easeInOut, onFinish: function onFinish() { tween(levelTxtOrange, { alpha: 1 }, { duration: 500, easing: tween.easeInOut, onFinish: function onFinish() { levelTxtOrange.visible = false; // Start respawning objects after the text has disappeared startRespawningObjects(); } }); } }); } }); } }); } // 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) { // Make the dragon follow the mouse cursor 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)); dragon.x = newX; dragon.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 assetId; if (level === 2) { assetId = 'Shout2'; } else if (level === 3) { assetId = 'Shout3'; } else if (level === 4) { assetId = 'Shout4'; } else { assetId = 'Shout1'; } var fireball = new Fireball(assetId); fireball.x = dragon.x; fireball.y = dragon.y; fireballs.push(fireball); game.addChild(fireball); } } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* 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: 2.0,
scaleY: 2.0,
smooth: true,
filterQuality: 2
});
self.speed = 20; // Further 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 (assetId) {
var self = Container.call(this);
var fireballGraphics = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.6,
scaleY: 0.9,
smooth: true,
filterQuality: 1
});
self.speed = 40; // Increase the speed of the fireballs
self.update = function () {
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
};
});
var Icerocks = Container.expand(function () {
var self = Container.call(this);
var icerocksGraphics = self.attachAsset('Icerocks', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: level === 3 ? 1.5 : 1.0,
scaleY: level === 3 ? 1.5 : 1.0,
smooth: true,
filterQuality: 1
});
self.speed = level === 3 ? 10 : 5; // Increase speed for level 3
self.update = function () {
self.y -= self.speed;
};
});
var Icerocks1 = Container.expand(function () {
var self = Container.call(this);
var icerocks1Graphics = self.attachAsset('Icerocks1', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: level === 3 ? 1.5 : 1.0,
scaleY: level === 3 ? 1.5 : 1.0,
smooth: true,
filterQuality: 1
});
self.speed = level === 3 ? 10 : 5; // Increase speed for level 3
self.update = function () {
// Move the icerocks upwards
self.y -= self.speed;
};
});
var Icerocks2 = Container.expand(function () {
var self = Container.call(this);
var icerocks2Graphics = self.attachAsset('Icerocks2', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: level === 3 ? 1.5 : 1.0,
scaleY: level === 3 ? 1.5 : 1.0,
smooth: true,
filterQuality: 1
});
self.speed = level === 3 ? 10 : 5; // Increase speed for level 3
self.update = function () {
// Move the icerocks upwards
self.y -= self.speed;
};
});
var Lavarocks = Container.expand(function () {
var self = Container.call(this);
var lavarocksGraphics = self.attachAsset('Lavarocks', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: level === 2 ? 1.5 : 1.0,
scaleY: level === 2 ? 1.5 : 1.0,
smooth: true,
filterQuality: 1
});
self.speed = level === 2 ? 15 : 5; // Further increase speed for level 2
self.update = function () {
self.y -= self.speed;
};
});
var Lavarocks1 = Container.expand(function () {
var self = Container.call(this);
var lavarocks1Graphics = self.attachAsset('Lavarocks1', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: level === 2 ? 1.5 : 1.0,
scaleY: level === 2 ? 1.5 : 1.0,
smooth: true,
filterQuality: 1
});
self.speed = level === 2 ? 15 : 5; // Further increase speed for level 2
self.update = function () {
// Move the lavarocks1 upwards
self.y -= self.speed;
};
});
var Lavarocks2 = Container.expand(function () {
var self = Container.call(this);
var lavarocks2Graphics = self.attachAsset('Lavarocks2', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: level === 2 ? 1.5 : 1.0,
scaleY: level === 2 ? 1.5 : 1.0,
smooth: true,
filterQuality: 1
});
self.speed = level === 2 ? 15 : 5; // Further increase speed for level 2
self.update = function () {
// Move the lavarocks2 upwards
self.y -= self.speed;
};
});
var Stone = Container.expand(function () {
var self = Container.call(this);
var stoneGraphics = self.attachAsset('Stone', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.3,
scaleY: 0.3,
smooth: true,
filterQuality: 1
});
self.speed = 10; // Increase speed for Stone objects
self.update = function () {
self.y -= self.speed;
};
});
var StoneCircle = Container.expand(function () {
var self = Container.call(this);
var stoneCircleGraphics = self.attachAsset('StoneCircle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.5,
scaleY: 0.5,
smooth: true,
filterQuality: 1
});
self.speed = 10; // Increase speed for StoneCircle objects
self.update = function () {
// Move the stones upwards
self.y -= self.speed;
};
});
var StoneSquare = Container.expand(function () {
var self = Container.call(this);
var stoneSquareGraphics = self.attachAsset('StoneSquare', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.5,
scaleY: 0.5,
smooth: true,
filterQuality: 1
});
self.speed = 10; // Increase speed for StoneSquare objects
self.update = function () {
// Move the stones upwards
self.y -= self.speed;
};
});
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('Tree01', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.0,
scaleY: 1.0,
smooth: true,
filterQuality: 1
});
self.speed = 15; // Further increase the speed of the tree
self.update = function () {
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
****/
// or via static code analysis based on their usage in the code.
function startRespawningObjects() {
// Logic to start respawning objects based on the current level
if (level === 1) {
// Respawn Explosions randomly in level 1
if (!LK.gameOver && LK.ticks % 50 == 0 && levelTxtOrange.visible === false) {
var newExplosion;
for (var i = 0; i < spheres.length; i++) {
if (spheres[i] instanceof Explosion && spheres[i].y < -200) {
newExplosion = spheres[i];
break;
}
}
if (!newExplosion) {
newExplosion = new Explosion();
spheres.push(newExplosion);
game.addChild(newExplosion);
}
newExplosion.x = Math.random() * (2048 - newExplosion.width) + newExplosion.width / 2;
newExplosion.y = 2732;
}
} else if (level === 2) {
// Respawn Lavarocks, Lavarocks1, and Lavarocks2 randomly in level 2
if (!LK.gameOver && LK.ticks % 50 == 0) {
var newLavarock;
for (var i = 0; i < spheres.length; i++) {
if ((spheres[i] instanceof Lavarocks || spheres[i] instanceof Lavarocks1 || spheres[i] instanceof Lavarocks2) && spheres[i].y < -200) {
newLavarock = spheres[i];
break;
}
}
if (!newLavarock) {
var randomType = Math.floor(Math.random() * 5);
if (randomType === 0) {
newLavarock = new Lavarocks();
} else if (randomType === 1) {
newLavarock = new Lavarocks1();
} else {
newLavarock = new Lavarocks2();
}
spheres.push(newLavarock);
game.addChild(newLavarock);
}
newLavarock.x = Math.random() * (2048 - newLavarock.width) + newLavarock.width / 2;
newLavarock.y = 2732;
var randomScale = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0
newLavarock.scaleX = randomScale;
newLavarock.scaleY = randomScale;
}
} else if (level === 3) {
// Respawn Icerocks randomly in level 3
if (!LK.gameOver && LK.ticks % 50 == 0) {
var newIcerock;
for (var i = 0; i < spheres.length; i++) {
if ((spheres[i] instanceof Icerocks || spheres[i] instanceof Icerocks1 || spheres[i] instanceof Icerocks2) && spheres[i].y < -200) {
newIcerock = spheres[i];
break;
}
}
if (!newIcerock) {
var randomType = Math.floor(Math.random() * 3);
if (randomType === 0) {
newIcerock = new Icerocks();
} else if (randomType === 1) {
newIcerock = new Icerocks1();
} else {
newIcerock = new Icerocks2();
}
spheres.push(newIcerock);
game.addChild(newIcerock);
}
newIcerock.x = Math.random() * (2048 - newIcerock.width) + newIcerock.width / 2;
newIcerock.y = 2732;
var randomScaleX = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0 for width
var randomScaleY = 0.5 + Math.random() * 2.0; // Random scale between 0.5 and 2.5 for height
newIcerock.scaleX = randomScaleX;
newIcerock.scaleY = randomScaleY;
}
}
}
// Function to show the level text twice
function showLevelTextTwice() {
levelTxtOrange.fill = 0x0000ff; // Blue color
levelTxtOrange.visible = true;
LK.setTimeout(function () {
levelTxtOrange.visible = false;
LK.setTimeout(function () {
levelTxtOrange.visible = true;
LK.setTimeout(function () {
levelTxtOrange.visible = false;
// Start respawning objects after the text has shown and hidden for the last time
startRespawningObjects();
}, 500);
}, 500);
}, 500);
}
// Add background image for level 4
var backgroundLevel4 = LK.getAsset('Stone_lands', {
anchorX: 0.0,
anchorY: 0.0
});
backgroundLevel4.smooth = true;
backgroundLevel4.filterQuality = 0; // Further increase the filter quality for better clarity
backgroundLevel4.scale.set(2048 / 800, 2732 / 800);
backgroundLevel4.smooth = true; // Enable smoothing to remove pixelation
backgroundLevel4.filterQuality = 0; // Adjust filter quality for better clarity
backgroundLevel4.visible = false; // Initially hidden
game.addChild(backgroundLevel4);
// 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
var backgroundLevel1 = LK.getAsset('Green_lands', {
anchorX: 0.0,
anchorY: 0.0
});
backgroundLevel1.smooth = true; // Enable smoothing to remove pixelation
backgroundLevel1.filterQuality = 0; // Adjust filter quality for better clarity
backgroundLevel1.scale.set(2048 / 800, 2732 / 800); // Scale the background to fit the screen
backgroundLevel1.smooth = true; // Enable smoothing to remove pixelation
backgroundLevel1.filterQuality = 0; // Further increase the filter quality for better clarity
backgroundLevel1.visible = true; // Initially visible
game.addChild(backgroundLevel1);
// Add background image for level 2
var backgroundLevel2 = LK.getAsset('Lava_lands', {
anchorX: 0.0,
anchorY: 0.0
});
backgroundLevel2.smooth = true;
backgroundLevel2.filterQuality = 0; // Further increase the filter quality for better clarity
backgroundLevel2.scale.set(2048 / 800, 2732 / 800);
backgroundLevel2.smooth = true; // Enable smoothing to remove pixelation
backgroundLevel2.filterQuality = 2; // Increase filter quality for better clarity
backgroundLevel2.visible = false; // Initially hidden
game.addChild(backgroundLevel2);
// Add background image for level 3
var backgroundLevel3 = LK.getAsset('Ice_lands', {
anchorX: 0.0,
anchorY: 0.0
});
backgroundLevel3.smooth = true;
backgroundLevel3.filterQuality = 2; // Increase filter quality for better clarity
backgroundLevel3.scale.set(2048 / 800, 2732 / 800); // Scale the background to fit the screen
backgroundLevel3.smooth = true; // Enable smoothing to remove pixelation
backgroundLevel3.filterQuality = 2; // Further increase the filter quality for better clarity
backgroundLevel3.visible = false; // Initially hidden
game.addChild(backgroundLevel3);
// Refresh the background to be clearer & fit to screen every 60 ticks
game.update = function () {
backgroundLevel1.x = 2048 / 2; // Center the background horizontally
backgroundLevel1.y = 2732 / 2; // Center the background vertically
if (backgroundLevel1.y >= 2732) {
backgroundLevel1.y = -2732;
}
};
// Function to handle game updates
game.update = function () {
// Reuse off-screen stones or spawn new ones if necessary
if (level === 4 && !LK.gameOver && LK.ticks % 50 == 0) {
var newStone;
for (var i = 0; i < spheres.length; i++) {
if ((spheres[i] instanceof Stone || spheres[i] instanceof StoneCircle || spheres[i] instanceof StoneSquare) && spheres[i].y < -200) {
newStone = spheres[i];
break;
}
}
if (!newStone) {
var randomType = Math.floor(Math.random() * 3);
if (randomType === 0) {
newStone = new Stone();
} else if (randomType === 1) {
newStone = new StoneCircle();
} else {
newStone = new StoneSquare();
}
spheres.push(newStone);
game.addChild(newStone);
}
var randomScale = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0
newStone.scaleX = randomScale;
newStone.scaleY = randomScale;
newStone.x = Math.random() * (2048 - newStone.width) + newStone.width / 2;
newStone.y = 2732;
}
// Check for collisions between fireballs and stones
for (var i = fireballs.length - 1; i >= 0; i--) {
if (fireballs[i].y > 2732) {
fireballs[i].destroy();
fireballs.splice(i, 1);
continue;
}
for (var j = spheres.length - 1; j >= 0; j--) {
if (fireballs[i].intersects(spheres[j]) && (spheres[j] instanceof Stone || spheres[j] instanceof StoneCircle || spheres[j] instanceof StoneSquare)) {
var explosion4 = LK.getAsset('Explosion4', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: spheres[j].scaleX,
scaleY: spheres[j].scaleY
});
explosion4.x = spheres[j].x;
explosion4.y = spheres[j].y;
game.addChild(explosion4);
tween(explosion4, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
explosion4.destroy();
}
});
// Add random points from 0-40 when the dragon shoots the stones
var points = Math.floor(Math.random() * 41);
score += points;
// Remove the fireball and the object from the game
fireballs[i].destroy();
fireballs.splice(i, 1);
spheres[j].destroy();
spheres.splice(j, 1);
break;
}
}
}
// Check for collisions between dragon and stones
for (var j = spheres.length - 1; j >= 0; j--) {
if (dragon.intersects(spheres[j]) && (spheres[j] instanceof Stone || spheres[j] instanceof StoneCircle || spheres[j] instanceof StoneSquare)) {
var explosion4 = LK.getAsset('Explosion4', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: spheres[j].scaleX,
scaleY: spheres[j].scaleY
});
explosion4.x = spheres[j].x;
explosion4.y = spheres[j].y;
game.addChild(explosion4);
tween(explosion4, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
explosion4.destroy();
}
});
// Decrease the dragon's health
healthBar.currentHealth -= 20; // Consistent damage taken by the dragon from all objects in level 4
// Remove the stone from the game
spheres[j].destroy();
spheres.splice(j, 1);
break;
}
}
if (!LK.gameOver && dragNode && dragNode.global) {
dragNode.x = game.toLocal(dragNode.global).x;
}
// Reuse off-screen trees or spawn new ones if necessary
if (level === 1 && !LK.gameOver && LK.ticks % 50 == 0) {
// Further reduce the interval for spawning trees to add more tree objects
// Reduce the interval for spawning trees
var newTree;
for (var i = 0; i < spheres.length; i++) {
if (spheres[i] instanceof Tree && 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);
}
var randomScale = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0
newTree.scaleX = randomScale;
newTree.scaleY = randomScale;
newTree.x = Math.random() * (2048 - newTree.width) + newTree.width / 2;
newTree.y = 2732;
}
// Check for collisions between fireballs and trees or Lavarocks
for (var i = fireballs.length - 1; i >= 0; i--) {
if (fireballs[i].y > 2732) {
fireballs[i].destroy();
fireballs.splice(i, 1);
continue;
}
for (var j = spheres.length - 1; j >= 0; j--) {
if (fireballs[i].intersects(spheres[j])) {
if (spheres[j] instanceof Tree) {
var explosion = LK.getAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: spheres[j].scaleX,
// Match the scale of the Tree object
scaleY: spheres[j].scaleY // Match the scale of the Tree object
});
explosion.x = spheres[j].x;
explosion.y = spheres[j].y;
game.addChild(explosion);
tween(explosion, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
explosion.destroy();
}
});
} else if (spheres[j] instanceof Lavarocks || spheres[j] instanceof Lavarocks1 || spheres[j] instanceof Lavarocks2) {
var explosion2 = LK.getAsset('Explosion2', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: spheres[j].scaleX,
// Match the scale of the Lavarocks object
scaleY: spheres[j].scaleY // Match the scale of the Lavarocks object
});
explosion2.x = spheres[j].x;
explosion2.y = spheres[j].y;
game.addChild(explosion2);
tween(explosion2, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
explosion2.destroy();
}
});
} else if (spheres[j] instanceof Icerocks || spheres[j] instanceof Icerocks1 || spheres[j] instanceof Icerocks2) {
var explosion3 = LK.getAsset('Explosion3', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: spheres[j].scaleX,
// Match the scale of the Icerocks object
scaleY: spheres[j].scaleY // Match the scale of the Icerocks object
});
explosion3.x = spheres[j].x;
explosion3.y = spheres[j].y;
game.addChild(explosion3);
tween(explosion3, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
explosion3.destroy();
}
});
}
// Add random points from 0-40 when the dragon shoots the trees or Lavarocks
var points = Math.floor(Math.random() * 41);
score += points;
// Remove the fireball and the object 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])) {
if (spheres[j] instanceof Tree) {
var explosion = LK.getAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: spheres[j].scaleX,
// Match the scale of the Tree object
scaleY: spheres[j].scaleY // Match the scale of the Tree object
});
explosion.x = spheres[j].x;
explosion.y = spheres[j].y;
game.addChild(explosion);
tween(explosion, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
explosion.destroy();
}
});
} else if (spheres[j] instanceof Lavarocks || spheres[j] instanceof Lavarocks1 || spheres[j] instanceof Lavarocks2) {
var explosion2 = LK.getAsset('Explosion2', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: spheres[j].scaleX,
// Match the scale of the Lavarocks object
scaleY: spheres[j].scaleY // Match the scale of the Lavarocks object
});
explosion2.x = spheres[j].x;
explosion2.y = spheres[j].y;
game.addChild(explosion2);
tween(explosion2, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
explosion2.destroy();
}
});
} else if (spheres[j] instanceof Icerocks || spheres[j] instanceof Icerocks1 || spheres[j] instanceof Icerocks2) {
var explosion3 = LK.getAsset('Explosion3', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: spheres[j].scaleX,
// Match the scale of the Icerocks object
scaleY: spheres[j].scaleY // Match the scale of the Icerocks object
});
explosion3.x = spheres[j].x;
explosion3.y = spheres[j].y;
game.addChild(explosion3);
tween(explosion3, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
explosion3.destroy();
}
});
}
// Decrease the dragon's health
healthBar.currentHealth -= 20; // Consistent damage taken by the dragon from all objects in level 1
// Remove the rock from the game
spheres[j].destroy();
spheres.splice(j, 1);
break;
}
// Decrease dragon's health if any object passes the top of the screen
if (spheres[j].y < -200) {
healthBar.currentHealth -= 10; // Decrease health by 10 for each object
spheres[j].destroy();
spheres.splice(j, 1);
}
// Reuse off-screen lavarocks or spawn new ones if necessary
}
// Update the score display when the dragon gets points
scoreTxt.setText('Score: ' + score);
// Check if health bar reaches 0 to trigger game over
if (healthBar.currentHealth <= 0 && !LK.gameOver) {
LK.effects.flashScreen(0xff0000, 1000); // Flash screen red for 1 second
LK.showGameOver(); // Show game over screen
}
// Regenerate player's health to max when score reaches 1000
if (score >= 1000) {
healthBar.currentHealth = healthBar.maxHealth;
}
// Transition to the next level when score reaches 532, 1064, and 1596
if (score >= 532 && level === 1 || score >= 1064 && level === 2 || score >= 1596 && level === 3) {
level++;
levelTxt.setText('Level: ' + level);
levelTxtOrange.setText('Level: ' + level);
showLevelTextOnce();
// Hide all backgrounds
backgroundLevel1.visible = false;
backgroundLevel2.visible = false;
backgroundLevel3.visible = false;
backgroundLevel4.visible = false;
// Show the current level background
if (level === 2) {
backgroundLevel2.visible = true;
var newLavarocks = new Lavarocks();
spheres.push(newLavarocks);
game.addChild(newLavarocks);
newLavarocks.x = Math.random() * (2048 - newLavarocks.width) + newLavarocks.width / 2;
newLavarocks.y = -100;
} else if (level === 3) {
backgroundLevel3.visible = true;
var newIcerocks = new Icerocks();
spheres.push(newIcerocks);
game.addChild(newIcerocks);
newIcerocks.x = Math.random() * (2048 - newIcerocks.width) + newIcerocks.width / 2;
newIcerocks.y = -100;
} else if (level === 4) {
backgroundLevel4.visible = true;
var newStone = new Stone();
spheres.push(newStone);
game.addChild(newStone);
newStone.x = Math.random() * (2048 - newStone.width) + newStone.width / 2;
newStone.y = -100;
}
}
// Respawn Lavarocks, Lavarocks1, and Lavarocks2 randomly in level 2
if (level === 2 && !LK.gameOver && LK.ticks % 50 == 0) {
var newLavarock;
for (var i = 0; i < spheres.length; i++) {
if ((spheres[i] instanceof Lavarocks || spheres[i] instanceof Lavarocks1 || spheres[i] instanceof Lavarocks2) && spheres[i].y < -200) {
newLavarock = spheres[i];
break;
}
}
if (!newLavarock) {
var randomType = Math.floor(Math.random() * 3);
if (randomType === 0) {
newLavarock = new Lavarocks();
} else if (randomType === 1) {
newLavarock = new Lavarocks1();
} else {
newLavarock = new Lavarocks2();
}
spheres.push(newLavarock);
game.addChild(newLavarock);
}
var randomScale = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0
newLavarock.scaleX = randomScale;
newLavarock.scaleY = randomScale;
newLavarock.x = Math.random() * (2048 - newLavarock.width) + newLavarock.width / 2;
newLavarock.y = 2732;
}
// Respawn Icerocks randomly in level 3
if (level === 3 && !LK.gameOver && LK.ticks % 50 == 0) {
var newIcerock;
for (var i = 0; i < spheres.length; i++) {
if ((spheres[i] instanceof Icerocks || spheres[i] instanceof Icerocks1 || spheres[i] instanceof Icerocks2) && spheres[i].y < -200) {
newIcerock = spheres[i];
break;
}
}
if (!newIcerock) {
var randomType = Math.floor(Math.random() * 3);
if (randomType === 0) {
newIcerock = new Icerocks();
} else if (randomType === 1) {
newIcerock = new Icerocks1();
} else {
newIcerock = new Icerocks2();
}
spheres.push(newIcerock);
game.addChild(newIcerock);
}
var randomScaleX = 0.5 + Math.random() * 1.5; // Random scale between 0.5 and 2.0 for width
var randomScaleY = 0.5 + Math.random() * 2.0; // Random scale between 0.5 and 2.5 for height
newIcerock.scaleX = randomScaleX;
newIcerock.scaleY = randomScaleY;
newIcerock.x = Math.random() * (2048 - newIcerock.width) + newIcerock.width / 2;
newIcerock.y = 2732;
}
};
// Initialize dragon
dragon = game.addChild(new Dragon());
dragon.x = 2048 / 2;
dragon.y = 200;
// Initialize start text
var startTxt = new Text2('Start', {
size: 150,
fill: 0xffa500,
// Orange color
font: "'Time new roman',Impact,'Arial Black',Tahoma"
});
startTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(startTxt);
// Function to show start text with alternating colors
function showStartText() {
startTxt.visible = true;
LK.setTimeout(function () {
startTxt.visible = false;
showLevelTextTwice(); // Show level status text before start text
}, 2000);
}
// 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
// Call the function to show start text at the beginning of the game
showStartText();
// Initialize second level display in orange
var levelTxtOrange = new Text2('Level: ' + level, {
size: 100,
fill: 0xffff00,
// Yellow color
font: "'Time new roman',Impact,'Arial Black',Tahoma"
});
levelTxtOrange.anchor.set(0.5, 0.5);
LK.gui.center.addChild(levelTxtOrange);
levelTxtOrange.visible = false; // Initially hidden
// Function to show the orange level text twice
function showLevelTextOnce() {
levelTxtOrange.fill = 0xffff00; // Yellow color
levelTxtOrange.visible = true;
tween(levelTxtOrange, {
alpha: 0
}, {
duration: 500,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(levelTxtOrange, {
alpha: 1
}, {
duration: 500,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(levelTxtOrange, {
alpha: 0
}, {
duration: 500,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(levelTxtOrange, {
alpha: 1
}, {
duration: 500,
easing: tween.easeInOut,
onFinish: function onFinish() {
levelTxtOrange.visible = false;
// Start respawning objects after the text has disappeared
startRespawningObjects();
}
});
}
});
}
});
}
});
}
// 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) {
// Make the dragon follow the mouse cursor
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));
dragon.x = newX;
dragon.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 assetId;
if (level === 2) {
assetId = 'Shout2';
} else if (level === 3) {
assetId = 'Shout3';
} else if (level === 4) {
assetId = 'Shout4';
} else {
assetId = 'Shout1';
}
var fireball = new Fireball(assetId);
fireball.x = dragon.x;
fireball.y = dragon.y;
fireballs.push(fireball);
game.addChild(fireball);
}
}
};