zoukankan      html  css  js  c++  java
  • python学习-71 turtle第三方库

    turtel意为:乌龟

    此库 是用来画图的,又称乌龟画图。

    因为最近在计划考证书。所以很久没更新了,但是小白也在努力的学习。2020一起努力,加油!

    下面给大家几个好看的编码。

    有几种编程方式可以:

    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(10000)
    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()

    执行之后 是一个插着腰的猫。

    2.小狗

    #-*-coding:GBK -*-
    import turtle as t
    
    t.screensize(500,500)
    t.pensize(5)
    t.color('black')
    # 狗头
    t.circle(20,80)
    t.circle(200,30)
    t.circle(30,60)
    t.circle(200,29.5)
    t.circle(20,60)
    t.circle(-150,22)
    t.circle(-50,10)
    t.circle(50,70)
    
    # 先确定鼻子的大概位置
    x_nose = t.xcor()
    y_nose = t.ycor()
    t.circle(30,62)
    t.circle(200,15)
    
    # 鼻子
    t.penup()
    t.goto(x_nose,y_nose+25)
    t.seth(90)
    t.pendown()
    t.begin_fill()
    t.circle(8)
    t.end_fill()
    
    
    # 眼睛
    t.penup()
    t.goto(x_nose+48,y_nose+55)
    t.seth(90)
    t.pendown()
    t.begin_fill()
    t.circle(8)
    t.end_fill()
    
    # 耳朵
    t.penup()
    t.goto(x_nose+100,y_nose+110)
    t.seth(182)
    t.pendown()
    t.circle(15,45)
    t.color('black')
    t.circle(10,15)
    t.circle(90,70)
    t.circle(25,110)
    t.rt(4)
    t.circle(90,70)
    t.circle(10,15)
    t.color('#444444')
    t.circle(15,45)
    
    # 身体
    
    t.pu()
    t.color('black')
    t.goto(x_nose+90,y_nose-30)
    t.seth(-130)
    t.pd()
    
    t.circle(250,28)
    t.circle(10,140)
    t.circle(-250,25)
    t.circle(-200,25)
    t.circle(-50,85)
    t.circle(8,145)
    t.circle(90,45)
    t.circle(550,5)
    
    #尾巴
    t.seth(0)
    t.circle(60,85)
    t.circle(40,65)
    t.circle(40,60)
    
    t.lt(150)
    t.circle(-40,90)
    t.circle(-25,100)
    
    t.lt(5)
    t.fd(20)
    
    t.circle(10,60)
    
    
    #
    t.rt(80)
    t.circle(200,35)
    
    
    # 项圈
    t.pensize(20)
    t.color('#F03c3F')
    t.lt(10)
    t.circle(-200,25)
    
    
    
    # 爱心铃铛
    t.pu()
    t.fd(18)
    t.lt(90)
    t.fd(18)
    t.pensize(2)
    t.seth(35)
    t.color('#FDAF17')
    t.begin_fill()
    t.lt(135)
    t.fd(6)
    t.right(180)
    t.circle(6,-180)
    
    t.backward(8)
    t.right(90)
    t.fd(6)
    t.circle(-6,180)
    t.fd(15)
    t.end_fill()
    
    
    # 前小腿
    t.pensize(5)
    t.pu()
    t.color('black')
    t.goto(x_nose+100,y_nose-125)
    t.pd()
    
    t.seth(-50)
    t.fd(25)
    t.circle(10,150)
    t.fd(25)
    
    
    # 后腿
    t.pensize(4)
    t.pu()
    t.goto(x_nose+314,y_nose-125)
    t.pd()
    
    t.seth(-95)
    t.fd(25)
    t.circle(-5,150)
    t.fd(2)
    
    t.hideturtle()
    t.done()

    执行结果是一个可爱的小狗。

    3.爱心

    import turtle as t
    t.color('red','red')
    t.pensize(2)
    t.begin_fill()
    
    #右半边
    t.left(50)
    t.fd(220)
    t.circle(80,220)
    #左半边
    t.right(180)
    t.circle(80,220)
    t.fd(220)
    
    t.end_fill()
    t.hideturtle()
    t.done()

    执行结果是一个很大的爱心。

    4.那么如何安装turtle库呢

    一个可以用pip

    pip install turtle

    pip3 install turtle

    或者从官网下载turtle库的包,具体安装步骤官网会有百度即可。

    如果有 time out 安装库时,提示网络超时的时候,需要用一下方法:(百度也有)

    pip install -i http://e.pypi.python.org/simple turtle

    格式:pip install -i 清华/豆瓣等能下载的国内链接 库名

    以下是几个网址:

    阿里云 http://mirrors.aliyun.com/pypi/simple/
    中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
    豆瓣(douban) http://pypi.douban.com/simple/
    清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
    中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

    等等。

    祝大家新年快乐!

     

  • 相关阅读:
    【题解】CF#983 E-NN country
    【题解】CF#403 D-Beautiful Pairs of Numbers
    【题解】CF#285 E-Positions in Permutations
    【题解】FJOI2015火星商店问题
    【题解】Atcoder AGC#01 E-BBQ Hard
    【题解】Atcoder AGC#03 E-Sequential operations on Sequence
    【题解】CF#280 C-Game on Tree
    【题解】CF#833 B-The Bakery
    [BZOJ3600] 没有人的算术 [重量平衡树+权值线段树]
    [bzoj3514][CodeChef GERALD07] Chef ans Graph Queries [LCT+主席树]
  • 原文地址:https://www.cnblogs.com/liujinjing521/p/12145490.html
Copyright © 2011-2022 走看看