第二讲 召唤画图小海龟

turtle模块

turtle模块是Python语言的内置绘图模块,可以把屏幕当成一块画布,通过控制一个小三角形(或小海龟)的画布上移动,从而绘制成图形。 

 

可以使用turtle模块绘制很多好看的图形:

 

第一步:引入海龟库

#  引入海龟库,imoprt是导入、引入的意思
import turtle

接下来我们可以对画布进行设置

 

画布(canvas)

画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置。

常用的画布方法有两个:screensize()setup()

(1)turtle.screensize(width=None, height=None, bg=None)

参数分别为画布的宽(单位像素), 高, 背景颜色

如:

turtle.screensize(800, 600, "green")
turtle.screensize() #返回默认大小(400, 300)

(2)turtle.setup(width=0.5, height=0.75, startx=None, starty=None)

参数:

    • width, height:输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例
    • (startx, starty):这一坐标表示 矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心
      如:
turtle.setup(width=0.6, height=0.6)
turtle.setup(width=800, height=800, startx=100, starty=100)

如果不进行设置,则会使用默认的画布大小以及背景颜色。


绘图控制

画笔运动控制

turtle.forward()
#向当前画笔方向移动像素长度
turtle.backward()
#向当前画笔相反方向移动像素长度
turtle.right()
#顺时针移动degree°
turtle.left()
#逆时针移动degree°
turtle.goto(x,y)
#将画笔移动到坐标为x,y的位置
turtle.penup()
#提起笔移动,不绘制图形,用于另起一个地方绘制
turtle.pendown()
#移动时绘制图形,缺省时也为绘制
turtle.circle()
#画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆,Steps参数将会在指定大小的圆内画一个指定边数的内切图形
turtle.seth()
#旋转画笔

练习:

  1. 在画布上画一个正方形
  2. 在画布上画一个Z 
  3. 在画布上画一个三角形
  4. 在画布上绘制奥迪车标

 

画笔的设置以及背景颜色

turtle.shape('turtle')
#把三角形画笔设置成小海龟
turtle.pencolor(color)
#设置画笔的颜色
turtle.pensize(size)
#设置画笔的粗细
turtle.reset()
#清除画布上的所有内容,并使画笔恢复初始状态
turtle.clear()
#只清除画布上的所有内容,保留画笔的当前状态

 

fillcolor('Blue') :指定填充颜色

begin_fill()

...

end_fill()

五角星

from turtle import *

pensize(5)
pencolor('yellow')

fillcolor('red')
begin_fill()
for i in range(5):
    forward(200)
    right(144)
end_fill()
done()

 作业:绘制小汽车

 绘制太阳花

from turtle import *

pensize(1.5)
pencolor('red')
speed(20)
fillcolor('yellow')
begin_fill()
while True:
    forward(240)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

彩色的螺旋图形

 

from turtle import *

bgcolor('black')
speed(100)
Colors = ['red', 'yellow', 'blue', 'purple', 'green']
for i in range(1000):
    pencolor(Colors[i%4])
    fd(i)
    left(91)
done()

 带名字的螺旋图

"""
   绘制圆形螺旋线(多种颜色)
"""

from turtle import *   # 导入海龟绘图工具

pensize(1)  # 设置画笔宽度

# 设置颜色列表,用于设置绘制圆形的颜色
colors = ['red', 'green', 'blue', 'yellow']
speed(30)
# 创建一个循环(loop),让程序可以多次执行循环体内的代码
# 下面的循环一共执行80(从0开始,到79结束。在计算机程序中,循环计数默认都是从0开始的)
for x in range(80): 
    pencolor(colors[x%4]) # 根据 x % 4 的得到的余数(0,1,2,3)来设置画笔颜色
    circle(2*x)  # 以坐标原点(0,0)开始,以2倍的x为半径,进行绘制圆形
    left(90)   # 将画笔左转指定的角度,当前设置为左转90度

done()
from turtle import *

bgcolor("black")

my_name = textinput("输入你的姓名", "你的名字?")
colors = ["red", "yellow", "purple", "blue"]
for x in range(100):
    pencolor(colors[x % 4])
    penup()
    forward(x * 4)
    pendown()
    write(my_name, font=("Arial", int((x + 4) / 4), "bold"))
    left(92)
done()

太极图

 

import turtle


# 绘制太极图函数
def draw_TJT(R):
    turtle.screensize(800, 600, "blue")  # 画布长、宽、背景色 长宽单位为像素
    turtle.pensize(1)  # 画笔宽度
    turtle.pencolor('black')  # 画笔颜色
    turtle.speed(10)  # 画笔移动速度

    TJT_color = {1: 'white', -1: 'black'}  # 太极图填充色 1 白色 -1 黑色
    color_list = [1, -1]

    """
    先画半边,再画另一边
    """
    for c in color_list:
        turtle.fillcolor(TJT_color.get(c))  # 获取该半边的填充色
        turtle.begin_fill()  # 开始填充

        # 开始画出半边的轮廓
        turtle.circle(R / 2, 180)
        turtle.circle(R, 180)
        turtle.circle(R / 2, -180)

        turtle.end_fill()  # 结束填充 上色完成

        # 绘制该半边的鱼眼
        turtle.penup()  # 提起画笔,移动不留痕
        turtle.goto(0, R / 3 * c)  # 移动到该半边的鱼眼的圆上 R/3*c 表示移动到哪边
        turtle.pendown()  # 放下画笔,移动留痕
        turtle.fillcolor(TJT_color.get(-c))  # 获取鱼眼填充色, 与该半边相反
        turtle.begin_fill()
        turtle.circle(-R / 6, 360)
        turtle.end_fill()

        # 回到原点,为下一循环的开始做准备
        turtle.penup()
        turtle.goto(0, 0)
        turtle.pendown()

    # 绘制文本
    turtle.penup()
    turtle.goto(0, -R - 50)
    turtle.pendown()



if __name__ == '__main__':
    R = 100  # 太极图半径
    draw_TJT(R)
    input('Press Enter to exit...')  # 防止程序运行完成后就自动关闭窗
View Code

绘制雪花

 

from turtle import *
from random import *


def ground():
    hideturtle()
    speed(100)
    for i in range(400):
        pensize(randint(5, 10))
        x = randint(-400, 350)
        y = randint(-280, -1)
        r = -y / 280
        g = -y / 280
        b = -y / 280
        pencolor(r, g, b)
        penup()
        goto(x, y)
        pendown()
        forward(randint(40, 100))


def snow():
    hideturtle()
    speed(100)
    pensize(2)
    for i in range(100):
        r = random()
        g = random()
        b = random()
        pencolor(r, g, b)
        penup()
        setx(randint(-350, 350))
        sety(randint(1, 270))
        pendown()
        dens = randint(8, 12)
        snowsize = randint(10, 14)
        for j in range(dens):
            forward(snowsize)
            backward(snowsize)
            right(360 / dens)


def main():
    setup(800, 600, 0, 0)
    # tracer(False)
    bgcolor("black")
    snow()
    ground()
    # tracer(True)
    mainloop()


main()
View Code

小猪佩奇

from turtle import *

def nose(x,y):#鼻子
    penup()#提起笔
    goto(x,y)#定位
    pendown()#落笔,开始画
    setheading(-30)#将乌龟的方向设置为to_angle/为数字(0-东、90-北、180-西、270-南)
    begin_fill()#准备开始填充图形
    a=0.4
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            left(3) #向左转3度
            forward(a) #向前走a的步长
        else:
            a=a-0.08
            left(3)
            forward(a)
    end_fill()#填充完成

    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    pencolor(255,155,192)#画笔颜色
    setheading(10)
    begin_fill()
    circle(5)
    color(160,82,45)#返回或设置pencolor和fillcolor
    end_fill()

    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255,155,192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160,82,45)
    end_fill()


def head(x,y):#
    color((255,155,192),"pink")
    penup()
    goto(x,y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300,-30)
    circle(100,-60)
    circle(80,-100)
    circle(150,-20)
    circle(60,-95)
    setheading(161)
    circle(-300,15)
    penup()
    goto(-100,100)
    pendown()
    setheading(-30)
    a=0.4
    for i in range(60):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            lt(3) #向左转3度
            fd(a) #向前走a的步长
        else:
            a=a-0.08
            lt(3)
            fd(a)
    end_fill()


def ears(x,y): #耳朵
    color((255,155,192),"pink")
    penup()
    goto(x,y)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50,50)
    circle(-10,120)
    circle(-50,54)
    end_fill()

    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50,50)
    circle(-10,120)
    circle(-50,56)
    end_fill()


def eyes(x,y):#眼睛
    color((255,155,192),"white")
    penup()
    setheading(90)
    forward(-20)
    setheading(0)
    forward(-95)
    pendown()
    begin_fill()
    circle(15)
    end_fill()

    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()

    color((255,155,192),"white")
    penup()
    seth(90)
    forward(-25)
    seth(0)
    forward(40)
    pendown()
    begin_fill()
    circle(15)
    end_fill()

    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()


def cheek(x,y):#
    color((255,155,192))
    penup()
    goto(x,y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()


def mouth(x,y): #
    color(239,69,19)
    penup()
    goto(x,y)
    pendown()
    setheading(-80)
    circle(30,40)
    circle(40,80)

def body(x,y):#身体
    color("red",(255,99,71))
    penup()
    goto(x,y)
    pendown()
    begin_fill()
    setheading(-130)
    circle(100,10)
    circle(300,30)
    setheading(0)
    forward(230)
    setheading(90)
    circle(300,30)
    circle(100,3)
    color((255,155,192),(255,100,100))
    setheading(-135)
    circle(-80,63)
    circle(-150,24)
    end_fill()


def hands(x,y):#
    color((255,155,192))
    penup()
    goto(x,y)
    pendown()
    setheading(-160)
    circle(300,15)
    penup()
    setheading(90)
    forward(15)
    setheading(0)
    forward(0)
    pendown()
    setheading(-10)
    circle(-20,90)

    penup()
    setheading(90)
    forward(30)
    setheading(0)
    forward(237)
    pendown()
    setheading(-20)
    circle(-300,15)
    penup()
    setheading(90)
    forward(20)
    setheading(0)
    forward(0)
    pendown()
    setheading(-170)
    circle(20,90)

def foot(x,y):#
    pensize(10)
    color((240,128,128))
    penup()
    goto(x,y)
    pendown()
    setheading(-90)
    forward(40)
    setheading(-180)
    color("black")
    pensize(15)
    fd(20)

    pensize(10)
    color((240,128,128))
    penup()
    setheading(90)
    forward(40)
    setheading(0)
    forward(90)
    pendown()
    setheading(-90)
    forward(40)
    setheading(-180)
    color("black")
    pensize(15)
    fd(20)

def tail(x,y):#尾巴
    pensize(4)
    color((255,155,192))
    penup()
    goto(x,y)
    pendown()
    seth(0)
    circle(70,20)
    circle(10,330)
    circle(70,30)

def setting():          #参数设置
    pensize(4)
    hideturtle()        #使乌龟无形(隐藏)
    colormode(255)      #将其设置为1.0或255.随后 颜色三元组的r,g,b值必须在0 .. cmode范围内
    color((255,155,192),"pink")
    setup(840,500)
    speed(10)

def main():
    setting()           #画布、画笔设置
    nose(-100,100)      #鼻子
    head(-69,167)       #
    ears(0,160)         #耳朵
    eyes(0,140)         #眼睛
    cheek(80,10)        #
    mouth(-20,30)       #
    body(-32,-8)        #身体
    hands(-56,-45)      #
    foot(2,-177)        #
    tail(148,-155)      #尾巴
    done()

if __name__ == '__main__':
    main()
社会人

作业:

绘制奥运五环

原文地址:https://www.cnblogs.com/lw1095950124/p/12132672.html