The Attribute and Usage of Python Brush

  • 2021-09-16 07:30:32
  • OfStack

Brushes have attributes such as color and width of lines.

1. turtle. pensize (): Set the width of the brush;

2. turtle. pencolor (): No parameter is passed in to return the current brush color; The passed-in parameter sets the brush color, which can be a string such as "green", "red", or an RGB 3 tuple


>>> pencolor('brown')
>>> tup = (0.2, 0.8, 0.55)
>>> pencolor(tup)
>>> pencolor()
'#33cc8c'

3. turtle. speed (speed): Set the moving speed of the brush. The speed range of the brush drawing is [0, 10] integer. The larger the number, the faster it is

Extension of knowledge points:

Turtle Graphics Library is a popular way to teach children to learn programming. It is a part of the original icon programming language developed by Wally, Feurzig and Seymour, Paper in 1966.

Imagine that there is a small machine turtle at the origin (x=0, y=0) of a 2-dimensional plane of x-y. The packet is introduced through import turtle, and the command turtle. forward (15) is issued to the small turtle. The small turtle moves 15 pixels along the current direction and draws a straight line. Issue the command turtle. right (25), then rotate 25 degrees clockwise. Similarly, the instruction turtle. left (25) rotates 25 degrees counterclockwise, and the instruction turtle. forward (-15) moves 15 pixels in the opposite direction.

By combining these similar commands into one, it is easy to draw intricate shapes and graphics.

For the example of a 5-point star, move 200 pixels to the right along the x axis from the origin, then 170 degrees counterclockwise, move another 200 pixels, and so on. The function turtle. pos () can obtain the current coordinates of the machine tortoise (x, y), while the method abs (turtle. pos ()) can obtain the straight line distance from the origin of the machine tortoise (calculated according to Pythagorean theorem). After several cycles, the machine turtle will pass through the origin again (x=0, y=0). At this time, if the straight line distance is less than 1, it will jump out of the cycle and the program execution will end.


Related articles: