Python 按类添加到列表中

Python 按类添加到列表中,python,class,python-2.7,Python,Class,Python 2.7,我做了个运动 编写一个名为GameBoard的类,其中包含名为Animal_list的属性 写入名为Add_animal的方法,该方法接收类型为animal的对象并将其添加到列表中 写一个接受指示的方法,从列表中随机选择一只动物,然后随机地将它变小 写一个方法,检查动物是否移动了另一个动物身上已经存在的点,也就是吃它的动物,他们从列表animal_列表中取出了死去的动物,并代表死去的动物将其添加到新的死亡动物列表中 这是我的代码: animal_list=[] dead_animal=[] c

我做了个运动 编写一个名为GameBoard的类,其中包含名为Animal_list的属性

写入名为Add_animal的方法,该方法接收类型为animal的对象并将其添加到列表中

写一个接受指示的方法,从列表中随机选择一只动物,然后随机地将它变小

写一个方法,检查动物是否移动了另一个动物身上已经存在的点,也就是吃它的动物,他们从列表animal_列表中取出了死去的动物,并代表死去的动物将其添加到新的死亡动物列表中

这是我的代码:

animal_list=[]
dead_animal=[]

class Point:
    def __init__(self):
        self.x=0
        self.y=0
    def __str__(self):
        return ("location (%d,%d)"% self.x,self.y)



class Animall:
    def __init__(self):
        self.spped=0
        self.location = Point()
        self.name =""
    def __self__(self):
        return "%s,%d,%d,%d"%    (self.name,self.spped,self.location.x,self.location.y)

    def __str__(self):
        return "name = %s ,spped : %d , location (%d,%d)"% (self.name,self.spped,self.location.x,self.location.y)


class GameBoard(Animall):
    def __init__(self):
    #animal_list=[]
    #dead_animal=[]
    pass


def add_animall(self,a):
    animal_list.append([[a.location.x,a.location.y],a.name])


def move_random(self,b):
    r =randint(0,len(animal_list))-1
    if b== "up":
        animal_list[r][0][1] += randint(0,10)
    if b == "down":
        animal_list[r][0][1] -= randint(0,10)
    if b=="left":
        animal_list[r][0][0] -= randint(0,10)
    elif b == "right":
        animal_list[r][0][0] += randint(0,10)
    x=0
    for i in animal_list:
        if animal_list[r][0][0] == i[0][0] and animal_list[r][0][1] == i[0][1]:
            dead_animal.append([animal_list[r][1],[animal_list[r][0][0],animal_list[r][0][1]]])
            animal_list.remove(animal_list[x])
        else:
            for i in animal_list:
                if i[1]==animal_list[r][1]:
                     i[0]=[animal_list[r][0][0],animal_list[r][0][1]]

        x+=1



a = Animall()
a.name="a"
c= Animall()
c.name="c"
d =Animall()
d.name="d"
e= Animall()
e.name="e"
b = GameBoard()
b.add_animall(a)
b.add_animall(c)
b.add_animall(d)
b.add_animall(e)

b.move_random("left")
print animal_list,dead_animal
b.move_random("right")
print animal_list,dead_animal
b.move_random("up")
print animal_list,dead_animal
b.move_random("down")
print animal_list,"\n",dead_animal
这就是输出

[[[0, 0], 'c'], [[0, 0], 'e']] [['a', [-8, 0]], ['c', [0, 0]]]
[[[0, 0], 'e']] [['a', [-8, 0]], ['c', [0, 0]], ['c', [4, 0]]]
[] [['a', [-8, 0]], ['c', [0, 0]], ['c', [4, 0]], ['e', [0, 1]]]
Traceback (most recent call last):
  File "my file address", line 87, in   <module>
    b.move_random("down")
  File "my file address", line 44, in  move_random
    animal_list[r][0][1] -= randint(0,10)
IndexError: list index out of range
[[0,0],[c'],[0,0],[e'][[a',[-8,0]],[c',[0,0]]
[0,0],[e'][a',[8,0]],[c',[0,0]],[c',[4,0]]
[][a'、[-8,0]]、['c'、[0,0]]、['c'、[4,0]]、[e'、[0,1]]
回溯(最近一次呼叫最后一次):
文件“我的文件地址”,第87行,在
b、 随机移动(“向下”)
文件“我的文件地址”,第44行,在move_random中
动物列表[r][0][1]=randint(0,10)
索引器:列表索引超出范围

为什么他将e、d位置与A、C位置的顺序不同?

你的
游戏板
类应该是

class GameBoard():
    def __init__(self):
        animal_list = []
    def add_animall(self,a):
        animal_list.append([[a.location.x,a.location.y],a.name])
那么你会使用它如下

a = Animal1()
g = GameBoard()
g.add_animall(a)

错误告诉您不能调用
GameBoard。添加animall
,它需要该类的实际实例来调用方法。

在GameBoard中,您需要设置一个空的
动物列表
,然后需要使用
self
访问该列表 此外,我认为您不需要继承这里的
Animall
,但可能误解了

class GameBoard():
    def __init__(self):
        self.animal_list = []

   def add_animal(self,a):
        self.animal_list.append([[a.location.x,a.location.y],a.name])
使用该类时,首先创建一个实例,然后对该实例调用该方法:

a = Animal()
gb = GameBoard()
gb.add_animal(a)

我还将拼写改为animal thoughout,因为我觉得它有点混乱

“我犯了一个错误”什么错误?您可以发布错误的完整回溯吗?您得到了什么错误,在哪一行?“TypeError:必须以GameBoard实例作为第一个参数调用unbound方法add\u animall()(改为获得animall实例)”