Python练习——Turtle库学习记录

概述:

turtle是Python标准库之一,能够进行基本的图形绘制

基本框架:

一个小海龟在坐标系中爬行,其爬行轨迹形成了绘制图形。对于小海龟来说,有“前进”、“后退”、“旋转”等爬行行为,对坐标系的探索也通过“前进方向”、“后退方向”、“左侧方向”和“右侧方向”等小海龟自身角度方位来完成。
在这里插入图片描述

turtle库使用:

在这里插入图片描述

画布:

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

turtle.screensize(canvwidth=None, canvheight=None, bg=None),参数分别为画布的宽(单位像素),, 背景颜色。
如:
turtle.screensize(800,600, "green")
turtle.screensize() #返回默认大小(400, 300)
turtle.setup(width=0.5, height=0.75, startx=None, starty=None),参数:width, height: 输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例,(startx, starty): 这一坐标表示矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心。

直接执行上面代码可能会出现闪退的问题,在代码结尾处加上turtle.done(),即可解决
在这里插入图片描述窗体函数:
turtle.setup(width, height, startx, starty)
作用:设置主窗体的大小位置
参数:width:窗口宽度,如果值是整数,表示像素值;如果值是小数,表示窗口宽度与屏幕的比例;height:窗口高度,如果值是整数,表示的像素值;如果值是小数,表示窗口高度与屏幕的比例;startx:窗口左侧与屏幕左侧的像素距离,如果值是None,窗口位于屏幕水平中央;starty:窗口顶部与屏幕顶部的像素距离,如果值是None,窗口位于屏幕垂直中央;

turtlr库常见命令:

(1) 画笔运动命令
在这里插入图片描述(2) 画笔控制命令
在这里插入图片描述(3) 全局控制命令
在这里插入图片描述(4) 其他命令
在这里插入图片描述
这里只是部分命令,详情见Python手册

样例:

1.五角星Plus

# coding=utf-8
import turtle
import time

turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("red")

turtle.begin_fill()
for _ in range(5):
    turtle.forward(200)
    turtle.right(144)
turtle.end_fill()
time.sleep(2)

turtle.penup()
turtle.goto(-150, -120)
turtle.color("violet")
turtle.write("Done", font=('Arial', 40, 'normal'))

turtle.mainloop()

在这里插入图片描述

2.简易五角星

import turtle
p = turtle.Turtle()
p.speed(3)
p.pensize(5)
p.color("black")
p.fillcolor("red")
p.begin_fill()
for i in range(5):
    p.forward(200)
    p.right(144)
p.end_fill()
turtle.done()

在这里插入图片描述

3.蟒蛇

import turtle
turtle.setup(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor('pink')
turtle.seth(-40)
for i in range(4):
    turtle.circle(40, 80)
    turtle.circle(-40, 80)
turtle.circle(40, 80/2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40*2/3)
turtle.done()

在这里插入图片描述

4.雪花plus

使用了random模块Python3 中 random模块

import turtle as t
import random as rd
def ground():
    t.hideturtle()
    t.speed(100)
    for i in range(400):
        t.pensize(rd.randint(5, 10))
        x = rd.randint(-400, 350)
        y = rd.randint(-280, -1)
        r = -y / 280
        g = -y / 280
        b = -y / 280
        t.pencolor((r, g, b))
        t.penup()
        t.goto(x, y)
        t.pendown()
        t.forward(rd.randint(40, 100))
def snow():
    t.hideturtle()
    t.pensize(2)
    t.speed(100)
    for i in range(100):
        r = rd.random()
        g = rd.random()
        b = rd.random()
        t.pencolor(r, g, b)
        t.penup()
        t.setx(rd.randint(-350, 350))
        t.sety(rd.randint(1, 270))
        t.pendown()
        dens = rd.randint(8, 12)
        snowsize = rd.randint(10, 14)
        for j in range(dens):
            t.forward(snowsize)
            t.backward(snowsize)
            t.right(360 / dens)
if __name__ == "__main__":
    t.setup(800, 600, 0, 0)
    t.tracer(False)
    t.bgcolor("black")
    snow()
    ground()
    t.tracer(True)
    t.mainloop()

在这里插入图片描述

5.科赫雪花

import turtle
def ice(size, n):
    if n == 0:
        turtle.fd(size)
    else:
        for angle in [0, 60, -120, 60]:
            turtle.left(angle)
            ice(size / 3, n - 1)
if __name__ == '__main__':
    turtle.setup(600, 600)
    turtle.penup()
    turtle.goto(-200, 100)
    turtle.pendown()
    turtle.pensize()
    level = 3
    ice(400, level)
    turtle.right(120)
    ice(400, level)
    turtle.right(120)
    ice(400, level)
    turtle.hideturtle()
    turtle.done()

在这里插入图片描述

6.太阳花

import turtle as t
import time
t.color("red","yellow")
t.speed(10)
t.begin_fill()
for x in range(50):
    t.forward(200)
    t.left(170)
t.end_fill()
time.sleep(2)
t.done()

在这里插入图片描述

7.彩虹线

import turtle as t
from random import randint as rint
t.shape("turtle")
t.pensize(5)
t.colormode(255)
t.bgcolor("black")
t.tracer(False)
for x in range(700):
    t.color(rint(0,255),rint(0,255),rint(0,255))
    t.circle(2*(1+x/4),5)
    t.speed(30)
    t.tracer(True)
t.done()

在这里插入图片描述

8.时钟

import turtle
from datetime import *
# 抬起画笔,向前运动一段距离放下
def Skip(step):
    turtle.penup()
    turtle.forward(step)
    turtle.pendown()
def mkHand(name, length):
    # 注册Turtle形状,建立表针Turtle
    turtle.reset()
    Skip(-length * 0.1)
    # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
    turtle.end_poly()
    # 返回最后记录的多边形。
    handForm = turtle.get_poly()
    turtle.register_shape(name, handForm)
def Init():
    global secHand, minHand, hurHand, printer
    # 重置Turtle指向北
    turtle.mode("logo")
    # 建立三个表针Turtle并初始化
    mkHand("secHand", 135)
    mkHand("minHand", 125)
    mkHand("hurHand", 90)
    secHand = turtle.Turtle()
    secHand.shape("secHand")
    minHand = turtle.Turtle()
    minHand.shape("minHand")
    hurHand = turtle.Turtle()
    hurHand.shape("hurHand")
    for hand in secHand, minHand, hurHand:
        hand.shapesize(1, 1, 3)
        hand.speed(0)
    # 建立输出文字Turtle
    printer = turtle.Turtle()
    # 隐藏画笔的turtle形状
    printer.hideturtle()
    printer.penup()
def SetupClock(radius):
    # 建立表的外框
    turtle.reset()
    turtle.pensize(7)
    for i in range(60):
        Skip(radius)
        if i % 5 == 0:
            turtle.forward(20)
            Skip(-radius - 20)
            Skip(radius + 20)
            if i == 0:
                turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
            elif i == 30:
                Skip(25)
                turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
                Skip(-25)
            elif (i == 25 or i == 35):
                Skip(20)
                turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
                Skip(-20)
            else:
                turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
            Skip(-radius - 20)
        else:
            turtle.dot(5)
            Skip(-radius)
        turtle.right(6)
def Week(t):
    week = ["星期一", "星期二", "星期三",
            "星期四", "星期五", "星期六", "星期日"]
    return week[t.weekday()]
def Date(t):
    y = t.year
    m = t.month
    d = t.day
    return "%s %d%d" % (y, m, d)
def Tick():
    # 绘制表针的动态显示
    t = datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second / 60.0
    hour = t.hour + minute / 60.0
    secHand.setheading(6 * second)
    minHand.setheading(6 * minute)
    hurHand.setheading(30 * hour)
    turtle.tracer(False)
    printer.forward(65)
    printer.write(Week(t), align="center",
                  font=("Courier", 14, "bold"))
    printer.back(130)
    printer.write(Date(t), align="center",
                  font=("Courier", 14, "bold"))
    printer.home()
    turtle.tracer(True)
    # 100ms后继续调用tick
    turtle.ontimer(Tick, 100)
if __name__ == "__main__":
    # 打开/关闭龟动画,并为更新图纸设置延迟。
    turtle.tracer(False)
    Init()
    SetupClock(160)
    turtle.tracer(True)
    Tick()
    turtle.mainloop()

在这里插入图片描述

9.小黄人

import turtle
t = turtle.Turtle()
wn = turtle.Screen()
turtle.colormode(255)
t.hideturtle()
t.speed(10)
t.penup()
t.pensize(4)
t.goto(100,0)
t.pendown()
t.left(90)
t.color((0,0,0),(255,255,0))
#身体绘制上色
t.begin_fill()
t.forward(200)
t.circle(100,180)
t.forward(200)
t.circle(100,180)
t.end_fill()
#右眼睛绘制上色
t.pensize(12)
t.penup()
t.goto(-100,200)
t.pendown()
t.right(100)
t.circle(500,23)
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()
t.penup()
t.goto(15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()
t.penup()
t.goto(35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#左眼睛绘制上色
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(90)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()
t.penup()
t.goto(-15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()
t.penup()
t.goto(-35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#嘴绘制上色
t.penup()
t.goto(-20,100)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(20,180)
t.left(90)
t.forward(40)
t.end_fill()
#裤子绘制上色
t.penup()
t.goto(-100,0)
t.pendown()
t.seth(0)
t.color("black","blue")
t.begin_fill()
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(160)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.seth(270)
t.penup()
t.goto(-100,0)
t.circle(100,180)
t.end_fill()
#左裤子腰带
t.penup()
t.goto(-70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(45)
t.forward(15)
t.left(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.left(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(-70,30)
t.dot()
#右裤腰带
t.penup()
t.goto(70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(135)
t.forward(15)
t.right(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.right(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(70,30)
t.dot()
#脚
t.penup()
t.goto(4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.left(90)
t.forward(40)
t.seth(20)
t.circle(10,180)
t.circle(400,2)
t.seth(90)
t.forward(20)
t.goto(4,-100)
t.end_fill()
t.penup()
t.goto(-4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(40)
t.seth(20)
t.circle(10,-225)
t.circle(400,-3)
t.seth(90)
t.forward(21)
t.goto(-4,-100)
t.end_fill()
#左手
t.penup()
t.goto(-100,50)
t.pendown()
t.seth(225)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.left(90)
t.forward(35)
t.seth(90)
t.forward(50)
t.end_fill()
#右手
t.penup()
t.goto(100,50)
t.pendown()
t.seth(315)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.right(90)
t.forward(36)
t.seth(90)
t.forward(50)
t.end_fill()
#
t.penup()
t.goto(0,-100)
t.pendown()
t.forward(30)
#
t.penup()
t.goto(0,-20)
t.pendown()
t.color("yellow")
t.begin_fill()
t.seth(45)
t.forward(20)
t.circle(10,180)
t.right(90)
t.circle(10,180)
t.forward(20)
t.end_fill()
#
t.penup()
t.color("black")
t.goto(-100,-20)
t.pendown()
t.circle(30,90)
t.penup()
t.goto(100,-20)
t.pendown()
t.circle(30,-90)
#头顶
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(135)
t.circle(100,40)
t.end_fill()
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(45)
t.circle(100,40)
t.end_fill()
turtle.done()

在这里插入图片描述

10.猫

from turtle import*
def curvemove():
    for i in range(200):
        right(1)
        forward(0.1)
def heart(x,y,s):
    pu()
    goto(x,y)
    seth(s)
    pendown()
    begin_fill()
    left(140)
    forward(11.1)
    curvemove()
    left(120)
    curvemove()
    forward(11.1)
    end_fill()
#初始化
setup(600,600)
pu()
goto(60,100)
pensize(4)
pendown()
#画左半边的头
for i in range(150,212,2):
    seth(i)
    fd(3)
seth(145)
fd(50)
left(125)
fd(50)
for i in range(240,318,2):
    if i==290:
        seth(190)
        fd(10)
        seth(10)
        fd(10)
    elif i==300:
        seth(200)
        fd(10)
        seth(20)
        fd(10)
    seth(i)
    fd(3)
#画右半边的头
pu()
goto(60,100)
pendown()
seth(45)
fd(50)
right(125)
fd(50)
for i in range(-60,-138,-2):
    if i==-110:
        seth(-10)
        fd(10)
        seth(170)
        fd(10)
    elif i==-120:
        seth(-20)
        fd(10)
        seth(160)
        fd(10)
    seth(i)
    fd(3)
#头部到这里就画好外观了
seth(-40)
fd(52)
seth(-135)
fd(45)
pu()
seth(-105)
fd(5)
pendown()
fd(17)
for i in range(130,106,-3):
    seth(i)
    fd(2.5)
for i in range(106,30,-10):
    seth(i)
    fd(2)
seth(38)
fd(25)
seth(135)
fd(31)
seth(169)
fd(6)
seth(270)
fd(105)
#右边的身子画好了
#开始画左边的身子
pu()
goto(-52,-30)
pendown()
seth(220)
fd(48)#52
seth(250)
fd(3)
seth(270)
fd(3)
seth(290)
fd(2)
seth(-40)
fd(44)
seth(228)
fd(20)
seth(5)
fd(22)
#画叉腰的动作
pu()
goto(-52,-84)
seth(133)
pendown()
fd(22)
seth(90)
fd(2)
seth(60)
fd(2)
seth(45)
fd(29)
seth(0)
fd(3)
seth(-93)
fd(102)
#叉腰动作结束 接下来画嘴巴 眼睛
pu()
goto(-43,38)
seth(0)
pendown()
begin_fill()
circle(5)
end_fill()
pu()
fd(108)
pendown()
begin_fill()
circle(5)
end_fill()
#调色环节
pu()
goto(60,24)
pencolor("pink")
pensize(6)
seth(225)
pendown()
fd(7)
pu()
goto(70,24)
seth(225)
pendown()
fd(7)
#右半边调色完毕
pu()
goto(-49,24)
seth(225)
pendown()
fd(7)
#画嘴巴
pu()
pensize(4)
pencolor("black")
goto(5,21)
seth(-45)
pendown()
fd(5)
goto(5,21)
seth(225)
fd(5)
#左边的颜色
pu()
pencolor("pink")
pensize(6)
goto(-39,24)
seth(225)
pendown()
fd(7)
#给耳朵填充颜色
pu()
goto(-40,92)
seth(80)
pendown()
fillcolor("pink")
begin_fill()
circle(14,360,3)
end_fill()
pu()
goto(72,100)
seth(-74)
pendown()
begin_fill()
circle(14,360,3)
end_fill()
#酷酷的黑翅膀
pu()
pensize(4)
color('black', 'black')
begin_fill()
goto(-90,-35)
seth(135)
pendown()
fd(25)
seth(225)
fd(45)
seth(25)
fd(15)
seth(-80)
pensize(2)
fd(15)
seth(55)
fd(15)
seth(25)
fd(10)
seth(-80)
fd(15)
seth(75)
fd(15)
goto(-90,-35)
end_fill()
#最后一个翅膀
pu()
goto(125,-30)
seth(45)
pendown()
begin_fill()
fd(25)
seth(-45)
fd(45)
seth(155)
fd(15)
seth(-80)
fd(15)
seth(120)
fd(17)
seth(170)
fd(15)
seth(-80)
fd(15)
seth(120)
fd(17)
goto(125,-30)
end_fill()
#画5颗心
speed(10)
color('red', 'pink')
pensize(2)
heart(0,140,0)
heart(-125,0,30)
heart(140,0,-30)
heart(145,-85,-30)
heart(-132,-85,30)
exitonclick()

在这里插入图片描述注:搬运的大佬的代码,膜拜,猫太可爱了~

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值