frame.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import numpy
  2. import pygame
  3. class Frame:
  4. def __init__(self, image: pygame.Surface, cx, cy):
  5. self.image = image
  6. self.cx = cx
  7. self.cy = cy
  8. self.alpha = None
  9. def get_alpha(self):
  10. if self.alpha is None:
  11. arr = pygame.surfarray.array_alpha(self.image).reshape((self.image.get_size()[0], self.image.get_size()[1], 1))
  12. if self.alpha:
  13. if not hasattr(self, 'alpha') or self.alpha.shape != (len(arr), len(arr[0]), 3):
  14. self.alpha = numpy.tile(arr, (1, 1, 3)) / 255 # 只有在需要时才重新生成数组
  15. # print(len(self.alpha))
  16. # self.alpha = numpy.tile(arr, (1, 1, 3)) / 255
  17. # print(len(self.alpha), len(self.alpha[0]), len(arr))
  18. def get_alpha_old(self):
  19. if self.alpha is None:
  20. arr = pygame.surfarray.array_alpha(self.image).reshape((self.image.get_size()[0], self.image.get_size()[1], 1))
  21. self.alpha = numpy.tile(arr, (1, 1, 3)) / 255
  22. def duplicate(self):
  23. img = self.image.copy()
  24. return Frame(img, self.cx, self.cy)