python海龟填充新法之fill命令示例1

python海龟填充新法之fill命令示例1

本程序需要最新版本sprites模块支持。安装方法: pip install sprites –upgrade

"""
   fill new method.py
   This is a example, demo fill  new method.
"""
from sprites import Sprite

turtle = Sprite('blank',visible=False)
screen = turtle.getscreen()
screen.delay(10)
# 设定鼠标移动事件,在标题栏显示鼠标指针坐标
screen.onmousemove(lambda x,y:screen.title(str(x)+"," + str(y)))

turtle.speed(1)
turtle.goto(0,200)
turtle.pensize(4)
turtle.pd()
for i in range(3):
    turtle.fd(30)
    turtle.right(120)
    turtle.fd(30)
    turtle.left(120)
turtle.circle(-10,180)
turtle.right(45)
turtle.fd(130)
turtle.goto(0,200)
turtle.fillcolor('pink')   # 设定填充颜色
turtle.penup()             #  remember penup (起新的线条项目)
turtle.fill(3,171,mode=2)  # mode为2会调用洪水填充函数

# 下面画个残缺圆进行填充
turtle.goto(-100,100)
turtle.pendown()
turtle.circle(-50,90)
turtle.circle(50,90)
turtle.circle(100,180)
turtle.circle(100,90)
turtle.fillcolor('cyan')
turtle.penup()            # remember penup 画完后要抬笔
turtle.fill(-150,200,mode=2)

# 空心十字架
turtle.pu();turtle.home();turtle.pd()
for  i in range(4):
    turtle.fd(50);turtle.left(90);turtle.fd(50)
    turtle.right(90);turtle.fd(50);turtle.right(90)
turtle.penup()
turtle.fillcolor('light green')
turtle.fill(10,-10)   # 不写第三个参数表示填充凸多边形

# 不规则图形
turtle.goto(-130,-130)
turtle.pd()
turtle.goto(-130,0)
turtle.goto(-230,-190)
turtle.goto(130,-200)
turtle.goto(-230,00)
turtle.penup()       # remember penup 
turtle.fillcolor('red')
turtle.fill(-137,-26)

# 凹
turtle.goto(150,-100);turtle.pd()
turtle.fd(50);turtle.right(90);turtle.fd(50);turtle.left(90);turtle.fd(50);turtle.left(90)
turtle.fd(50);turtle.right(90);turtle.fd(50);turtle.right(90);turtle.fd(100);turtle.right(90)
turtle.fd(150);turtle.right(90);turtle.fd(100);turtle.penup()
turtle.fillcolor('gray')
turtle.fill(200,-170)

# ☆
turtle.goto(200,200);turtle.pd()
for i in range(5):
    turtle.fd(50)
    turtle.left(144)
turtle.penup()          # remember penup 
turtle.fillcolor('magenta')
turtle.fill(195,237)
turtle.fillcolor('red')
turtle.fill(204,225)
turtle.fillcolor('blue')
turtle.fill(197,210)
turtle.fillcolor('green')
turtle.fill(182,218)
turtle.fillcolor('orange')
turtle.fill(183,231)
turtle.fillcolor('lime')
turtle.fill(194,223,mode=1)

screen.mainloop()

李兴球

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