Use turtle to draw simple graphics library

  turtle python library is the basis for drawing library, which was introduced as one of the most common methods used to introduce programming knowledge library, which is mainly used for design entry, is one of the standard library, the use of turtle can make a lot of complex drawings .

  turtle name meaning "turtle", we imagine a turtle, right in the heart of the display window, walk on the canvas, its trajectory walk on the formation of graphics rendering.
Turtle motion is controlled by a program, which can change color, change the size (width) and the like.

  

  • Drawing coordinate system

  turtle.setup(width,height,startx,starty)

  Turtle use of the setup function, may generate a window screen (form), size and location of the form set, the window is the range of the canvas.

 

 

  •  Brush Control Functions
  1. penup(): Lift the brush;
  2. pendown(): Drop brush;
  3. pensize(width): Pen width;
  4. pencolor(color): Pen color;

 

  • Motion control function

  1.直线       

    turtle.forward(d) # turtle.fd(d) d为参数行进距离 控制画笔向前走d长度的直线 d可以为负数,表示向后运动。

  2.曲线

    turtle.circle(r,extent=None) # 根据半径r绘制extent角度的弧形 r 默认圆心在画笔左端点距离r长度的地方extent是绘制的角度,默认绘制完整的圆形

    turtle.circle(100) # 在画笔的左侧(也就是上方)100距离的位置上然后以弧形来运动,没有设置extent参数,因此会绘制整个圆形

    turtle.circle(-100,90) #圆心在画笔的右侧100距离(也就是下方)上,然后extent为90,因此绘制一个90度的弧形。

 

  • Direction control function

 

 绝对角度转向函数
   turtle.setheading(angle) # turtle.seth(angle) 改变画笔的面向的角度( 初始方向是画布的正右方) 参数angle是绝对坐标系的角度 画笔角度转向函数
   turtle.left(angle) # 向左转angle度    turtle.right(angle) # 向右转angle度

 

  • 实例

 

  Code:

  #coding=utf-8
  #绘制蟒蛇
  import turtle
  turtle.penup()
  turtle.pencolor("blue")
  turtle.forward(-250)
  turtle.pendown()
  turtle.pensize(10)
  turtle.right(45)
  for i in range(4):
   turtle.circle(40, 80)
  turtle.circle(-40, 80)
  turtle.circle(40, 80 / 2)
  turtle.fd(40)
  turtle.circle(16, 180)
  turtle.fd(40 * 2 / 3)
  turtle.done()

 

  运行结果:

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/DrcProgrammingCool/p/11517232.html