Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # No classes
- player1_name = "Player 1"
- player1_hp = 10
- player1_attack = 1
- player1_defense = 1
- player2_name = "Player 2"
- player2_hp = 10
- player2_attack = 1
- player2_defense = 1
- # Player 1 attacks Player 2
- player2_hp = player2_hp - player1_attack + player2_defense
- # Player 2 attacks Player 1
- player1_hp = player1_hp - player2_attack + player1_defense
- # Now with classes
- class Player:
- def __init__(self, name, hp, attack, defense):
- self.name = name
- self.hp = hp
- self.attack = attack
- self.defense = defense
- def attack(self, target):
- target.hp = target.hp - self.attack + target.defense
- player1 = Player("Player 1", 10, 1, 1)
- player2 = Player("Player 2", 10, 1, 1)
- # Player 1 attacks Player 2
- player1.attack(player2)
- # Player 2 attacks Player 1
- player2.attack(player1)
Add Comment
Please, Sign In to add comment