python简单的图片切换形成动画效果程序

python简单的图片切换形成动画效果程序

简单事情复杂化了,这个程序实现这个效果还能再度精简很多代码,把类去掉吧,你能自行完成吗?

import turtle

class Window:
    def __init__(self,width,height,bgimages,title="",alt_delay=120,bgcolor="white"):
        self.screen = turtle.Screen()
        self.screen.delay(0)
        self.screen.setup(width,height)
        self.screen.title(title)
        self.screen.bgcolor(bgcolor)
        self.bgimages = bgimages    # 图片列表
        self.amounts = len(bgimages)
        self.index = 0              # 索引列表
        self.alt_delay = alt_delay  # 切换背景图延时
        if self.amounts==1:         # 只有一张背景图则设为背景,不切换
            self.screen.bgpic(bgimages[0])
        elif self.amounts >1:       # 超过一张背景图则不断地换背景图
            self.alt_image()       
        
    def bgcolor(self,color):
        self.screen.bgcolor(color)

    def bgpic(self,pic):
        self.screen.bgpic(pic)

    def alt_image(self):         
        self.bgpic(self.bgimages[self.index])
        self.index = self.index + 1
        self.index = self.index % self.amounts
        self.screen.ontimer(self.alt_image,self.alt_delay)
        
    def run(self):
        self.screen.mainloop()

if __name__== "__main__":

    images = ["girl1/0001.png",
              "girl1/0002.png",
              "girl1/0003.png",
              "girl1/0004.png",
              "girl1/0005.png",
              "girl1/0006.png",
              "girl1/0007.png",
              "girl1/0008.png",
              "girl1/0009.png",
              "girl1/0010.png",
              "girl1/0011.png",
              "girl1/0012.png"
              ]
    
    window  = Window(480,360,images,"女孩跳舞")
    window.run ()
    
 

 

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。