Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randrange
- class Character():
- def __init__ (self, health, attack, defense):
- self.health = health
- self.base_attack = attack
- self.base_defense = defense
- self.defending = False
- self.exp = 0
- self.inventory = {}
- def inflict (self, damage):
- calculated_damage = damage - self.base_defense
- if self.defending:
- calculated_damage = calculated_damage // 2
- if calculated_damage < 0:
- calculated_damage = 0
- self.health -= calculated_damage
- return calculated_damage
- def give_item (self, item, quantity):
- original_quantity = self.inventory.get(item, 0)
- self.inventory.update({item: original_quantity + quantity})
- def attack (self, other_character):
- self.defending = False
- return other_character.inflict(self.base_attack)
- def defend (self):
- self.defending = True
- def use_item (self, item):
- self.defending = False
- quantity = self.inventory.get(item, 0)
- if quantity == 0:
- return "you do not have the item"
- self.inventory.update({item: quantity - 1})
- if item == "potion":
- self.health += 20
- return "you were healed 20 hp"
- return "it had a mysterious effect"
- def has_item (self, item):
- quantity = self.inventory.get(item, 0)
- return quantity > 0
- player = Character(100, 10, 6)
- player.give_item("potion", 3)
- enemies_defeated = 0
- while player.health > 0:
- player_level = player.exp // 10 + 1
- enemy = Character(player_level * 10 + randrange(10*player_level, 20*player_level),
- player_level * 10 + randrange(2*player_level, 10*player_level),
- player_level + randrange(player_level, 2*player_level))
- if randrange(1, 10) > 4:
- enemy.give_item("potion", randrange(1, 5))
- print("A wild beast appears!")
- while enemy.health > 0:
- action = None
- while action is None:
- user_input = input("Action? Attack, Defend, Item: ").strip().lower()
- if user_input == "attack" or user_input == "defend":
- action = user_input
- elif user_input == "item":
- print ("You have the following items:")
- for item in player.inventory:
- print (item, ": ", player.inventory[item])
- print ("Type the item name to use it")
- elif player.has_item(user_input):
- action = user_input
- else:
- print ("Invalid command")
- if action == "attack":
- actual_damage = player.attack(enemy)
- print("You did ", actual_damage, " damage to the beast")
- elif action == "defend":
- player.defend();
- print("You defend")
- else:
- print ("You use the ", action, " and ", player.use_item(action))
- if enemy.health > 0:
- enemy_action = randrange(1, 100)
- if enemy_action < 50:
- print ("The beast attacks and deals ", enemy.attack(player), " damage")
- print ("You have ", player.health, " health")
- if player.health <= 0:
- break
- elif enemy_action < 90:
- enemy.defend()
- print ("The beast defends")
- else:
- if enemy.has_item("potion"):
- enemy.use_item("potion")
- print ("The beast uses a potion")
- else:
- print ("The beast ponders the meaning of life")
- if player.health > 0:
- enemies_defeated += 1
- print ("The beast was slain!")
- if enemy.has_item("potion"):
- player.give_item("potion", enemy.inventory["potion"])
- print ("You receive ", enemy.inventory["potion"], " potions")
- player.exp += 7
- print ("You receive 7exp")
- new_player_level = player.exp // 10 + 1
- if new_player_level > player_level:
- print ("You reached level ", new_player_level)
- player.health += 100
- player.base_attack += 5
- player.base_defense += 3
- else:
- print ("You were slain")
- print ("You had defeated ", enemies_defeated, " beasts")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement