Python Turtle Learning Chapter 1: Draw some simple graphics with Turtle

Preparation

First, to use Pythonthe built-in Turtledrawing library, you need to add the following code before the program:

import turtle

It can also be written like this:

from turtle import *

Let's talk about their differences: when
using importit, you need to define a variable as a parameter control item, such as:

import turtle
t=turtle.Pen()
# 画图
t.forward(10) # 向右画10像素,详见下表

When using from, you can directly command the program without redundant statements

from turtle import *

# 画图
forward(10) # 向右画10像素,详见下表

Basic functions and usage

function Function
forward(x)Shorthand:fd(x) Advance the length of pixels, if negative, draw in the opposite direction . xThe default forward direction is
backward(x)Shorthand:bk(x) Advance the length of pixels, if it is a negative value , draw xin the opposite direction ( )
goto(x,y) From current position to (x,y)draw line
circle(r,extent = x) draws an arc rwith radius and xangle
left(x)Shorthand:lt(x) xturn left
right(x)Shorthand:rt(x) xturn right
pencolor("red") Set the brush color to 红色(can be modified)
fillcolor("red") set 填充颜色to red
bgcolor(black) set 背景色to black
down() pen down function,没有参数
up() pen up function,没有参数
pensize(x) Set the brush thickness tox
speed(x) Set the drawing speed to x, the larger the value, the faster it 0is, the fastest
done() Pause the program, stop the brush drawing, but the drawing form will not be closed until the user closes Turtlethe graphical window,没有参数

drawing graphics

1. Draw a square

import turtle

t=turtle.Pen()
t.pencolor("orange") # 画笔颜色设置为橙色
for i in range(1,5):
    t.fd(50) # 向右画50像素
    t.lt(90) # 转90度,画下一条边

square

2. Draw a triangle

Steps: First draw a side, turn right 120°, then draw another side, turn right 120°, and finally draw a side

import turtle

t=turtle.Pen()

t.pencolor("orange") # 画笔颜色设置为橙色

t.fd(150)
t.rt(120)
t.fd(150)
t.rt(120)
t.fd(150)

triangle

3. Draw an octagonal star

import turtle
t=turtle.Pen()
t.pencolor("orange")
for x in range(1,9): # 执行8次
	t.fd(100)
	t.lt(135)

octagonal star

4. Draw an octagon

Key point: rotate after each side is drawn45°

import turtle

t=turtle.Pen()
t.pencolor("orange")
for i in range(1,9): #循环8次
    t.forward(50) #画线
    t.right(45) #转向

Octagon

We can also Turtledraw some interesting graphics

5. Other graphics

#test 1.1
import turtle

t=turtle.Pen()
t.pencolor("orange")

for i in range(1,4):
    t.fd(50)
    t.rt(120)
    t.fd(50)
    t.rt(120)
    t.fd(50)

1.1

import turtle         #导入turtle库
t = turtle.Pen()
t.pencolor("orange")
for i in range(5):    
    t.forward(100)      # 向右移动300
    t.right(180-180/5)  # 180-五角星的内角和/5
t.done()

Pentagram


So far, the graphics we have drawn have only one color, so how can we draw patterns of multiple colors?

We can first define a color list, and take out a color every time we draw 随机, and we can draw colorful patterns

colors=["red","orange","yellow","green","blue","purple"]

The code to randomly take out the color:

t.pencolor([colors%5]) 

No.1, colorful five-pointed star

import turtle         #导入turtle库
t = turtle.Pen()
colors=["red","orange","yellow","green","blue","purple"]
for i in range(5):   
	t.pencolor(colors[i%5]) 
    t.forward(100)      # 向右移动300
    t.right(180-180/5)  # 180-五角星的内角和/5
t.done()

No.2, colorful octagonal star

import turtle
t=turtle.Pen()
colors=["red","orange","yellow","green","blue","purple"]
for i in range(1,9): # 执行8次
	t.pencolor(colors[i%6])
	t.fd(100)
	t.lt(135)

No.3, four round mosaic

import turtle
t=turtle.Pen()
colors=["red","green","yellow","blue"]
for i in range(1,100):
	t.pencolor(colors[i%4])
	t.circle(i) #画圆
	t.lt(91)

No.4, rainbow spiral

import turtle
t=turtle.Pen()
colors=["red","orange","yellow","green","blue","purple"]
turtle.bgcolor("black")
for i in range(500000):
	t.pencolor(colors[i%6])
	t.fd(i)
	t.lt(64)
	t.width(i/10-1)


Guess you like

Origin blog.csdn.net/weixin_45122104/article/details/125812224