下面的程序从sprites模块导入Sprite类和Screen命令,前者用于新建角色,后者用于新建窗口,还从gameturtle模块中导入了makegif和makevideo命令,前者用于把frames中的每一帧合成gif动画,后者则把frames中的所有帧压成mp4视频。最新版gameturtle模块中包括了makevideo命令,需要用pip install gameturtle –upgrade来安装gameturtle模块的最新版本。
from sprites import Sprite,Screen from gameturtle import makegif,makevideo # pip install gameturtle --upgrade screen = Screen() screen.setup(480,360) screen.onmousemove(lambda x,y:screen.title(f"{x},{y}")) bug = Sprite() bug.pendown() bug.pensize(5) frames =[] for i in range(5): bug.fd(200);frames.append(screen.save()) bug.right(144);frames.append(screen.save()) bug.penup() bug.hide() frames.append(screen.save()) # 屏幕截取到frames列表中 cs = ['red','orange','yellow','green','cyan','blue'] cors = [(47,-16),(100,25),(153,-16),(133,-78),(63,-78),(96,-34)] for fc,xy in zip(cs,cors): bug.fillcolor(fc) # 设定填充颜色 bug.goto(xy) # 到达xy坐标 bug.fill() # 填充区域,默认采用洪水填充方式 frames.append(screen.save()) makegif(frames,'生成的gif.gif') # 生成动图 makevideo(frames,'生成的video.mp4') # 生成mp4视频 screen.mainloop()
发表评论