首页 文章

pygame rect碰撞小于图像

提问于
浏览
3

如何在pygame中定义比图像小的矩形碰撞检测?我想像第二张图像一样有碰撞模式,但是当尝试在方法rect中设置宽度和高度时,我有一个切割图像 .

enter image description here

当我尝试使用图像大小设置时,我将碰撞检测设为红色

self.rect = pygame.rect.Rect(location, self.image.get_size())

如果我使用宽度和高度设置大小,我只有第三个图像

self.rect = pygame.rect.Rect(location, (32, 150))

我真的不想使用像素完美碰撞,因为是最慢的碰撞检测,所以有人知道如何使用Rect实现第二次图像碰撞的方法?谢谢 .

2 回答

  • 0

    您似乎正在使用 sprite 模块中内置的pygames . (如果我错了,请纠正我)

    您可能知道每个精灵都包含 image (在表面上绘制)和 rect object (设置 image 的位置和大小(!)) .

    正如Luke Taylor建议的那样,你可以在 player 类中创建一个 new rect 对象......

    self.collideRect =  pygame.rect.Rect((0, 0), (32, 150))
    

    ...并将其位置(根据您的图形)设置为

    self.collideRect.midbottom = self.rect.midbottom
    

    每次更改 player 的位置时,您也必须调用此行,因此您的 self.collideRect rect对象"moves"与您的 player 在屏幕上 .

    要测试点(例如鼠标坐标)是否在 self.collideRect 内,请调用

    if self.collideRect.collidepoint(x, y) == True:
        print("You clicked on me!")
    
  • 1

    尝试绘制一个与图像后面的图像分开的全新矩形,但是如果图像的位置经常设置为该位置 .

相关问题