为了使一个表面绕其中心旋转,我们首先旋转图像,然后得到新的矩形,将前一个矩形的center
坐标传递给它以保持图像居中。要围绕任意点旋转,我们可以做类似的事情,但我们还必须添加一个偏移向量到中心位置(枢轴点)来移动矩形,这个向量在每次旋转图像时需要旋转。
因此,我们必须将枢轴点(图像或精灵的原始中心)- 存储在元组、列表、向量或矩形中 - 以及偏移向量(我们将矩形移动的量),并将它们传递给rotate
函数。然后我们旋转图像和偏移向量,得到一个新的矩形,将枢轴点+偏移作为center
参数传递,最后返回旋转后的图像和新矩形。
import pygame as pg
def rotate(surface, angle, pivot, offset):
"""Rotate the surface around the pivot point.
Args:
surface (pygame.Surface): The surface that is to be rotated.
angle (float): Rotate by this angle.
pivot (tuple, list, pygame.math.Vector2): The pivot point.
offset (pygame.math.Vector2): This vector is added to the pivot.
"""
rotated_image = pg.transform.rotozoom(surface, -angle, 1)
rotated_offset = offset.rotate(angle)
rect = rotated_image.get_rect(center=pivot+rotated_offset)
return rotated_image, rect
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
IMAGE = pg.Surface((140, 60), pg.SRCALPHA)
pg.draw.polygon(IMAGE, pg.Color('dodgerblue3'), ((0, 0), (140, 30), (0, 60)))
pivot = [200, 250]
offset = pg.math.Vector2(50, 0)
angle = 0
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
keys = pg.key.get_pressed()
if keys[pg.K_d] or keys[pg.K_RIGHT]:
angle += 1
elif keys[pg.K_a] or keys[pg.K_LEFT]:
angle -= 1
if keys[pg.K_f]:
pivot[0] += 2
rotated_image, rect = rotate(IMAGE, angle, pivot, offset)
screen.fill(BG_COLOR)
screen.blit(rotated_image, rect)
pg.draw.circle(screen, (30, 250, 70), pivot, 3)
pg.draw.rect(screen, (30, 250, 70), rect, 1)
pg.display.set_caption('Angle: {}'.format(angle))
pg.display.flip()
clock.tick(30)
pg.quit()
这是一个带有pygame.sprite.Sprite
的版本:
import pygame as pg
from pygame.math import Vector2
class Entity(pg.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pg.Surface((122, 70), pg.SRCALPHA)
pg.draw.polygon(self.image, pg.Color('dodgerblue1'),
((1, 0), (120, 35), (1, 70)))
self.orig_image = self.image
self.rect = self.image.get_rect(center=pos)
self.pos = Vector2(pos)
self.offset = Vector2(50, 0)
self.angle = 0
def update(self):
self.angle += 2
self.rotate()
def rotate(self):
"""Rotate the image of the sprite around a pivot point."""
self.image = pg.transform.rotozoom(self.orig_image, -self.angle, 1)
offset_rotated = self.offset.rotate(self.angle)
self.rect = self.image.get_rect(center=self.pos+offset_rotated)
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
entity = Entity((320, 240))
all_sprites = pg.sprite.Group(entity)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
keys = pg.key.get_pressed()
if keys[pg.K_d]:
entity.pos.x += 5
elif keys[pg.K_a]:
entity.pos.x -= 5
all_sprites.update()
screen.fill((30, 30, 30))
all_sprites.draw(screen)
pg.draw.circle(screen, (255, 128, 0), [int(i) for i in entity.pos], 3)
pg.draw.rect(screen, (255, 128, 0), entity.rect, 2)
pg.draw.line(screen, (100, 200, 255), (0, 240), (640, 240), 1)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()