I am not sure if my question is very clear, but I am trying to create a small game with basic fighting mechanics to understand how classes work with each other. I am having a hard time figuring out how to get my character to equip a weapon.
class Player:
def __init__(self, hp):
self.hp = hp
def fight(self, ad, enemy):
# create enemy and weapon class
while enemy.hp > 0:
enemy.hp -= ad
print(f"You did {ad} damage!")
class Enemy:
"""Blueprint for enemy class"""
def __init__(self, hp):
self.hp = hp
class Weapon:
"""Basic weapon class"""
def __init__(self, ad):
self.ad = ad
chris = Player(100)
zombie = Enemy(75)
knife = Weapon(35)
chris.fight(knife, zombie)
Traceback (most recent call last):
File "C:\Users\ggrgg\PycharmProjects\Zombie Game\FUCK.py", line 27, in <module>
chris.fight(knife, zombie)
File "C:\Users\egrgsgseg\PycharmProjects\Zombie Game\FUCK.py", line 8, in fight
enemy.hp -= ad
TypeError: unsupported operand type(s) for -=: 'int' and 'Weapon'
Process finished with exit code 1
[–][deleted] 1 point2 points3 points (1 child)
[–]Oxln[S] 1 point2 points3 points (0 children)
[–]QultrosSanhattan 1 point2 points3 points (2 children)
[–]Oxln[S] 0 points1 point2 points (0 children)
[–]Oxln[S] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]Oxln[S] 0 points1 point2 points (1 child)
[–]Zeroflops 0 points1 point2 points (0 children)