I have some code:
mouseX, mouseY = pygame.mouse.get_pos()
angle = math.degrees(math.atan2(mouseY - self.rect.centery, mouseX - self.rect.centerx))
rotated = pygame.transform.rotate(self.rect, -angle)
self.rect = lol.get_rect()
How should I modify this code to slowly turn the rectangle depending on the mouse angle (10deg/s etc.)?
Edit: I want to rotate CLASS object (based on pygame.sprite.Sprite class)
Just don't rotate your image to the target angle, but only a little bit until you reach the target angle.
Here's a very simple example. The arrow will slowly follow the mouse:
import pygame, math, base64
pygame.init()
screen = pygame.display.set_mode((200, 200))
img = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAAAAOwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFEAAADUAAAAXgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAAAAP8AAADTAAAAVAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQAAAATwAAAP8AAADTAAAAhQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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wAAAP8AAADTAAAATwAAAP8AAADTAAAA7wAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAA7wAAAC8AAAAAAAAAAAAAAAAAAAAAAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAADwAAAAAAAAAAAAAAAAAAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAAUgAAAFMAAAAAAAAAAAAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAGQAAAAAAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAIAAAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAAggAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABRAAAATgAAAF4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4AAAA7AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=='
surf = pygame.image.fromstring(base64.b64decode(img, 'UTF-8'), (32, 32), 'RGBA')
def rot_center(image, angle):
orig_rect = image.get_rect()
rot_image = pygame.transform.rotate(image, angle)
rot_rect = orig_rect.copy()
rot_rect.center = rot_image.get_rect().center
rot_image = rot_image.subsurface(rot_rect).copy()
return rot_image
current_angle = 0
while True:
if pygame.event.get(pygame.QUIT): break
pygame.event.get()
mouseX, mouseY = pygame.mouse.get_pos()
rect = surf.get_rect(center=(92, 92))
target_angle = math.degrees(math.atan2(mouseY - rect.centery, mouseX - rect.centerx))
if current_angle > target_angle:
current_angle -= 0.01
if current_angle < target_angle:
current_angle += 0.01
screen.fill((40, 140, 40))
screen.blit(rot_center(surf, -current_angle), rect)
pygame.display.update()