I'm making a "pong" style game, where the ball rotates 15 degrees every time it hits either the top or the bottom of the screen. My problem is that when rotating, it still moves in either x or y direction, and being new to pygame, I am unsure of how to keep the ball moving "forward" relative to itself, in the direction of the rotation, and was not sure of how to phrase the question in Google. The rough code:
class Ball():
def __init__(self) -> None:
self.x, self.y = display_width / 2, display_height / 2
self.raw_sprite = pygame.image.load("assets/ball.png")
self.raw_size = self.raw_sprite.get_size()
self.sprite = pygame.transform.scale(self.raw_sprite, (int(self.raw_size[0]/2), int(self.raw_size[1]/2)))
self.rect = self.sprite.get_rect()
self.width, self.height = self.sprite.get_width(), self.sprite.get_height()
self.rect.x = self.x
self.rect.y = self.y
def render(self) -> None:
screen.blit(self.sprite, (self.x,self.y))
def is_collided_with(self, sprite) -> bool:
return self.rect.colliderect(sprite.rect)
def move(self, direction) -> None:
speed = 5
if direction == "left":
self.x -= speed
else:
self.x += speed
def rotate(self) -> None:
angle = 15
self.sprite = pygame.transform.rotate(self.sprite, (angle))
self.rect = self.sprite.get_rect()
ball = Ball()
ball_direction = "left"
running = True
while running:
if (ball.y == (0 + ball.height)) or (ball.y == (display_height - ball.height)):
ball.rotate()
screen.fill(BLACK)
ball.render()
ball.move(ball_direction)
pygame.quit()
quit()
I've omitted a lot of the code and only left the necessary parts to show the issue.
Thanks for your time.