Reputation: 3
I am making a text style adventure game and don't have too much knowledge with python. I am trying to use my (enemyHealth) which has a random integer assigned to it. I've set this variable to be global and I want to use it within my function. But before the function is created I want to print out the variable outside of the function. I am wanting these to be the same value and am unsure what I am supposed to do. The function I am referring to is my (attack_function()).
Heres's a section of my code.
#enemy
enemyEasy = ["goblin archer", "thieves", "ghouls", "goblin swordsman"]
encounter = random.choice(enemyEasy)
global enemyHealth
enemyHealth = random.randint(9, 13)
#attack functions
attackLow = ("attack")
pursuadeDown = ("pursuade")
fleeDown = ("flee")
options = ["Attack", "Pursuade", "Flee"]
#conditions for correct grammar
if (encounter == enemyEasy[1]) or (encounter == enemyEasy[2]):
print ("\nYou walk down a path isolated when you are approached by a group of figures in the distance")
time.sleep(2)
print ("\nThe figures is a group of " + encounter + "!")
time.sleep(2)
else:
print ("\nYou walk down a path isolated when you are approached by a figure in the distance")
time.sleep(2)
print ("\nThe figure is a " + encounter + "!")
time.sleep(2)
#first encounter
print ("\nEnemy Health:", enemyHealth)
time.sleep(1)
print("\nWhat do you do?: ")
print (options)
action = input()
#attack function
def attack_function():
while (enemyHealth > 0):
if (enemyHealth > 0):
#miss function
missChance = random.randint(1,8)
if (missChance > 6):
print ("You attack the enemy and miss\n")
time.sleep(3)
else:
#random user damage from 1 - 6
userDamage = random.randint(1, 6)
print ("You attack the", encounter, "and did", userDamage, "damage!")
enemyHealth = (enemyHealth - userDamage)
print ("Enemy Health:", enemyHealth, "\n")
time.sleep(2)
if (enemyHealth > 0):
missChance = random.randint(1,8)
if (missChance > 6):
print("The enemy attacks you and misses\n")
time.sleep(3)
else:
#random enemy damage from 3 - 4
enemyDamage = random.randint(3, 4)
print ("The enemy attacked you and did", enemyDamage, "damage!")
health = (health - enemyDamage)
print ("Health:", health, "\n")
time.sleep(3)
else:
#XP System
xpGain = random.randint(2, 4)
XP = (XP + xpGain)
#enemy defeat
print ("\nYou defeated the enemy!")
print ("You gained", xpGain, "XP!")
if (XP == 10):
level = (level + 1)
print("you are now level 2")
userDamage = (random.randint(1, 6) + 3)
print ("XP:",XP)
break
#if action is to attack use attack function
if (action == options[0]) or (action == attackLow):
attack_function()
Upvotes: 0
Views: 72
Reputation: 736
Might I suggest using classes?
import random
class Enemy:
def __init__(self) -> None:
self.type = random.choice(["goblin archer", "thieves", "ghouls", "goblin swordsman"])
self.health = random.randint(9, 13)
You can then replace your first 4 lines (everything under #enemy) with:
enemy = Enemy()
This assumes you've either imported Enemy from another file or put the Enemy class at the very top. You can then access, update, etc., the health with enemy.health
Edit: as an FYI, you should not need to use the global keyword here, as long as you define enemy
in the top-most scope (outside all your functions).
Edit:
self
refers to the object. You only use self in the __init__
function and then use whatever variable name you want outside of that. For your case:
import random
class Enemy:
def __init__(self) -> None:
self.type = random.choice(["goblin archer", "thieves", "ghouls", "goblin swordsman"])
self.health = random.randint(9, 13)
enemy = Enemy()
#attack functions
attackLow = ("attack")
pursuadeDown = ("pursuade")
fleeDown = ("flee")
options = ["Attack", "Pursuade", "Flee"]```
Upvotes: 1
Reputation: 77337
global
works differently than you may expect. You don't declare variables in python, they are created dynamically on assignment. At the global module level, assignments go to the global namespace.
But in a function its different. Assignment goes to the local function namespace. That's where global
comes in. global
is used in a function to override this rule. It tells that single function that an assignment to the variable should go to the global enclosing namespace, not the local.
So - use global
in the function, and do not use it in the global namespace.
Your code has a bunch of stuff unrelated to the problem. Python makes it easy to experiment and it helps to boil bigger problems into simple examples.
enemyHealth = 1
print("health in global", enemyHealth)
def attach_function():
print("health in function", enemyHealth)
global enemyHealth
enemHealth = 99
attach_function()
print("health in global", enemyHealth)
Upvotes: 0