pygame动态背景之熊熊大火燃烧场景类

"""动态背景之熊熊大火燃烧场景,本程序的类是用来做动态背景的"""

import time
import pygame
from pygame.locals import *

class Dynamic_background(pygame.sprite.Sprite):
    """动态背景类,把一系列帧图像做为背景不断地显示"""
    
    def __init__(self,images,size):
        """images:已转换成surface的对象列表,size:屏幕尺寸"""
        pygame.sprite.Sprite.__init__(self)
        self.screen_size = size
        self.images = images                   # images是surface列表
        self.image_index = 0                   # 切换造型从0开始
        self.images_amount = len(images)       # 造型数量
        self.image = self.images[0]            # 当前待渲染的图像
        self.rect = self.image.get_rect()      # 获取图像的矩形对象
        self.rect.bottom = self.screen_size[1] # 定位在左下角
        self.interval = 0.1                    # 造型切换间隔时间
        self.start_time = time.time()          # 记录起始时间 

本文隐藏内容 登陆 后才可以浏览
def main(): """主要的调用函数""" width,height = 960,720 pygame.init() screen = pygame.display.set_mode((width,height)) pygame.display.set_caption("pygame动态背景之熊熊大火燃烧场景,作者:李兴球") images = ["bgs/frame" + str(i) + ".png" for i in range(25)] images = [pygame.image.load(image) for image in images] bgs = Dynamic_background(images,(width,height)) running = True while running : event = pygame.event.poll() if event.type == QUIT:running = False bgs.update() screen.fill((0,0,0)) screen.blit(bgs.image,bgs.rect) pygame.display.update() pygame.quit() if __name__ == "__main__": main()

动态背景之熊熊大火燃烧场景,本程序的类是用来做动态背景的