Dee*_*wal 6 python pygame rotation python-2.7
我想围绕中心以外的点旋转矩形.到目前为止我的代码是:
import pygame
pygame.init()
w = 640
h = 480
degree = 45
screen = pygame.display.set_mode((w, h))
surf = pygame.Surface((25, 100))
surf.fill((255, 255, 255))
surf.set_colorkey((255, 0, 0))
bigger = pygame.Rect(0, 0, 25, 100)
pygame.draw.rect(surf, (100, 0, 0), bigger)
rotatedSurf = pygame.transform.rotate(surf, degree)
screen.blit(rotatedSurf, (400, 300))
running = True
while running:
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)
我可以改变度数以获得不同的旋转但旋转大约是中心.我想将矩形中心以外的点设置为旋转点.
要围绕其中心旋转曲面,我们首先旋转图像,然后获得一个新的矩形,我们将center前一个矩形的坐标传递给它以使其保持居中.要围绕任意点旋转,我们可以做几乎相同,但我们还必须向中心位置(枢轴点)添加偏移矢量以移动矩形.每次旋转图像时都需要旋转此矢量.
因此,我们必须以存储所述枢转点(图像或子画面的原始中心) -在一个元组,列表,向量或一个矩形-和偏移向量(通过我们转移RECT的量),并将它们传递到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) # Rotate the image.
rotated_offset = offset.rotate(angle) # Rotate the offset vector.
# Add the offset vector to the center/pivot point to shift the rect.
rect = rotated_image.get_rect(center=pivot+rotated_offset)
return rotated_image, rect # Return the rotated image and shifted rect.
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
# The original image will never be modified.
IMAGE = pg.Surface((140, 60), pg.SRCALPHA)
pg.draw.polygon(IMAGE, pg.Color('dodgerblue3'), ((0, 0), (140, 30), (0, 60)))
# Store the original center position of the surface.
pivot = [200, 250]
# This offset vector will be added to the pivot point, so the
# resulting rect will be blitted at `rect.topleft + offset`.
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 version of the image and the shifted rect.
rotated_image, rect = rotate(IMAGE, angle, pivot, offset)
# Drawing.
screen.fill(BG_COLOR)
screen.blit(rotated_image, rect) # Blit the rotated image.
pg.draw.circle(screen, (30, 250, 70), pivot, 3) # Pivot point.
pg.draw.rect(screen, (30, 250, 70), rect, 1) # The rect.
pg.display.set_caption('Angle: {}'.format(angle))
pg.display.flip()
clock.tick(30)
pg.quit()
Run Code Online (Sandbox Code Playgroud)
这是一个版本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)))
# A reference to the original image to preserve the quality.
self.orig_image = self.image
self.rect = self.image.get_rect(center=pos)
self.pos = Vector2(pos) # The original center position/pivot point.
self.offset = Vector2(50, 0) # We shift the sprite 50 px to the right.
self.angle = 0
def update(self):
self.angle += 2
self.rotate()
def rotate(self):
"""Rotate the image of the sprite around a pivot point."""
# Rotate the image.
self.image = pg.transform.rotozoom(self.orig_image, -self.angle, 1)
# Rotate the offset vector.
offset_rotated = self.offset.rotate(self.angle)
# Create a new rect with the center of the sprite + the offset.
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()
Run Code Online (Sandbox Code Playgroud)
我也遇到了这个问题,并找到了一个简单的解决方案:您可以创建一个更大的表面(双倍的长度和双倍的高度)并将较小的表面传输到较大的表面,以便旋转点是较大表面的中心。现在您可以围绕中心旋转较大的一个。
def rotate(img, pos, angle):
w, h = img.get_size()
img2 = pygame.Surface((w*2, h*2), pygame.SRCALPHA)
img2.blit(img, (w-pos[0], h-pos[1]))
return pygame.transform.rotate(img2, angle)
Run Code Online (Sandbox Code Playgroud)
(如果你会制作草图,它会更有意义,但相信我:它有效,并且在我看来比其他解决方案更易于使用和理解。)
我同意 MegaIng 和 skrx 的观点。但我也不得不承认,读完他们的答案后,我无法真正理解这个概念。然后我发现了这个关于在其边缘旋转的佳能的游戏教程。
运行后,我心里仍然有疑问,但后来我发现他们用于大炮的图像是技巧的一部分。
图像不是以大炮的中心为中心,而是以枢轴点为中心,图像的另一半是透明的。在那次顿悟之后,我将同样的方法应用到我的虫子的腿上,它们现在都工作得很好。这是我的轮换代码:
def rotatePivoted(im, angle, pivot):
# rotate the leg image around the pivot
image = pygame.transform.rotate(im, angle)
rect = image.get_rect()
rect.center = pivot
return image, rect
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
我认为你必须为此创建一个自己的函数。
如果你创建一个 Vector 类,那就容易多了。
也许是这样的:
def rotate(surf, angle, pos):
pygame.transform.rotate(surf, angle)
rel_pos = surf.blit_pos.sub(pos)
new_rel_pos = rel_pos.set_angle(rel_pos.get_angle() + angle)
surf.blit_pos = pos.add(new_rel_pos)
Run Code Online (Sandbox Code Playgroud)
现在你已经有了它。你唯一需要做的就是使用方法“add()”、“sub()”、“get_angle()”和“set_angle()”的 Vector 类。如果你正在挣扎,只需谷歌寻求帮助。
最后,您将得到一个很好的 Vector 类,您可以在其他项目中扩展和使用它。