User prompt
only have enemy's shoot arrow when player Is in path
User prompt
have enemy's shoot arrow at random moment
User prompt
give the enemy's bows and arrows
Code edit (1 edits merged)
Please save this source code
User prompt
Gladiator Gauntlet: Endless Arena
Initial prompt
create the gladiator games. In this game you will have to defeat many opponents by killing them with swords and knifes. As you kill more opponents It will get harder and harder. This game is an endless runner so keep track of the score. watch out for men on horses and swords cause they can kill you.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { highScore: 0 }); /**** * Classes ****/ var BowAndArrow = Container.expand(function () { var self = Container.call(this); var arrowGraphics = self.attachAsset('weapon', { anchorX: 0.5, anchorY: 0.5, rotation: Math.PI / 2 // Horizontal orientation }); self.speed = 10; self.damage = 5; self.update = function () { self.y += self.speed; }; return self; }); var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5, rotation: Math.PI // Face down }); self.speed = 3 + Math.random() * 2; self.damage = 10; self.health = 1; self.points = 10; self.type = 'basic'; self.update = function () { self.y += self.speed; // Check if player is in path and shoot an arrow if (Math.abs(gladiator.x - self.x) < 50) { var arrow = new BowAndArrow(); arrow.x = self.x; arrow.y = self.y; weapons.push(arrow); game.addChild(arrow); } self.lastArrowShot = self.lastArrowShot || 0; if (Date.now() - self.lastArrowShot > 2000 && Math.random() < 0.05) { // Shoot an arrow every 2 seconds on average var arrow = new BowAndArrow(); arrow.x = self.x; arrow.y = self.y; weapons.push(arrow); game.addChild(arrow); self.lastArrowShot = Date.now(); } }; self.takeDamage = function (amount) { self.health -= amount; // Flash red when hit LK.effects.flashObject(enemyGraphics, 0xFF0000, 300); if (self.health <= 0) { return true; // Enemy died } return false; // Enemy still alive }; return self; }); var Horseman = Enemy.expand(function () { var self = Enemy.call(this); // Replace the enemy graphics with horseman graphics self.removeChild(self.getChildAt(0)); var horsemanGraphics = self.attachAsset('horseman', { anchorX: 0.5, anchorY: 0.5, rotation: Math.PI // Face down }); self.speed = 5 + Math.random() * 3; self.damage = 20; self.health = 2; self.points = 25; self.type = 'horseman'; return self; }); var Gladiator = Container.expand(function () { var self = Container.call(this); var gladiatorGraphics = self.attachAsset('gladiator', { anchorX: 0.5, anchorY: 0.5 }); // Shield effect var shieldGraphics = self.attachAsset('shield', { anchorX: 0.5, anchorY: 0.5, y: 80, alpha: 0 }); // Health bar var healthBarBg = self.attachAsset('healthBar', { anchorX: 0.5, anchorY: 0.5, y: -120, width: 150, color: 0xFF0000 }); var healthBarFg = self.attachAsset('healthBar', { anchorX: 0, anchorY: 0.5, x: -75, y: -120, width: 150 }); self.health = 100; self.maxHealth = 100; self.shielding = false; self.shieldDuration = 1500; self.shieldCooldown = 0; self.maxShieldCooldown = 3000; self.down = function (x, y, obj) { if (self.shieldCooldown <= 0) { self.activateShield(); } }; self.activateShield = function () { self.shielding = true; shieldGraphics.alpha = 0.8; self.shieldCooldown = self.maxShieldCooldown; LK.setTimeout(function () { self.shielding = false; shieldGraphics.alpha = 0; }, self.shieldDuration); }; self.takeDamage = function (amount) { if (!self.shielding) { self.health -= amount; // Update health bar var healthPercent = Math.max(0, self.health / self.maxHealth); healthBarFg.width = 150 * healthPercent; // Flash red when hit LK.effects.flashObject(gladiatorGraphics, 0xFF0000, 300); if (self.health <= 0) { LK.getSound('death').play(); LK.setTimeout(function () { LK.showGameOver(); }, 500); } else { LK.getSound('hit').play(); } return true; // Damage was dealt } else { // Shield blocked the hit LK.effects.flashObject(shieldGraphics, 0xFFFFFF, 300); LK.getSound('block').play(); return false; // Damage was blocked } }; self.update = function () { // Update shield cooldown if (self.shieldCooldown > 0) { self.shieldCooldown -= 16.67; // Approximate ms per frame at 60fps } }; return self; }); var Weapon = Container.expand(function () { var self = Container.call(this); var weaponGraphics = self.attachAsset('weapon', { anchorX: 0.5, anchorY: 0.5, rotation: Math.PI / 2 // Horizontal orientation }); self.speed = 7; self.damage = 15; self.update = function () { self.y += self.speed; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x2F4F4F }); /**** * Game Code ****/ // Initialize arena var arena = LK.getAsset('arena', { anchorX: 0.5, anchorY: 0.5, alpha: 0.7 }); arena.x = 2048 / 2; arena.y = 2732 / 2; game.addChild(arena); // Game variables var score = 0; var level = 1; var enemies = []; var weapons = []; var spawnInterval = 2000; // Initial spawn interval in milliseconds var lastSpawn = 0; var weaponSpawnChance = 0.1; var horsemanSpawnChance = 0.05; var mouseX = 2048 / 2; var mouseY = 2732 / 2; var isDragging = false; // Create player var gladiator = new Gladiator(); gladiator.x = 2048 / 2; gladiator.y = 2732 - 400; game.addChild(gladiator); // Score display var scoreTxt = new Text2('SCORE: 0', { size: 90, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Level display var levelTxt = new Text2('LEVEL 1', { size: 70, fill: 0xFFFF00 }); levelTxt.anchor.set(0.5, 0); levelTxt.y = 100; LK.gui.top.addChild(levelTxt); // High score display var highScoreTxt = new Text2('HIGH SCORE: ' + storage.highScore, { size: 60, fill: 0xAAFFAA }); highScoreTxt.anchor.set(0.5, 0); highScoreTxt.y = 180; LK.gui.top.addChild(highScoreTxt); // Handle touch/mouse events game.down = function (x, y, obj) { isDragging = true; mouseX = x; mouseY = y; }; game.up = function (x, y, obj) { isDragging = false; }; game.move = function (x, y, obj) { if (isDragging) { mouseX = x; mouseY = y; } }; // Spawn enemies function spawnEnemy() { var enemy; // Random chance to spawn horseman based on current level if (Math.random() < horsemanSpawnChance) { enemy = new Horseman(); } else { enemy = new Enemy(); } // Position enemy randomly across top of the screen enemy.x = Math.random() * (2048 - 200) + 100; enemy.y = -200; // Adjust difficulty based on level enemy.speed += (level - 1) * 0.5; enemy.health += Math.floor((level - 1) / 3); enemies.push(enemy); game.addChild(enemy); // Random chance to spawn weapon if (Math.random() < weaponSpawnChance) { spawnWeapon(); } } // Spawn thrown weapons function spawnWeapon() { var weapon = new Weapon(); weapon.x = Math.random() * (2048 - 200) + 100; weapon.y = -100; weapons.push(weapon); game.addChild(weapon); } // Track last update time var lastUpdateTime = Date.now(); var elapsedGameTime = 0; // Game update logic game.update = function () { // Calculate delta time var currentTime = Date.now(); var deltaTime = currentTime - lastUpdateTime; lastUpdateTime = currentTime; // Move gladiator smoothly toward touch position if (isDragging) { var dx = mouseX - gladiator.x; var dy = mouseY - gladiator.y; // Limit player movement to bottom half of screen var targetY = Math.min(mouseY, 2732 - 300); targetY = Math.max(targetY, 2732 / 2); gladiator.x += dx * 0.1; gladiator.y += (targetY - gladiator.y) * 0.1; } // Keep track of elapsed time for spawning elapsedGameTime += deltaTime; // Spawn enemies based on interval if (elapsedGameTime - lastSpawn > spawnInterval) { spawnEnemy(); lastSpawn = elapsedGameTime; } // Update enemies for (var i = enemies.length - 1; i >= 0; i--) { var enemy = enemies[i]; // Check for collision with gladiator if (!enemy.collisionChecked && enemy.intersects(gladiator)) { enemy.collisionChecked = true; // Player attacks enemy var enemyDied = enemy.takeDamage(1); if (enemyDied) { // Update score score += enemy.points; scoreTxt.setText('SCORE: ' + score); // Check high score if (score > storage.highScore) { storage.highScore = score; highScoreTxt.setText('HIGH SCORE: ' + storage.highScore); } // Remove enemy enemy.destroy(); enemies.splice(i, 1); // Level up check if (score >= level * 100) { levelUp(); } } else { // Enemy hits player if not killed var damageDealt = gladiator.takeDamage(enemy.damage); // If attack was blocked, knockback the enemy if (!damageDealt) { enemy.y -= 100; } } } else if (enemy.y > 2732 + 100) { // Enemy went off screen enemy.destroy(); enemies.splice(i, 1); } } // Update weapons for (var i = weapons.length - 1; i >= 0; i--) { var weapon = weapons[i]; // Check for collision with gladiator if (!weapon.collisionChecked && weapon.intersects(gladiator)) { weapon.collisionChecked = true; // Weapon hits player gladiator.takeDamage(weapon.damage); // Remove weapon weapon.destroy(); weapons.splice(i, 1); } else if (weapon.y > 2732 + 50) { // Weapon went off screen weapon.destroy(); weapons.splice(i, 1); } } // Gradually decrease spawn interval based on time (make game harder) spawnInterval = Math.max(500, 2000 - elapsedGameTime / 10000); }; // Level up function function levelUp() { level++; levelTxt.setText('LEVEL ' + level); // Increase difficulty horsemanSpawnChance = Math.min(0.4, 0.05 + (level - 1) * 0.05); weaponSpawnChance = Math.min(0.5, 0.1 + (level - 1) * 0.05); // Visual effect LK.effects.flashScreen(0xFFFF00, 500); LK.getSound('levelUp').play(); } // Start background music LK.playMusic('arenaMusic', { fade: { start: 0, end: 0.4, duration: 1000 } });
===================================================================
--- original.js
+++ change.js
@@ -36,10 +36,10 @@
self.points = 10;
self.type = 'basic';
self.update = function () {
self.y += self.speed;
- if (Math.random() < 0.01) {
- // Random chance to shoot an arrow
+ // Check if player is in path and shoot an arrow
+ if (Math.abs(gladiator.x - self.x) < 50) {
var arrow = new BowAndArrow();
arrow.x = self.x;
arrow.y = self.y;
weapons.push(arrow);