使用Python类制作游戏 - 如何更新自我初始化?

时间:2015-04-30 13:03:40

标签: python

我正在使用类系统在python上创建一个基于文本的游戏来跟踪主要角色的变化(比如它的名字)。我正在主要功能内部的主要角色类之外编写游戏的主要代码。

我很挣扎,因为我需要将Main Character类中的self.character_name更新为main函数内用户的输入。我不确定如何做到这一点,我有下面写的代码 - 但它没有更新Main Character类中的名称。我怎么能改写这个?

我还担心在尝试更新宠物时会遇到此问题,characters_known。但是,我似乎没有更新Health或XP ....

class Main_Character():

def __init__(self):
    self.health=100
    self.exp=0    
    self.level=0
    self.character_name=""
    self.characters_known={None}
    self.pets={None}
    self.progression_tracker=0

def __str__(self):
    return "Name: "+ str(self.character_name)+"  |  "+ "Health:"+ str(self.health) + "  |  " +"XP:"+ str(self.exp) + "  |  "+ "Level:"+ str(self.level)+"  |  "+"Pets:"+str(self.pets)

def Char_Name(self,name):
    if name.isalpha()==False:
        print("You entered a name containing non-alphabetic characters, pease reenter a new name:")
        main()
    elif len(name)>=10:
        print("You entered a name containing 10 or more characters, pease reenter a new name:")
        main()
    else:
        self.character_name=name


def Char_Level_Experience(self,exp,b):
    self.exp+=exp
    b=2
    if exp<=0:
        exp=1
    ans = 1
    level=0
    while ans<exp:
        ans *= b
        level += 1
    if ans == exp:
        self.level=level
        print("You have reached level", self.level)
    else:
        level = int(log(exp, 2))
        level = min(level, exp) 
        if level>=0:
            self.level=level
        else:
            level=0


def healing(self,heal):
    if self.health+heal>=100:
        self.health=100
    else:
        self.health+=heal


def other_answers(answer):
    if answer=='quit':
        raise SystemExit
    if answer=='pets':
        print("Pets owned:", Main_Character().pets)
        user_decision=input("Would you like to continue where you left off?    Type 'yes' to continue, or 'no' to go back to main menu")
        if user_decision=='yes':
            if Main_Character().progression_tracker==0:
                main()
            elif Main_Character().progression_tracker==1:
                choice1()
        if user_decision=='no':
                main()
        else:
            other_answers(user_decision)
    if answer=='characters':
        print("Characters met:", Main_Character().characters_known)
        user_decision=input("Would you like to continue where you left off? Type 'yes' to continue, or 'no' to go back to main menu:")
        if user_decision=='yes':
            if Main_Character().progression_tracker==0:
                main()
            if Main_Character().progression_tracker==1:
                choice1()
        if user_decision=='no':
                main()
        else:
            other_answers(user_decision)

def start_check():
    print("If you understand the game, type 'go' to continue- if not, type 'more information' to receive more information about how to play the game")
    begin_game=input("")
    if begin_game=="go":
        choice1()
    if begin_game=='more information':
        print("\n","The object of the game is to gain XP [experience points] without dying")
        start_check()
    else:
        other_answers(begin_game)

def choice1():
    Main_Character().progression_tracker=1
    print("You are a knight in the Kings Guard- the King has asked to meet with you about a very special mission")
    print("What would you like to do?")
    print("  1.Go Directly to King","\n", "2. Finish your dinner")
    choice=input("1 or 2?")
    if choice=="1":
        Main_Character().Char_Level_Experience(1,2)
    elif choice=="2":
        Main_Character().Char_Level_Experience(.5,2)
    else:
        other_answers(choice)
    print(Main_Character())

def main(): 
    print("Welcome!")
    unfiltered_name=input("Please enter the name of your character:")
    Main_Character().Char_Name(unfiltered_name)
    print("Welcome,", Main_Character().character_name,"!", "Here are your current stats!")
    print(Main_Character())
    start_check()

2 个答案:

答案 0 :(得分:2)

你还不太清楚类和实例是如何工作的。

当你需要一个新角色时,你就可以调用这个类。每次调用Main_Character()时,都会获得一个全新的实例 - 使用__init__中设置的默认值。如果你的每个朋友都有角色,你可以将它们称为一个时间。然后,您需要将每个实例保存在变量中,以便每次都可以再次引用它们。

所以,例如:

my_character = Main_Character()
unfiltered_name=input("Please enter the name of your character:")
my_character.Char_Name(unfiltered_name)
print("Welcome,", my_character.character_name,"!", "Here are your current stats!")
print(my_character)

答案 1 :(得分:0)

每次拨打Main_Character时,都会创建字符。相反,你应该叫它一次:

the_character = Main_Character()
...
the_character.name = "..."