Python Turtle Drawing Instance Tutorial _python

Source: Internet
Author: User
Tags goto

In this paper, the use of Python turtle module, turtle Drawing, is introduced as an example, and it is believed to be of reference value for friends who need graphic programming.

Python Turtle Module Introduction:
A simple drawing tool introduced in the python2.6 version, called Turtle drawing (Turtle Graphics)

1. Use turtle Drawing First we need to import turtle, as follows:

 From Turtle Import * #将turtle中的所有方法导入

2. Turtle Drawing Properties:

(1) Location
(2) direction
(3) brush ( properties of the brush, color, width of the drawing line )

3. There are many commands for manipulating turtle drawings, which can be divided into two types: one for motion commands and one for brush control commands

(1) Motion Command:

  Forward (degree)  #向前移动距离degree代表距离
  Backward (degree)  #向后移动距离degree代表距离 right
  (degree)    # How many degrees left
 (degree)  #向左移动多少度
 Goto (x,y)  #将画笔移动到坐标为x is moved to the right, and the position of Y
  stamp ()     #复制当前图形
 Speed (speed)  #画笔绘制的速度范围 [0,10] Integer

(2) Brush Control Command:

 Down () #移动时绘制图形, by default, drawing up
 () #移动时不绘制图形
 pensize (width) #绘制图形时的宽度
 color (colorstring) #绘制图形时的颜色
 FillColor (colorstring) #绘制图形的填充颜色
 Fill (ture)
 Fill (false)

4. About Turtle Introduction Many of the following we look at an example:

(i) draw a square:

 Import Turtle
 Import time
#定义绘制时画笔的颜色
 turtle.color ("purple")
#定义绘制时画笔的线条的宽度
 turtle.size ( 5)
#定义绘图的速度 
turtle.speed (Ten)
#以0, 0 for the start of the drawing
 Turtle.goto (0,0)
#绘出正方形的四条边 for
 i Range (4):
   Turtle.forward (turtle.right)
#画笔移动到点 ( -150,-120) do not draw
 turtle.up ()
 Turtle.goto ( -150,-120)
#再次定义画笔颜色
 turtle.color ("Red")
#在 ( -150,-120) dot print "Done"
 turtle.write ( "Done")
 Time.sleep (3)

(ii) Drawing of the pentagram:

Import Turtle
Import time
turtle.color ("purple")
turtle.pensize (5)
Turtle.goto (0,0)
Turtle.speed for I in
Range (6):
 turtle.forward
 turtle.right (144)
turtle.up ()
Turtle.forward (m)
Turtle.goto ( -150,-120)
turtle.color ("Red")
turtle.write ("Done"
) Time.sleep (3)

Here are two simple examples, we can be based on the above ideas and methods to further expand, draw some more complex graphics.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.