0
\$\begingroup\$

I'm writing a class in pygame to create a sprite object, and I'd like to be able to rotate it. It works fine with an image, and rotates without issue. But when rotating a surface with a plain colour, the box appears to grow and shrink. I know that this is a result of the surface changing size to fit the vertices of the rectangle inside, but how do I stop it? I'd like to see a visual rotation.

I've created some sample code to show the problem that I'm facing, running it causes the box to simply change in size.

import sys, pygame
from pygame.locals import *

pygame.init()
SCREEN = pygame.display.set_mode((200, 200))
CLOCK  = pygame.time.Clock()

surface = pygame.Surface((50 , 50))
surface.fill((0, 0, 0))
rotated_surface = surface
rect = surface.get_rect()
angle = 0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    SCREEN.fill((255, 255, 255))
    angle += 5
    rotated_surface = pygame.transform.rotate(surface, angle)
    rect = rotated_surface.get_rect(center = (100, 100))
    SCREEN.blit(rotated_surface, (rect.x, rect.y))

    pygame.display.update()
    CLOCK.tick(30)

How do I fix this issue, to make the surface rotate how I want?

Any help would be appreciated!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

After looking around a bit more, I found an answer to my question. To solve the problem experienced, you need to give the surface an alpha value. This can easily accomplished by replacing the line:

surface = pygame.Surface((50, 50))

With the line:

surface = pygame.Surface((50, 50), pygame.SRCALPHA)
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .