PyGame pygame.sprite.collide_mask没有检测到碰撞?

时间:2018-07-02 17:35:50

标签: 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.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 = Bird(x_loc,y_loc,velocity)
bird_group.add(bird)

pipe_list = []
init_pipe_x = 500
for make in range(pipe_count):
    pipe_x = init_pipe_x+((between_pipe+pipe_width)*make)
    pipe_height = (round(random.uniform(0.2,0.8), 2))*(display_height-pipe_gap)
    upper = UpperPipe(pipe_x,pipe_height,pipe_speed)
    lower = LowerPipe(pipe_x,pipe_height,pipe_speed)
    add_pipe = [upper,lower]
    pipe_list.append(add_pipe)
    pipe_group.add(upper)
    pipe_group.add(lower)

为了在我的游戏循环中进行检测,我使用以下代码:

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

2 个答案:

答案 0 :(得分:0)

您尚未在课程中定义任何collide_mask:

self.mask = pygame.mask.from_surface(image) 

因此,如果您的上部和下部管道的rect对应于这些的斜线:只需使用

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

或在class中创建一个self.mask以使用

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

答案 1 :(得分:0)

您传递给pygame.Surface的{​​{1}}必须具有一个Alpha通道。这意味着您必须调用曲面的convert_alphaset_colorkey方法。

mask.from_surface