FPS Game Example
let playerX = 10;
let playerY = 20;
let enemyX = 15;
let enemyY = 25;
// Calculate the distance between the player and the enemy
let distance = Math.sqrt(Math.pow(enemyX - playerX, 2) + Math.pow(enemyY - playerY, 2));
// Check if the distance between the player and the enemy is less than 10
if (distance < 10) {
// If the distance is less than 10, the enemy is close enough to attack the player
console.log("The enemy is within range! Take cover!");
} else {
// If the distance is greater than or equal to 10, the enemy is too far away to attack
console.log("The enemy is too far away. Keep moving!");
}Last updated