0

How can I access the angle in this class for use somewhere else?

I would like to get this angle from the class and use it for shooting a bullet along the same angle as the player is facing.

Here is the class:



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)


domefist
  • 79
  • 3
  • 1
    In the `rotate` method do, `self.radius, self.angle = direction.as_polar()`, and now it's an `instance` attribute and will be available from the `instance` `object`. And a `self.angle = 0` in `__init__` is recommended ( Good to initialize attributes of the instance) – han solo Apr 13 '19 at 10:37
  • Great. Thank you. Should I initialise the radius too? – domefist Apr 13 '19 at 10:42
  • If you don't need it, then no need to make `radius` and instance attribute :) – han solo Apr 13 '19 at 10:49

0 Answers0