Draw graphics with Python's turtple

Source: Internet
Author: User

I do not know if you remember in primary school or junior high school, we have contact with a language called logo language, the language can draw squares, draw triangles, draw a circle. and using Python to draw a graphic is a bit like the meaning of logo language.


Before drawing, we need to start a module, Turtple. Then in this turtple there is a property called a pen, as the name implies is the meaning of the brush. When I finish typing pen this property, idle will open a thing called Python Turtle Graphics, in the middle of the screen there is a triangular origin, this point is a bit like the logo point, we are operating it to move back and forth left traces. After writing a sentence in idle, the graphics in the back will be drawn together, and then one more sentence, then a drawing. If it is a direct. py file, then the graphics will appear as steps.

Import Turtplet=turtple. Pen () #设定这个t就是整个的变量, note uppercase P


Let the origin go forward 50 pixels: T.forward (50)

Let the origin turn left 90 degrees: T.left (90)

Turn the origin to the right 30 degrees: T.right (30)

Let the origin lift up: t.up () #这样使用forward就不会留下痕迹, because the pen is lifted on paper.

Let the origin point fall: T.down ()

Let the origin be Blue: T.color ("Blue")

Need to use a blue hatch pattern: T.begin_fill () #需要和上面的语句搭配

Hatch Pattern Stop: T.end_fill ()

Clear left and right patterns and origin back to original position: T.reset ()

Let the origin move to the location of (70,100): T.goto (70,100) #注意中间是逗号, the default entire pattern is four quadrants

By understanding a few simple statements above, you can draw a eight-edged shape, such as this one:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/61/wKioL1c_G8ijLP1uAABke6ok2YM677.png "title=" 11.png "alt=" Wkiol1c_g8ijlp1uaabke6ok2ym677.png "/>

The program for this diagram is more flexible:

Import timeimport turtle#x Edge and 180* (x-3) #函数: gives the size and number of vertices to draw the star #size: The core of the star is an equilateral polygon, which is the distance from the vertex of the polygon to its center # Point: Top Def funcdrawstar (size, point):     t = turtle. Pen ()      #调校坐标位置     t.up ()     t.backward (200)     t.right (    t.forward)     t.left (90)     t.down ()      #开始画图     t.color (1, 0, 0 )     t.begin_fill ()     for x in range (1, point  * 2 + 1):         t.forward (size)          if x % 2 == 0:             t.left (        else):             t.right (180 *  (point - 2)  / point - 60)      t.end_fill ()      #funcDrawStar (100, 6) Funcdrawstar (100, 9) time.sleep (5)




There are some simple applications, but know that Python can also draw wonderful graphics, such as:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/63/wKiom1c_GEyDBxhSAAB1vJJy87k758.png "title=" 11.png "alt=" Wkiom1c_geydbxhsaab1vjjy87k758.png "/>


The code of this figure is as follows (the code is reproduced, thanks to Daniel to share, Python 3.5 Pro test, and the whole process is very beautiful, strongly recommend you feel. )

From  numpy import *from random import randomimport turtleturtle.reset () X = array ([[. 5],[.5]]) P = [0.85,0.92,0.99,1.00]a1 = array ([[.85,   0.04],&NBSP;[-0.04,.85]] B1 = array ([[[0],[1.6]]) a2 = array ([[0.20,-0.26], [0.23,0.22]] ]) B2 = array ([[[0],[1.6]]) A3 = array ([[[ -0.15,0.28],[0.26,0.24]]) b3 = array ([[0],[ 0.44]] A4 = array ([[[0,0], [0,0.16]]) turtle.color ("Red") cnt = 1while true:     cnt += 1    if cnt == 2000:         break    r = random ()     if  r < p[0]:        x = dot (A1 , x )  + b1    elif r < p[1]:         x = dot (a2 , x)  + b2    elif r < p[2]:         x = dot (a3 , x)  + b3     Else:        x = dot (a4 , x)       #print  x[1]    turtle.up ()     turtle.goto (x[0][0] *  50,x[1][0] * 40 - 240)       turtle.down ()        turtle.dot ()


This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1775558

Draw graphics with Python's turtple

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.