python可爱的小猫动画_菜根谭小猫

"""
   菜根谭小猫.py
   本程序演示四只行走的小猫,碰到屏幕边缘会反向,有时候它也会向后转。
   它会时不时地说《菜根谭》里面的句子。它有时候还会发出猫叫声。
   作者:李兴球,日期:2018/10/2。
"""

import os
from turtle import *
from random import randint,choice

class Sprite(Turtle):
    def __init__(self,ycor,imagesL,imagesR,sentence,sound = None):
        """参数说明:
           ycor:初始y坐标
           imagesL: 左行列表图
           imgaesR: 右行列表图
           sentence:话语列表
           sound:喵叫声
        """
        Turtle.__init__(self,visible=False)
        self.screen_width = self.screen.window_width()   #屏幕宽度属性
        self.up()
        self.sety(ycor)          #设置初始y坐标
        self.sentence = sentence # 句子列表
        self.sound = sound       #猫叫声
        self.imagesL = imagesL   #左造型列表         
        self.imagesR = imagesR   #右造型列表         
        self.index = 0
        self.images_amount = len(imagesL)   #造型数量 
        self.images = self.imagesL if self.heading()==180 else self.imagesR
        self.word_turtle = Turtle(visible = False,shape='blank')  #用于在self头顶上显示字符的海龟对象
        self.word_turtle.penup()
        self.word_turtle.pencolor("green")
        self.st()
        self.move()
        
    def move(self):
        """让小猫移动,让它有时候会向后转,有时候会说话。"""
        pass
           
    def turn_back(self):
        """向后转"""
        
        self.rt(180)             
        self.images = self.imagesL if self.heading()==180 else self.imagesR
        self.index = 0
        
    def next_image(self):
        """切换到下一个造型"""
        pass
        
    def bounce_on_edge(self):
        
        """对象是离边缘50像素时,让它向后转"""
        pass                             #到了边缘会向后转
            
    def miao(self):
        """设置一定的机率,让小猫发出喵叫声"""
        if randint(1,300) == 1:
            try:self.sound.play()
            except:pass
        
    def speak(self):
        """让小猫的头顶上显示一行句子,好像小猫在说话"""
        words = choice(self.sentence)
        x = self.xcor()
        y = self.ycor() + 55
        self.word_turtle.goto(x,y)
        self.word_turtle.write(words,align='center',font=("黑体",12,"normal"))
        wait_time = 1000 * len(words) //2         #设定等待时间
        self.screen.ontimer(self.move,wait_time)  # 根据句子的长度决定等待时间
        
        
def load_sentence(filename):
    """读文件内容,返回句子组成的列表,参数为文件名路径"""
    words = []
    f = open(filename)
    for line in f:
        if line.strip() != "": words.append(line)
    f.close()
    return words

def init_screen(width,height,title,background):
    """初始化屏幕,参数分别是:宽度,高度,窗口标题,背景图路径"""
    screen = Screen()
    screen.bgpic(background)
    screen.setup(width,height)
    screen.title(title)    
    screen.delay(0)
    return screen
    
def register_gif(screen):
    """注册gif图形,参数为屏幕对象"""
    pass

def init_audio():
    """播放背景音乐,加载‘喵’声。"""
    try:
        import pygame
        pygame.mixer.init()                  #初始化混音器
        pygame.mixer.music.load("BGM10.mp3") #加载背景音乐
        pygame.mixer.music.play(-1,0)        #循环播放背影音乐
        miao = pygame.mixer.Sound("喵.wav")  #生成音效对象
    except:
        print("音频初始化失败,可能没有安装pygame模块。")
        miao = None
    return miao


def main():
    
    """定义主程序,它的执行步骤为:
    初始化屏幕,注册gif图形,初始化音频,装载菜根谭名句,新建4个小猫对象,主循环
    """    
    screen = init_screen(960,720,"《菜根谭》小猫","stripes.png")  #初始化屏幕
    
    images0,images1 = register_gif(screen)                        #注册gif图到形状列表

    miao = init_audio()                                           #初始化音频
    
    句子 = load_sentence("菜根谭名句摘要.txt")                    #装载菜根谭名句到列表

    [Sprite(y,images0,images1,句子,miao) for y in (80,260,-120,-290)] #生成4只小猫
    
    screen.mainloop()                                            #主循环
    
               
    
if __name__=="__main__":

    main()








    
        
        
        

 

python可爱的小猫动画

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李兴球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值