我正在关注如何检测类玩家与精灵群体MudballGroup之间的冲突。当在语句pg.sprite.spritecollide(Player,mudballGroup,False)中设置碰撞时,我得到错误类型对象'Player'没有属性'rect'。我从我的播放器精灵的代码显示rect在下面的语句中定义:self.rect = self.image.get_rect()。我不知道为什么我得到这个错误。请有人帮忙。玩家与精灵群体之间的Pygame中的碰撞检测mudballGroup
class Player(pg.sprite.Sprite):
def __init__(self, game):
pg.sprite.Sprite.__init__(self)
self.game = game
self.walking = False
self.jumping = False
self.current_frame = 0
self.last_update = 0
self.load_images()
self.image = self.girl_standing[0]
#Isn't this my rect attribute for Player?
self.rect = self.image.get_rect()
self.rect.center = (WIDTH/2, HEIGHT/2)
self.pos = vec(WIDTH/2, HEIGHT/2)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
self.clock = pg.time.Clock()
def shoot(self):
mudball = MUDBALL(self.rect.centerx, self.rect.centery)
print("SHOOT function")
self.game.all_sprites.add(mudball)
mudballGroup = pg.sprite.Group()
mudballGroup.add(mudball)
# Me attempting collision
hits = pg.sprite.spritecollide(self.player, mudballGroup, False)
if hits:
print("HITS!!!!!!!!", hits)
def hailing(self):
jets = JETS()
print("FLYOVER")
self.game.all_sprites.add(jets)
jetsGroup = pg.sprite.Group()
jetsGroup.add(jets)
class MUDBALL(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = pg.image.load("SLIMEballGREEN.png")
# self.mudballGroup = pg.sprite.Group()
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.speedx = -30
def update(self):
self.rect.x += self.speedx
#kill if moves off screen
if self.rect.centerx < 0:
self.kill()
class JETS(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.image.load("JETSscaled.png")
#self.jetsGroup = pg.sprite.Group()
self.rect = self.image.get_rect()
self.rect.x = 1366
self.rect.y = 0
self.speedx = -70
def update(self):
self.rect.x += self.speedx
#kill if moves off screen
if self.rect.x + 848 < 0:
self.kill()
''Player''是一类。你需要制作一个实例来实际使用它。程序。请参阅https://learnpythonthehardway.org/book/ex40.html –
我宁愿推荐程序街机游戏[关于类和精灵的章节](http://programarcadegames.com/index.php?chapter=introduction_to_classes&lang=de#section_12 )(12和13)。 – skrx
请记住,您应该在这里发布[完整但很少的示例](http://stackoverflow.com/help/mcve)。我们需要我们可以复制,粘贴和运行的代码。 – skrx