主程序源代码
"""
文字单摆运动.py
"""
import time # 导入时间模块
from turtle import * # 从海龟模块导入所有命令
from write_patch import * # 本补丁模块也是由turtle模块开发的
from threading import Thread # 从线程模块导入Thread类
width,height = 480,360
screen = Screen()
screen.tracer(0,0)
screen.setup(width,height)
screen.bgcolor('yellow')
screen.title("Python Turtle Graphics 本程序由纯turtle模块制作")
def pendulum():
cp = Turtle(visible=False)
cp.penup()
cp.speed(0)
cp.goto(0,100)
a = 0
da = 1
zt = ('',17,'normal')
while True:
cp.clear()
cp.write('writed by lixingqiu',font=zt,angle=a)
time.sleep(0.01)
a = a - da
if a > 0.8 and a<0.9 :
print(a)
time.sleep(1)
if a < -90:
da = da - 0.1
else:
da = da + 0.1
Thread(target=pendulum).start()
def gundong():
info = '本程序由纯海龟模块实现'
tom = Turtle(visible=False)
tom.sety(-50)
tom.penup()
tom.speed(1)
zt = ('黑体',32,'normal')
a = 0
da = 1 # 每次转动的角度
while True:
for zi in info:
tom.setx(-width/2-50)
while tom.xcor() -50 < width/2:
tom.clear()
tom.dot(100,'cyan')
tom.color('red')
tom.pendown()
tom.fd(50)
tom.penup()
tom.bk(50)
tom.write(zi,align='center',font=zt,angle=a)
tom.right(da)
tom.setx(tom.xcor() + da * 2*3.14159*50/360)
time.sleep(0.01)
a = a - da
a = a % 360
Thread(target=gundong).start()
w = Turtle(shape='blank')
w.penup()
w.sety(-140)
w.write('免费下载网址: www.lixingqiu.com',align='center',font=('',14,'normal'))
screen.mainloop()
write_patch.py源代码:
from turtle import RawTurtle,TurtleScreenBase,Turtle,Screen
__author__ = '李兴球'
__blog__ = 'www.lixingqiu.com'
def __write(self, pos, txt, align, font, pencolor,angle):
"""
用指定的颜色和字体在画布上写文本。返回文本项目和其绑定盒的右x-1坐标。
"""
x, y = pos
x = x * self.xscale
y = y * self.yscale
anchor = {"left":"sw", "center":"s", "right":"se" }
item = self.cv.create_text(x-1, -y, text=txt, anchor=anchor[align],
fill= pencolor, font=font,angle=angle)
x0, y0, x1, y1 = self.cv.bbox(item)
self.cv.update()
return item, x1-1
TurtleScreenBase._write = __write
def _write(self,txt,align,font,angle=0):
"""海龟的write的预定义方法
"""
item, end = self.screen._write(self._position, txt,
align, font,self._pencolor,angle)
self.items.append(item)
if self.undobuffer:
self.undobuffer.push(("wri", item))
return item,end # 本来只返回end,这里增加了item
def _writea(self, arg, move=False, align="left", font=("黑体",12,"normal"),angle=0):
"""在海龟的当前坐标写文本。
参数:
arg -- 要写在海龟画图屏幕上的信息,
move (可选) -- True/False,
align (可选) -- 左,中,右( "left", "center" or right"),
font (可选) -- 三元组 (字体名称, 字体大小,字体类型),
angle (可选) -- 角度值,如90,180
根据对齐方式和给定的字体样式在屏幕写文本。
如果move为真,那么海龟(画笔)会移到文本的右下角,缺省为假。
举例 (假设有一个海龟实例为turtle):
>>> turtle.write('风火轮编程 ', True, align="center")
>>> turtle.write((0,0), True)
"""
if self.undobuffer:
self.undobuffer.push(["seq"])
self.undobuffer.cumulate = True
item,end = self._write(str(arg), align.lower(), font,angle)
if move:
x, y = self.pos()
self.setpos(end, y)
if self.undobuffer:
self.undobuffer.cumulate = False
return item # 这里本来不返回item
RawTurtle._write = _write # 重定义_write
RawTurtle.write = _writea # 重定义write
if __name__ == "__main__":
tom = Turtle(visible=False)
tom.write('风火轮编程',angle=45)
tom.screen.mainloop()

