ClearCode

class_intro

Apr 27th, 2022
1,362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. # health = 0
  2. # def scope_test():
  3. #   health += 1
  4. # scope_test()
  5. # print(health)
  6.  
  7. class Monster:
  8.  
  9.     def __init__(self,health,energy):
  10.         self.health = health
  11.         self.energy = energy
  12.  
  13.     # methods
  14.     def attack(self,amount):
  15.         print('The monster has attacked!')
  16.         print(f'{amount} damage was dealt')
  17.         monster.energy -= 20
  18.         print(self.energy)
  19.  
  20.     def move(self,speed):
  21.         print('The monster has moved')
  22.         print(f'It has a speed of {speed}')
  23.  
  24.     def get_damage(self,amount):
  25.         self.health -= amount
  26.  
  27. class Hero:
  28.     def __init__(self,health,damage,monster):
  29.         self.health = health
  30.         self.damage = damage
  31.         self.monster = monster
  32.  
  33.     def attack(self):
  34.         self.monster.get_damage(self.damage)
  35.  
  36. monster = Monster(health = 50, energy = 40)
  37. # monster.weapon = 'axe'
  38. # print(vars(monster))
  39.  
  40. print(monster.health)
  41. hero = Hero(health = 100, damage = 20, monster = monster)
  42. hero.attack()
  43. print(monster.health)
Advertisement
Add Comment
Please, Sign In to add comment