3. Python learning (1) Introduction to the turtle library and canvas coordinates of the turtle module

Insert image description here
1. Introduction to turtle library

Turtle, also called turtle renderer, uses the Turtle library to draw pictures, which is also called turtle drawing. The Turtle library is a very popular function library for drawing images in the Python language.
Imagine a little turtle, starting from the origin of a coordinate system with the horizontal axis as Graphics are drawn on the path.

2. Canvas (that is, we need a piece of rice paper to draw)

The canvas is the turtle that expands the drawing area for us, and we can set its size and initial position.
In this lesson, we mainly study the Cartesian coordinate system, learn to use Python coordinates for positioning, and draw using the turtle module.

Set canvas size

turtle.screensize(canvwidth=None,canvheight=None,bg=None) , the parameters are the width (unit pixel), height, and background color of the canvas.

如:turtle.screensize(800,600,“green”)

turtle.screensize() #Return to the default size (400,300)

turtle.setup(width=0.5,height=0.75,startx=None,starty=None) , parameters: width, height: When the input width and height are integers, they represent pixels; when they are decimals, they represent the proportion of the computer screen, ( startx,starty): This coordinate represents the position of the upper left corner of the rectangular window. If it is empty, the window is located in the center of the screen.

如:turtle.setup(width=0.6,height=0.6)

turtle.setup(width=800,height=800,startx=100,starty=100)

Demo:
Insert image description here

​Coordinate system for turtle drawing.
The coordinate system for turtle drawing is the same as the mathematical plane rectangular coordinate system . The initial point is (0, 0). Although there is a blank canvas
in the middle of the canvas , the coordinates of the turtle refer to the following x-axis and y-axis. The two Insert image description here
numerical
axes that
are perpendicular to each other on the same plane and have a common origin constitute a plane rectangular coordinate system, referred to as the rectangular coordinate system (Rectangular Coordinates). Usually, the two number axes are placed in the horizontal position and the vertical position respectively, and the right and upward directions are the positive directions of the two number axes respectively. The horizontal number axis is called the x-axis or the horizontal axis, the vertical number axis is called the y-axis (y-axis) or the vertical axis. The x-axis and the y-axis are collectively called the coordinate axes, and their common origin O is called the rectangular coordinate system. Origin, the plane rectangular coordinate system with point O as the origin is recorded as the plane rectangular coordinate system xOy.

Demo:

Insert image description here
At the beginning, the little turtle is at the origin, so the position is printed out and called print(t.pos()), which is displayed as (0, 0).
Then we let the little turtle crawl to (100, 100). According to the plane rectangular coordinate system, It should be crawling to the right and upward.
Therefore, the little turtle crawls diagonally upward as shown in the figure. The final position is printed out and is displayed as (100, 100)

Guess you like

Origin blog.csdn.net/weixin_30197685/article/details/100831907