Python海龟turtle画图常见画图代码大全

转载于https://blog.csdn.net/July__July/article/details/99543992
玫瑰花

import turtle

# 设置初始位置
turtle.penup()  # 提起画笔
turtle.left(90)  # 逆时针旋转九十度
turtle.fd(200)  # 向前移动一段距离 fd=forward
turtle.pendown()  # 放下画笔移动画笔开始绘制
turtle.right(90)   # 顺时针旋转九十度

# 花蕊
turtle.fillcolor("red")  # 填充颜色
turtle.begin_fill()  # 开始填充
turtle.circle(10, 180)  # 画一圆,10是半径,180是弧度
turtle.circle(25, 110)
turtle.left(50)
turtle.circle(60, 45)
turtle.circle(20, 170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30, 110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90, 70)
turtle.circle(30, 150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80, 90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150, 80)
turtle.left(50)
turtle.circle(150, 90)
turtle.end_fill()  # 结束填充

# 花瓣1
turtle.left(150)
turtle.circle(-90, 70)
turtle.left(20)
turtle.circle(75, 105)
turtle.setheading(60)  # urtle.setheading(angle) 或 turtle.seth(angle):改变行进方向 angle:行进方向的绝对角度,可以为负值
turtle.circle(80, 98)
turtle.circle(-90, 40)

# 花瓣2
turtle.left(180)
turtle.circle(90, 40)
turtle.circle(-80, 98)
turtle.setheading(-83)

# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80, 90)
turtle.right(90)
turtle.circle(-80, 90)
turtle.end_fill()

turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)

# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80, 90)
turtle.left(90)
turtle.circle(80, 90)
turtle.end_fill()

turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200, 60)

# 设置成画完不会自动退出
turtle.done()

画爱心

import turtle
import time


# 画爱心的顶部
def LittleHeart():
    for i in range(200):
        turtle.right(1)
        turtle.forward(2)


# 输入表白的语句,默认I Love you
love = input('Please enter a sentence of love, otherwise the default is "I Love you": ')   #
# 输入署名或者赠谁,没有不执行
me = input('Please enter pen name, otherwise the default do not execute: ')
if love == '':
    love = 'I Love you'
# 窗口大小
turtle.setup(width=900,height=500)
# 颜色
turtle.color('red', 'pink')
# 笔粗细
turtle.pensize(3)
# 速度
turtle.speed(1)
# 提笔
turtle.up()
# 隐藏笔
turtle.hideturtle()
# 去到的坐标,窗口中心为0,0
turtle.goto(0, -180)
turtle.showturtle()
# 画上线
turtle.down()
turtle.speed(1)
turtle.begin_fill()
turtle.left(140)
turtle.forward(224)
# 调用画爱心左边的顶部
LittleHeart()
# 调用画爱右边的顶部
turtle.left(120)
LittleHeart()
# 画下线
turtle.forward(224)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.hideturtle()
# 在心中写字 一次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('#CD5C5C', 'pink')
# 在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
time.sleep(2)
# 在心中写字 二次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('red', 'pink')
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
# 写署名
if me != '':
    turtle.color('black', 'pink')
    time.sleep(2)
    turtle.goto(180, -180)
    turtle.showturtle()
    turtle.write(me, font=(20,), align="center", move=True)

# 点击窗口关闭
window = turtle.Screen()
window.exitonclick()

画樱花树

from turtle import *
from random import *
from math import *
class Tree:
    def __init__(self):
        setup(1000, 700)
        bgcolor(1, 1, 1)  # 背景色
        # ht()  # 隐藏turtle
        speed(10)  # 速度 1-10渐进,0 最快
        # tracer(1, 100)    # 设置绘图屏幕刷新频率,参数1设置在正常刷新频次的第参数1次刷新,参数2设置每次刷新的时延
        tracer(0, 0)
        pu()  # 抬笔
        backward(100)
        # 保证笔触箭头方向始终不向下,此处使其左转90度,而不是右转
        left(90)  # 左转90度
        backward(300)  # 后退300
    def tree(self, n, l):
        pd()  # 下笔
        # 阴影效果
        t = cos(radians(heading() + 45)) / 8 + 0.25
        pencolor(t, t, t)
        pensize(n / 1.2)
        forward(l)  # 画树枝
        if n > 0:
            b = random() * 15 + 10  # 右分支偏转角度
            c = random() * 15 + 10  # 左分支偏转角度
            d = l * (random() * 0.25 + 0.7)  # 下一个分支的长度
            # 右转一定角度,画右分支
            right(b)
            self.tree(n - 1, d)
            # 左转一定角度,画左分支
            left(b + c)
            self.tree(n - 1, d)
            # 转回来
            right(c)
        else:
            # 画叶子
            right(90)
            n = cos(radians(heading() - 45)) / 4 + 0.5
            pencolor(n, n * 0.8, n * 0.8)
            fillcolor(n, n * 0.8, n * 0.8)
            begin_fill()
            circle(3)
            left(90)
            end_fill()
            # 添加0.3倍的飘落叶子
            if random() > 0.7:
                pu()
                # 飘落
                t = heading()
                an = -40 + random() * 40
                setheading(an)
                dis = int(800 * random() * 0.5 + 400 * random() * 0.3 + 200 * random() * 0.2)
                forward(dis)
                setheading(t)
                # 画叶子
                pd()
                right(90)
                n = cos(radians(heading() - 45)) / 4 + 0.5
                pencolor(n * 0.5 + 0.5, 0.4 + n * 0.4, 0.4 + n * 0.4)
                fillcolor(n, n * 0.8, n * 0.8)
                begin_fill()
                circle(2)
                left(90)
                end_fill()
                pu()
                # 返回
                t = heading()
                setheading(an)
                backward(dis)
                setheading(t)
                # pass
        pu()
        backward(l)  # 退回
def main():
    tree = Tree()
    tree.tree(12, 100)  # 递归7层
    done()
if __name__ == '__main__':
    main()

画皮卡丘

# coding:utf-8
import turtle as t
import time
# 皮卡丘
# 基础设置
t.screensize(800, 600)
t.pensize(2)  # 设置画笔的大小
t.speed(10)  # 设置画笔速度为10
# 画左偏曲线函数
def radian_left(ang, dis, step, n):
    for i in range(n):
        dis += step  # dis增大step
        t.lt(ang)  # 向左转ang度
        t.fd(dis)  # 向前走dis的步长
def radian_right(ang, dis, step, n):
    for i in range(n):
        dis += step
        t.rt(ang)  # 向左转ang度
        t.fd(dis)  # 向前走dis的步长
# 画耳朵
def InitEars():
    t.color("black", "yellow")
    # 左耳朵曲线
    t.pu()  # 提笔
    t.goto(-50, 100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(110)  # 画笔角度
    t.begin_fill()
    radian_left(1.2, 0.4, 0.1, 40)
    t.setheading(270)  # 画笔角度
    radian_left(1.2, 0.4, 0.1, 40)
    t.setheading(44)  # 画笔角度
    t.forward(32)
    t.end_fill()
    # 右耳朵曲线
    t.pu()  # 提笔
    t.goto(50, 100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(70)  # 画笔角度
    t.begin_fill()
    radian_right(1.2, 0.4, 0.1, 40)
    t.setheading(270)  # 画笔角度
    radian_right(1.2, 0.4, 0.1, 40)
    t.setheading(136)  # 画笔角度
    t.forward(32)
    t.end_fill()
    # 耳朵黑
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(88, 141)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(35)  # 画笔角度
    radian_right(1.2, 1.6, 0.1, 16)
    t.setheading(270)  # 画笔角度
    radian_right(1.2, 0.4, 0.1, 25)
    t.setheading(132)  # 画笔角度
    t.forward(31)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(-88, 141)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(145)  # 画笔角度
    radian_left(1.2, 1.6, 0.1, 16)
    t.setheading(270)  # 画笔角度
    radian_left(1.2, 0.4, 0.1, 25)
    t.setheading(48)  # 画笔角度
    t.forward(31)
    t.end_fill()
# 画尾巴
def InitTail():
    # 尾巴
    t.begin_fill()
    t.fillcolor("yellow")
    t.pu()  # 提笔
    t.goto(64, -140)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(10)  # 画笔角度
    t.forward(20)
    t.setheading(90)  # 画笔角度
    t.forward(20)
    t.setheading(10)  # 画笔角度
    t.forward(10)
    t.setheading(80)  # 画笔角度
    t.forward(100)
    t.setheading(35)  # 画笔角度
    t.forward(80)
    t.setheading(260)  # 画笔角度
    t.forward(100)
    t.setheading(205)  # 画笔角度
    t.forward(40)
    t.setheading(260)  # 画笔角度
    t.forward(37)
    t.setheading(205)  # 画笔角度
    t.forward(20)
    t.setheading(260)  # 画笔角度
    t.forward(25)
    t.setheading(175)  # 画笔角度
    t.forward(30)
    t.setheading(100)  # 画笔角度
    t.forward(13)
    t.end_fill()
# 画脚
def InitFoots():
    # 脚
    t.begin_fill()
    t.fillcolor("yellow")
    t.pensize(2)
    t.pu()  # 提笔
    t.goto(-70, -200)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(225)  # 画笔角度
    radian_left(0.5, 1.2, 0, 12)
    radian_left(35, 0.6, 0, 4)
    radian_left(1, 1.2, 0, 18)
    t.setheading(160)  # 画笔角度
    t.forward(13)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("yellow")
    t.pensize(2)
    t.pu()  # 提笔
    t.goto(70, -200)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(315)  # 画笔角度
    radian_right(0.5, 1.2, 0, 12)
    radian_right(35, 0.6, 0, 4)
    radian_right(1, 1.2, 0, 18)
    t.setheading(20)  # 画笔角度
    t.forward(13)
    t.end_fill()
# 画身体
def InitBody():
    # 外形轮廓
    t.begin_fill()
    t.pu()  # 提笔
    t.goto(112, 0)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(90)  # 画笔角度
    t.circle(112, 180)
    t.setheading(250)  # 画笔角度
    radian_left(1.6, 1.3, 0, 50)
    radian_left(0.8, 1.5, 0, 25)
    t.setheading(255)  # 画笔角度
    radian_left(0.4, 1.6, 0.2, 27)
    radian_left(2.8, 1, 0, 45)
    radian_right(0.9, 1.4, 0, 31)
    t.setheading(355)  # 画笔角度
    radian_right(0.9, 1.4, 0, 31)
    radian_left(2.8, 1, 0, 45)
    radian_left(0.4, 7.2, -0.2, 27)
    t.setheading(10)  # 画笔角度
    radian_left(0.8, 1.5, 0, 25)
    radian_left(1.6, 1.3, 0, 50)
    t.end_fill()
def InitEyes():
    # 左眼睛
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(-46, 10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(90)  # 画笔角度
    t.circle(5, 360)
    t.end_fill()
    # 右眼睛
    t.begin_fill()
    t.fillcolor("black")
    t.pu()  # 提笔
    t.goto(46, 10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(-90)  # 画笔角度
    t.circle(5, 360)
    t.end_fill()
# 画脸
def InitFace():
    # 脸蛋
    t.begin_fill()
    t.fillcolor("red")
    t.pu()  # 提笔
    t.goto(-63, -10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(90)  # 画笔角度
    t.circle(10, 360)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("red")
    t.pu()  # 提笔
    t.goto(63, -10)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(-90)  # 画笔角度
    t.circle(10, 360)
    t.end_fill()
    # 嘴巴
    t.pensize(2.2)
    t.pu()  # 提笔
    t.goto(0, 0)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(235)  # 画笔角度
    radian_right(5, 0.8, 0, 30)
    t.pu()  # 提笔
    t.goto(0, 0)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(305)  # 画笔角度
    radian_left(5, 0.8, 0, 30)
# 画手
def InitHands():
    # 左手
    t.pensize(2)
    t.pu()  # 提笔
    t.goto(-46, -100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(285)  # 画笔角度
    radian_right(0.4, 1.2, 0, 26)
    radian_right(5, 0.35, 0, 26)
    radian_right(0.3, 1.2, 0, 15)
    # 右手
    t.pu()  # 提笔
    t.goto(46, -100)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(255)  # 画笔角度
    radian_left(0.4, 1.2, 0, 26)
    radian_left(5, 0.35, 0, 26)
    radian_left(0.3, 1.2, 0, 15)
def CloseEyes():
    # 左眼睛
    t.pu()  # 提笔
    t.goto(-46, 12)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(180)  # 画笔角度
    t.forward(10)
    # 右眼睛
    t.pu()  # 提笔
    t.goto(46, 12)  # 笔头初始位置
    t.pd()  # 下笔
    t.setheading(0)  # 画笔角度
    t.forward(10)
# 初始化
def Init():
    InitEars()
    InitTail()
    InitFoots()
    InitBody()
    InitFace()
    InitHands()
    InitEyes()
# 眨眼睛
def Upgarde():
    InitEars()
    InitTail()
    InitFoots()
    InitBody()
    InitFace()
    InitHands()
    CloseEyes()
def Upgarde_Init():
    InitEars()
    InitTail()
    InitFoots()
    InitBody()
    InitFace()
    InitHands()
    InitEyes()
def main():
    Init()
    t.tracer(False)
    # 眨眼睛动画
    for i in range(30):
        if i % 2 == 0:
            t.reset()
            t.hideturtle()
            Upgarde()
            t.update()
            time.sleep(0.3)
        else:
            t.reset()
            t.hideturtle()
            Upgarde_Init()
            t.update()
            time.sleep(1)
main()
# 结束画笔
t.done()

画实时时钟

# -*- coding:utf-8 –*-
# 用turtlr画时钟
# 以自定义shape的方式实现
import turtle as t
import datetime as d
def skip(step):  # 抬笔,跳到一个地方
    t.penup()
    t.forward(step)
    t.pendown()
def drawClock(radius):  # 画表盘
    t.speed(0)
    t.mode("logo")  # 以Logo坐标、角度方式
    t.hideturtle()
    t.pensize(7)
    t.home()  # 回到圆点
    for j in range(60):
        skip(radius)
        if (j % 5 == 0):
            t.forward(20)
            skip(-radius - 20)
        else:
            t.dot(5)
            skip(-radius)
        t.right(6)
def makePoint(pointName, len):  # 钟的指针,时针、分针、秒针
    t.penup()
    t.home()
    t.begin_poly()
    t.back(0.1 * len)
    t.forward(len * 1.1)
    t.end_poly()
    poly = t.get_poly()
    t.register_shape(pointName, poly)  # 注册为一个shape
def drawPoint():  # 画指针
    global hourPoint, minPoint, secPoint, fontWriter
    makePoint("hourPoint", 100)
    makePoint("minPoint", 120)
    makePoint("secPoint", 140)
    hourPoint = t.Pen()  # 每个指针是一只新turtle
    hourPoint.shape("hourPoint")
    hourPoint.shapesize(1, 1, 6)
    minPoint = t.Pen()
    minPoint.shape("minPoint")
    minPoint.shapesize(1, 1, 4)
    secPoint = t.Pen()
    secPoint.shape("secPoint")
    secPoint.pencolor('red')
    fontWriter = t.Pen()
    fontWriter.pencolor('gray')
    fontWriter.hideturtle()
def getWeekName(weekday):
    weekName = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    return weekName[weekday]
def getDate(year, month, day):
    return "%s-%s-%s" % (year, month, day)
def realTime():
    curr = d.datetime.now()
    curr_year = curr.year
    curr_month = curr.month
    curr_day = curr.day
    curr_hour = curr.hour
    curr_minute = curr.minute
    curr_second = curr.second
    curr_weekday = curr.weekday()
    t.tracer(False)
    secPoint.setheading(360 / 60 * curr_second)
    minPoint.setheading(360 / 60 * curr_minute)
    hourPoint.setheading(360 / 12 * curr_hour + 30 / 60 * curr_minute)
    fontWriter.clear()
    fontWriter.home()
    fontWriter.penup()
    fontWriter.forward(80)
    # 用turtle写文字
    fontWriter.write(getWeekName(curr_weekday), align="center", font=("Courier", 14, "bold"))
    fontWriter.forward(-160)
    fontWriter.write(getDate(curr_year, curr_month, curr_day), align="center", font=("Courier", 14, "bold"))
    t.tracer(True)
    print(curr_second)
    t.ontimer(realTime, 100)  # 每隔100毫秒调用一次realTime()
def main():
    t.tracer(False)
    drawClock(160)
    drawPoint()
    realTime()
    t.tracer(True)
    t.mainloop()
if __name__ == '__main__':
    main()

画星空

from turtle import *
from random import random,randint
screen = Screen()
width ,height = 800,600
screen.setup(width,height)
screen.bgcolor("black")
screen.mode("logo")
screen.delay(0)#这里要设为0,否则很卡
t = Turtle(visible = False,shape='circle')
t.pencolor("white")
t.fillcolor("white")
t.penup()
t.setheading(-90)
t.goto(width/2,randint(-height/2,height/2))
stars = []
for i in range(200):
    star = t.clone()
    s =random() /3
    star.shapesize(s,s)
    star.speed(int(s*10))
    star.setx(width/2 + randint(1,width))
    star.sety( randint(-height/2,height/2))
    star.showturtle()
    stars.append(star)
while True:
    for star in stars:
        star.setx(star.xcor() - 3 * star.speed())
        if star.xcor()<-width/2:
            star.hideturtle()
            star.setx(width/2 + randint(1,width))
            star.sety( randint(-height/2,height/2))
            star.showturtle()

    
    ~~~
    
  • 117
    点赞
  • 879
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值