我已经弄清楚了如何在游戏前围绕图像的中心点旋转图像,但是这样做会导致背景透明性下降。
class Agent:
def __init__(self,display_width,display_height):
self.x = random.randint(0,display_width)
self.y = random.randint(0,display_height)
self.angle = random.randint(0,359)
self.sprite_original = pygame.image.load('agent.png').convert()
self.sprite = self.sprite_original
def rot_center(self, 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
def action(self,keys):
#a rotate left
if keys[97] == True:
self.angle +=1 % 360
self.sprite = self.rot_center(self.sprite_original,self.angle)
#d rotate right
if keys[119] == True:
self.angle -=1 % 360
self.sprite = self.rot_center(self.sprite_original,self.angle)
我希望我的agent.png图像保持其背景透明性,但事实并非如此。