Python twist Drawing Library & drawing name example

Time:2020-7-7

*****Take a look at the change() and run() functions I defined******

Drawing coordinate system:

Function: set the size and position of the main form


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

#Width: the width of the window. If the value is an integer, it means the pixel value; if it is a decimal, it means the ratio of the window width to the screen.
#Height: window height. If the value is an integer, it means the pixel value; if it is a decimal, it means the ratio of the window height to the screen.
#Startx: the pixel distance between the left side of the window and the left side of the screen. If the value is none, the window is located in the horizontal center of the screen.
#Start: the pixel distance between the top of the window and the top of the screen. If the value is none, the window is located in the vertical center of the screen.

Brush control function:

The paintbrush in turtle can be controlled by a set of functions;

turtle.penup (); ා lift the brush (no trace when the brush moves)
turtle.pendown ();; ා put down the brush (when the brush moves, it is in the painting)
turtle.pensize (width) ා sets the size of the brush
turtle.width () ා sets the brush width, and returns the current brush width when there is no parameter input
turtle.pencolor (colorstring) ා set brush color
turtle.goto (x, y) ා move the brush to (x, y)

Shape drawing function:

turtle.fd (distance)
turtle.seth (angle) ා sets the direction of the current brush to angle
turtle.circle (radius, extension = none) ා draws an arc with an extension angle based on the radius radius

In order to simplify the code, I define two functions:

Change (x, y): set the brush position to (x, y)

Run (angle, length): the direction is angle, and the brush advance length is unit length


def run(angle, lenth):
  turtle.seth(angle)
  turtle.fd(lenth)
 
def change(x, y):
  turtle.penup()
  turtle.goto(x, y)
  turtle.pendown()

Here I use the Chinese character “Zhong” as an example

We abstract the word “Zhong” into a dot. (six points a, B, C, D, e, F as shown in the figure)

Then, we just want to connect them. Let’s first connect the four points of ABCD.

It turns into the picture below

Then connect two EF points. Done.

Corresponding code:

#zhong
Change (- 100, 80) ා place the brush at coordinate a (- 100, 80)
Run (0, 200) ා draw from point a to point B
Run (- 90, 120) ා draw from B to C
Run (180, 200) ා draw from point C to point D
Run (90, 120) ා draw from point d to point a
 
Change (0, 150) ා place brush at coordinate e (0150)
Run (- 90, 300) ා draw from point e to point F

Drawing your own name


import turtle
 
def run(angle, lenth):
  turtle.seth(angle)
  turtle.fd(lenth)
 
def change(x, y):
  turtle.penup()
  turtle.goto(x, y)
  turtle.pendown()
def init():
  turtle.pensize(10)
  turtle.pencolor("purple")
  
turtle.setup(800, 400, 200, 200)
init()
#wu
change(-350, 100)
run(0, 100)
run(-90, 70)
run(180, 100)
run(90, 70)
change(-350, 0)
run(0, 100)
change(-400, -50)
run(0, 200)
change(-300, 0)
run(-90, 50)
run(-125, 100)
change(-300, -50)
run(-55, 100)
run(0,10)
#feng
change(-65, 70)
run(0, 130)
change(-50, 10)
run(0, 100)
change(-100, -70)
run(0, 200)
change(0, -160)
run(90, 280)
#yuan
change(125, 100)
run(-35, 40)
change(125, 25)
run(-35, 40)
change(125, -125)
run(55, 90)
change(220, 100)
run(0, 110)
change(220, 100)
run(-95,230)
change(285, 85)
run(-145, 40)
change(250, 65)
run(0, 70)
run(-90, 80)
run(180, 70)
run(90, 80)
change(250, 25)
run(0, 70)
change(285,-15)
run(-90, 120)
run(135, 20)
change(250, -50)
run(-125, 40)
change(315, -50)
run(-55, 50)

The above example of Python turtle drawing library and name drawing is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developpaer more.