Python PyGame PyGame.sprite.collide_掩码未检测到碰撞?

Python PyGame PyGame.sprite.collide_掩码未检测到碰撞?,python,python-3.x,pygame,pygame-surface,Python,Python 3.x,Pygame,Pygame Surface,我正在尝试构建一个简单版本的Flappy Bird。为了检测我的圆圈(Flappy Bird)和矩形(Pipes)之间的碰撞,我使用了pygame.sprite.collide_rect(),但我想要一种更好的处理碰撞的方法 但使用遮罩碰撞不会导致检测到碰撞。圆直接穿过矩形,就好像它不在那里一样 这是我的密码: bird_group = pygame.sprite.Group() pipe_group = pygame.sprite.Group() class Bird(pygame.spri

我正在尝试构建一个简单版本的Flappy Bird。为了检测我的圆圈(Flappy Bird)和矩形(Pipes)之间的碰撞,我使用了pygame.sprite.collide_rect(),但我想要一种更好的处理碰撞的方法

但使用遮罩碰撞不会导致检测到碰撞。圆直接穿过矩形,就好像它不在那里一样

这是我的密码:

bird_group = pygame.sprite.Group()
pipe_group = pygame.sprite.Group()

class Bird(pygame.sprite.Sprite):
    def __init__(self, x_loc, y_loc, velocity):
        super(Bird, self).__init__()
        self.velocity = velocity
        self.x_loc = x_loc
        self.y_loc = y_loc
        self.image = pygame.image.load(os.path.join(game_folder,"index2.png")).convert()
        self.image.set_colorkey(WHITE)
        self.image = pygame.transform.scale(self.image,(60,65))
        self.rect = self.image.get_rect()
        self.rect.center = (x_loc,y_loc)
    def update(self):
        self.rect.y += self.velocity
        self.velocity = self.velocity+1
        self.mask = pygame.mask.from_surface(self.image)
    def jump(self):
        self.velocity = -10
    def boundary_collison(self):
        if self.rect.bottom+100>=display_height or self.rect.top<=0:
            return True

class UpperPipe(pygame.sprite.Sprite):
    """docstring for UpperPipe"""
    def __init__(self, pipe_x, pipe_height, pipe_speed):
        super(UpperPipe, self).__init__()
        self.pipe_speed = pipe_speed
        self.image = pygame.Surface((pipe_width, pipe_height))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.x = (pipe_x)
        self.rect.y = (0)
    def update(self):
        self.rect.x -= self.pipe_speed
        self.mask = pygame.mask.from_surface(self.image)
    def x_cord(self):
        return self.rect.x

class LowerPipe(pygame.sprite.Sprite):
    """docstring for UpperPipe"""
    def __init__(self, pipe_x, pipe_height, pipe_speed):
        super(LowerPipe, self).__init__()
        self.pipe_speed = pipe_speed
        self.image = pygame.Surface((pipe_width, display_height-(pipe_gap+pipe_height)))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.x = (pipe_x)
        self.rect.y = (pipe_height+pipe_gap)
    def update(self):
        self.rect.x -= self.pipe_speed
        self.mask = pygame.mask.from_surface(self.image)
    def x_cord(self):
        return self.rect.x
为了在我的游戏循环中进行检测,我使用以下代码:

bird_hits = pygame.sprite.spritecollide(bird,pipe_group,False,pygame.sprite.collide_mask)
if bird_hits:
    gameExit = True

您尚未在类中定义任何碰撞遮罩:类似

self.mask = pygame.mask.from_surface(image) 
因此,如果上下管道的rect对应于这些管道的hitbix:只需使用

bird_hits = pygame.sprite.spritecollide(bird,pipe_group,False)
或者在
类中创建要使用的self.mask

bird_hits = pygame.sprite.spritecollide(bird,pipe_group,False,pygame.sprite.collide_mask)

传递到
mask.from_Surface
pygame.Surface
s必须具有alpha通道。这意味着您必须调用曲面的或方法

class UpperPipe(pygame.sprite.Sprite):

    def __init__(self, pipe_x, pipe_height, pipe_speed):
        super(LowerPipe, self).__init__()
        self.pipe_speed = pipe_speed
        # Either call `convert_alpha` ...
        self.image = pygame.Surface((pipe_width, display_height-(pipe_gap+pipe_height))).convert_alpha()
        self.image.fill(GREEN)
        # ... or call `set_colorkey`.
        # I think surfaces converted with convert_alpha are blitted faster.
        # self.image.set_colorkey((0, 0, 0))

        # You also don't have to create the mask repeatedly in the
        # update method. Just call it once in the __init__ method.
        self.mask = pygame.mask.from_surface(self.image)

self.mask=pygame.mask.from_surface(self.image)是在类的更新函数中定义的。请编辑您的问题并提供答案。这将使我们更容易分析程序并找到bug。
class UpperPipe(pygame.sprite.Sprite):

    def __init__(self, pipe_x, pipe_height, pipe_speed):
        super(LowerPipe, self).__init__()
        self.pipe_speed = pipe_speed
        # Either call `convert_alpha` ...
        self.image = pygame.Surface((pipe_width, display_height-(pipe_gap+pipe_height))).convert_alpha()
        self.image.fill(GREEN)
        # ... or call `set_colorkey`.
        # I think surfaces converted with convert_alpha are blitted faster.
        # self.image.set_colorkey((0, 0, 0))

        # You also don't have to create the mask repeatedly in the
        # update method. Just call it once in the __init__ method.
        self.mask = pygame.mask.from_surface(self.image)