【问题标题】:pygame sprite collision with background elements [duplicate]pygame精灵与背景元素的碰撞[重复]
【发布时间】:2020-04-09 21:29:38
【问题描述】:

我一直在寻找解决方案,但任何其他线程都可以帮助我。

我一直在尝试让精灵在背景图像上移动。背景有透明的街道,应该是精灵的可能路径。

我需要检测精灵何时与不透明的背景图像的其他部分发生碰撞。我尝试了完美的碰撞方法,但我认为这不是我的情况的正确解决方案,因为background.rect 没有任何意义。 我也尝试了重叠方法,但它总是返回 true。

图片为 32 位深度,我打电话给convert_alpha()

class sprites(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.x = 200   
        self.y = 300
        self.img= player_sprite
        self.rect = player_sprite.get_rect()
        self.mask = pygame.mask.from_surface(player_sprite)


    def position(self):

        dx = mouse_x-self.x
        dy = self.y-mouse_y
        d = float((dx**2+dy**2)**0.5)

        displ_x = dx/d
        displ_y = dy/d
        self.x += displ_x
        self.y += displ_y

        if  type(self.mask.overlap(object_background.mask,(0,0))):  
            self.x -= displ_x
            self.y -= displ_y


class object_background_class(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.img = object_background_img
        self.rect = object_background_img.get_rect()
        self.mask = pygame.mask.from_surface(object_background_img.convert_alpha())

object_background = object_background_class()
player = sprites()


player.position() changes each time the coordinates of the sprite accordind to the mouse(x,y) and check if with the new x,y of the player make it collides with the background

game_state = False

while not game_state:

    for e in pygame.event.get():
        if e == pygame.QUIT:
            game_state = True
        if e.type == pygame.KEYDOWN and e.type == pygame.K_ESCAPE:
            game_state = True
        if e.type == pygame.KEYDOWN:
            if e.key == 27:
                game_state = True

    mouse_x, mouse_y = pygame.mouse.get_pos()

    player.position()

    DISPLAYSURFACE.blit(color_background, (0, 0))
    DISPLAYSURFACE.blit(player.img, (player.x, player.y))
    DISPLAYSURFACE.blit(object_background.img, (0, 0))


    pygame.display.flip()

【问题讨论】:

  • 你必须用街道画(paintshop)一个面具
  • 我已经尝试使用街道的像素颜色来检查精灵,但我正在寻找一种替代方法来检测是否有任何精灵边缘与街道的边界发生碰撞,而不仅仅是他的绳索 x,y。
  • 是否可以在不分割背景元素的情况下检测精灵和具有透明部分(街道)和不透明部分(障碍物)的背景图像元素之间的碰撞?
  • 我需要检测与不透明元素的碰撞,但我不能将它们溢出。

标签: python pygame


【解决方案1】:

游戏画面是一个很大的坐标平面,只要说玩家的x,y坐标超过或低于任何x,y坐标阈值而不是做某事

【讨论】:

  • 我无法映射背景元素的所有 x,y 线,因为障碍物是不规则的。
  • 你有没有想过使用精灵在屏幕上做一个阈值点,如果你在背景被blit之前将它blit到屏幕上,阈值精灵将是不可见的
【解决方案2】:

我也尝试过重叠方法,但它总是返回 true。

当然。 pygame.sprite.collide_mask() 使用 sprite 对象的 .rect.mask 属性进行碰撞检测。
移动播放器并更改播放器的self.xself.y 属性后,您必须更新self.rect

class sprites(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.x = 200   
        self.y = 300
        self.img= player_sprite
        self.rect = player_sprite.get_rect()
        self.mask = pygame.mask.from_surface(player_sprite)

    def position(self):

        dx = mouse_x-self.x
        dy = self.y-mouse_y
        d = float((dx**2+dy**2)**0.5)

        displ_x = dx/d
        displ_y = dy/d
        self.x += displ_x
        self.y += displ_y

        if  type(self.mask.overlap(object_background.mask,(0,0))):  
            self.x -= displ_x
            self.y -= displ_y

        self.rect.topleft = (round(self.x), round(self.y)) # <--- THIS IS MISSING

现在你可以使用collide_mask:

if pygame.sprite.collide_mask(player, object_background):
    # [...]

【讨论】:

  • 检查背景png。我怎样才能让我的精灵在透明的街道上移动?
  • background.rect 没有意义
猜你喜欢
  • 2017-09-14
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 2018-10-26
  • 2023-03-05
相关资源
最近更新 更多