Here given the code I use to draw a rectangle and an ellipse within it.(Purple one).
pygame.draw.rect(screen, BLACK, [50, 50, 500, 200], 2)
pygame.draw.ellipse(screen, BLACK, [50, 50, 500, 200], 2)
I need to draw a rotated ellipse like my green image. Is there a code I can use in pygame for that?
Thank you.
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 :
pygame.Surface
object with a per-pixel alpha format and with the size of the shape.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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.