抖动的云朵_帧图生成样本示例

"""抖动的云朵_帧图生成样本示例,本程序会新建云类,它生成后会不断地抖动。
更重要的是程序会保存游戏的每一帧放在frames文件夹里面。所以可以通过继续编写程序把这些png文件合成gif或视频。
由于要不断地写磁盘,所以程序运行速度变慢了,改进的方法是跳帧,即跳过某些帧,如让index为5的倍数时才写入。
"""

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

class Cloud(pygame.sprite.Sprite):
    def __init__(self,image,screen):
        super().__init__()               # 调用超类初始化方法
        self.screen = screen             # screen属性,这样能访问screen
        self.sw = screen.get_width()
        self.sh = screen.get_height()
        self.scale = randint(1,10)/10
        self.image = image
        w,h = image.get_width() * self.scale,image.get_height() * self.scale
        self.image = pygame.transform.scale(self.image,(int(w),int(h)))
        self.dx = randint(-5,5)
        self.dy = randint(-5,5)
        self.rect = self.image.get_rect()
        self.rect.center = randint(0,self.sw),randint(0,self.sh)
        self.interval = 0.02
        self.start_time = time.time()
        
    def update(self):
        """更新云朵的坐标"""
        if time.time() - self.start_time > self.interval:
            self.dx = randint(-5,5)
            self.dy = randint(-5,5)
            self.rect.move_ip(self.dx,self.dy)
            self.start_time = time.time()

def main():

    """主函数"""
    width,height = 960,720
    screen = pygame.display.set_mode((width,height))
    pygame.display.set_caption("抖动的云朵_帧图生成样本示例 www.lixingqiu.com")

    pygame.mixer.init()
    pygame.mixer.music.load("inception.wav")
    pygame.mixer.music.play(-1,0)

    image = pygame.image.load("cloud.png").convert_alpha()
    

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