这是本人编写的中途岛海战的淡入淡出的云彩类模块。供给有需要的人士。下面是部分代码预览:
"""
中途岛海战淡入淡出的云彩类程序
"""
__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):
"""更新坐标与造型"""
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]
.................................
def main():
width,height = 960,720
screen = pygame.display.set_mode((width,height),0,32)
images = load_cloud_images('images/Cloud2.png')
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()
如需要查看完整源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

