1 回答

TA貢獻1893條經驗 獲得超10個贊
如何在 pygame 中用我旋轉和移動的汽車旋轉我的碰撞箱?
你不知道。你必須使用pygame.mask.Mask
和overlap()
。掩碼可以從創建pygame.Surface
并pygame.mask.from_surface()
包含有關不透明像素的信息。overlap()
在相交的 2 個蒙版中找到 1 個不透明像素。
例如:
class Car:
# [...]
def draw(self):
# [...]
self.mask = pygame.mask.from_surface(rotated)
self.mask_rect = new_rect
obstacle_mask = pygame.mask.from_surface(obstacle_surface)
offset_x = obstacle_rect.x - car1.mask_rect.x
offset_y = obstacle_rect.y - car1.mask_rect.y
if car1.mask.overlap(obstacle_mask, (offset_x, offset_y)):
print("hit")
請注意, 的偏移量參數overlap()是從該掩碼到另一個掩碼(到第一個參數)的偏移量。它是參數中從掩碼到另一個掩碼的向量。如果self的位置是 ( x1 , y1 ) 而other的位置是 ( x2 , y2 ) 那么偏移量是 ( x2 - x1 , y2 - y1 )。
如果障礙物只是一個矩形并且您沒有 Surface ( ),那么您可以從( ) 對象obstacle_surface生成一個完全不透明的 Mask :pygame.Rectobstacle_rect
obstacle_mask = pygame.mask.Mask(obstacle_rect.width, True)
添加回答
舉報