我如何在这堂课中找到角度?

时间:2019-04-13 10:35:42

标签: python class pygame

如何在此类中访问角度以在其他地方使用?

我想从课堂上获得这个角度,并用它以与玩家面对的相同角度射击子弹。

这是课程:



class Player(pygame.sprite.Sprite):

    def __init__(self, pos):
        super().__init__()
        self.image = pygame.Surface((50, 30), pygame.SRCALPHA)
        pygame.draw.polygon(self.image, pygame.Color('steelblue2'), [(0, 0), (50, 15), (0, 30)])
        self.original_image = self.image
        self.rect = self.image.get_rect(center=pos)
        self.pos = Vector2(pos)
        self.velocityx = Vector2(6, 0)
        self.velocityy = Vector2(0, 6)

    def update(self):
        self.rect.center = (int(self.pos.x), int(self.pos.y))
        clamp_rect = self.rect.clamp(screen.get_rect())
        if clamp_rect != self.rect:
            self.rect = clamp_rect
            self.pos.x, self.pos.y = self.rect.center
        self.rotate()

    def rotate(self):
        direction = pygame.mouse.get_pos() - self.pos
        radius, angle = direction.as_polar()
        self.image = pygame.transform.rotate(self.original_image, -angle)
        self.rect = self.image.get_rect(center=self.rect.center)


0 个答案:

没有答案