2019年9月12日版中途岛海战淡入淡出的云彩类程序

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

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

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 load_cloud_images(image,n):
    """加载云图片
       image:图像文件,n:数量
    """
    image = pygame.image.load(image).convert_alpha() 
    images = [image.copy() for _ in range(n)]
    
    # alphas是存储每张图片的透明通道的列表
    alphas = [pygame.surfarray.pixels_alpha(image) for image in images]    
    

You must log in to read the rest of this article. Please log in or register as a user.