Question
How do I code this using python3? I need to follow the instructions and get the output written in the first and second images. And
How do I code this using python3? I need to follow the instructions and get the output written in the first and second images. And the 3rd and last images show the codes that are the base which should be added while coding.Plases help me!!
import random
#create a set of inventory items items = {"loaf of bread", "gold coin", "roll of twine", "candle", "amethyst crystal", "dagger"}
''' Checks for finding a health elixir. Health can be restored by a value between 0-5. ''' def check_for_health_elixir(hero): random_chance = random.randint(0,5) if random_chance == 5: health_amt = random.randint(2,5) print("You have found a health elixir.") print("Drinking it restores your health by " + str(health_amt) + ".") hero.restore_health(health_amt) print("Your current health is " + str(hero.health) + ". ")
''' Checks for finding inventory. ''' def check_for_inventory(hero): random_chance = random.randint(0,4) if random_chance == 4: item = random.sample(items, 1)[0] print("You have found a " + item + ".") hero.add_inventory(item) print("Your current inventory is " + str(hero.get_inventory()) + ". ")
''' Determines how much damage is inflicted by running from an enemy (a value from 0 to half the enemy's damage power). ''' def run(hero, enemy): damage = random.randint(0, enemy.damage//2) print("The " + enemy.name + " inflicted a damage of " + str(damage) + " as you ran away.") health = hero.take_damage(damage) print("Your current health is " + str(health) + ". ")
''' Determines how much damage is incurred by fighting an enemy. If hero wins, 0 damage is taken. If enemy wins, then the enemy's full damage power is taken. ''' def fight(hero, enemy): winner = random.randint(0,1) if winner == 0: print("You have defeated the " + enemy.name + ".") else: print("You have lost the fight. The " + enemy.name + " has inflicted a damage of " + str(enemy.damage) + ".") health = hero.take_damage(enemy.damage) print("Your current health is " + str(hero.health) + ". ")
def main():
#create 1 hero and 4 enemies hero = Hero('Elsa', 40) goblin = Enemy('Goblin', 10, 5) dragon = Enemy('Dragon', 30, 10) ogre = Enemy('Ogre', 20, 8) wizard = Enemy('Wizard', 20, 10)
#put the enemies in a set enemies = {goblin, dragon, ogre, wizard}
print("You are the hero " + hero.name + ". Your current health is " + str(hero.health) + ". ") game_over = False while not game_over:
#check for health elixir check_for_health_elixir(hero)
#check for inventory check_for_inventory(hero)
#encounter an enemy - randomly select one from the set of enemies enemy = random.sample(enemies, 1)[0] print("You have encountered a " + enemy.name + ".") action = input("Do you want to run or fight?").strip()
while action != "run" and action != "fight": action = input("Do you want to run or fight?").strip()
#respond to the user's action (run or fight) if action == "run": run(hero, enemy)
elif action == "fight": fight(hero, enemy)
#check hero's health if hero.health
if __name__ == '__main__': main()
Your program must contain a main function, which in turn calls other functions. Turn in one program file that does all of the following things: You are going to create the classes for a simple, text-based role player game (RPG). In this game, there are good characters (heroes) and bad characters (enemies) All characters, whether good or bad, have a health value. Once a character's health value drops to (or below) zero, the character dies. Characters will encounter each other in the game and can inflict damage upon each other, reducing the health value of characters that get attacked. Create a Character class. A character should be initialized with a name and an initial health value. Characters should have a method called take damage0 that takes in a damage amount and reduces the character's health by that amount, then returns the Character's resulting health value Also implement the str0 method for Character so that printing a Character prints their name and their current health value in exactly this format - for an example Character with the name 1. Trinity" and a health value of 75 Trinity (health-75) 2. Create a subclass of Character called Hero. A Hero should be initialized with a name and an initial health value. Heroes are able to regain health points by finding healing elixirs. Heroes should have a method called restore health0 that takes in a restore amount and increases the character's health by that amount. Heroes can also have an inventory of items. Create a private variable for inventory and initialize it to an empty list in the initializer. Create methods add inventory and remove inventory which take in a single item as an argument and add or remove that item from the hero's inventory list. Create method get _inventory that returns the inventory list. 3. Create a subclass of Character called Enemy. An enemy should be initialized with a name, an initial health value, and a damage amount. The damage amount is how much damage this enemy will take from the hero if this enemy wins a battle with the hero. Enemies do not have any additional methods. 4. Once your classes are created, create a main0 that tests your classes by simulating some encounters between Characters. In main0 Create a Hero named "Han" with health of 40 Create an Enemy named "Zombie" with health of 20 and damage of 15. Create an Enemy named "Warewolf" with health of 15 and damage of 10. a. Print "Start:" and then print the three characters. Start: Han (health-40) Zombie (health-20) Warewolf (health-15) An encounter occurs between the hero and the warewolf. The hero damages the warewolf by 10. The warewolf damages the hero by its full damage strength. Write the lines of code b. to reflect this (hint: 2 lines of code). Print "Battle 1:" and then print the hero and the warewolf Battle 1: Han (health-30) WAERNOlf (health#5) An encounter occurs between the hero and the zombie. The hero damages the zombie by 20. The zombie damages the hero by its full damage strength. Write the lines of code to reflect this. c. Print "Battle 2:" and then print the hero and the zombie. Battle 2: Han (health-15) Zombie (health-o) The hero finds a health elixir to restore his health by 5 points. Write the code to reflect this. d. Print "Restore Health" and then print the hero. Restore Health: Han (health-20) e. The hero finds a gold coin. Add "gold coin" to the hero's inventory. The hero finds a candle, Add "candle" to the hero's inventory. Print "Inventory:" and then print the hero's inventory by using the get inventory method. Inventory: 'gold coin', 'candle' 1 All together, your printouts should look exactly like this: Start: Han (health-40) Zombie (health-20) Warewolt (health-15) Battle 1: Han (health-30) Warewolf (health#5) Battle 2: Han (health= 15 ) Zombie (health-0) Restore Health: Han (health-20) Inventory: I'gold coin', 'candle' 5. Use the provided play rPg py file to test your code and play the role player game (RPG) In your homework file, wrap your call to main0 in an if-statement, as we discussed in class: a. if name main: main() b. At the top of the play.xpS.py file, change "Hw8" in the import statement to your homework file name. import random #create a set of inventory items items "loaf of bread", "gold coin", "roll of twine", "candle", "amethyst crystal", "dagger" Checks for finding a health elixir. Health can be restored by a value between 0-5. def check for health elixirChero): random.chance random.randint(0,5) if random_chance5: health amt random.randint (2,5) print( You have found a health elixir." print("Drinking it restores your health by + str(health amt) +".") hero.restore_ healthChealth_amt) print("Your current health is"strChero.health) ".In" Checks for finding inventory def check for-inventory(hero): random_chance random.randint(0,4) if random-chance-4: item random. sample(items, 1)0] print( You have found a " item" hero.add inventory(item) print("Your current inventory is " + str(hero. get-inventory) + "An") Determines how much damage is inflicted by running from an enemy (a value from 0 to half the enemy's damage power). def runChero, enemy): damage random. randintce, enemy.damage//2) print(C"The " enemy.name " inflicted a damage of " health - hero.take_damage(damage) print("Your current health is" str(health)n + str(damage) +"as you ran away." Determines how much damage is incurred by fighting an enemy. If hero wins, 0 damage is taken. If enemy wins, then the enemy's full damage power is taken. def fightChero, enemy): winner random.randint(o,1) if winner0: printC"You have defeated the" + enemy name + ".") def fight(hero, enemy): winner = random, randi nt(0,1) if winner0: printC"You have defeated the "enemy.name +"." printC"You have lost the fight. The " else: + enemy.name + " has inflicted a damage of " + str(enemy.damage) ".") health hero.take_damage Cenemy.damage) print("Your current health is " str(hero.health) .n") def mainO: #create 1 hero and 4 enemies hero Hero('Elsa', 40) goblin Enemy'Goblin', 10, 5) dragon = Enemy("Dragon", 30, 10) ogre Enemy('0gre, 20, 8) wizard Enemy('Wizard', 20, 10) #put the enemies in a set enemies goblin, dragon, ogre, wizard print("You are the hero" + hero.name ". Your current health is " str(hero.health). In") game over False while not game_over: #check for health elixir check_for_health_elixirChero) #check for inventory check for inventory(hero) #encounter an enemy - randomly select one from the set of enemies enemy= random.sample(enemies, 1)[0] print("You have encountered a enemy.name ".") action input(" Do you want to run or fight?").stripO while action != "run" and action !- "fight": action input("Do you want to run or fight?").strip) #respond to the user's action (run or fight) if action"run": runChero, enemy) elif action"fight": fight(hero, enemy) #check hero's health if hero . healthStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started