利用turtle模块画图

利用turtle模块画图

代码:

import turtle 
#导入画图模块,查看已安装的模块dir('modules')或者help('modules')命令,查看模块详情help('turtle')
t=turtle.Turtle() 
#调用模块里面的Turtle工具,调用模块:模块名.函数名    turtle.Turtle()
t.speed(0) 
#画笔速度1-9依次变快,0为最快速度
def setpen(x,y):
      t.penup()
      t.goto(x,y)
      t.pendown()
      t.setheading(0)
#定义函数,定义画笔的起点及朝向(x,y)为起点 t.setheading(0)设置朝向为0
#penup提笔,pundown落笔,goto移动
#也可直接跟参数定义函数。
# def setpen():
#     t.penup()
#     t.goto(10,10)
#     t.pendown()
#     t.sedheading(0)
# setpen()   调用
def circle(x,y,r,color):
      n=36
      angle=360/n
      p=3.1415926
      c=2*p*r
      l=c/n
      point_x=x-l/2
      point_y=y+r
      setpen(point_x,point_y)
      t.pencolor(color)
      t.fillcolor(color)
      t.begin_fill()
      for i in range(n):
           t.forward(l)
           t.right(angle)
      t.end_fill() 
#定义圆,pencolor画笔颜色,fillcolor填充颜色,range(n)区间,边数大于36以上时,可认为是圆形。
#for循环一般形式:
#for <variable> in <sequence>:
#    <statements>
#else:
#    <statements>

#while循环语句一般形式:
#while 判断条件:
#      语句
#else:
#      语句
circle(0,0,200,'red')
circle(0,0,155,'white')
circle(0,0,110,'red')
circle(0,0,65,'blue')
#调用定义的函数。
def five_star():
      setpen(0,0)
      t.setheading(161)
      t.forward(65)
      t.setheading(0)
      t.fillcolor('white')
      t.begin_fill()
      t.hideturtle()
      t.penup()
      for i in range(5):
          t.forward(124)
          t.right(144)
      t.end_fill() 
#定义五角星,hideturtle隐藏画笔
five_star()
#调用定义的函数
turtle.done()
#完成

猜你喜欢

转载自blog.51cto.com/13689359/2411439