中途岛海战淡入淡出的云彩类测试程序

"""
   中途岛海战淡入淡出的云彩类测试程序,本程序还有待改进,留此版本以做纪念.
"""

__author__ = "李兴球"
__date__ = "2019/9/9"

import pygame
from pygame.locals import *
from random import randint

class Cloud(pygame.sprite.Sprite):
    """从上到下移动淡入淡出的云彩类"""
    def __init__(self,images,screen):
        """
           images:越来越透明的每像素图形,screen:屏幕
        """
        pygame.sprite.Sprite.__init__(self)
        self.images = images
        self.amounts = len(images)  # 造型数量
        self.sw = screen.get_width()
        self.sh = screen.get_height()
        self.index = 0
        self.image = images[0]      # 最透明的图
        self.rect = self.image.get_rect()
        self.rect.centerx = randint(0,self.sw)
        self.rect.bottom = 0

    def update(self):
        """更新坐标与造型"""
        self.rect.move_ip(0,1)
        if self.rect.centery < self.sh: # 没到最下边
            if self.index < self.amounts-1:
                self.index += 1
                self.image = self.images[self.index]
        
        else:
            if  self.index > 0:
                self.index -= 1
                self.image = self.images[self.index]
            else:
                self.kill()
        
def main():

    width,height = 960,720
    
    screen = pygame.display.set_mode((width,height),0,32)
    
    image = pygame.image.load('images/Cloud2.png').convert_alpha()
 
    images = [image.copy() for _ in range(16)]

    alphas = [pygame.surfarray.pixels_alpha(image) for image in images]    
    
    for index in range(0,16):
        print(index * 16)
        alphas[index][:] = index * 16
    del alphas

    for image in images:
        pxarray = pygame.PixelArray(image)
        pxarray.replace(268435456,0)
        pxarray.replace(536870912,0)
        pxarray.replace(805306368,0)
        pxarray.replace(1073741824,0)
        pxarray.replace(1342177280,0)
        pxarray.replace(1610612736,0)
        pxarray.replace(1879048192,0)
        pxarray.replace(-2147483648,0)
        pxarray.replace(-1879048192,0)
        pxarray.replace(-1610612736,0)
        pxarray.replace(-1342177280,0)
        pxarray.replace(-1073741824,0)
        pxarray.replace(-805306368,0)
        pxarray.replace(-536870912,0)
        pxarray.replace(-268435456,0)
        pxarray.close()

    cloud = Cloud(images,screen)
    cloud_group = pygame.sprite.Group()

    pygame.time.set_timer(USEREVENT,1000) # 1秒产生一片云朵
    clock = pygame.time.Clock()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:running = False
            if event.type == USEREVENT:
                cloud_group.add(Cloud(images,screen))
                
        cloud_group.update()
        
        screen.fill((90,120,0))
        cloud_group.draw(screen)
        
        pygame.display.update()
        clock.tick(60)
    pygame.quit()

if __name__== "__main__":

    main()