I have this bit of code
health = 12
manager_health = 10
def manager_boss_fight_used_for_backup():
sleep(1)
print("manager health is ",manager_health)
print("your health is ",health)
sleep(1)
print("the boss gets first attack")
manager_boss_damage = random.randint(1,8)
print(manager_boss_damage)
print(health)
health = health - manager_boss_damage
sleep(1)
print("the boss did",manager_boss_damage,"damage")
sleep(1)
print("your health is now",health)
if health <= 0:
sleep(1)
print("you have died")
sleep(1)
print("better luck next time")
exit()
sleep(1)
print("your turn to attack")
sleep(1)
heal_or_attack = input("Do you wish to heal or attack?(1/2)")
if heal_or_attack == "1":
healing = random.randint(1,7)
print("you have healed by",healing)
health = health + healing
manager_boss_fight_used_for_backup()
if heal_or_attack == "2":
print("you attack the boss")
sleep(1)
attack_damage = random.randint(1,6)
print("You did",attack_damage,"damage")
manager_health = manager_health - attack_damage
if manager_health <= 0:
sleep(1)
print("You have killed the boss")
sleep(1)
if manager_health > 0:
manager_boss_fight_used_for_backup()
and in the parts of code where for example health = health - manager_boss_damage it will error out. I have fiddled with global variables and all that but I cant get it to work so I came here. Any answers appreciated!
As probably everyone will tell you, using global variables is pretty bad. Why don't you use OOP instead? You can create a class Manager: and a class Player: with health as property and damage and take_damage as a method of the class, for example:
class Manager:
def __init__(self):
self.health = 10
def attack(self):
damage = ramdom.randint(1,8)
return damage
def take_damage(self, damage):
self.health -= damage
if (self.health <= 0):
self.health = 0
return self.health
then the Player class would be more or less the same:
class Player:
def __init__(self):
self.health = 12
def attack(self):
damage = ramdom.randint(1,8)
return damage
def take_damage(self, damage):
self.health -= damage
if (self.health <= 0):
self.health = 0
return self.health
def heal(self, amount):
self.health += amount
return self.health
Ideally of course, you can create one class called "Actor" or "Enemy" and derive those from there, but let's stick with this for now:
# Instantiate the classes like this:
manager = Manager()
player = Player()
# and then just invoke the methods from the classes:
sleep(1)
print("manager health is ",manager.health)
print("your health is ",player.health)
sleep(1)
print("the boss gets first attack")
damage = manager.Attack()
print("You were hit by {} points".format(damage))
player.take_damage(damage)
print("Your Health is {}".format(player.health))
Etc.
Whenever you make an assignment to health inside the manager_fight_used_for_backup() function, Python attempts to assign a new variable called health by default.
The fastest fix is to use the global keyword at the beginning of the function body, to make it clear that you're referring to the health variable outside of the function:
global health
This will tell Python that you're referring to the health variable outside of the function. However, the best solution is to refactor this code so that functions don't modify a shared global state.
Related
I'm writing a simple Python program which is a text-based fighting simulator, and I'm using classes to allow myself to create heroes and enemies. I've created an attack function within my hero class, but it doesn't seem to be subtracting the enemy health from the hero strength like I want it to.
Here's the code:
import random
class Enemy:
eName = "Name"
eHealth = 0
eStrength = 0
def __init__ (self, eName, eHealth, eStrength):
self.eName = eName
self.eHealth = eHealth
self.eStrength = eStrength
def attack (self):
print("The enemy attacked you and dealt", self.eStrength, "damage!")
Hero.health -= self.eStrength
def __repr__(self):
if self.eName == "Zombie":
return "Zombie"
elif self.eName == "Skeleton":
return "Skeleton"
else:
return "Spider"
class Hero:
name = "Name"
health = 0
strength = 0
def __init__ (self, name, health, strength):
self.name = name
self.health = health
self.strength = strength
def attack(self, enemy):
print("You attacked", enemy, "for", self.strength, "damage!\n")
Enemy.eHealth -= self.strength
print(enemy, "now has", enemy.eHealth, "health points left!\n")
print("Welcome to my fighting simulator!")
hName = input("Please input your character's name:\n")
hHealth = int(input("Please enter your hero's amount of health points (10-25):\n"))
hStrength = int(input("Please enter your hero's amount of strength points (2-4): \n"))
character = Hero(hName, hHealth, hStrength)
zombie = Enemy("Zombie", 25, 3)
skeleton = Enemy("Skeleton", 15, 4)
spider = Enemy("Spider", 20, 2)
randEnemy = random.randint(1, 3)
if randEnemy == 1:
print("\nYour enemy will be a zombie!\n")
chosenEnemy = zombie
elif randEnemy == 2:
print("\nYour enemy will be a skeleton!\n")
chosenEnemy = skeleton
else:
print("\nYour enemy will be a spider!\n")
chosenEnemy = spider
while True:
if character.health == 0:
print("You died!")
elif chosenEnemy.eHealth == 0:
print("You won!")
action = input("What would you like to do? (h = heal, a = attack): ")
if (action == 'a') or (action == 'A'):
character.attack(chosenEnemy)
The main things that need to be seen are the variables in the Enemy class, the attack function in the Hero class, and the input variables for the heroes stats.
You just had a typo. In the attack() method, you wrote
Enemy.eHealth -= self.strength
instead of
enemy.eHealth -= self.strength
And since you are unable to subtract health from the Enemy class, the described problem that you mentioned occurs.
So, I have recently got into coding and I am currently developing a small turn-based RPG, but I have encountered some real issue with the battle system. I am still learning and I never thought about asking questions here. Anyway, after getting many things done correctly, I have encountered this issue where using the defend command rises the player hp for some reason. Here is the code:
import random
import sys
import os
class Entity():
def __init__(self, hp, atk, dif):
self.hp = hp
self.atk = atk
self.dif = dif
class Battle():
def Attack(self, attacker, defender):
damage = attacker.atk - defender.dif
defender.hp -= damage
def Combat(self, player, enemy):
turns = []
while True:
while len(turns) < 5:
turn = random.randint(1, 2)
if turn == 1:
turns.append("player")
else:
turns.append("enemy")
for current_turn in turns:
print(player.hp, enemy.hp)
if current_turn == "player":
print(f"TURNS: \n{current_turn}\n{turns[turns.index(current_turn)+ 1:]}")
choice = input("1. Attack\n2. Defend\n")
if choice == "1":
self.Attack(player, enemy)
player.dif = 20
elif choice == "2":
player.dif *= 2
else:
print("Lost your chance!")
elif current_turn == "enemy":
print("Enemy turn!")
print(f"Next turns are: {turns}")
enemy_choice = random.randint(1, 2)
if enemy_choice == 2:
print("He attacks you!")
self.Attack(enemy, player)
enemy.dif = 0
else:
print("He defends himself.")
enemy.dif = 10
os.system("clear")
turns.pop(0)
if player.hp <= 0:
print("You died!")
sys.exit(0)
elif enemy.hp <= 0:
print("YOU WON!")
sys.exit(0)
break
charachter = Entity(100, 15, 15)
boss = Entity(300, 40, 0)
testbattle = Battle()
testbattle.Combat(charachter, boss)
In your Battle.Attack method:
def Attack(self, attacker, defender):
damage = attacker.atk - defender.dif
defender.hp -= damage
If attacker.atk - defender.dif is a negative number, then you'll be subtracting a negative number from defender.hp, increasing it.
If I'm the player, and my defense starts out as 15, and then I defend, my defense will become 30 because of player.dif *= 2. If I defend again the next turn, my defense will be 60, which is greater than the boss' attack of 40. So, if the boss then attacks me in Battle.Attack, we would get damage = 40 - 60, which is damage = -20, effectively healing the player by twenty points.
The entity will be healed if the attackers attack value is less than the defender's defence value. Perhaps instead of subtracting the 2 values you could use a defence divisor/ multiplier
still working on my Text RPG and have run into an error in my magic doing damage i was fallowing the same format as my melee damage but then realized that i had a separate function to determine best_weapon.damage and in my spells I am just trying to pull the damage value from the spell object to modify the enemy hp.
Here is my magic.py file iv only got 2 spells for testing purposes
import player
class Spell:
def __init__(self):
raise NotIMplementedError("Do not create raw spell objects!")
def __str__(self):
return self.name
class magic_missle(Spell):
def __init__(self):
self.name = "Magic Missle"
self.discription = "A bolt of condensed magical power that you fling at an opponent."
self.damage = 15
self.mana = 10
class fire_ball(Spell):
def __init__(self):
self.name = "Fire Ball"
self.discription = "A ball of fire that explodes on impact."
self.damage = 20
self.mana = 15
this is the combat function I'm having issues with the line enemy.hp =- magic.damage which i thought would pull the damage value of the previously selected spell but i just keep getting the error module object has no value magic.
def attack(self):
spell = [magic_spell for magic_spell in self.spell_book if isinstance(magic_spell,magic.Spell)]
if not spell:
print("You have no spells to cast!")
user_input = input('What do you want to attack with? Melee or Magic: ')
if user_input == str('magic'):
for i, magic_spell in enumerate(spell, 1):
print("Choose a spell to cast: ")
print("{}. {}".format(i,magic_spell))
valid =False
while not valid:
choice = input("")
try:
if self.mana == 0:
print("You dont have enough mana")
else:
room = world.tile_at(self.x,self.y)
enemy = room.enemy
print("You use {} against {}!".format(spell,enemy.name))
enemy.hp -= magic.damage
self.mana = self.mana - spell.mana
if not enemy.is_alive():
print("You killed {}!".format(enemy.name))
else:
print("{} HP is {}.".format(enemy.name,enemy.hp))
except(ValueError,IndexError):
print("Invalid choice, try again")
elif user_input == str('melee'):
best_weapon = self.most_powerful_weapon()
room = world.tile_at(self.x,self.y)
enemy = room.enemy
print("You use {} against {}!".format(best_weapon.name,enemy.name))
enemy.hp -= best_weapon.damage
if not enemy.is_alive():
print("You killed {}!".format(enemy.name))
else:
print("{} HP is {}.".format(enemy.name,enemy.hp))
thanks for any help.
If you run the code you should see that the print says "14" for example, but It retracts something else from enemies HP.
Calculating attack damage for each "spell":
from random import randint
import time
class Player(object):
def __init__(self, health):
self.health = health
#staticmethod
def use_heal():
return randint(9, 21)
#staticmethod
def attack_slice():
return randint(5, 29)
#staticmethod
def attack_bash():
return randint(11, 18)
class Enemy(object):
def __init__(self, health):
self.health = health
#staticmethod
def enemy_attack():
return randint(9, 19)
For setting HP:
player = Player(100)
enemy = Enemy(100)
The loop that is the "game":
while True:
print(f"Your hp: {player.health}\nEnemy hp: {enemy.health}\n")
print("(1) Bash _ (2) Slice _ (3) Heal")
attack_choice = int(input(">>"))
if attack_choice == 1:
print(f"You hit for {Player.attack_bash()}")
enemy.health -= Player.attack_bash()
elif attack_choice == 2:
print(f"You hit for {Player.attack_slice()}")
enemy.health -= Player.attack_slice()
elif attack_choice == 3:
print(f"You heal for {Player.use_heal()}")
player.health += Player.use_heal()
when it calls Player.attack_* it returns a random value to print, and then calls it a second time to actualy damage the enemy so it is likely a defarent value. what it should do is call it once, store it in a variable and use the variable
while True:
print(f"Your hp: {player.health}\nEnemy hp: {enemy.health}\n")
print("(1) Bash _ (2) Slice _ (3) Heal")
attack_choice = int(input(">>"))
if attack_choice == 1:
damage = Player.attack_bash()
print(f"You hit for {damage}")
enemy.health -= damage
elif attack_choice == 2:
damage = Player.attack_slice()
print(f"You hit for {damage}")
enemy.health -= damage
elif attack_choice == 3:
damage = Player.use_heal()
print(f"You heal for {damage}")
player.health += damage
The problem is you are generating two random numbers for each case: The one that gets print and the one that gets subtracted/added.
...
print(f"You hit for {Player.attack_bash()}") # Generates a random number
enemy.health -= Player.attack_bash() # Generates another random number
...
You need to use a temporary variable so you can use the same value twice:
...
damage = Player.attack_bash()
print(f"You hit for {damage}")
enemy.health -= damage
...
Ive just started python yesterday and tried to make a randomized fighting game for bets with friends. I think this script could be alot shorter, but i dont know how. Ive just googled everything. plr1_hit and plr2_hit are mostly the same. Is there a way to make it just one thing? Like i said, im fairly new to python. Any help would be very appreciated. Thanks in advance!
import random
import time
dmg = random.randrange(1, 40)
plr2_health = 100
plr1_health = 100
plr2_live = True
plr1_live = True
def plr1_hit():
global plr1_health
global plr2_health
global plr2_live
dmg = random.randrange(1, 40)
plr2_health = plr2_health - dmg
if plr2_health < 1:
plr2_live = False
print("-----------------")
print("PLAYER 1 LAYS DOWN THE FINAL PUNCH")
print("Player 2 had died. Player 1 wins!")
else:
print("-----------------")
if dmg < 30:
if dmg < 15:
if dmg < 5:
hit = "pokes"
else:
hit = "slaps"
else:
hit = "punches"
else:
hit = "DESTROYS"
print("player 1 " + hit + " player 2")
print("--")
print("--")
print("Damage done:")
print(dmg)
print("--")
print("Player 1 health:")
print(plr1_health)
print("Player 2 health:")
print(plr2_health)
print("-----------------")
def plr2_hit():
global plr1_health
global plr2_health
global plr1_live
dmg = random.randrange(1, 40)
plr1_health = plr1_health - dmg
if plr1_health < 1:
plr1_live = False
print("-----------------")
print("PLAYER 2 LAYS DOWN THE FINAL PUNCH")
print("Player 1 had died. Player 2 wins!")
else:
print("-----------------")
if dmg < 30:
if dmg < 15:
if dmg < 5:
hit = "pokes"
else:
hit = "slaps"
else:
hit = "punches"
else:
hit = "DESTROYS"
print("player 2 " + hit + " player 1")
print("--")
print("--")
print("Damage done:")
print(dmg)
print("--")
print("Player 1 health:")
print(plr1_health)
print("Player 2 health:")
print(plr2_health)
print("-----------------")
while plr1_live == True and plr2_live == True:
plr1_hit()
time.sleep(5)
if plr1_live == True:
plr2_hit()
time.sleep(5)
This seems like a pretty natural place to use a class for your players. If you've not learned anything about Object Oriented Programming yet, this may seem complicated, but it's not really all that bad (though you might want to read a tutorial). A class is a convenient way to store some data (like the health of a player) along with some methods, which are functions that operate on that data.
Here's how I'd modify your plrX_hit functions so they were implemented by a single hit method of a Player class:
class Player:
def __init__(self, name): # the __init__ method sets up an instance
self.name = name
self.health = 100 # save the player stats as attributes, rather than globals
self.alive = True
def hit(self, hitby): # this works like your functions, self gets hit by hitby
dmg = random.randrange(1, 40)
self.health -= dmg
if self.health < 1:
self.alive = False
print("-----------------")
print(f"{hitby.name} LAYS DOWN THE FINAL PUNCH") # use the name variables
print(f"{self.name} has died. {hitby.name} wins!") # rather than hardcoding
else:
print("-----------------")
if dmg < 30:
if dmg < 15:
if dmg < 5:
hit_descr = "pokes"
else:
hit_descr = "slaps"
else:
hit_descr = "punches"
else:
hit_descr = "DESTROYS"
print(f"{hitby.name} {hit_desc} {self.name}")
print("--")
print("--")
print("Damage done:")
print(dmg)
print("--")
# the last part of the function, printing the health of both players,
# is moved out of the method, because it was always in a specific order
# (not related to who hit who).
player1 = Player("Player 1") # here we create two instances of the Player class
player2 = Player("Player 2")
while player1.alive and player2.alive: # you never need == True, just use the value
player1.hit(player2)
print("Player 1 health:")
print(player1.health)
print("Player 2 health:")
print(player2.health)
print("-----------------")
time.sleep(5)
if player1.alive:
player2.hit(player1)
print("Player 1 health:")
print(player1.health)
print("Player 2 health:")
print(player2.health)
print("-----------------")
time.sleep(5)
There are many possible ways to write this shorter and a bit more elegant.
You said yourself, that the two methods are mostly the same, so that's a clear sign, that you could combine them. All you need for a general hit_func() is a parameter you send when the function is called. With this parameter you could specify which player is hit.
And when you call the function, you do this alternating with player1, then with player2, etc.
Maybe something like this:
activePlayer = player1
while plr1_live == True and plr2_live == True:
hit(activePlayer)
time.sleep(5)
# change the player that is hit
if activeplayer == player1:
activeplayer = player2
else:
activeplayer = player1
In the hit function you then could use the passed in activeplayer to decide which health to reduce and what messages to print, etc.
Even more convenient you could write it with a player class and two objects for the players. Then every player object could have a hit method and the necessary variables like health, alive, etc. With this approach you could get rid of your global variables easily, which usually should be avoided.
class Player:
def __init__(self, health):
self.health = health
self.alive = True
def hit(self):
#generate random damage and reduce the health of self accordingly, etc
Then you can create two player instances and hit them as before in a loop like this:
player1 = Player(100)
player2 = Player(100)
while player1.alive and player2.alive:
#call the hit method alternating for both players
I hope that helps and gives you some ideas for improvements and new things to learn.