0

我有一些代码:

 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()

我应该如何修改此代码以根据鼠标角度(10度/秒等)缓慢转动矩形?

编辑:我想旋转CLASS对象(基于 pygame.sprite.Sprite 类)

4

1 回答 1

1

只是不要将图像旋转到目标角度,而只是一点点,直到达到目标角度。

这是一个非常简单的例子。箭头会慢慢跟随鼠标:

import pygame, math, base64
pygame.init()
screen = pygame.display.set_mode((200, 200))

img = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAAAAOwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFEAAADUAAAAXgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAAAAP8AAADTAAAAVAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQAAAATwAAAP8AAADTAAAAhQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAK0AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAA7wAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAA7wAAAC8AAAAAAAAAAAAAAAAAAAAAAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAADwAAAAAAAAAAAAAAAAAAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAAUgAAAFMAAAAAAAAAAAAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAGQAAAAAAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAIAAAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAAggAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABRAAAATgAAAF4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4AAAA7AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=='

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()

在此处输入图像描述

于 2015-05-19T15:21:52.177 回答