天天看點

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

    
    ~~~