Detailed explanation of the ‘Turtle’ library in python: control the turtle mobile and brush attributes
Detailed explanation of the ‘Turtle’ library in python: control the turtle mobile and brush attributes
‘Turtle’ is a very interesting and powerful class library in Python. It provides a simple way to control the movement of turtles and draw graphics.The design of this class library is inspired by 'Turtle Graphics' in the LOGO language.
Using the "TURTLE" library, we can create a window on the screen and draw a variety of graphics by controlling the movement of the turtle.This article will introduce the use of the "TURTLE 'class library and some basic programming code and related configuration.
Before starting, we need to ensure that Python has been installed on the computer.If you do not install python, download and install the latest version from the official website (https://www.python.org/).
After installing Python, we can use the "Turtle 'class library.Below is a simple code for creating a window and a turtle graphic:
import turtle
window = turtle.Screen()
turtle = turtle.Turtle()
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
window.mainloop()
The above code first introduced the "TURTLE" library, and then created a window object named `Window` and a turtle object named` turtle`.By calling some methods of the `Turtle` object, we can control the movement of turtles and drawing graphics.
In the above code, `Turtle.Forward (100)` command lets the turtles move 100 pixels forward, `turtle.richt (90)` command let the turtles rotate to the right at 90 degrees, and then use the `turtle.Forward (100) The command let the turtles move 100 pixels forward.Finally, use the method of `Window.mainLoop () to keep the window open in order to view the drawn graphics.
The "Turtle" library also provides many other useful methods and attributes, which can be used to control the movement of turtles and the attributes of brushes.For example:
-`turtle.penup ()`: lifted the brush to make the turtles move without any traces.
-`turtle.pendown ()`: Put the brush down so that the turtles move the traces.
-` turtle.speed (1) `: Set the speed of turtles move, and the parameters can be any integer between 0 and 10.
-` turtle.color ("blue") `: Set the color of the brush, the parameter can be the color name or RGB value.
In addition to controlling the movement and attributes of turtles and brushes, the ‘Turtle’ library also supports many other functions, such as drawing circular, drawing polygon, and filling color.
To sum up, the "Turtle" library provides a simple and interesting way to control the movement and drawing graphics of the turtles.By learning and using this class library, we can create a variety of wonderful graphics and animations.Whether it is a beginner or an experienced developer, the ‘Turtle’ library is a very valuable tool.Let's explore it together!