xaviermontenegro

Untitled

Jul 22nd, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. # No classes
  2. player1_name = "Player 1"
  3. player1_hp = 10
  4. player1_attack = 1
  5. player1_defense = 1
  6.  
  7. player2_name = "Player 2"
  8. player2_hp = 10
  9. player2_attack = 1
  10. player2_defense = 1
  11.  
  12. # Player 1 attacks Player 2
  13. player2_hp = player2_hp - player1_attack + player2_defense
  14.  
  15. # Player 2 attacks Player 1
  16. player1_hp = player1_hp - player2_attack + player1_defense
  17.  
  18. # Now with classes
  19. class Player:
  20.     def __init__(self, name, hp, attack, defense):
  21.         self.name = name
  22.         self.hp = hp
  23.         self.attack = attack
  24.         self.defense = defense
  25.  
  26.     def attack(self, target):
  27.         target.hp = target.hp - self.attack + target.defense
  28.  
  29. player1 = Player("Player 1", 10, 1, 1)
  30. player2 = Player("Player 2", 10, 1, 1)
  31.  
  32. # Player 1 attacks Player 2
  33. player1.attack(player2)
  34.  
  35. # Player 2 attacks Player 1
  36. player2.attack(player1)
Add Comment
Please, Sign In to add comment