python海龟画图模块实现单击滑动小猫叫动画

python海龟画图模块实现单击滑动小猫叫动画

"""本程序用海龟画图模块实现单击滑动小猫叫。
刚才用了pygame zero实现一只小猫从左到右不断地滑动,想了想用turtle模块实现一下。

"""

from turtle import *
from winsound import PlaySound,SND_ASYNC

width,height = 480,360
cats_images = ["scratch.gif","scratch1.gif"]
cats_images_index = 0

screen = Screen()
screen.setup(width,height)
screen.bgpic("moon.png")
screen.title("海龟画图点滑动小猫动画_作者:李兴球")
screen.delay(0)
screen.addshape("scratch.gif")
screen.addshape("scratch1.gif")

# 小猫的宽度是184,高度是200
小猫 = Turtle(shape="scratch.gif")
小猫.penup()

def move():
    小猫.setx(小猫.xcor() + 1)
    right = 小猫.xcor() - 92
    if right > width//2 :
        小猫.ht()
        小猫.setx(-width//2-92)
        小猫.st()        
    screen.ontimer(move,10)
move()

def act(x,y):
    global cats_images_index
    print("你用鼠标左键点到我了")
    PlaySound("喵.wav",SND_ASYNC)
    cats_images_index = 1- cats_images_index
    小猫.shape(cats_images[cats_images_index])
    
小猫.onclick(act)
screen.mainloop()
     
    
李兴球

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

评论已关闭。