五十七、教用Python中的turtle海龜畫圖(下篇)

@Author:Runsen
@Date:2020/5/27

作者介紹:Runsen目前大三下學期,專業化學工程與工藝,大學沉迷日語,Python, Java和一系列數據分析軟件。導致翹課嚴重,專業排名中下。.在大學60%的時間,都在CSDN。決定今天比昨天要更加努力。

在Python中的turtle海龜畫圖,我相信大家都有了解,我在之前也寫過這類的博文,但是我發現真的不行,今天就重寫,保留好的地方,添加更多的要點


我覺得沒有什麼東西可以寫,就是需要不斷地實踐。

畫一個朵花

import turtle
t = turtle.Pen()
def draw_leaf():
    for i in range(2):
        for j in range(15):
            t.forward(5)
            t.right(6)
        t.right(90)
t.goto(0,-150)
t.left(90)
t.forward(50)
draw_leaf()
t.forward(150)
for i in range(6):
    draw_leaf()
    t.right(60)
t.down()

上圖是畫一朵花的程序,使用了函數來定義drawleaf,每一掰葉子由兩條弧線組成,每一條弧線重複畫15次,每次前進5步,右轉6度。

看不懂看下步驟

這朵花由4部分組成,一個向上長50步的直線,一片葉子,長150步的直線和6片葉子組成的花。

最後給葉子填充顏色,給花朵填充顏色。

import turtle
t = turtle.Pen()
def draw_leaf():
    for i in range(2):
        for j in range(15):
            t.forward(5)
            t.right(6)
        t.right(90)
t.goto(0,-150)
t.left(90)
t.forward(50)
t.fillcolor("green")
t.begin_fill()
draw_leaf()
t.end_fill()
t.forward(50)
t.right(270)
t.fillcolor("green")
t.begin_fill()
draw_leaf()
t.end_fill()
t.right(90)
t.forward(130)
t.fillcolor("red")
t.begin_fill()
for i in range(6):
    draw_leaf()
    t.right(60)
t.end_fill()
t.down()

酷炫的圖形

import turtle

for i in range(360):
   turtle.rt(1)
   turtle.circle(100,None,4)
turtle.exitonclick()

畫時鐘



# -*- coding:utf-8 -*-
# time :2019/4/19 22:53
# author: 毛利

import turtle
import datetime
# 移動一段距離
def skip(distance):
    """
    移動烏龜一段距離,不留痕跡
    :param distance: 像素
    :return:
    """
    turtle.penup()
    turtle.forward(distance)
    turtle.pendown()

def draw_clock():
    # 先畫表盤
    # 先畫點
    # 移動一段距離,畫一個點,然後退回
    # 轉動6°,再移動一段距離,畫一個點,然後退回
    # 循環 60次
    # 讓烏龜的方向默認向上
    turtle.reset()
    turtle.hideturtle()
    for i in range(60):

        skip(160)
        # 根據 5格一個時鐘
        if i % 5 == 0:
            turtle.pensize(7)
            # 畫時鐘
            turtle.forward(20)
            if i == 0:
                turtle.write(12, align='center', font=('Courier', 14, 'bold'))
            elif i == 25 or i == 30 or i == 35:
                skip(25)
                turtle.write(int(i/5), align='center', font=('Courier', 14, 'bold'))
                skip(-25)

            else:
                turtle.write(int(i/5), align='center', font=('Courier', 14, 'bold'))
            skip(-20)
        else:
            turtle.pensize(1)
            turtle.dot()
        skip(-160)
        turtle.right(6)


def get_week(t):
    week = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    return week[t.weekday()]


def create_hand(length, name):
    turtle.reset()
    skip(-length * 0.1)
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    turtle.end_poly()
    # 註冊
    turtle.register_shape(name, turtle.get_poly())
    hand = turtle.Turtle()
    hand.shape(name)
    hand.shapesize(1, 1, 3)
    return hand

def run():
    # 不停的獲取時間
    t = datetime.datetime.today()
    bob.forward(65)
    bob.write(get_week(t), align='center', font=('Courier', 14, 'bold'))
    bob.back(130)
    bob.write(t.strftime('%Y-%m-%d'), align='center', font=('Courier', 14, 'bold'))
    bob.home()
    # 指針移動
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second/60
    hour = t.hour + minute/60
    turtle.tracer(True)
    second_hand.setheading(6*second)
    minute_hand.setheading(6*minute)
    hour_hand.setheading(30*hour)
    turtle.ontimer(run, 200)
if __name__ == '__main__':
    # 畫秒針,分針,時針
    turtle.mode('logo')
    turtle.hideturtle()
    global second_hand, minute_hand, hour_hand, bob

    second_hand = create_hand(135, 'second_hand')
    minute_hand = create_hand(125, 'minute_hand')
    hour_hand = create_hand(90, 'hour_hand')
    # 創建一個新的turtle對象,去循環的操作
    bob = turtle.Turtle()
    bob.hideturtle()
    bob.penup()

    turtle.tracer(False)
    draw_clock()
    run()

    turtle.mainloop()

對於urtle海龜畫圖,沒必要研究這麼深,就是小孩子的玩意,學習下,以後教下兒子就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章