rotate image around center pygame code example

Example 1: pygame rotate image

import pygame
import pygame.font

pygame.init()
size = (400,400)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

def blitRotate(surf, image, pos, originPos, angle):

    # calcaulate the axis aligned bounding box of the rotated image
    w, h       = image.get_size()
    box        = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
    box_rotate = [p.rotate(angle) for p in box]
    min_box    = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
    max_box    = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])

    # calculate the translation of the pivot 
    pivot        = pygame.math.Vector2(originPos[0], -originPos[1])
    pivot_rotate = pivot.rotate(angle)
    pivot_move   = pivot_rotate - pivot

    # calculate the upper left origin of the rotated image
    origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])

    # get a rotated image
    rotated_image = pygame.transform.rotate(image, angle)

    # rotate and blit the image
    surf.blit(rotated_image, origin)

    # draw rectangle around the image
    pygame.draw.rect (surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)

font = pygame.font.SysFont('Times New Roman', 50)
text = font.render('image', False, (255, 255, 0))
image = pygame.Surface((text.get_width()+1, text.get_height()+1))
pygame.draw.rect(image, (0, 0, 255), (1, 1, *text.get_size()))
image.blit(text, (1, 1))
w, h = image.get_size()

angle = 0
done = False
while not done:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key==pygame.K_ESCAPE:
                done = True

    pos = (screen.get_width()//2, screen.get_height()//2)
    pos = (200, 200)

    screen.fill(0)
    blitRotate(screen, image, pos, (w//2, h//2), angle)
    angle += 1

    pygame.draw.line(screen, (0, 255, 0), (pos[0]-20, pos[1]), (pos[0]+20, pos[1]), 3)
    pygame.draw.line(screen, (0, 255, 0), (pos[0], pos[1]-20), (pos[0], pos[1]+20), 3)
    pygame.draw.circle(screen, (0, 255, 0), pos, 7, 0)

    pygame.display.flip()

pygame.quit()

Example 2: rotation image pygame

import pygame as pg # Hey hey ! I hope this code will help you ! :)
# It is not the perfect method to rotate but it is easy to understand and efficient
pg.init()
Win = pg.display.set_mode((400, 400)) # You can choose width and height but wisely !

image = pg.image.load("""Choose an image among your files !!!""")

angle = 0
y0 = x0 = 100 # Choose the location of the image on screen
w, h = image.get_size()
Working = True #Mainloop initialization
while Working :
    pg.time.delay(50)
    for event in pg.event.get() :
        if event.type == pg.QUIT :
            En_marche = False

    Win.fill((0,0,0))
    rotated_image = pg.transform.rotate(image, angle) # Rotate...
    w1, h1 = (rotated_image.get_rect()[2], rotated_image.get_rect()[3]) # An intresting part
    x1 = x0 - w1 + w # New location to remove translation of the image
    y1 = x0 - h1 + h # Try to understand how it works ;)
    Win.blit(rotated_image, (x1/2, y1/2)) # A still image
    pg.display.update() # update...
    angle += 1 # Optional, your image will rotate more each turn