我在类中定义的属性收到属性错误。我怎样才能解决这个问题?

时间:2019-12-10 04:48:48

标签: python class pygame attributeerror

这是我得到的错误:

 File "/home/ore/PycharmProjects/testing grounds/venv/Moses and the Snakes.py", line 209, in <module>
    if stick.y - stick.radius < snake.the_hitbox[1] + snake.the_hitbox[3] and stick.y + stick.radius > snake.the_hitbox[1]:
AttributeError: type object 'snake' has no attribute 'the_hitbox'

这是我所有相关的代码:

class snake(object):
    rle_slither = [pygame.image.load(os.path.join(path, 'pixil-frame-2(3).png')),
                   pygame.image.load(os.path.join(path, 'pixil-frame-1(3).png')),
                   pygame.image.load(os.path.join(path, 'pixil-frame-0(4).png'))]
    lle_slither = [pygame.transform.flip(pygame.image.load(os.path.join(path, 'pixil-frame-2(3).png')), True, False),
                   pygame.transform.flip(pygame.image.load(os.path.join(path, 'pixil-frame-1(3).png')), True, False),
                   pygame.transform.flip(pygame.image.load(os.path.join(path, 'pixil-frame-0(4).png')), True, False)]

    def __init__(self, x, y, wid, len, end):
        self.x = x
        self.y = y
        self.wid = wid
        self.len = len
        self.end = end
        self.path = [self.x, self.end]
        self.walk_count = 0
        self.speed = 15
        self.the_hitbox = (self.x, self.y + 5, 78, 25)

    def movement(self):
        if self.speed > 0:
            if self.x + self.speed < self.path[1]:
                self.x += self.speed
            else:
                self.speed = self.speed * -1
                self.walk_count = 0
        else:
            if self.x - self.speed > self.path[0]:
                self.x += self.speed
            else:
                self.speed = self.speed * -1
                self.walk_count = 0

    def animate(self, screen):
        self.movement()
        if self.walk_count + 1 >= 9:
            self.walk_count = 0
        if self.speed > 0:
            screen.blit(self.rle_slither[self.walk_count // 3], (self.x, self.y))
            self.walk_count += 1
        else:
            screen.blit(self.lle_slither[self.walk_count // 3], (self.x, self.y))
            self.walk_count += 1
        self.the_hitbox = (self.x, self.y + 5, 78, 25)
        pygame.draw.rect(screen, (150, 0, 0), self.the_hitbox, 2)

    def got_hit(self):
        print("Snake was hit!")
    for stick in staff_list:
        if stick.y - stick.radius < snake.the_hitbox[1] + snake.the_hitbox[3] and stick.y + stick.radius > snake.the_hitbox[1]:
            if stick.x + stick.radius > snake.the_hitbox[0] and stick.x - stick.radius < snake.the_hitbox[0] - snake.the_hitbox[2]:
                snake.got_hit()
                staff_list.reomve(stick)

1 个答案:

答案 0 :(得分:1)

snake替换为selfsnake是类的名称,而self是对该类实例的引用。

此代码有三件事:

  1. 您正在尝试在其自己的定义块中使用snake类(因为最底部的for循环中的缩进)
  2. 您正在尝试使用该类的名称作为该类的实例。实例化一个实例后,这样做将替换类定义。
  3. 您从未创建snake的实例。根据经验,您应该使用PascalCase为类命名,以便轻松将它们与类实例区分开。

请参见以下修补程序:

# Pascal case
class Snake(object):
    rle_slither = [pygame.image.load(os.path.join(path, 'pixil-frame-2(3).png')),
                   pygame.image.load(os.path.join(path, 'pixil-frame-1(3).png')),
                   pygame.image.load(os.path.join(path, 'pixil-frame-0(4).png'))]
    lle_slither = [pygame.transform.flip(pygame.image.load(os.path.join(path, 'pixil-frame-2(3).png')), True, False),
                   pygame.transform.flip(pygame.image.load(os.path.join(path, 'pixil-frame-1(3).png')), True, False),
                   pygame.transform.flip(pygame.image.load(os.path.join(path, 'pixil-frame-0(4).png')), True, False)]

    def __init__(self, x, y, wid, len, end):
        self.x = x
        self.y = y
        self.wid = wid
        self.len = len
        self.end = end
        self.path = [self.x, self.end]
        self.walk_count = 0
        self.speed = 15
        self.the_hitbox = (self.x, self.y + 5, 78, 25)

    def movement(self):
        if self.speed > 0:
            if self.x + self.speed < self.path[1]:
                self.x += self.speed
            else:
                self.speed = self.speed * -1
                self.walk_count = 0
        else:
            if self.x - self.speed > self.path[0]:
                self.x += self.speed
            else:
                self.speed = self.speed * -1
                self.walk_count = 0

    def animate(self, screen):
        self.movement()
        if self.walk_count + 1 >= 9:
            self.walk_count = 0
        if self.speed > 0:
            screen.blit(self.rle_slither[self.walk_count // 3], (self.x, self.y))
            self.walk_count += 1
        else:
            screen.blit(self.lle_slither[self.walk_count // 3], (self.x, self.y))
            self.walk_count += 1
        self.the_hitbox = (self.x, self.y + 5, 78, 25)
        pygame.draw.rect(screen, (150, 0, 0), self.the_hitbox, 2)

    def got_hit(self):
        print("Snake was hit!")

# Assuming staff_list is defined

# Instantiate a new Snake() instance and call it `snake`
snake = Snake()

for stick in staff_list:
    if stick.y - stick.radius < snake.the_hitbox[1] + snake.the_hitbox[3] and stick.y + stick.radius > snake.the_hitbox[1]:
        if stick.x + stick.radius > snake.the_hitbox[0] and stick.x - stick.radius < snake.the_hitbox[0] - snake.the_hitbox[2]:
            snake.got_hit()
            staff_list.reomve(stick)