python - How can i rotate an image from the center rather than the corner in pygame? -


i creating game , need rotate ship, how can rotate ship center rather top left corner? using python 2.7 , pygame 1.9.

here code have rotate image.

shipimg = pygame.transform.rotate(shipimg,-90) 

however rotates image corner.

rotate sprite set center of new rect center of old rect. way new 1 has same center when you're done, making rotated around center.

here's example function pygame wiki:

def rot_center(image, rect, angle):     """rotate image while keeping center"""     rot_image = pygame.transform.rotate(image, angle)     rot_rect = rot_image.get_rect(center=rect.center)     return rot_image,rot_rect 

and here's how use in example:

# draw ship image centered around 100, 100 oldrect = shipimg.get_rect(center=(100,100))  screen.blit(shipimg, oldrect)   #  rotate ship , draw new rect,  # keep centered around 100,100 shipimg, newrect = rot_center(shipimg,oldrect,-90) screen.blit(shipimg, newrect)  

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -