Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating a rectangle (not image) in pygame

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.

like image 388
Keatinge Avatar asked Apr 08 '16 23:04

Keatinge


People also ask

How do you rotate a shape in Pygame?

To rotate the image we use the pygame. transform. rotate(image, degree) method where we pass the image that we are going to rotate and the degree by which rotation is to be done.

How do you rotate a polygon in Pygame?

Rotation in the polar coordinate system is as simple as moving your shape up/down, left/right in x-y coordinates. When you want to draw, you can then follow formals such as: x = cx + r * math. cos(a + rotation_angle), y = cy + r * math. sin(a + rotation_angle).

How do you draw a rectangle in the Pygame screen?

The most basic way to create a Rect object in Pygame is to pass two tuples into the Pygame. Rect() Class. The first tuple (left, top) represents the position of the rect on the window, whereas the second tuple (width, height) represents the dimensions of the Rect.


2 Answers

You cannot rotate a rectangle drawn by pygame.draw.rect. You have to create a transparent pygame.Surface and rotate the Surface:

rect_surf = pygame.Surface((widht, height), pygame.SRCLAPHA)
rect_surf.fill(color)

See How do I rotate an image around its center using PyGame?, to rotate the Surface.

like image 123
Rabbid76 Avatar answered Oct 22 '22 06:10

Rabbid76


See the second answer here: Rotating a point about another point (2D)

I think rectangles can only be horiz or vertical in their oreintation. You need to define the corners and rotate them and then draw and fill between them.

The other way is to make a class

class myRect(pygame.Surface):
    def __init__(self, parent, xpos, ypos, width, height):
      super(myRect, self).__init__(width, height)
      self.xpos = xpos
      self.ypos = ypos
      self.parent = parent

    def update(self, parent):
      parent.blit(self, (self.xpos, self.ypos))

    def rotate(self, angle):
      #(your rotation code goes here)

and use that instead, as then you will be able to rotate it using the transform function.

like image 21
marienbad Avatar answered Oct 22 '22 06:10

marienbad