Syntax analysis of turtle program

Posted by smith.james0 on Tue, 04 Jan 2022 13:35:30 +0100

Syntax analysis of turtle program

1. Library reference and import

Tuttle Python drawing example code:

import turtle
turtle.setup(650.350.200.200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
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()

Library reference: a way to extend Python program functions

-① It is completed by using the import reserved word and adopts the encoding style of, ()

Import < library name >

< library name >< Function name > (< function parameter >)

-import more usage

-② Use the from and import reserved words together

From < library name > Import < function name >

From < library name > Import*

< function name > (< function parameter >)

At this time, the above functions are modified as follows:

from turtle import*
setup(650,350,200,200)
penup
fd(-250)
pendown()
pensize(25)
pencolor("purple")
seth(-40)
for i in range(4):
    circle(40,80)
    circle(-40,80)
circle(40,80/2)
fd(40)
circle(16,180)
fd(40*2/3)
done()

The above two methods have their own advantages and disadvantages

Import < library name >

***< library name >< Function name > (< function parameter >)***

In the above method, because < library name >< Function name > adds up to the function name in the new program. This form itself is a unified name, and there will be no duplication of function names first

***From < library name > Import < function name >***

From < library name > Import*

< function name > (< function parameter >)

In this method, since < function name > is a function, the function name in the library may be consistent with the user-defined function name, and the function name conflicts, resu lt ing in inconsistent program operation

Considering the above two methods, a new library reference method is formed:

-③ Using import and as reserved words

Import < library name > as < library name >

< library alias >< Function name > (< function parameter >)

To put it simply, the library is given a small name, which is used to call the function, and * * * < library alias >< The function name > * * * also constitutes a separate name in the program

This method minimizes the amount of redundant code and prevents the problem of duplicate library names

Rewrite the code as follows:

import turtle as t
t.setup(650.350.200.200)
t.penup()
t.fd(-250)
t.pendown()
t.pensize(25)
t.pencolor("purple")
t.seth(-40)
for i in range(4):
	t.circle(40,80)
    t.circle(-40,80)
t.circle(40,80/2)
t.fd(40)
t.circle(16,180)
t.fd(40*2/3)
t.done()

- writing a shorter and more suitable name for the external library called will bring a lot of convenience to the program.

2.turtle brush control function

Taking Python python drawing as an example, we use

penup()			pendown()
pensize()		pencolor()

The four brush control functions are always effective after the brush operation, and generally appear in pairs (such as penup() & pendown()). Generally speaking, we use penup() to make the sea ghost "fly" to a certain position, and then use pendown() to generate a track

turtle.pensize(width) alias turtle Width (width) is used to set the turtle's "waist circumference"

turtle. In pencolor (color), color is the color string or r, g, b value, which is used to change the color of the brush

- color string:

turtle.pencolor("purple")

Note that purple is in string form and should be in lowercase

- small value of RGB:

turtle.pencolor(0.63,0.13,0.94)

- RGB tuple value:

turtle.pencolor((0.63,0.13,0.94))

3.turtle motion control function

fd()            circle()

-turtle.forword(d) alias turtle fd(d)

Walking forward, the turtle walks in a straight line (it's not hard to think that bk is walking backward, which is the same as the effect of d being negative)

-turtle.circle(r,extent=None)

Draw an arc of extent angle according to radius r

-r: The default center is at the R distance to the left of the turtle. Two examples are given below:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-cogj8vwi-1640841885731) (C: \ users \ administration \ appdata \ roaming \ typora user images \ image-20211230124715646. PNG)]

4. Tilt direction control function

Controlling turtle facing direction: absolute angle & Turtle angle (two systems)

turtle.setheading(angle) alias turtle.seth(angle)

-Angle: change the direction of travel and the turtle's angle

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-fjqjkuev-1640841885733) (C: \ users \ administration \ appdata \ roaming \ typora user images \ image-20211230125243224. PNG)]

At Turtle angle:

turtle.left(angle) the turtle turns left

turtle.right(angle) the turtle turns right

Note: the direction control function only changes the turtle's travel angle and does not change the turtle's motion state

5. Basic loop statement

-for and in reserved words

For < variable > in range

< statements executed circularly >

-< variable > indicates the count of each cycle, from 0 to < times > - 1

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-yjclfbc-1640841885733) (C: \ users \ administration \ appdata \ roaming \ typora user images \ image-20211230130329278. PNG)]

range() function

Generate count sequence:
-range(N)

Generate an integer sequence from 0 to N-1, N in total

range(5)

​ 0,1,2,3,4

-range(M,N)

Generate an integer sequence from m to N-1, N-M in total

​ range(2,5)

​ 2, 3, 4

6.Python python drawing code analysis

import turtle	//Call library function
turtle.setup(650.350.200.200)	//Set window size and position
turtle.penup()	//Turtle Flight 
turtle.fd(-250)	//250 pixels backward in absolute direction coordinates
turtle.pendown//The turtle landed and began to produce painting marks
turtle.pensize(25)	//Set turtle "waist" 25
turtle.pencolor("purple")	//Set turtle Color Purple
turtle.seth(-40)	//Adjust - 40 ° in absolute angle
for i in range(4):	//Perform a crawl cycle
	turtle.circle(40,80)	
    turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)	//Forward 40
turtle.circle(16,180)	//Take the left distance 16 as the center of the circle and draw a circle with an arc of 180 degrees
turtle.fd(40*2/3)
turtle.done()

Topics: Python Pycharm IDE