In pygame I use pygame.draw.rect(screen, color, rectangle)
for all the rectangles in my program. I want to be able to rotate these rectangles to any angle. I have seen the following code to rotate IMAGES but my question is with RECTANGLES.
pygame.transform.rotate(image, angle)
But I am working with rectangles, I don't have an image or "surface" that I can rotate. When I try to rotate a rectangle with
rect = pygame.draw.rect(screen, self.color, self.get_rectang())
rotatedRect = pygame.transform.rotate(rect, self.rotation)
screen.blit(rotatedRect)
This gives TypeError: must be pygame.Surface, not pygame.Rect
on the line with .rotate()
My question is, how can I rotate a and display a RECTANGLE(x,y,w,h)
, not an image, in pygame.
The linked post that this is a "potential duplicate" of is not a duplicate. One answer explains about the consequences of rotating a rectangle and the other uses code for rotating an image.
x2= cos a * x1 - sin a y1
y2= sin a *x1 + cos a y1
, but the rectangle function might only plot a rectangle that is aligned square with the axes, so you'd have to look for a polygon plotting function. Alternatively you could make asurface
with visible outline and transparent fill (alpha), which is probably easier. A rectangle may just be a helper function in pygame, and not be directly displayable, so there are two separate questions in your last line. – Oldfashionedimage2 = pygame.transform.rotate(image1, angle)
and the accepted answer doesn't explain anything about actually rotating a rectangle, just about some of the unexpected consequences – Making