Changed around line 1
+ const player = document.getElementById('player');
+ const enemy = document.getElementById('enemy');
+ const npc = document.getElementById('npc');
+ const messageBox = document.getElementById('message-box');
+
+ let playerX = 400;
+ let playerY = 300;
+ let enemyX = 200;
+ let enemyY = 200;
+ let npcX = 600;
+ let npcY = 400;
+
+ const speed = 5;
+ const enemySpeed = 2;
+ const npcSpeed = 1;
+ const attackRange = 50;
+ const escapeThreshold = 20;
+ let enemyHealth = 100;
+
+ function updatePositions() {
+ player.style.left = `${playerX}px`;
+ player.style.top = `${playerY}px`;
+ enemy.style.left = `${enemyX}px`;
+ enemy.style.top = `${enemyY}px`;
+ npc.style.left = `${npcX}px`;
+ npc.style.top = `${npcY}px`;
+ }
+
+ function movePlayer(direction) {
+ switch (direction) {
+ case 'ArrowUp': playerY -= speed; break;
+ case 'ArrowDown': playerY += speed; break;
+ case 'ArrowLeft': playerX -= speed; break;
+ case 'ArrowRight': playerX += speed; break;
+ }
+ updatePositions();
+ }
+
+ function moveEnemy() {
+ const dx = playerX - enemyX;
+ const dy = playerY - enemyY;
+ const distance = Math.sqrt(dx * dx + dy * dy);
+
+ if (distance < attackRange) {
+ enemyX += dx / distance * enemySpeed;
+ enemyY += dy / distance * enemySpeed;
+ if (distance < 10) {
+ messageBox.textContent = 'Enemy attacks!';
+ messageBox.style.display = 'block';
+ setTimeout(() => messageBox.style.display = 'none', 1000);
+ }
+ } else if (enemyHealth < escapeThreshold) {
+ enemyX -= dx / distance * enemySpeed;
+ enemyY -= dy / distance * enemySpeed;
+ } else {
+ enemyX += Math.cos(Date.now() / 1000) * enemySpeed;
+ enemyY += Math.sin(Date.now() / 1000) * enemySpeed;
+ }
+ updatePositions();
+ }
+
+ function moveNPC() {
+ npcX += (Math.random() - 0.5) * npcSpeed;
+ npcY += (Math.random() - 0.5) * npcSpeed;
+ updatePositions();
+ }
+
+ npc.addEventListener('click', () => {
+ messageBox.textContent = 'NPC: Hello, adventurer!';
+ messageBox.style.display = 'block';
+ setTimeout(() => messageBox.style.display = 'none', 2000);
+ });
+
+ document.addEventListener('keydown', (e) => {
+ if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(e.key)) {
+ movePlayer(e.key);
+ }
+ });
+
+ setInterval(moveEnemy, 100);
+ setInterval(moveNPC, 100);