我一直試圖在使用中圍繞其中心旋轉(zhuǎn)圖像,pygame.transform.rotate()但它不起作用。特別是掛起的部分是rot_image = rot_image.subsurface(rot_rect).copy()。我得到了例外:ValueError: subsurface rectangle outside surface area以下是用于旋轉(zhuǎn)圖像的代碼: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
3 回答

慕姐8265434
TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
您正在刪除旋轉(zhuǎn)創(chuàng)建的矩形。您需要保留rect,因?yàn)樗谛D(zhuǎn)時(shí)會(huì)改變大小。
如果要保留對(duì)象位置,請(qǐng)執(zhí)行以下操作:
def rot_center(image, angle):
"""rotate a Surface, maintaining position."""
loc = image.get_rect().center #rot_image is not defined
rot_sprite = pygame.transform.rotate(image, angle)
rot_sprite.get_rect().center = loc
return rot_sprite
# or return tuple: (Surface, Rect)
# return rot_sprite, rot_sprite.get_rect()
添加回答
舉報(bào)
0/150
提交
取消