python_绘图函数turtle学习

绘图函数
用turtle库绘制下方图形

from turtle import *

import time

bgcolor("black")

pencolor("red")

speed(0)

def HT(side, sides):
    for i in range(sides):
        fd(side)
        rt(360 / sides)


for x in range(10):
    HT(100, 5)
    rt(360 / 10)

done()

python_绘图函数turtle学习


from turtle import *

import time

bgcolor("black")

pencolor("red")

speed(0)


def HT(side, sides):
    for i in range(sides):
        fd(side)
        rt(360 / sides)


for x in range(10):
    HT(150, 4)
    rt(360 / 10)

done()

time.sleep(10)

python_绘图函数turtle学习
python_绘图函数turtle学习
python_绘图函数turtle学习

# 画五角星
import turtle as t
t.goto(100,0)

for i in range(50):
    t.left(80)
    t.fd(100)
    t.left(135)
    t.fd(105)

python_绘图函数turtle学习


# 画棒棒糖

import turtle
import time

colors = ['red', 'purple', 'blue', 'green', 'yellow', 'orange']

t = turtle.Pen()

turtle.bgcolor('black')

for x in range(36):
    t.pencolor(colors[x % 6])
    t.width(100)
    t.forward(8.5)
    t.left(10)

t.right(90)

for y in range(100):
    t.pencolor(colors[y % 6])
    t.width(20)
    t.forward(2)

for x in range(18):
    t.pencolor(colors[x % 6])
    t.width(20)
    t.forward(8.5)
    t.left(10)

time.sleep(10)

python_绘图函数turtle学习


# coding=utf-8


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.speed(0)

    # 建立输出文字Turtle

    printer = turtle.Turtle()

    # 隐藏画笔的turtle形状

    printer.hideturtle()

    printer.penup()


def SetupClock(radius):
    # 建立表的外框

    turtle.reset()

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


if __name__ == "__main__":
    main()

python_绘图函数turtle学习

上一篇:Python Turtle 初学者指南


下一篇:Python函数和代码复用(第5周)