简体   繁体   English

我的python类有问题

[英]I have a issue with python classes

Ok so i'm making a text based RPG game but i have a issue with python classes that i'm not sure how to fix I've tried looking up this problem but haven't been able to find any posts that pertain to this maybe i'm looking up the wrong things? 好的,所以我正在做一个基于文本的RPG游戏,但是我遇到了python类的问题,我不确定如何解决我尝试了查找此问题,但是找不到与该问题有关的任何帖子也许我在找错东西? i don't know. 我不知道。 anyways here's my issue i have this class in a file called unit.py in a folder named subfiles 无论如何,这是我的问题,我在名为subfiles的文件夹中的名为unit.py的文件中拥有此类

class Enemy(object):
    def __init__(self,name,hp,atk):
        self.name = name
        self.hp = hp
        self.atk = atk

in my main file i have this 在我的主文件中,我有这个

from subfiles.unit import *
wolf = Enemy("Wolf", 100, 10)
cow = Enemy("Cow", 50, 0)
NMElist = [wolf,cow]
def gennme():
    global NME
    ##Choose a enemy to fight
    NME = random.choice(NMElist)
def attack():
    NME.hp -= 10
    print NME.hp
attack()

when I run the attack definition it subtracts from the wolf.hp like it should, but that's not exactly what i'm trying to achieve, because the next time a battle comes up against a wolf it will be already dead so how can i have it where wolf = Enemy("Wolf", 100, 10) is just the set out default values and after the battle is over and the wolf is killed, the next time i fight a wolf it loads from the defaults. 当我运行攻击定义时,它会像应有的那样从wolf.hp中减去,但这并不是我想要达到的目标,因为下次与狼进行战斗时,它已经死了,我怎么能在其中wolf = Enemy(“ Wolf”,100,10)只是设定的默认值,在战斗结束并且狼被杀死之后,下次我与狼战斗时,它将从默认值加载。 I hope i'm making sense here i'm sure there's a simple way to achieve this its just evading me perhaps my understanding of how classes work is flawed? 我希望我在这里有道理,我敢肯定有一种简单的方法可以做到这一点,这只是在逃避我的理解,也许我对类的工作方式的理解是有缺陷的? anyways I look forward to your responses ans suggestions oh by the way this same thing applies for cow as well as wolf. 无论如何,我期待着您的回复和建议,哦,这同样适用于牛和狼。

Yeah, maybe it's practice to create a new Wolf after one is killed. 是的,也许是一种做法,那就是在杀死一头狼后再制造一头新狼。 But you don't have a alive flag, so is it alive? 但是您没有活着的旗帜,那么它还活着吗?


You could have the enemy deafult to the deafult hp if the hp is 0. In a manner that lets you check if the wolf is killed. 如果hp为0,则可能使敌方对hp无效。这种方式可以让您检查狼是否被杀死。 A way to do this: 一种方法是:

class Enemy(object):
    def __init__(self,name,hp,atk):
        self.name = name
        self._hp = hp
        self.atk = atk
        self._deafult_hp = hp

    @property
    def hp(self):
        return self._hp

    @hp.setter
    def hp(self, value):
        if not self._hp:
            self._hp = self._deafult_hp
        self._hp = value

Now in the battle you can check if the wolfs hp is 0, and in the next battle the wolfs hp will deafult to the 100 hp or what ever you choose. 现在,在战斗中,您可以检查狼的hp是否为0,在下一次战斗中,狼的hp将默认为100 hp或您选择的任何值。 ^^ ^^

You could have separate instances of Enemy created each time a new one is encountered. 每次遇到新的敌人时,您可以创建一个单独的Enemy实例。 You can defer creation of the actual instances by wrapping them in a callable: 您可以通过将实际实例包装在可调用对象中来推迟实际实例的创建:

NMElist = [lambda : Enemy("Wolf", 100, 10),
           lambda : Enemy("Cow", 50, 0)]

This is a simple example of an enemy factory . 这是一个敌人工厂的简单例子。 To get a new random enemy you would need to call the function returned to generate a new cow or wolf: 要获得新的随机敌人,您需要调用返回的函数以生成新的牛或狼:

NME = random.choice(NMElist)()

Note the parentheses () at the end which executes the random enemy factory returned by random.choice to create the actual object. 请注意最后的括号() ,该括号执行random.choice返回的随机敌人工厂以创建实际对象。 Another layer you could add would be to use inheritance to create Wolf and Cow subclasses of enemy that initialize the proper values, using the super python builtin: 您可以添加的另一层是使用super Python内置函数,通过继承来创建敌人的WolfCow子类,以初始化适当的值:

class Enemy(object):
   def __init__(self,name,hp,atk):
       self.name = name
       self.hp = hp
       self.atk = atk

class Wolf(Enemy):
    def __init__(self):
        super(Wolf, self).__init__("Wolf", 100, 10)

class Cow(Enemy):
    def __init__(self):
        super(Cow, self).__init__("Cow", 50, 0)

And then NMElist can contain classes that get instantiated for each enemy encountered 然后NMElist可以包含为遇到的每个敌人实例化的类

NMElist = [Wolf,Cow]
def gennme():
    global NME
    ##Choose a enemy to fight
    NME = random.choice(NMElist)()

Your random function will choose among the already existing uniq objects contained in NMElist (with their previous status) 您的随机函数将在NMElist中包含的现有uniq对象中进行选择(具有其先前的状态)

Intantiating wolf and cow in gennme function may fix this. 加强牛和牛的基因组功能可以解决此问题。

from subfiles.unit import *

def gennme():
    global NME
    ##Choose a enemy to fight
    wolf = Enemy("Wolf", 100, 10)
    cow = Enemy("Cow", 50, 0)
    nme_list = [wolf,cow]
    NME = random.choice(nme_list)
def attack():
    NME.hp -= 10
    print NME.hp
attack()

This is far from being the best implementation but It might give you a better understanding of your mistake. 这远非最佳的实现方式,但可以使您更好地理解自己的错误。

I like Arthur's answer, but another way of doing the same thing is with this: 我喜欢亚瑟的答案,但是另一种做同一件事的方法是:

NMElist = [("Wolf", 100, 10),("Cow", 50, 0)]

Then, inside your gennme() function, select one randomly and pass the values inside of it to the Enemy class: 然后,在您的gennme()函数中,随机选择一个,并将其中的值传递给Enemy类:

enemySelection = random.choice(NMElist)
NME = Enemy(*enemySelection)

Basically, this approach would be good if you want to isolate the implementation of the Enemy() class from the values that are associated with the Enemy attributes. 基本上,如果要将Enemy()类的实现与与Enemy属性关联的值隔离开来,则此方法会很好。 Using the * , you can pass the list item values all together as arguments to the Enemy class. 使用* ,可以将列表项的值一起作为参数传递给Enemy类。 By instantiating the enemy at this point, you will be sure to generate another one fresh when the gennme() function is called again. 通过在这一点上实例化敌人,您将确保在再次调用gennme()函数时生成另一个新gennme()

People answer fast on SO. 人们对SO的回答很快。 It took me awhile to work this one out :) 我花了一段时间才解决这个问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM