【问题标题】:Rotating image in pygame在pygame中旋转图像
【发布时间】:2012-09-22 08:31:31
【问题描述】:

所以我有一个箭头作为图像。从外观上看,它似乎围绕着中心旋转。但是,当它旋转时,无论哪种方式,当它在每边大约 75 度的角度时都会出现错误: ValueError:表面区域外的次表面矩形

我不确定问题是什么,我从 pygames 网站获得了旋转功能。 如果有人知道问题出在哪里,我将非常感谢您的帮助。 代码如下:

screen = pygame.display.set_mode(ss)
arrow = pygame.image.load("obj.png").convert_alpha()

x = 400
y= 300
a = 0
turn = 2

def rot_center(image, angle):
    """rotate an image while keeping its center and size"""
    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

while True:
    screen.fill((255, 255, 255))
    if turn == 1:
        a += 10
    if turn == 2:
        a -=10
    if a == 90:
        turn = 2
    if a == -90:
        turn = 1

    rotarrow = rot_center(arrow, a)
    screen.blit(rotarrow, (x, y))
    pygame.display.update()
    sleep(0.1)

【问题讨论】:

    标签: python graphics python-3.x rotation pygame


    【解决方案1】:

    该页面上的第一个功能,仅适用于方形图像。

    对于任意维度,使用第二个:

    def rot_center(image, rect, angle):
            """rotate an image while keeping its center"""
            rot_image = pygame.transform.rotate(image, angle)
            rot_rect = rot_image.get_rect(center=rect.center)
            return rot_image,rot_rect
    

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 2022-08-07
      • 2021-09-01
      相关资源
      最近更新 更多