亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 pygame 中用我旋轉和移動的汽車旋轉我的碰撞箱?

如何在 pygame 中用我旋轉和移動的汽車旋轉我的碰撞箱?

溫溫醬 2023-03-16 16:44:43
我正在嘗試將我的汽車精靈與另一個矩形碰撞,現在我正在使用矩形碰撞和我的汽車碰撞視頻,但每次我旋轉碰撞箱時它都會移動到其他地方。有沒有辦法讓我的汽車精靈與那個碰撞箱發生碰撞,而不是使用汽車碰撞箱?我的汽車等級:            class Car:              def __init__(self, x, y, height, width, color):              self.x = x - width / 2            self.y = y - height / 2            self.height = height            self.width = width            self.color = color            self.rect = pygame.Rect(x, y, height, width)            self.surface = pygame.Surface((height, width), pygame.SRCALPHA)            self.surface.blit(img, (0, 0))            self.angle = 250            self.speed = 0# 2            self.hitbox = (self.x + 90, self.y + 620, 370, 63)# CARS HITBOX            pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)            def draw(self): #3            self.rect.topleft = (int(self.x), int(self.y))            rotated = pygame.transform.rotate(self.surface, self.angle)            surface_rect = self.surface.get_rect(topleft = self.rect.topleft)            new_rect = rotated.get_rect(center = surface_rect.center)            window.blit(rotated, new_rect.topleft)            self.hitbox = (self.x, self.y, 70, 40)# CARS HITBOX BLITTING AT car x, y            pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)# draw the car hitbox            white = (255, 255, 255)            car1 = Car(100, 630, 73, 73, white)# 4我的完整代碼在這里
查看完整描述

1 回答

?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

如何在 pygame 中用我旋轉和移動的汽車旋轉我的碰撞箱?

你不知道。你必須使用pygame.mask.Maskoverlap()。掩碼可以從創建pygame.Surfacepygame.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)


查看完整回答
反對 回復 2023-03-16
  • 1 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號