简体   繁体   English

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

[英]I am getting an attribute error for an attribute defined inside the class. How can I fix this?

This is the error I'm getting:这是我得到的错误:

 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'

This is my all my relevant code:这是我所有的相关代码:

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)

Replace snake with self . self替换 snake snake is the name of the class, while self is the reference to instances of that class. snake是类的名称,而 self是对该类实例的引用。

Three things wrong with this code:这段代码有三处错误:

  1. You are trying to use the snake class in its own definition block (because of the indents in your for loop at the very bottom)您正在尝试在自己的定义块中使用蛇类(因为最底部的 for 循环中的缩进)
  2. You are trying to use the name of the class as an instance of that class.您正在尝试使用类的名称作为该类的实例。 Doing so will replace the class definition after you just instantiate one instant.这样做将在您实例化一个瞬间后替换类定义。
  3. You never created an instance of snake .您从未创建过snake的实例。 As a rule of thumb, you should name classes using PascalCase in order to easily differentiate them from class instances.根据经验,您应该使用 PascalCase 命名类,以便轻松地将它们与类实例区分开来。

See below for fix:请参阅下面的修复:

# 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)

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

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