📜  Python Enemy NPC CLass - Python (1)

📅  最后修改于: 2023-12-03 15:33:59.844000             🧑  作者: Mango

Python Enemy NPC Class

This is a Python class that can be used to create non-playable characters (NPCs) for use as enemies in a game or simulation.

Features
  • Customizable enemy stats such as health, damage, and armor
  • Randomized loot drops upon defeat
  • Attack and defend functions for combat
Usage

To create an NPC instance, simply call the class with the desired stats:

enemy = EnemyNPC(name="Orc", health=50, damage=10, armor=5, loot={"gold": 20, "weapon": "sword"})

Attack and defend functions can then be used for combat:

player_damage = 15
enemy.defend(player_damage)
if enemy.health <= 0:
    print("Enemy defeated!")
    print(f"Loot dropped: {enemy.loot}")
else:
    enemy_attack = enemy.attack()
    player.defend(enemy_attack)
    if player.health <= 0:
        print("Game over!")
Full class code
import random

class EnemyNPC:
    def __init__(self, name, health, damage, armor, loot):
        self.name = name
        self.health = health
        self.max_health = health
        self.damage = damage
        self.armor = armor
        self.loot = loot
    
    def attack(self):
        return random.randint(0, self.damage)
    
    def defend(self, damage):
        damage_taken = damage - self.armor
        self.health -= damage_taken
    
    def __str__(self):
        return f"{self.name} ({self.health}/{self.max_health})"
Conclusion

With this Python Enemy NPC Class, game developers and simulation programmers have a powerful tool to create varied and challenging enemies. The class is easily customizable and can be integrated into a larger codebase with ease.