访问其外部函数中的参数和变量(Python)

时间:2018-09-11 00:50:44

标签: python python-3.x

import time


def classBase(class_name, class_health, class_damage, class_hit_chance):
    print("You are a " + class_name + "!")

def enemyBase(enemy_name, enemy_health, enemy_damage, enemy_hit_chance, enemy_alive):
    time.sleep(2)
    print("Out of nowhere, a " + enemy_name + " appears!")
    enemy_total_health = enemy_health



print("Welcome to Blades of Goblonia!")
user_name = input("What is your name?")
type(user_name)
print("Hello, " + user_name + "!")
user_class_choice = input("""What class would you like to be?
A) Warrior
B) Hunter
C) Wizard""")
if user_class_choice == "A" :
    user_class = classBase("Warrior", 50, 7, 95)
elif user_class_choice == "B" :
    user_class = classBase("Hunter", 40, 10, 85)
elif user_class_choice == "C" :
    user_class = classBase("Wizard", 35, 12, 80)

enemyBase("Goblin", 30, 10 , 60, True)

time.sleep(1)

user_action_choice = input("""Would you like to
    A) Hit
    B) Run
    C) Heal""")

if user_action_choice == "A" :
    print("Hit")
elif user_action_choice == "B" :
    print("Run")
elif user_action_choice == "C" :
    print("Heal")

在我的游戏代码中,当我重新创建敌人时,我试图访问它并更改它的运行状况。要创建伤害效果,我需要更改敌人_总生命值,但是我不能引用该变量。我将如何编写代码以访问局部变量和参数?谢谢:)

3 个答案:

答案 0 :(得分:1)

出于您的目的,您应该将Enemy设为一个类,并将地精设为Enemy的一个实例,以便跟踪作为对象的敌人的所有属性:

class Enemy:
    def __init__(self, name, health, damage, hit_chance, alive):
        self.name = name
        self.health = health
        self.total_health = health
        self.damage = damage
        self.hit_chance = hit_chance
        self.alive = alive
        print("Out of nowhere, a " + name + " appears!")

goblin = Enemy("Goblin", 30, 10, 60, True)
print('%s has a total health of %d' % (goblin.name, goblin.total_health))

答案 1 :(得分:0)

您要问的主要答案:

  • 函数不能以您尝试使用的方式使用。你要上课。

其他提示:

  • 使用字典将类名映射到特定统计信息
  • 需要用户输入“ a”,“ b”,“ c”等时,创建一个def calc_average(server_name): temp = server_time_differences.copy() temp.pop(server_name) sum_ = sum(temp.values()) try: return sum_ / len(temp.keys()) except ZeroDivisionError: raise RuntimeError('No other server available to calculate average server time difference with %s.' % server_name) for server in server_time_differences: try: discrepancies[server] = abs(server_time_differences[server] - calc_average(server)) except RuntimeError as e: print(e) 函数以供重用。
  • 使用get_choice函数限制变量范围

已实施:

main()

答案 2 :(得分:0)

我认为您的方法应该更像是类层次结构,其中代表了游戏中角色的每个类别:

例如,一个Character可以代表一个通用字符,一个Warrior是一个可以杀死敌人的Character,一个Magician是一个Character会施放法术...

代码不必在每个类中重复,Character的属性可从其子类WarriorMagician访问;每个子类可以具有自己的属性,以及其特定功能(不同的动作)

也许是这样的:

class Character:

    def __init__(self, name, health=10, damage=0, hit_chance=.2, life_status=True):
        self.name = name
        self.health = health
        self.total_health = health
        self.damage = damage
        self.hit_chance = hit_chance
        self.life_status = life_status
        print('Out of nowhere, a ' + self.__class__.__name__ + ' named ' + self.name + " appears!")

    def action(self):
        return "does nothing"

    def meet_somebody(self):
        return self.name + " says hello," + ' and ' + self.action()


class Warrior(Character):
    def action(self):    
        return "kills enemy"


class Magician(Character):
    def action(self):    
        return "casts a spell"


if __name__ == '__main__':

    fred = Warrior('Fred')
    jack = Magician('Jack')

    print(fred.meet_somebody())
    print(jack.meet_somebody())

Out of nowhere, a Warrior named Fred appears!
Out of nowhere, a Magician named Jack appears!
Fred says hello, and kills enemy
Jack says hello, and casts a spell