将具有“自我”的类的实例传递给另一个类,并在那里也使用“自我”

时间:2019-08-19 05:28:13

标签: python oop object-oriented-analysis

我想将我在代码#1中初始化的蛇发送到代码#2中另一个类的另一个文件中,并将其添加到self.snakes列表中

代码#1 :(蛇类)

def __init__(self, keys, game, pos, color, direction):
    self.keys = keys
    self.game = game
    self.pos = pos
    self.color = color
    self.direction = direction
    game_manager.GameManager.add_snake(self, self)
    self.cells = [pos]
    pass

def get_head(self):
    return cells[len(cells)-1]
    pass

def next_move(self):
    def f(pos, direction):
        return {
            'UP': (pos[0]+1, pos[1]),
            'DOWN': (pos[0]-1, pos[1]),
            'LEFT': (pos[0], pos[1]-1),
            'RIGHT': (pos[0], pos[1]+1)
        }[direction]
    self.pos = f(self.pos, self.direction)
    pass

def handle(self, keys):
    if len(keys) != 0:
        if keys[0] in self.keys:
            direction_key = ''
            for i in keys:
                if keys[i] == self.direction:
                    direction_key = i
            change_dir = keys[0] + direction_key
            not_valid_change = ['ws', 'sw', 'ad', 'da']
            if change_dir not in not_valid_change:
                self.direction = self.keys[keys[0]]
    pass

代码2:(GameManager类)

def __init__(self, size, screen, sx, sy, block_cells):
    self.size = size
    self.screen = screen
    self.sx = sx
    self.sy = sy
    self.block_cells = block_cells
    self.turn = 0
    self.snakes = []
    pass

def add_snake(self, snake):
    self.snakes.append(snake)
    pass

def get_cell(self, pos):
    def in_range(valid, pos):
        if valid > pos and pos >= 0:
            return True
        return False
    return pos if in_range(self.size[0], pos[0]) and in_range(self.size[1], pos[1]) else None

def kill(self, killed_snake):
    self.snakes.remove(killed_snake)
    pass

def handle(self, keys):
    for snake in self.snakes:
        snake(keys)
    for snake in self.snakes:
        snake.next_move()
    self.turn += 1
    pass

我知道,在我发送的代码#1(自身,自我)中,并在逻辑上在GameManager中的add_snake中,“自我”将指向Snake类实例而不是GameManager。但是self.snakes = []在GameManager中。

我很困惑。这是错误: AttributeError:“蛇”对象没有属性“蛇”

1 个答案:

答案 0 :(得分:0)

我建议使用一个包含蛇的类变量。然后可以添加蛇干旱静态方法。您所有的GameManager实例将具有相同的蛇列表

class Snake:
    def __init__(self, keys, game, pos, color, direction):
        self.keys = keys
        self.game = game
        self.pos = pos
        self.color = color
        self.direction = direction
        GameManager.add_snake(self)
        self.cells = [pos]


class GameManager:
    snakes = []

    def __init__(self, size, screen, sx, sy, block_cells):
        self.size = size
        self.screen = screen
        self.sx = sx
        self.sy = sy
        self.block_cells = block_cells
        self.turn = 0
        self.snakes = []

    @staticmethod
    def add_snake(snake):
        GameManager.snakes.append(snake)
        pass


if __name__ == '__main__':
    m = GameManager("m", "m", "m", "m", "m")

    s1 = Snake("s1", "s1", "s1", "s1", "s1")
    s2 = Snake("s1", "s1", "s1", "s1", "s1")

    print(GameManager.snakes)