0

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

2
  • 2
    One way: encapsulate all methods in a game state class and make rounds a member attribute. Commented Feb 26, 2019 at 17:26
  • I think the variables inside a function block are only accessible from inside that function you’d have to return it as a global variable. Don’t quote me on this Commented Feb 26, 2019 at 17:28

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.