ideas resetting stats in class - python

I made a simple program but when my monster dies the stats don't reset (hp mainly) I am lost In how to make it reset every time the monsters hp reaches 0 and the xp is awarded. I know that I can wright the code over again and again but I would like to be able to make it continue is as little amount of code as possible. I'm still learning python so I don't really know as much as everyone else here. Ive gotten to classes but not so much in depth here is the code:
import random
def title():
print"hello Hero, welcome to staghold"
print"you have traveld along way, and you find yourself"
print"surrounded by and army of monsters as far as the eye can see"
print"begin to draw your sword.....you run full speed towards the army"
print"how many monsters can you kill before the inevatable comes?"
raw_input("(press enter to continue)")
def stats():
print"you have 200 health"
print"your level is 1"
print"you have 0 exp"
raw_input("(press enter to continue)")
class monster:
hp=50
monsterattack=random.randint
xp=random.randint(20,50)
health=200
level=1
exp=0
wave=1
title()
stats()
print"you run into a wave"
while level==1:
if monster.hp<=0:
print"you have defeated this wave of monsters"
wave+=1
exp+=monster.xp
print" you get, "+str(monster.xp)+" exp from the monster"
print"you now have, "+str(exp)+" exp"
if exp>=300:
level+=1
if level==2:
print"CONGRADULATIONS YOU HAVE REACHED LEVEL 2"
elif monster.hp>=0:
choice=raw_input("Will you 'fight' or 'run' from this horde?")
if choice=='fight':
print"you swing your sword at the monster"
att=random.randint(2, 13)
health-=monster.monsterattack(2,15)
monster.hp-=att
hp=200-health
print"you do, "+str(att)+" damage to the monster"
print"the monster does, "+str(hp)+" damage to you"
print"you have, "+str(health)+" health left"
print"the monster has, "+str(monster.hp)+" health left"
elif choice=="run":
print"you got away from this wave safely"
else:
print"NOT A VALID CHOICE"

I can see you're pretty early in your programming journey-
in your example, monster is a class. This means it is a definition of how an object will behave. That is good- but you never define an example of a monster. That would be something like
lion = monster()
which would create a new monster named lion. You need to set up a constructor in the monster class that tells the program how to build a new monster, for example
class Monster:
def __init__(self):
self.hp = 50
self.xp = random.randint(20,50)
def monsterattack(self):
return random.randint()
This would allow you to create a monster, and allow you to have that monster attack with
damage = lion.monsterattack
You could then create a new lion, of class monster, during each loop - and have your hero fight it. The lion would only exist within the current run through of the loop, so each time you create a lion, it would be a completely new monster.
I like your dedication- keep at it, read some basic tutorials!

Related

Changing a local variable in multiple functions in Python?

background: I'm currently writing a text-based adventure and each enemy has a certain amount of turns you can attack it before it attacks back.
So to handle this the code sets an argument in the function for the fight dictating how many times you can attack.
def fight_sequence(rounds):
while rounds > 0:
attack = input()
if attack == magic:
magic("you teleport away to safety. Congratulations you have stayed alive through your journey and found a few souvenoirs. nice job!", 1, "you muster up all of your energy, chant the spell.... and nothing happens.Cthulu... seems unimpressed", 1, "")
elif attack == sword:
sword(1)
def magic(teleportmessage, teleportsuccess, firemessage, firefail, winmessage):
x = 0
while x == 0:
fightorflight = input("""you open the book to cast a spell
Do you want to try teleporting or use a fireball?""").lower()
if "teleport" in fightorflight:
if teleportsuccess = 1:
print(teleportmessage)
x = 1
else:
choice = input("You can't teleport out of this battle. Would you like to try a fireball?")
if choice == yes:
fightorflight = "fireball"
else:
x = 1
elif "fire" in fightorflight:
print(firemessage)
if firefail == 1:
choice = input("do you want to try to teleport instead?").lower()
if "yes" in choice:
fightorflight = "teleport"
else:
x = 1
else:
print(winmessage)
else:
print("Sorry not sure what you mean")
def sword(attacksuccess):
if attacksuccess == 1:
print("You pull out the sword and swing at the monster damaging it severely.")
else:
print("You pull out the sword and swing at the monster, but its immune to blunt objects.")
fight_sequence(3)
both magic() and sword() need to be able to decrease rounds by 1, originally i just did that before entering the magic or sword function. however some items to attack with allow you to attack more than once if you want so that won't work for them. Such as magic if they also choose to teleport. Is there a way to allow me to change the variable rounds while inside of another function?
I think using a return value might help but I'm not sure how to go about it
You can simply add a new argument to the magic function, and pass the 'rounds' variable through when you call it.
e.g.
def fight_sequence(rounds):
...
magic("some message", false, "fired", false, "you won", rounds)
def magic(teleportmessage, teleportsuccess, firemessage, firefail, winmessage, rounds):
Your passing through the variable (not just the value) so it will change in every context where rounds can be seen.
HTH.
I would recommend using classes to create this game rather than lots of functions, an example of a class in a hero game below.
class Hero:
def __init__(self):
self.health = 10
def eatApple(self):
self.health += 1
def takeDamage(self):
self.health -= 1
init function runs as class is initialized.
player = Hero()
print(player.health) # will print 10
player.takeDamage()
print(player.health) # will print 9
This way you can have global variables for you functions which can be changed in each function and is much more organised.

How can i create multiple if statments that uses a def function as a condtion without having the def function repeat itself

I'm creating a game where one of the bosses (Suru) acts differently based on what the user inputs. The user input is created in a def() function inside a class (Player) and has to use returns to pass the results to the main def() function (boss_battle). However, whenever i try to use an if code to set conditions for the battle function, the user input repeats itself until the last line of the if statement.
Here is the if code that's causing the problem:
if player.attack(boss)== 'mercy':
kind = True
break
#If the player input returns 'mercy' then the battle will stop
#However the results don't show as the code moves on to repeat itself in the next line
elif player.attack(boss) in ('heal, no_mercy'):
pass
#If the player chooses to heal or fails to give mercy then the battle will continue as normal, however Suru won't be able to attack the player that turn
#The same happens with this line
elif player.attack(boss)==('attack'):
wait = 'no'
#If the player chooses to fight then the battle will commence as normal, however Suru will no be able to attack throughout the rest of the battle
#This line prints out the results but the results end up being inaccurate as 'mercy' or 'heal' aren't taken into effect
What is supposed to happen is whatever the user inputs (and the code returns) will affect what Suru does: 'heal' and 'no_mercy) causes him to wait (not attacking on that turn), 'mercy' causes the battle to end and 'attack' causes Suru to attack the player back (and continue to do so for the rest of the battle).
However what is does instead is repeat the player input 3 times, only taking the third input into account. This causes the code to be inaccurate as the condition meet by the third input will only work for attack (allowing the Suru to attack the player) and not for mercy or heal / no_mercy.
Keep in mind that even if you enter the correct condtions for the statement, the code will continue to go through each if statement (causing the user input to be requiered 3 times instead of 1).
Below is a sample code to run if needed while explaining the bases of the code and what im trying to achieve.
class Character:
#Creates the base for the Player and the enemy setup
def __init__(self, health, power, speed, core, max_health):
self.health = health
self.power = power
self.speed = speed
self.core = core
def attack(self, other):
raise NotImplementedError
#Allows for the enemy_attack and attack_enemy methods (functions) to be placed into separate classes
#Allows for the player and enemy to be able to attack each other
class Player(Character):
#Creates Data and stats for the Player
def __init__(self, name, health, power, speed, core, max_health, heal, counter, endure):
super().__init__(health, power, speed, core, max_health)
#Creates the base for the stats required for the player
#Super() allows for the parent class in (charecter class) to be called and used to create stats the child class (Player)
self.name = name
self.power = power
self.speed = speed
self.core = core
self.max_health = max_health
self.heal = heal
self.counter = counter
self.endure = endure
def attack(self, other):
action = input("\nWhat move would you like to make (fight, heal or mercy)? ").lower()
print()
sleep(1)
#Asks the user what to do
if action in ('fight', 'heal', 'mercy'):
if action == 'heal':
print("\nYou try to heal your wounds")
sleep(1)
self.health += self.heal
print("You have {0.health} hp.\n".format(self))
sleep(1)
return 'heal'
#Returns that the user has healed that turn and did not attack, allowing wait to remain as 'yes' - unless the user has attacked before
elif action == 'mercy':
chance = (random.randint(1, 10))
response = random.choice(["You tell the {0.name} that you don't want to fight.".format(other),
"You refuse to fight.", "You remain still to show the {0.name} that you don't wan't to fight.",
"You try to give the {0.name} mercy.".format(other)])
re_response = random.choice(response)
print(response)
sleep(1)
if chance >= other.mercy_count:
response_fail = random.choice(["But you were unable to convince the {0.name} not to fight.".format(other),
"But the {0.name} didn't believe you.".format(other),
"Yet the {0.name} still wants to fight.".format(other),
"But it didn't work."])
print(response_fail)
sleep(1)
return 'no_mercy'
elif chance < other.mercy_count:
response_pass = random.choice(["You were able to convince the {0.name} not to fight.".format(other),
"The {0.name} decided believe you.".format(other),
"As it turns out, the {0.name} didn't want to fight.".format(other),
"It worked."])
print(response_pass)
return 'mercy'
sleep(1)
#Mercy is based on a selective chance, if the player is able to spare Suru, then it returns 'mercy', causing the battle to end
elif action == 'fight':
print("You attack the {0.name}.".format(other))
other.health -= int(self.power * random.randint(1,3))
sleep(1)
#other.health = enemy health
#uses the Player's power to randomly (within reason) generate the damage taken by the enemy
#Also allows for a way to bypass the problem of not being able to convert player.power to a numeric figure
return 'attack'
#Returns attack - stating that the user has chosen to fight - and allows for the code to inishiate Suru's attack pattern
else:
print("you stumble...")
sleep(1)
#If the entry isn't valid then the message is diplayed and the sequence is conitnued
class Boss(Character):
def __init__(self, name, health, power, speed, core, max_health, mercy_count):
super().__init__(health, power, speed, core, max_health)
self.name = name
self.power = power
self.speed = speed
self.mercy_count = mercy_count
self.max_health = max_health
def attack(self, other):
print("The {0.name} attacks...".format(self))
sleep(1)
other.health -= int(self.power * random.randint(1,3)
def boss_battle(player, boss):
#Battle Function
kind = False
#variable used for mercy
wait = 'yes'
#variable used to determine whether on not Suru is to attack on that turn
print ("\n{0.name} confronts you!".format(boss))
sleep(0.5)
print ("{0.name} has {0.health} hp.".format(boss))
sleep(1)
print("\nYour stats are: {0.health} hp {0.power} atk {0.speed} spd\n".format(player))
sleep(0.75)
while player.health > 0 and boss.health > 0:
#Loop runs while both the player and Suru have hp left
#The following if code sparks the requested problem
if player.speed >= boss.speed:
#If the player's speed is greater than the bosses
if player.attack(boss)== 'mercy':
kind = True
break
#If the player input returns 'mercy' then the battle will stop
#However the results don't show as the code moves on to repeat itself in the next line
elif player.attack(boss) in ('heal'. 'no_mercy'):
pass
#If the player chooses to heal then the battle will continue as normal, however Suru won't be able to attack the player that turn
#The same happens with this line
elif player.attack(boss)==('attack'):
wait = 'no'
#If the player chooses to fight then the battle will commence as normal, however Suru will no be able to attack throughout the rest of the battle
#This line prints out the results but the results end up being inaccurate as 'mercy' or 'heal' aren't taken into effect
#End of problem if code
if boss.health <= 0:
break
#If Suru is dead (health lower than 0) then the battle ends
print ("{0.name} has {0.health} hp.\n".format(boss))
#Displays a message telling the user the hp stsatus of the enemy
sleep(0.5)
if boss.name == 'Suru':
if wait == 'yes':
b_response = random.choice(["Suru is watching you.", "Suru seems hesitant to strike."])
print(b_response)
#Suru skips it's turn
else:
boss.attack(player)
# Suru attacks the player
if player.health <= 0:
break
#If the player is dead (health lower than 0) then the battle ends
print("You have {0.health} hp.\n".format(boss))
sleep(1)
#Displays the player's health
if kind:
print("\nYou part ways with {0.name}.\n\n".format(boss))
sleep(1)
else:
if player.health > 0:
print("\nYou killed {0.name}.".format(boss))
print()
sleep(1)
elif boss.health > 0:
print("\nThe {0.name} has killed you.".format(boss))
print()
if __name__ == '__main__':
#Allows for the main script to be run and variables to be created and entered
players = (Player("Hero", 100, 18, 50, 300, 100, 50, 0, 0))
#Player list allows for the player to have stats that can be changed
bosses = [Boss("Feng", 100, 15, 70, 50, 100, 8), Boss("Shen", 150, 15, 80, 100, 150, 5),
Boss("Suru", 200, 20, 20, 200, 200, 3), Boss ("Mlezi", 45, 10, 60, 0, 45, 6)]
boss_battle(players, bosses[2])
#Allows for Suru boss battle
This is what the code displays:
(Not sure how to display a picture, sorry)
Suru confronts you!
Suru has 200 hp.
Your stats are: 100 hp 18 atk 50 spd
What move would you like to make (fight, heal or mercy)? fight (user input)
You attack the Suru.
What move would you like to make (fight, heal or mercy)? fight
(it repeats due to the second line of the if code even though it's not supposed to)
You attack the Suru.
What move would you like to make (fight, heal or mercy)? heal
(it repeats a third time due to the third line of the if code even though it's not supposed to)
You try to heal your wounds
You have 100 hp.
Suru has 110 hp.
Suru is watching you.
You have 110 hp.
(Finally displays the result but is in accurate as i initially entered 'fight' which causes Suru to attac'; however, i change the final input to 'heal' to show how the code doesn't accept the first input (even though it should) and only accepts the third input (despite the fact the third and second inputs shouldn't exists)
What move would you like to make (fight, heal or mercy)?
(Repeats for the next turn)
I've already tried researching my question on stackflow, and other sites, using phrases such as 'how to check multiple conditions in if statement' (since i thought a way to anwser my question was to have python check the condtions in one line, limiting the amount of elifs and player.attack()) however this only turned up with ways to make multiple if statments / condtions more visualy appealing). Another search i tried was 'if statement in user defined function' but that didn't give me the results that i needed (or found useful).
I would really appreciate any feedback, not only on my code (however this is what's mainly addressed) but also any feedback on way i could improve my overall code (not as important).
Thanks in advance
It sounds like you're just asking how to store a value in a variable:
attack_result = player.attack(boss)
if attack_result == 'mercy':
# etc.
elif attack_result in ('heal, no_mercy'):
# etc.
elif attack_result == 'attack':
# etc.
This way, instead of calling player.attack(boss) over and over, we just call it once, and then use the result over and over.
While we're at it, as pointed out by David in a comment, in ('heal, no_mercy') probably isn't testing what you wanted.
'heal, no_mercy' is a single string. So ('heal, no_mercy') is a single string wrapped in meaningless parentheses. It's very different from ('heal', 'no_mercy'), which is a pair of strings.
What you've written will do a substring search—which will actually be true for heal and no_mercy, but also for mercy and al and any other substring. You probably want to change it to:
elif attack_result in ('heal', 'no_mercy'):

Tracking player health text based game - Python

I am having a hard time tracking and adjusting player["health"] I created a function reduce_health(): that lowers the player health based on enemy attack, but when I run a fight the health does not work. I just simply win the fight or when I edit the code it just goes back and forth attacking forever. How can I create a function the reduce health and tracks it after each attack until the player dies? Note the code may not look well structured because I'm still working on it and will clean it up after I figure this out.
import random
import time
#Tracking weapon and player health
player = {"weapon":None, "health": None}
#function for questions avoide repeat
def ask(question):
answer = input(question + " [y/n]")
return answer in ["y", "Y", "Yes", "YES", "yes"]
def game_over():
print ("You Lose")
#initial question to start
print ("The adventures Of Magical Nadia")
#Question to start game or end game
if ask("Do you wish to embark on this great adventure?"):
print ("You have accepted the adventure. God Speed my young rass!")
player["health"] = 100
else:
print ("You are a coward and shall not in bark on a great adventure!")
#Dic of all the weapons in the game
WEAPONS = {
"Spear": (3, 10), None:(1,3), "knife":(4,16), "Gun":(16,25), "Glass Bottle":(4,16)
}
#to give the player weapons code
#player["weapon"] = "Spear"
#Enemys type
enemy = {"name":None, "health":None, "attack":None }
Gaint_spider = {"name":"Spider","health":(10), "attack":(7, 10) }
Dogs = {"name":"Dogs","health": (50), "attack":(4,15)}
Dragon = {"name":"Dragon","health": (150), "attack":(35,45)}
def reduce_health():
healthcheck = int(player["health"])
enemyattack = int("enemy_damage")
player["health"] = healthcheck - enemyattack
print (player["health"])
if player["health"] <= 0:
game_over()
#Function each fight gives random dmg, have a player and enemy, winner and loser
def combat (player, enemy):
player_damage = random.randint (*WEAPONS[player["weapon"]])
enemy_damage = random.randint(*enemy["attack"])
player_win = player_damage >= enemy["health"]
enemy_win= enemy_damage >= player["health"]
return player_damage, player_win , enemy_damage, enemy_win
#Structure of a fight
Sample_FIGHT = {
"player_damage": "You desperately try to stop the %s for %i damage",
"enemy_damage": "%s gores you for %i damage",
"player_win": "The %s collapses with a thunderous boom",
"enemy_win": "You are squished"
}
# describe the fight in a function
def describe_combat(player, enemy, fight_description,reduce_health):
player_damage, player_win , enemy_damage, enemy_win = combat(player, enemy)
print (fight_description["player_damage"] % (enemy["name"], player_damage))
time.sleep(1.0)
print (fight_description["enemy_damage"] % (enemy["name"], enemy_damage) )
return reduce_health
if player_win:
print (fight_description["player_win"] % enemy["name"])
return True
if enemy_win:
print (fight_description["player_win"] % enemy["name"])
return False
return None # fight is a draw
fight_result = describe_combat(player, Gaint_spider, Sample_FIGHT, reduce_health)
while fight_result is None:
describe_combat(player, Gaint_spider, Sample_FIGHT,reduce_health)
if True:
print ("You have won the fight")
else:
print ("You lost")
Returns this:
The adventures Of Magical Nadia
Do you wish to embark on this great adventure? [y/n] Y
You have accepted the adventure. God Speed my young rass!
You desperately try to stop the Spider for 2 damage
Spider gores you for 7 damage
You have won the fight
Goal:
The adventures Of Magical Nadia
Do you wish to embark on this great adventure? [y/n] Y
You have accepted the adventure. God Speed my young rass!
You desperately try to stop the Spider for 2 damage
Spider gores you for 7 damage
Player health 93
You desperately try to stop the Spider for 2 damage
Spider gores you for 7 damage
Player health 86
Instead of using a dict to store player attributes, why not use a class?
class Player:
def __init__(self, weapon, health):
self.weapon = weapon
self.health = health
def reduce_health(amount):
self.health -= amount
You can add additional methods like calculate_damage() to the Player class that take into account the type of weapon the player has. If you also create a monster class, your fight sequence could look something like
def fight():
monster.reduce_health(player.calculate_damage())
monster.check_dead()
player.reduce_health(monster.calculate_damage())
player.check_dead()
...

calling method outside class python

I'm relatively new to Python and OOP, I'm trying to write a mini adventure game with classes and have gotten stuck with my BattleEngine class.
The idea is to have options to fight or outwit an opponent based on your characters and the opponents 'strength' and 'wit'.
I get an error when I try to call my attack method here:
class Armory(Scene):
def enter(self):
print "This room appears to be some kind of armory. There are shelves full of weaponry lining"
print "the walls. You walk around admiring the shiny weapons, you reach out to pick up a"
print "massive battleaxe. Right as your fingers touch it you hear voices from behind the"
print "next door. They sound like they're getting closer. As the voices grow nearer you must make"
print "a decision. Will you stand and fight (enter 'fight') or will you use your wit to outsmart"
print "your opponent(enter 'wit')?"
decision = raw_input("> ")
battle = BattleEngine()
if decision == "fight":
attack(self, Player.strength, 3)
if player_wins:
print "A man in light armour walks in and sees you with your sword drawn. A look of"
print "shock and disbelief is on his face. You act quickly and lunge at him."
print "The soldier struggles to unsheath his sword as you run him through."
print "He collapses to the ground wearing the same look of disbelief."
print "Your strength has increased by 1."
Player.strength += 1
elif decision == "wit":
outwit(self, Player.wit, 3)
Here is where I defined my BattleEngine class:
class BattleEngine(object):
def attack(self, player_strength, nonplayer_strength):
player_roll = randint(1,6)
nonplayer_roll = randint(1,6)
if (player_roll + player_strength) >= (nonplayer_roll + nonplayer_strength):
return player_wins
else:
return 'death'
def outwit(self, player_wit, nonplayer_wit):
player_roll = randint(1,6)
nonplayer_roll = randint(1,6)
if (player_roll + player_wit) >= (nonplayer_roll + nonplayer_wit):
return player_wins
else:
return 'death'
Once I get to this point in my program is receive the error that : 'attack global name is not defined'
I'm not sure what I'm doing wrong exactly. Any help would be fantastic!
You need to call attack on your BattleEngine instance, and you do not need to pass in self:
battle = BattleEngine()
if decision == "fight":
player_wins = battle.attack(Player.strength, 3)
Note that you need to receive the return value of the .attack() method.
The same applies to the .outwit() method further on:
elif decision == "wit":
player_wins = battle.outwit(Player.wit, 3)
You probably need to fix the return values in the .attack() and .outwit() methods; instead of return player_wins and return 'death', return True or False perhaps.
The self parameter is taken care of for you by Python, and will refer to the specific BattleEngine instance.
Not that you really need classes here, your BattleEngine() class currently has no per-instance state, for example. You don't use self in any of the methods, because there is nothing on the instance to refer to.

How to make a function change the True/False statements for other functions

Here's the basic idea I'm having trouble with: I'm trying to make a simple game where you are in one room and you have 2 rooms branching from the first that need to be "completed" before continuing. I want the 2'nd and 3'rd room to change my original True statements to False statements that all need to be switched before proceeding in the game.
from sys import exit
def room_1():
print "You're in room one, there are two doors to room 2 and 3."
print "Where do you want to go?"
done_2=True
done_3=True
while True:
move=raw_input("'room 2' or 'room 3'? >")
if move == 'room 2':
room_2()
elif move == 'room 3':
room_3()
else:
print "not a valid answer"
print "You Win!"
exit(0)
def room_2():
print "You finished room 2!"
done_1=False
raw_input('Press button')
room_1()
def room_3():
print "You finished room 3!"
raw_input('press button')
done_3=False
room_1()
room_1()
How do I change the done_ statements from within rooms 2 and 3?
~
In Python, you have to declare global variables before you can assign to them; otherwise, any assignment will shadow the global variable.
def room_2():
global done_1 # <- right here
print "You finished room 2!"
done_1=False
raw_input('Press button')
room_1()
def room_3():
global done_3 # <- right here
print "You finished room 3!"
raw_input('press button')
done_3=False
room_1()
However!
This is generally bad style, especially for such a simple case as this. It makes it more difficult to reason about how your functions work, what they change, and in what order.
It'd be much easier, more readable, and simpler to just return True or False from your functions as needed.
If you try to think of your functions as "black boxes" which have inputs, and guarantee certain outputs, it will generally help in avoiding many confusing bugs that can arise.
You need to declare done_1 and done_2 as global variables, outside of function room_1()

Categories