Unfortunately, there is no direct way to draw a rotated shape. pygame.transform.rotate()
can rotate a pygame.Surface
object but you cannot rotate a shape directly. You need to draw the shape on a Surface and rotate that Surface:
- Create a
pygame.Surface
object with a per-pixel alpha format and with the size of the shape.
- Draw the shapeon the _Surface.
- Rotate the Surface with the shape around its center. See How do I rotate an image around its center using PyGame?
blit
the Surface with the shapeonto the target Surface.
Write a functions that draws the rotated shapes:
def draw_rect_angle(surface, color, rect, angle, width=0):
target_rect = pygame.Rect(rect)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, (0, 0, *target_rect.size), width)
rotated_surf = pygame.transform.rotate(shape_surf, angle)
surface.blit(rotated_surf, rotated_surf.get_rect(center = target_rect.center))
def draw_ellipse_angle(surface, color, rect, angle, width=0):
target_rect = pygame.Rect(rect)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.ellipse(shape_surf, color, (0, 0, *target_rect.size), width)
rotated_surf = pygame.transform.rotate(shape_surf, angle)
surface.blit(rotated_surf, rotated_surf.get_rect(center = target_rect.center))
Use the functions:
angle = 30
draw_rect_angle(screen, BLACK, [50, 50, 500, 200], angle, 2)
draw_ellipse_angle(screen, BLACK, [50, 50, 500, 200], angle, 2)

Minimal example:
import pygame
import test
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
def draw_rect_angle(surface, color, rect, angle, width=0):
target_rect = pygame.Rect(rect)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, (0, 0, *target_rect.size), width)
rotated_surf = pygame.transform.rotate(shape_surf, angle)
surface.blit(rotated_surf, rotated_surf.get_rect(center = target_rect.center))
def draw_ellipse_angle(surface, color, rect, angle, width=0):
target_rect = pygame.Rect(rect)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.ellipse(shape_surf, color, (0, 0, *target_rect.size), width)
rotated_surf = pygame.transform.rotate(shape_surf, angle)
surface.blit(rotated_surf, rotated_surf.get_rect(center = target_rect.center))
angle = 00
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window_center = window.get_rect().center
window.fill((255, 255, 255))
draw_rect_angle(window, (0, 0, 0), (75, 150, 250, 100), angle, 2)
draw_ellipse_angle(window, (0, 0, 0), (75, 150, 250, 100), angle, 2)
angle += 1
pygame.display.flip()
pygame.quit()
exit()