fork download
  1. class Character:
  2. def __init__(self, name, team, is_player=False):
  3. # Thuộc tính cơ bản
  4. self.name = name
  5. self.team = team # 'Blue' hoặc 'Red'
  6. self.is_player = is_player
  7. self.health = 100
  8. self.damage = 10
  9. self.position = (0, 0) # Ví dụ: vị trí trên bản đồ (x, y)
  10. self.target = None # Mục tiêu hiện tại
  11.  
  12. def take_damage(self, amount):
  13. self.health -= amount
  14. # Dùng cú pháp print của Python 2 (không cần dấu ngoặc đơn)
  15. # và dùng phương pháp định dạng chuỗi cũ hơn %s / %d
  16. if self.health <= 0:
  17. print "%s da bi ha guc!" % self.name
  18. return True # Nhân vật đã chết
  19. return False
  20.  
  21. def move(self, new_position):
  22. self.position = new_position
  23.  
  24. def attack(self, target_character):
  25. print "%s tan cong %s" % (self.name, target_character.name)
  26. target_character.take_damage(self.damage)
  27.  
  28. def __str__(self):
  29. # Dùng phương thức format() hoặc %
  30. return "[%s] %s | HP: %d" % (self.team, self.name, self.health)
  31.  
Success #stdin #stdout 0.06s 62944KB
stdin
Game
stdout
Standard output is empty