我查看了类似的问题,但我找不到问题的答案
我正在和Pygame一起玩游戏,但是我遇到了一个我没有与另一个人相处的错误。这个想法是这个代码返回一个名为'platform_group'的组中的精灵列表,它们正在触及Goon类的实例:
self.block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_group, False)
此行不断返回此错误:
line 251, in update
self.block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_group, False)
AttributeError: 'int' object has no attribute 'platform_group'
奇怪的是,我遇到了Goon类的这个问题,但没有使用Player类。它使用与上面完全相同的行,但它的工作方式与播放器完全相同。
播放器和Goon类的代码:
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([40, 40])
self.image.fill(RED)
self.rect = self.image.get_rect()
self.change_x = 0
self.change_y = 0
self.level = 0
self.block_hit_list = []
def update(self):
self.calc_grav()
self.rect.x += self.change_x
self.block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_group, False)
for block in self.block_hit_list:
if self.change_x > 0:
self.rect.right = block.rect.left
elif self.change_x < 0:
self.rect.left = block.rect.right
class Goon(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([25, 25])
self.image.fill(PURPLE)
self.rect = self.image.get_rect()
self.change_x = 0
self.change_y = 0
self.block_hit_list = []
self.rect.x = x
self.rect.y = y
self.level = 0
def update(self):
self.calc_grav
self.rect.x += self.change_x
self.block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_group, False)
for block in self.block_hit_list:
if self.change_x > 0:
self.rect.right = block.rect.left
self.change_y = -6
这是一些主要代码:
def main():
pygame.init()
size = [SCREEN_WIDTH, SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
global walls
walls = []
pygame.display.set_caption("Practice Game")
player = Player()
level_list = []
level_list.append(Level_01(player))
current_level_no = 0
current_level = level_list[current_level_no]
player.level = current_level
current_level.enemy_group.level = current_level
player.rect.x = player_spawn[0]
player.rect.y = player_spawn[1]
active_sprite_list.add(player)
done = False
clock = pygame.time.Clock()
这是Level_01的类:
class Level_01(Level):
def __init__(self):
Level.__init__(self)
self.level_limit = -1000
x = y = 0
level = ["WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W W",
"W WWWW W",
"W W",
"WW W",
"W WW W",
"W W WWWWWWW WWWW",
"W W",
"WWWWWWW WWW W",
"W W W",
"W WW",
"W WWW WWWWW",
"W W",
"W S WWG W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
]
for row in level:
for col in row:
if col == "W":
block = Platform(x, y)
self.platform_group.add(block)
if col == "S":
global player_spawn
player_spawn = [x, y]
if col == "G":
goon = Goon(x, y)
self.enemy_group.add(goon)
self.enemy_list.append(goon)
x += 40
y += 40
x = 0
谢谢!