2

Right, so this is out of all the questions and bugs I have had in Python/Pygame, the strangest one to me. So I just learned how to rotate something in Pygame and it was working fine with everything else except this one sprite.

This is what rotates it: rotated_roads = pygame.transform.rotate(roads, angle)

This is what blits it: screen.blit(rotated_roads, (240 + x - 465, 0))

I don't know what the problem is, but it's not rotating the way I was expecting it to; to me, it kind of looked like it was rotating like a collision box. [The sprite's shape is a rectangle] I don't know what the problem is, so can you help me??

Thanks.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Duck Duck
  • 159
  • 6
  • 1
    Do you mean it doesn't rotate around its center? Or is it not transparent? – Rabbid76 Nov 28 '20 at 18:12
  • uh.. well I suppose you could call that "not rotating around its center", but I'm not really familiar with this kind of stuff so I'm not too sure. – Duck Duck Nov 28 '20 at 18:16
  • So why do you not use the code from your previous question? - [Why is it not blitting a sprite in pygame?](https://stackoverflow.com/questions/65045730/why-is-it-not-blitting-a-sprite-in-pygame) – Rabbid76 Nov 28 '20 at 18:18
  • What do you expect by `(240 + x - 465, 0)`? – Rabbid76 Nov 28 '20 at 18:18
  • well, the `(240 + x - 465, 0)` definitely isn't causing the problem, my other rotating sprites using `(240 + x 465, 0)` as well and they don't have this problem.... I think. The other rotating sprites are much smaller. – Duck Duck Nov 28 '20 at 18:20
  • I am using the same code as the last question, but for some reason this particular sprite doesn't seem the work.... Now I"m starting to think that it's got nothing to do with the code but it's because the costume itself is off-center – Duck Duck Nov 28 '20 at 18:21
  • No The code in the question does not rotate around the center. – Rabbid76 Nov 28 '20 at 18:22
  • oh, well this rotation thing it still pretty confusing.... – Duck Duck Nov 28 '20 at 18:26

1 Answers1

2

See How do I rotate an image around its center using PyGame?.

Get the rectangle of the original image and set the position. Get the rectangle of the rotated image and set the center position through the center of the original rectangle:

rotated_roads = pygame.transform.rotate(roads, angle)
rect = roads.get_rect(topleft = (240 + x - 465, 0))
rotated_rect = rotated_roads.get_rect(center = rect.center)
screen.blit(rotated_roads, rotated_rect)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174