动态背景_蓝天绿地太阳云朵户外场景动画演示

"""动态背景_蓝天绿地太阳云朵户外场景动画演示"""

__author__ = "李兴球"
__date__ = "2019/6/3"
__website__ = "www.lixingqiu.com"

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

class Sun:
    def __init__(self,images,position):
        self.images = images
        self.index = 0             # 造型索引
        self.amounts = len(images) # 造型数量
        self.interval = 0.1        # 切换间隔
        self.start_time = time.time()
        self.set_costume(self.index)
        self.rect = self.image.get_rect()
        self.rect.center = position        
        
    def update(self):
        pass

    def set_costume(self,index):
        """设置造型"""
        self.image = self.images[index]

class Cloud(pygame.sprite.Sprite):
    def __init__(self,images,group,screen_width):
        super().__init__()
        pass

if __name__ == "__main__":

    width,heigt = 480,360
    screen = pygame.display.set_mode((480,360))
    pygame.display.set_caption("动态背景_蓝天绿地太阳云朵户外场景动画演示www.lixingqiu.com")

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

    background = pygame.image.load("images/background.png")

    cloud_images = [pygame.image.load(f"images/cloud{index+1}.png").convert_alpha() for index in range(3)]    
    cloud_sizes = [(int(image.get_width()/3),int(image.get_height()/3)) for image in cloud_images]
    cloud_images = [pygame.transform.scale(cloud_images[index],
                    (cloud_sizes[index][0],cloud_sizes[index][1])) for index in range(3)]    
    cloud_group = pygame.sprite.Group()

    sun_images =  [pygame.image.load(f"images/sun{index+1}.png").convert_alpha() for index in range(3)]
    sun_sizes = [(int(image.get_width()/3),int(image.get_height()/3)) for image in sun_images]
    sun_images = [pygame.transform.scale(sun_images[index],
                    (sun_sizes[index][0],sun_sizes[index][1])) for index in range(3)]
    
    sun = Sun(sun_images,(150,50))

    clock = pygame.time.Clock()
    running = True
    while running:
        event = pygame.event.poll()
        if event.type == QUIT: running = False
        if randint(1,100) == 1 : Cloud(cloud_images,cloud_group,width )
 
        sun.update()
        cloud_group.update()

        screen.blit(background,(0,0))
        screen.blit(sun.image,sun.rect)
        cloud_group.draw(screen)
        pygame.display.update()
        clock.tick(30)

    pygame.quit()


        

动态背景_蓝天绿山绿地太阳云朵