这是我得到的错误:
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)
答案 0 :(得分:1)
将 snake
替换为self
。 snake
是类的名称,而self
是对该类实例的引用。
此代码有三件事:
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)