# Rotating an image in Python

So I am making an "Asteroids" like game and I can principally get my sprites image to rotate but when I do, the image gets kinda torn apart. This is the method I am using for rotation. Any help would be appreciated.

def turnLeft(self):

   rotatedSurf1 = pygame.transform.rotate(self.image,10)
rotatedRect1 = rotatedSurf1.get_rect()
rotatedRect1.center = (25,25)
self.image.blit(rotatedSurf1,rotatedRect1)

• Please define "torn apart", if possible via a screenshot with a before and after part. – Philip Allgaier Dec 17 '14 at 15:23
• The image kinda like swirls into a mess of pixels, like it is being blended, it gets worse as you rotate more and more. It might be good to mention also the the image rotates as the player keeps the left arrow key down. – SyntheticWeb Dec 17 '14 at 16:08
• You want to change this line self.image.blit(rotatedSurf1,rotatedRect1) to something like self.backgroundSurface.blit(rotatedSurf1,rotatedRect1). – Syntac_ Dec 18 '14 at 11:45

I'm not familiar with pygame, but likely it does not like the image being repeatedly rotated and blitted onto itself.

Check out if this answer on StackOverflow works for you.

Explanation

Rotating an image is inaccurate. Unless you rotate exactly by multiples of 90 degrees, you never get an exact pixel-to-pixel mapping. In other words, you loose a small amount of information when you rotate. Since you replace the original image with the rotated version, this information is lost forever. Thus, the effect gets worse the more you rotate.

You should never modify the original image. Instead, store the rotation angle in a variable, increase/decrease this angle when pressing left/right, and rotate by that angle. Then, you can store the rotate image (rotatedSurf1, rotatedRect1) as a member variable and blit it to the screen when drawing the scene. Do not blit the rotated image back onto the original!

def turnLeft(self):
self.angle += 10
self.rotatedSurf = pygame.transform.rotate(self.image, self. angle)
self.rotatedRect = self.self.rotatedSurf.get_rect()
self.rotatedRect.center = (25, 25)


Don't forget to initalize self.angle to 0. In the drawing function instead of doing something like display.blit(self.image), now do display.blit(self.rotatedSurf, rotatedRect).

• I am using this method by storing the surface of the sprite in rotatedSurft1. – SyntheticWeb Dec 17 '14 at 17:00
• Doesn't image.blit draw the source (rotatedSurf1 in your case) into image? – kazemakase Dec 17 '14 at 20:17