如何在 pygame 中用旋转和移动的汽车旋转我的碰撞箱? [英] How can I rotate my hitbox with my rotating and moving car in pygame?

查看:83
本文介绍了如何在 pygame 中用旋转和移动的汽车旋转我的碰撞箱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的汽车精灵与另一个矩形碰撞,现在我正在使用矩形碰撞和我的汽车碰撞视频,但每次我旋转碰撞箱时,它都会移动到其他地方.

I am trying to collide my car sprite with another rect, right now I am using rect collisions and my car collision video, but each time I rotate the hitbox it moves somewhere else.

有没有办法让我的汽车精灵与那个碰撞箱碰撞而不是使用汽车碰撞箱?

Is there a way I could collide my car sprite with that hitbox instead of using a car hitbox?

我的汽车课:

            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

我的完整代码在这里

推荐答案

如何在 pygame 中使用旋转和移动的汽车旋转我的碰撞箱?

How can I rotate my hitbox with my rotating and moving car in pygame?

你没有.你必须使用 pygame.mask.Mask重叠().可以从 pygame.Surface 来自 pygame.mask.from_surface() 并包含有关不透明像素的信息.overlap() 在 2 个相交的蒙版中找到 1 个不透明像素.

You don't. You have to use pygame.mask.Mask and overlap(). A mask can be created from a pygame.Surface by pygame.mask.from_surface() and contains the information about the opaque pixels. overlap() finds the 1 opaque pixel in 2 masks, which are intersecting.

例如:

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).

Note, the offset argument of overlap(), is the offset from this mask to the other mask (to the 1st argument). It is the vector from the mask to the other mask in the argument. If the location of self is (x1, y1) and the location of other is (x2, y2) then the offset is (x2 - x1, y2 - y1).

如果障碍物只是一个矩形而您没有 Surface (obstacle_surface),那么您可以生成一个完全不透明的 Mask 形式 pygame.Rect (obstacle_rect)对象:

If the obstacle is just a rectangle and you don't have a Surface (obstacle_surface), then you can generate a completely opaque Mask form the pygame.Rect (obstacle_rect) object:

obstacle_mask = pygame.mask.Mask(obstacle_rect.width, True)

这篇关于如何在 pygame 中用旋转和移动的汽车旋转我的碰撞箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆