This article mainly introduces the turtle canvas size setting in python, which has certain reference value. Friends in need can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to understand it together.
1. Introduction and basic knowledge of turtle
1.1. Introduction to turtle module
Turtle Graphics, a Python built-in module, is a simple drawing tool. Using Turtle Graphics, you can write programs that repeatedly perform simple actions to draw fine and complex shapes. There are two tool elements in turtle: canvas (Canvas) and brush. Installation steps for the Python interpreter.
2. Canvas
2.1. Introduction to canvas
The canvas is the area used by the turtle module for drawing. There is a coordinate axis on the canvas, and the coordinate origin is at the center of the canvas.
Coordinate system: The positive direction of thex-axis in the turtle module points to the right, and the positive direction of the y-axis points upward. The coordinate origin is at the center of the canvas.
2.2. Use of canvas
Mainly introduces two functions:
1. Set the size and background color of the canvas
turtle.screensize(canvwidth=None, canvheight=None, bg=None)
- canvwidth: width of canvas
- canvheight: the height of the canvas
- bg: background color
- When the width or height is an integer, it represents pixels; when it is a decimal, it represents the proportion of the computer screen. Scroll bars appear when the height or width exceeds the window size.
- If no value is set, the default parameter is (400, 300, None)
2. Set the size and position of the canvas
turtle.setup(width, height, startx=None, starty=None)
- 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 vertex of the rectangular window. If empty, the window is centered on the screen
3. Brush
3.1. Brush status
The turtle module uses position and direction to describe the status of the brush.
By default, the coordinates of the brush are the origin of the coordinates, the direction points to the positive direction of the x-axis, and the brush is in the down state.
3.2. Properties of brushes
Brush attributes: color, line width, movement speed, etc.
turtle.pensize(): Set the width of the brush. The larger the number, the thicker the brush;
turtle.pencolor(): No parameters are passed in, and the current brush color is returned. The passed parameters set the brush color, which can be strings such as “green”, “red”, or rgb3 tuple. Note: When it is rgb, the values of r, g, and b are between 0.0 and 1.0.
For example:
turtle.pencolor(1.0, 0.0, 0.0) #or tup1=(1.0, 0.0, 0.0) #turtle.pencolor(tup1) #Of course, other iterable sequences such as lists can also be used. You can also use turtle.pencolor((tup1)) turple.pencolor("red") turtle.pencolor("#32c18f")
turtle.speed(speed): Set the pen movement speed. The pen drawing speed range is [0,10] integer. The larger the number, the faster the movement speed.
3.3. Drawing commands
Drawing commands are usually divided into three categories: brush movement commands, brush control commands, and global control commands.
3.3.1. Brush movement command
turtle.penup() #Pick up the brush turtle.pendown() #Put down the brush and start the default state turtle.forward(distance)#Move distance pixel length to the current brush direction turtle.backward(distance) #Move distance pixel length in the opposite direction of the current brush turtle.right(degree) #Move degree° clockwise turtle.left(degree) #Move degree° counterclockwise turtle.goto(x,y) #Move the brush to the position with coordinates x,y setx() #Move the current x-axis to the specified position sety() #Move the current y-axis to the specified position turtle.circle() #Draw a circle with a positive (negative) radius, which means the center of the circle is to the left (right) of the brush to draw a circle setheading(angle) #Set the current heading to angle. home() #Return the brush to the origin and point the brush to the right. NOTE: When the pen is put down, movement marks will be left. dot(r,color) #Draw a dot with diameter size and color color. #If size is not specified, the diameter takes the larger of pensize + 4 and 2*pensize. #If color is not specified, color is pencolor.
3.3.2. Brush control commands
turtle.fillcolor(colorstring) #Fill color for drawing graphics turtle.color(color1, color2) #Set pencolor=color1, fillcolor=color2 at the same time turtle.filling() #Return whether it is currently filling turtle.begin_fill() #Ready to start filling graphics turtle.end_fill() #Filling completed turtle.hideturtle() #Hide the turtle shape of the brush turtle.showturtle() #Display the turtle shape of the brush
3.3.3. Global control commands
turtle.done() #So that the drawing window will not disappear automatically turtle.clear() #Clear the turtle window, but the position and status of the turtle will not change turtle.reset() #Clear the window and reset the turtle state to the starting state turtle.undo() #Undo the previous turtle action turtle.isvisible() #Return whether the current turtle is visible stamp() #Copy the current graphic turtle.write(s [,font=("font-name",font_size,"font_type")]) #Write text, s is the text content, font is the parameter of the font, which are the font name, size and type;
4. Example
Example 1, draw a five-pointed star:
import turtle t=turtle.Turtle() #Create a turtle object (recommended method) so that you can create multiple brushes t.pencolor("red") #Set the brush color to red t.penup() #Put up the brush first t.goto(-100,100) #Move the brush position so that the final image is centered t.pendown() #Put the brush t.speed(2) #Control brush speed t.fillcolor("red") #The fill color is red t.begin_fill() #Start filling color for i in range(0,5): t.forward(200) t.right(144) t.end_fill() #Stop filling t.hideturtle() #Hide the turtle shape of the brush turtle.done() #So that the turtle window will not disappear automatically
Running screenshot:
Example 2, draw a medal – a five-pointed star nested in a circle:
import math import turtle turtle.colormode(255) #Switch the color mode, turtle.colormode(cmode=None) returns the color mode or sets it to 1.0 or 255. There are 1.0 mode and 255 mode t1=turtle.Turtle() t2=turtle.Turtle() #Create two objects, turtle1 is used to draw circles, turtle2 is used to draw five-pointed stars #drawcircle r=100 #radius is 100 t1.color((217,217,25),(217,217,25)) #The first parameter is pencolor, the next parameter is fillcolor t1.pensize(3) t1.penup() t1.goto(0,200) t1.pendown() t1.begin_fill() t1.circle(r*(-1)-10) #In fact, the original radius was 100, but for the sake of beauty, the radius was enlarged. t1.end_fill() t1.hideturtle() #drawpentacle t2.color("red","red") t2.penup() t2.goto(r*(-1)*math.cos(math.pi/10),r*math.sin(math.sin(math.pi/10)) + r) t2.pendown() t2.begin_fill() for i in range(0,5): t2.forward(100*math.cos(math.pi/10)*2) t2.right(144) t2.end_fill() t2.hideturtle() turtle.done()
The effect is as follows: