import numpy import pygame class Frame: def __init__(self, image: pygame.Surface, cx, cy): self.image = image self.cx = cx self.cy = cy self.alpha = None def get_alpha(self): if self.alpha is None: arr = pygame.surfarray.array_alpha(self.image).reshape((self.image.get_size()[0], self.image.get_size()[1], 1)) if self.alpha: if not hasattr(self, 'alpha') or self.alpha.shape != (len(arr), len(arr[0]), 3): self.alpha = numpy.tile(arr, (1, 1, 3)) / 255 # 只有在需要时才重新生成数组 # print(len(self.alpha)) # self.alpha = numpy.tile(arr, (1, 1, 3)) / 255 # print(len(self.alpha), len(self.alpha[0]), len(arr)) def get_alpha_old(self): if self.alpha is None: arr = pygame.surfarray.array_alpha(self.image).reshape((self.image.get_size()[0], self.image.get_size()[1], 1)) self.alpha = numpy.tile(arr, (1, 1, 3)) / 255 def duplicate(self): img = self.image.copy() return Frame(img, self.cx, self.cy)