python畫圖代碼:python畫愛心代碼大全

turtle畫一個彩色螺旋圖形

import turtle


turtle.shape('turtle')
turtle.speed(9)            # 畫筆速度
turtle.pensize(2)			# 畫筆的寬度
turtle.bgcolor("black")		# 畫布背景色
colors = ["red","yellow","green","blue"]	# 定義畫筆線色

for i in range(255):		# 循環一次 畫一條線
    turtle.forward(2 * i) 	        # 向當前方向前進n像素
    turtle.color(colors[i % 4])	# 根據求余 調整畫筆線色
    turtle.left(91)                # 向左旋轉91度

turtle.done()
Python的turtle庫使用-畫出好看的圖形

仙人球

import turtle


turtle.shape('turtle')
turtle.color('orange', 'green')
turtle.begin_fill()
while True:
    turtle.forward(200)
    turtle.left(170)
    if abs(turtle.pos()) < 1:
        break
turtle.end_fill()
turtle.done()

Python的turtle庫使用-畫出好看的圖形

國旗

import turtle


turtle.shape('turtle')
turtle.screensize(2000, 2000, 'white')  # 設置畫布大小
turtle.speed(9)
# 繪製旗面
turtle.pencolor('red')
# pu()
turtle.goto(-300, -200)
turtle.pd()
turtle.fillcolor('red')
turtle.begin_fill()
for i in range(0, 2):
    turtle.fd(600)
    turtle.lt(90)
    turtle.fd(400)
    turtle.lt(90)
turtle.end_fill()


# 繪製大五角星
turtle.pu()
turtle.pencolor('yellow')
turtle.goto(-260, 120)
turtle.pd()
turtle.fillcolor('yellow')
turtle.begin_fill()
for i in range(0, 5):
    turtle.fd(113.137)  # 大星一划的邊長
    turtle.rt(144)
turtle.end_fill()

# 繪製四個小五角星
list1 = [(-100, 160), (-60, 120), (-60, 60), (-100, 20)]  # 四個五角星的中心坐標
list2 = [31.98, 8.13, -15.59, -38.66]  # 相對角度0的後退1.111需要左轉的角度

for j in range(0, 4):
    turtle.seth(0)  # 這是龜頭角度為0
    turtle.pu()
    turtle.goto(list1[j])  # 定位到五角星中心
    turtle.lt(list2[j])  # 旋轉角度,以背向指向大五角星的角尖
    turtle.bk(20)  # 從五角星中心到指向大五角星的角尖(龜倒着爬)退一個小圓半徑
    turtle.lt(18)  # 五角星的半角角度
    turtle.pd()
    turtle.begin_fill()
    for i in range(0, 5):
        turtle.fd(113.137 / 3)  # 小星一划的邊長
        turtle.rt(144)
    turtle.end_fill()
turtle.pu()
turtle.ht()
turtle.done()

Python的turtle庫使用-畫出好看的圖形

玫瑰花

import turtle
import time


turtle.shape('turtle')
turtle.speed(5)
# 設置初始位置
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)
# 花蕊
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(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)
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()

Python的turtle庫使用-畫出好看的圖形

彩色樹

import turtle


turtle.shape('turtle')
# 設置色彩模式是RGB:
turtle.colormode(255)
turtle.lt(90)
lv = 14
l = 120
s = 45
turtle.width(lv)
# 初始化RGB顏色:
r = 0
g = 0
b = 0
turtle.pencolor(r, g, b)
turtle.penup()
turtle.bk(l)
turtle.pendown()
turtle.fd(l)
def draw_tree(l, level):
    global r, g, b
    # save the current pen width
    w = turtle.width()
    # narrow the pen width
    turtle.width(w * 3.0 / 4.0)
    # set color:
    r = r + 1
    g = g + 2
    b = b + 3
    turtle.pencolor(r % 200, g % 200, b % 200)
    l = 3.0 / 4.0 * l
    turtle.lt(s)
    turtle.fd(l)
    if level < lv:
        draw_tree(l, level + 1)
    turtle.bk(l)
    turtle.rt(2 * s)
    turtle.fd(l)
    if level < lv:
        draw_tree(l, level + 1)
    turtle.bk(l)
    turtle.lt(s)
    # restore the previous pen width
    turtle.width(w)
turtle.speed("fastest")
draw_tree(l, 4)
turtle.done()

Python的turtle庫使用-畫出好看的圖形

隨機櫻花樹

# 每次運行 樹的形狀是隨機的
import turtle as T
import random
import time


# 畫櫻花的軀幹(60,t)
def Tree(branch, t):
    time.sleep(0.0005)
    if branch > 3:
        if 8 <= branch <= 12:
            if random.randint(0, 2) == 0:
                t.color('snow')  # 白
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 3)
        elif branch < 8:
            if random.randint(0, 1) == 0:
                t.color('snow')
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 2)
        else:
            t.color('sienna')  # 赭(zhě)色
            t.pensize(branch / 10)  # 6
        t.forward(branch)
        a = 1.5 * random.random()
        t.right(20 * a)
        b = 1.5 * random.random()
        Tree(branch - 10 * b, t)
        t.left(40 * a)
        Tree(branch - 10 * b, t)
        t.right(20 * a)
        t.up()
        t.backward(branch)
        t.down()

# 掉落的花瓣
def Petal(m, t):
    for i in range(m):
        a = 200 - 400 * random.random()
        b = 10 - 20 * random.random()
        t.up()
        t.forward(b)
        t.left(90)
        t.forward(a)
        t.down()
        t.color('lightcoral')  # 淡珊瑚色
        t.circle(1)
        t.up()
        t.backward(a)
        t.right(90)
        t.backward(b)

# 繪圖區域

t = T.Turtle()
# 畫布大小
w = T.Screen()
t.shape('turtle')
# t.hideturtle()  # 隱藏畫筆
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat')  # wheat小麥
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')

# 畫櫻花的軀幹
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()
T.done()

Python的turtle庫使用-畫出好看的圖形

愛情樹

import random
import turtle


def love(x, y):  # 在(x,y)處畫愛心lalala
    lv = turtle.Turtle()
    lv.hideturtle()
    lv.up()
    lv.goto(x, y)  # 定位到(x,y)

    def curvemove():  # 畫圓弧
        for i in range(20):
            lv.right(10)
            lv.forward(2)

    lv.color('red', 'pink')
    lv.speed(0)
    lv.pensize(1)
    # 開始畫愛心lalala
    lv.down()
    lv.begin_fill()
    lv.left(140)
    lv.forward(22)
    curvemove()
    lv.left(120)
    curvemove()
    lv.forward(22)
    lv.write("{}".format("I Love You!"), font=("Arial", 10, "normal"), align="center")
    lv.left(140)  # 畫完複位
    lv.end_fill()


def tree(branchLen, t):
    if branchLen > 5:  # 剩餘樹枝太少要結束遞歸
        if branchLen < 20:  # 如果樹枝剩餘長度較短則變綠
            t.color("green")
            t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
            t.down()
            t.forward(branchLen)
            love(t.xcor(), t.ycor())  # 傳輸現在turtle的坐標
            t.up()
            t.backward(branchLen)
            t.color("brown")
            return
        t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
        t.down()
        t.forward(branchLen)
        # 以下遞歸
        ang = random.uniform(15, 45)
        t.right(ang)
        tree(branchLen - random.uniform(12, 16), t)  # 隨機決定減小長度
        t.left(2 * ang)
        tree(branchLen - random.uniform(12, 16), t)  # 隨機決定減小長度
        t.right(ang)
        t.up()
        t.backward(branchLen)


def Fonts():
    t.penup()
    t.goto(-300, -300)
    t.pencolor('red')
    t.write("我愛你!^_^", font=('方正行黑簡體', 30, 'normal'))


myWin = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
t.pensize(32)
t.forward(60)
tree(100, t)
Fonts()
myWin.exitonclick()

Python的turtle庫使用-畫出好看的圖形

哆啦A夢

import turtle


def flyTo(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
def drawEye():
    turtle.tracer(False)
    a = 2.5
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a -= 0.05
        else:
            a += 0.05
        turtle.left(3)
        turtle.fd(a)
    turtle.tracer(True)
def beard():
    """ 畫鬍子, 一共六根
    """
    # 左邊第一根鬍子
    flyTo(-37, 135)
    turtle.seth(165)
    turtle.fd(60)
    # 左邊第二根鬍子
    flyTo(-37, 125)
    turtle.seth(180)
    turtle.fd(60)
    # 左邊第三根鬍子
    flyTo(-37, 115)
    turtle.seth(193)
    turtle.fd(60)
    # 右邊第一根鬍子
    flyTo(37, 135)
    turtle.seth(15)
    turtle.fd(60)
    # 右邊第二根鬍子
    flyTo(37, 125)
    turtle.seth(0)
    turtle.fd(60)
    # 右邊第三根鬍子
    flyTo(37, 115)
    turtle.seth(-13)
    turtle.fd(60)
def drawRedScarf():
    """ 畫圍巾
    """
    turtle.fillcolor("red")  # 填充顏色
    turtle.begin_fill()
    turtle.seth(0)  # 朝向右
    turtle.fd(200)  # 前進10個單位
    turtle.circle(-5, 90)
    turtle.fd(10)
    turtle.circle(-5, 90)
    turtle.fd(207)
    turtle.circle(-5, 90)
    turtle.fd(10)
    turtle.circle(-5, 90)
    turtle.end_fill()
def drawMouse():
    flyTo(5, 148)
    turtle.seth(270)
    turtle.fd(100)
    turtle.seth(0)
    turtle.circle(120, 50)
    turtle.seth(230)
    turtle.circle(-120, 100)
def drawRedNose():
    flyTo(-10, 158)
    turtle.fillcolor("red")  # 填充顏色
    turtle.begin_fill()
    turtle.circle(20)
    turtle.end_fill()
def drawBlackdrawEye():
    turtle.seth(0)
    flyTo(-20, 195)
    turtle.fillcolor("#000000")  # 填充顏色
    turtle.begin_fill()
    turtle.circle(13)
    turtle.end_fill()
    turtle.pensize(6)
    flyTo(20, 205)
    turtle.seth(75)
    turtle.circle(-10, 150)
    turtle.pensize(3)
    flyTo(-17, 200)
    turtle.seth(0)
    turtle.fillcolor("#ffffff")
    turtle.begin_fill()
    turtle.circle(5)
    turtle.end_fill()
    flyTo(0, 0)
def drawFace():
    turtle.forward(183)  # 前行183個單位
    turtle.fillcolor("white")  # 填充顏色為白色
    turtle.begin_fill()  # 開始填充
    turtle.left(45)  # 左轉45度
    turtle.circle(120, 100)  # 右邊那半邊臉
    turtle.seth(90)  # 朝向向上
    drawEye()  # 畫右眼睛
    turtle.seth(180)  # 朝向左
    turtle.penup()  # 抬筆
    turtle.fd(60)  # 前行60
    turtle.pendown()  # 落筆
    turtle.seth(90)  # 朝向上
    drawEye()  # 畫左眼睛
    turtle.penup()  # 抬筆
    turtle.seth(180)  # 朝向左
    turtle.fd(64)  # 前進64
    turtle.pendown()  # 落筆
    turtle.seth(215)  # 修改朝向
    turtle.circle(120, 100)  # 左邊那半邊臉
    turtle.end_fill()  #
def drawHead():
    """ 畫了一個被切掉下半部分的圓
    """
    turtle.penup()  # 抬筆
    turtle.circle(150, 40)  # 畫圓, 半徑150,圓周角40
    turtle.pendown()  # 落筆
    turtle.fillcolor("#00a0de")  # 填充色
    turtle.begin_fill()  # 開始填充
    turtle.circle(150, 280)  # 畫圓,半徑150, 圓周角280
    turtle.end_fill()
def drawAll():
    drawHead()
    drawRedScarf()
    drawFace()
    drawRedNose()
    drawMouse()
    beard()
    flyTo(0, 0)
    turtle.seth(0)
    turtle.penup()
    turtle.circle(150, 50)
    turtle.pendown()
    turtle.seth(30)
    turtle.fd(40)
    turtle.seth(70)
    turtle.circle(-30, 270)
    turtle.fillcolor("#00a0de")
    turtle.begin_fill()
    turtle.seth(230)
    turtle.fd(80)
    turtle.seth(90)
    turtle.circle(1000, 1)
    turtle.seth(-89)
    turtle.circle(-1000, 10)
    turtle.seth(180)
    turtle.fd(70)
    turtle.seth(90)
    turtle.circle(30, 180)
    turtle.seth(180)
    turtle.fd(70)
    turtle.seth(100)
    turtle.circle(-1000, 9)
    turtle.seth(-86)
    turtle.circle(1000, 2)
    turtle.seth(230)
    turtle.fd(40)
    turtle.circle(-30, 230)
    turtle.seth(45)
    turtle.fd(81)
    turtle.seth(0)
    turtle.fd(203)
    turtle.circle(5, 90)
    turtle.fd(10)
    turtle.circle(5, 90)
    turtle.fd(7)
    turtle.seth(40)
    turtle.circle(150, 10)
    turtle.seth(30)
    turtle.fd(40)
    turtle.end_fill()
    # 左手
    turtle.seth(70)
    turtle.fillcolor("#FFFFFF")
    turtle.begin_fill()
    turtle.circle(-30)
    turtle.end_fill()
    # 腳
    flyTo(103.74, -182.59)
    turtle.seth(0)
    turtle.fillcolor("#FFFFFF")
    turtle.begin_fill()
    turtle.fd(15)
    turtle.circle(-15, 180)
    turtle.fd(90)
    turtle.circle(-15, 180)
    turtle.fd(10)
    turtle.end_fill()
    flyTo(-96.26, -182.59)
    turtle.seth(180)
    turtle.fillcolor("#FFFFFF")
    turtle.begin_fill()
    turtle.fd(15)
    turtle.circle(15, 180)
    turtle.fd(90)
    turtle.circle(15, 180)
    turtle.fd(10)
    turtle.end_fill()
    # 右手
    flyTo(-133.97, -91.81)
    turtle.seth(50)
    turtle.fillcolor("#FFFFFF")
    turtle.begin_fill()
    turtle.circle(30)
    turtle.end_fill()
    # 口袋
    flyTo(-103.42, 15.09)
    turtle.seth(0)
    turtle.fd(38)
    turtle.seth(230)
    turtle.begin_fill()
    turtle.circle(90, 260)
    turtle.end_fill()
    flyTo(5, -40)
    turtle.seth(0)
    turtle.fd(70)
    turtle.seth(-90)
    turtle.circle(-70, 180)
    turtle.seth(0)
    turtle.fd(70)
    # 鈴鐺
    flyTo(-103.42, 15.09)
    turtle.fd(90)
    turtle.seth(70)
    turtle.fillcolor("#ffd200")
    turtle.begin_fill()
    turtle.circle(-20)
    turtle.end_fill()
    turtle.seth(170)
    turtle.fillcolor("#ffd200")
    turtle.begin_fill()
    turtle.circle(-2, 180)
    turtle.seth(10)
    turtle.circle(-100, 22)
    turtle.circle(-2, 180)
    turtle.seth(180 - 10)
    turtle.circle(100, 22)
    turtle.end_fill()
    flyTo(-13.42, 15.09)
    turtle.seth(250)
    turtle.circle(20, 110)
    turtle.seth(90)
    turtle.fd(15)
    turtle.dot(10)
    flyTo(0, -150)
    drawBlackdrawEye()
def main():
    turtle.screensize(800, 6000, "#F0F0F0")
    turtle.pensize(3)
    turtle.speed(9)
    drawAll()
if __name__ == "__main__":
    main()
    turtle.mainloop()

Python的turtle庫使用-畫出好看的圖形

時鐘

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.pencolor('orange')
        hand.speed(0)

    # 建立輸出文字Turtle
    printer = turtle.Turtle()
    printer.pencolor('red')
    # 隱藏畫筆的turtle形狀
    printer.hideturtle()
    printer.penup()


def SetupClock(radius):
    # 建立表的外框
    turtle.reset()
    turtle.pencolor('blue')
    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)


def main():
    # 打開/關閉龜動畫,並為更新圖紙設置延遲。
    turtle.tracer(False)
    Init()
    SetupClock(160)
    turtle.tracer(True)
    Tick()
    turtle.done()


if __name__ == "__main__":
    main()

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/223981.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 14:35
下一篇 2024-12-09 14:35

相關推薦

發表回復

登錄後才能評論