I have a python script to simulate a wrench moving, I have calculated it's origins position in x and y as a function of time, the problem is when I try to rotate the image, my code moves the image out of frame.

import pygame, sys
import math

screen = pygame.display.set_mode((700, 400))
wrench = pygame.image.load('prueba.bmp')
white = (255,255,255)

ox = 20
oy = 32
running = True
posX = 100 - ox
posY = 100 - oy

while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        screen.blit(wrench, (posX, posY))
    if(pygame.time.get_ticks() <= 20000):
            t = pygame.time.get_ticks() / 1000
            ang = math.degrees((math.pi / 2 * math.cos(math.pi * t)))
            posX = 100 + (50 * math.cos(math.pi * t)) - ox 
            posY = 100 + (20 * math.sin(math.pi * t)) - oy
            wrench = pygame.transform.rotate(wrench, ang)
            screen.blit(wrench, (posX, posY))

The wrench satic The wrench rotating

Does anybody knows why this is happening?

  • \$\begingroup\$ Can you add a screenshot to visualise the issue. \$\endgroup\$
    – ErnieDingo
    Feb 27, 2018 at 20:46
  • \$\begingroup\$ There it is with the screenshots \$\endgroup\$
    – Mntfr
    Feb 28, 2018 at 15:28
  • \$\begingroup\$ Actually, re-reading your question, I'm not sure if my answer is what you're looking for. Can you explain what you mean by moving out of frame? Your image seems to show that the wrench is still within the frame... Regardless, updating the image every time it's rotated will create an image that gets more and more distorted every time it rotates, you should rotate and translate the raw image data instead. \$\endgroup\$
    – Kyy13
    Feb 28, 2018 at 16:43

1 Answer 1


I'm not very familiar with Python, but I believe it's because of this line:

wrench = pygame.transform.rotate(wrench, ang);

wrench is updating itself, and the origin of rotation <0,0> will be in a different place with respect to the image each time. This is because the wrench's bounding rectangle is being updated as it turns.

To fix the problem, keep track of the rotation angle, and rotate the raw image only.

rotated_wrench = pygame.transform.rotate(wrench, ang);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .