pygame精灵冲突检测不起作用

时间:2019-06-15 12:39:43

标签: python pygame sprite collision-detection

当我尝试检测两个矩形之间的碰撞时 hit = pygame.sprite.groupcollide(enemy_sprites, player_sprites, 1, 1)仅在启动程序时打印一次“ HIT”

我也试图检测与 hit = pygame.sprite.spritecollide(player_sprites, enemy_sprites, 1),但出现此错误:

  

回溯(最近通话最近):
  
中的文件“ D:/Python/PyCharm/test/src/Main/test.py”,第104行   hit = pygame.sprite.spritecollide(player_sprites,敌人_sprites,1)
  spritecollide中的文件“ D:\ Programme \ python \ lib \ site-packages \ pygame \ sprite.py”,第1523行   ''''spritecollide = sprite.rect.colliderect   AttributeError:“组”对象没有属性“矩形”

    import pygame


class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)

        # def colours
        self.GREEN = (0, 255, 255)
        self.BLACK = (0, 0, 0)

        self.playerx = x
        self.playery = y
        self.playerW = width
        self.playerH = height
        self.vel = 5

        self.player_image = pygame.Surface((self.playerW, self.playerH))
        self.player_image.fill(self.GREEN)
        self.rect = self.player_image.get_rect()

    def handleKeys(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_UP]:
            if self.playery >= self.vel:
                self.playery -= self.vel
        elif keys[pygame.K_DOWN]:
            if self.playery + 50 <= screenH - self.vel:
                self.playery += self.vel
        elif keys[pygame.K_LEFT]:
            if self.playerx >= self.vel:
                self.playerx -= self.vel
        elif keys[pygame.K_RIGHT]:
            if self.playerx + 50 <= screenW - self.vel:
                self.playerx += self.vel
        else:
            pass

    def drawPlayer(self, surface):
        player_sprites.update()

        surface.fill(self.BLACK)

        surface.blit(self.player_image, (self.playerx, self.playery))


class Enemy(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)

        self.enemyX = x
        self.enemyY = y
        self.enemyW = width
        self.enemyH = height

        self.RED = (255, 0, 0)

        self.enemy_image = pygame.Surface((self.enemyW, self.enemyH))
        self.enemy_image.fill(self.RED)
        self.rect = self.enemy_image.get_rect()

    def drawEnemy(self, surface):
        enemy_sprites.update()

        surface.blit(self.enemy_image, (self.enemyX, self.enemyY))


# def some variables
pygame.init()

clock = pygame.time.Clock()

screenW = 500
screenH = 500
playerx = 200
playery = 200

wn = pygame.display.set_mode((screenW, screenH))
pygame.display.set_caption("Test program")

# player
player_sprites = pygame.sprite.Group()
player = Player(100, 100, 50, 50)
player_sprites.add(player)

# enemy
enemy_sprites = pygame.sprite.Group()
enemy = Enemy(200, 200, 50, 50)
enemy_sprites.add(enemy)

# main loop
run = True
while run:

    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    hit = pygame.sprite.groupcollide(enemy_sprites, player_sprites, 1, 1)

    if hit:
        print("HIT")
        hit = False

    player.handleKeys()
    player.drawPlayer(wn)
    enemy.drawEnemy(wn)

    pygame.display.flip()

pygame.quit()

1 个答案:

答案 0 :(得分:1)

pygame.sprite.groupcollide比较组中pygame.sprite.Sprite对象中的.rect属性。

还要注意,如果pygame.sprite.groupcollide()的第二个和第三个参数不是False,则在碰撞时将“杀死”精灵并将其从组中删除。
您可以通过传递False来避免这种情况:

hit = pygame.sprite.groupcollide(enemy_sprites, player_sprites, False, False)

初始化或更改位置后,您必须更新.rect属性:

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)

        # [...]

        self.player_image = pygame.Surface((self.playerW, self.playerH))
        self.player_image.fill(self.GREEN)
        self.rect = self.player_image.get_rect()

        self.rect.topleft = (self.playerx, self.playery)  # <--- init .rect.topleft

    def handleKeys(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_UP]:
        # [...]        

        self.rect.topleft = (self.playerx, self.playery) # <---- update .rect.topleft
class Enemy(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)

        # [...]

        self.enemy_image = pygame.Surface((self.enemyW, self.enemyH))
        self.enemy_image.fill(self.RED)
        self.rect = self.enemy_image.get_rect()

        self.rect.topleft = (self.enemyX, self.enemyY)   # <--- init .rect.topleft

或者,您可以分别使用self.playerx self.playery来删除self.enemyXself.enemyYself.rect.xseld.rect.y。参见pygame.Rect