The default direction of turtle brush in python, turtle drawing with text in python

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. Introducing the 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 x-axis direction in the turtle module points to the right, and the positive y-axis direction 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. Paintbrush

3.1. Brush status

The turtle module uses position and direction to describe the state 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-in parameter sets the brush color, which can be a string such as "green" , "red", can also be an 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) 
#或者tup1=(1.0, 0.0, 0.0)
#turtle.pencolor(tup1) #当然列表等其他可迭代序列也可以。同样也可以使用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() #提起画笔

turtle.pendown() #放下画笔,开始默认状态

turtle.forward(distance)#向当前画笔方向移动distance像素长度

turtle.backward(distance) #向当前画笔相反方向移动distance像素长度

turtle.right(degree) #顺时针移动degree°

turtle.left(degree) #逆时针移动degree°

turtle.goto(x,y) #将画笔移动到坐标为x,y的位置

setx( ) #将当前x轴移动到指定位置

sety( ) #将当前y轴移动到指定位置

turtle.circle() #画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆

setheading(angle) #设置当前朝向为angle角度

home() #将画笔回到原点,并且将画笔指向右侧。注意:当笔放下时,会留下移动到痕迹。

dot(r,color) 
#绘制一个直径为size,颜色为 color 的圆点。
#若size未指定,则直径取pensize+4和2*pensize中的较大值。
#若未指定color则color为pencolor。
3.3.2. Brush control commands
turtle.fillcolor(colorstring) #绘制图形的填充颜色

turtle.color(color1, color2) #同时设置pencolor=color1, fillcolor=color2

turtle.filling() #返回当前是否在填充状态

turtle.begin_fill() #准备开始填充图形

turtle.end_fill() #填充完成

turtle.hideturtle() #隐藏画笔的turtle形状

turtle.showturtle() #显示画笔的turtle形状
3.3.3. Global control commands
turtle.done() #使绘图窗口不会自动消失

turtle.clear() #清空turtle窗口,但是turtle的位置和状态不会改变

turtle.reset() #清空窗口,重置turtle状态为起始状态

turtle.undo() #撤销上一个turtle动作

turtle.isvisible() #返回当前turtle是否可见

stamp() #复制当前图形

turtle.write(s [,font=("font-name",font_size,"font_type")]) 
#写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;

4. Example

Example 1, draw a five-pointed star:

import turtle

t=turtle.Turtle() #创建turtle对象(建议方式)这样就可以创建多个画笔了
t.pencolor("red") #设置画笔颜色为红色

t.penup() #先提画笔
t.goto(-100,100) #移动画笔位置,使最后的图像居中
t.pendown() #放画笔

t.speed(2) #控制画笔速度
t.fillcolor("red") #填充颜色为红色
t.begin_fill() #开始填充颜色
for i in range(0,5):
    t.forward(200)
    t.right(144)
t.end_fill() #停止填充

t.hideturtle() #隐藏画笔的turtle形状
turtle.done() #使turtle窗口不会自动消失

Running screenshot:

Example 2, draw a medal - a five-pointed star nested in a circle:

import math
import turtle

turtle.colormode(255) #切换颜色模式,turtle.colormode(cmode=None)返回颜色模式或将其设为 1.0 或 255。有1.0模式和255模式
t1=turtle.Turtle()
t2=turtle.Turtle() #创建两个对象,turtle1用来画圆,turtle2用来画五角星

#画圆
r=100 #半径为100
t1.color((217,217,25),(217,217,25)) #前面一个参数是pencolor,后一个参数是fillcolor
t1.pensize(3)

t1.penup()
t1.goto(0,200)
t1.pendown()
t1.begin_fill()
t1.circle(r*(-1)-10) #其实原本半径为100,但是为了美观,就将半径放大了
t1.end_fill()
t1.hideturtle()
#画五角星
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:

Guess you like

Origin blog.csdn.net/chatgpt002/article/details/133277519