Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
class Item:
"""
"""
def __init__(self, item_name, item_description, item_health, item_attack, item_defence):
self.item_name = item_name
self.item_description = item_description
self.item_health = item_health
self.item_attack = item_attack
self.item_defence = item_defence
class Character:
"""
Character class
Attributes: name, health, attack, defence
Methods: hit
"""
def __init__(self, name = "Unknown", health=100, attack=12, defence=10, inventory = []):
"""
Default constructor for the object of class Character
The values can be modified at the initialisation with parameters
"""
self.name = name
self.health = health
self.attack = attack
self.defence = defence
self.inventory = inventory
def __str__(self):
"""
Method used to print the attributes of the object
:param self: Object of the class Character
:return: The attributes of the object
"""
return f"{self.name}. \n Health: {self.health} \n Attack: {self.attack} \n" \
f" Defence: {self.defence}. \n"
def hit(self, other):
"""
Method used for battle
:param self: Own character object
:param other: The target object
:return: Modifies the health of the target, receiving damage depending on the self attack and target's defece.
"""
other.health -= self.attack*((100-other.defence)/100)
def equip(self, other):
self.health += other.item_health
self.attack += other.item_attack
self.defence += other.item_defence
self.inventory.append(other)
#For demonstration purpose
if __name__ == '__main__':
print("Welcome to THE NAME OF THE GAME! \n You are in a room and this is all you know:")
player = Character("Lucian")
print(player)
print("You are searching the room...\n")
sword = Item("sword", "a rusty sword", 0, 7, 5)
print(f"You found an item: {sword.item_description}")
ans = input("Do you want to equip it?")
if ans == "Yes":
player.equip(sword)
print(f"You succesfully equipped {sword.item_description}")
print("You have the following items in your inventory: ")
for _item in player.inventory:
print(_item.item_description)
#wolf = Character("aggressive wolf", 35, 20, 5)
#print(f"You encountered: {wolf}")
#player.hit(wolf)
#print(wolf)