python内置库turtle绘制有趣图形

一:绘制六边形

import turtle

t = turtle.Pen()
turtle.bgcolor("black")
sides = 6
colors = ["red", "yellow", "green", "blue", "orange", "purple"]
for x in range(360):
    t.pencolor(colors[x % sides])
    t.forward(x * 3 / sides + x)
    t.left(360 / sides + 1)
    t.width(x * sides / 200)

效果:
python内置库turtle绘制有趣图形_第1张图片

二:自定义边框

import turtle

t = turtle.Pen()
turtle.bgcolor("black")
sides = eval(input("输入要绘制的边的数目(2-6)!"))
colors = ["red", "yellow", "green", "blue", "orange", "purple"]
for x in range(100):
    t.pencolor(colors[x % sides])
    t.forward(x * 3 / sides + x)
    t.left(360 / sides + 1)
    t.width(x * sides / 200)

效果:
python内置库turtle绘制有趣图形_第2张图片

三:带有文字的图形绘制

import turtle

t = turtle.Pen()
turtle.bgcolor("black")

my_name = turtle.textinput("输入你的姓名", "你的名字?")
colors = ["red", "yellow", "purple", "blue"]
for x in range(100):
    t.pencolor(colors[x % 4])
    t.penup()
    t.forward(x * 4)
    t.pendown()
    t.write(my_name, font=("Arial", int((x + 4) / 4), "bold"))
    t.left(92)

效果:
python内置库turtle绘制有趣图形_第3张图片

四:绘制雪花

from turtle import *
from random import *


def ground():
    hideturtle()
    speed(100)
    for i in range(400):
        pensize(randint(5, 10))
        x = randint(-400, 350)
        y = randint(-280, -1)
        r = -y / 280
        g = -y / 280
        b = -y / 280
        pencolor(r, g, b)
        penup()
        goto(x, y)
        pendown()
        forward(randint(40, 100))


def snow():
    hideturtle()
    speed(100)
    pensize(2)
    for i in range(100):
        r = random()
        g = random()
        b = random()
        pencolor(r, g, b)
        penup()
        setx(randint(-350, 350))
        sety(randint(1, 270))
        pendown()
        dens = randint(8, 12)
        snowsize = randint(10, 14)
        for j in range(dens):
            forward(snowsize)
            backward(snowsize)
            right(360 / dens)


def main():
    setup(800, 600, 0, 0)
    tracer(False)
    bgcolor("black")
    snow()
    ground()
    tracer(True)
    mainloop()


main()

效果:
python内置库turtle绘制有趣图形_第4张图片
五:绘制科赫雪花

import turtle


def coch(size, n):
    if n == 0:
        turtle.fd(size)
    else:
        for angle in [0, 60, -120, 60]:
            turtle.left(angle)
            coch(size / 3, n - 1)


def main():
    turtle.setup(600, 600)
    turtle.penup()
    turtle.bgcolor("white")
    turtle.pensize(2)
    turtle.goto(-200, 100)
    turtle.pendown()
    level = 3
    coch(400, level)
    turtle.right(120)
    coch(400, level)
    turtle.right(120)
    coch(400, level)
    turtle.hideturtle()


main()

效果:
python内置库turtle绘制有趣图形_第5张图片
六:绘制七彩线条

import turtle as t

t.setup(1000, 1000)
t.pen(shown=False, pendown=False, pensize=10, speed=0)

colorlist = [(255, 0, 0), (255, 165, 0), (255, 255, 0), \
             (0, 255, 0), (0, 255, 255), (0, 0, 255), (139, 0, 255)]
colorlist.reverse()

t.fd(-250)
t.seth(-40)

t.colormode(255)

t.pendown()
for color in colorlist[:-1]:
    t.pencolor(color)
    t.circle(30, 80)
    t.circle(-30, 80)

t.pencolor(colorlist[-1])
t.circle(30, 80 / 2)
t.fd(40)
t.circle(25, 180)
t.fd(40 * 2 / 3)
t.done()

效果:
python内置库turtle绘制有趣图形_第6张图片
七:绘制太极图

import turtle


# 绘制太极图函数
def draw_TJT(R):
    turtle.screensize(800, 600, "blue")  # 画布长、宽、背景色 长宽单位为像素
    turtle.pensize(1)  # 画笔宽度
    turtle.pencolor('black')  # 画笔颜色
    turtle.speed(10)  # 画笔移动速度

    TJT_color = {1: 'white', -1: 'black'}  # 太极图填充色 1 白色 -1 黑色
    color_list = [1, -1]

    """
    先画半边,再画另一边
    """
    for c in color_list:
        turtle.fillcolor(TJT_color.get(c))  # 获取该半边的填充色
        turtle.begin_fill()  # 开始填充

        # 开始画出半边的轮廓
        turtle.circle(R / 2, 180)
        turtle.circle(R, 180)
        turtle.circle(R / 2, -180)

        turtle.end_fill()  # 结束填充 上色完成

        # 绘制该半边的鱼眼
        turtle.penup()  # 提起画笔,移动不留痕
        turtle.goto(0, R / 3 * c)  # 移动到该半边的鱼眼的圆上 R/3*c 表示移动到哪边
        turtle.pendown()  # 放下画笔,移动留痕
        turtle.fillcolor(TJT_color.get(-c))  # 获取鱼眼填充色, 与该半边相反
        turtle.begin_fill()
        turtle.circle(-R / 6, 360)
        turtle.end_fill()

        # 回到原点,为下一循环的开始做准备
        turtle.penup()
        turtle.goto(0, 0)
        turtle.pendown()

    # 绘制文本
    turtle.penup()
    turtle.goto(0, -R - 50)
    turtle.pendown()
    turtle.write("太极图 made by kjshen", font=('Arial', 12, 'normal'))


if __name__ == '__main__':
    R = 100  # 太极图半径
    draw_TJT(R)
    input('Press Enter to exit...')  # 防止程序运行完成后就自动关闭窗口

效果:
python内置库turtle绘制有趣图形_第7张图片
八:绘制五角星

import turtle

turtle.setup(2000, 2000, 0, 0)
turtle.pensize(20)
turtle.pencolor("black")
turtle.seth(0)
turtle.fd(400)
turtle.seth(-144)
turtle.fd(400)
turtle.seth(-144 - 144)
turtle.fd(400)
turtle.seth(-144 - 144 - 144)
turtle.fd(400)
turtle.seth(-144 - 144 - 144 - 144)
turtle.fd(400)
input("")

效果:
python内置库turtle绘制有趣图形_第8张图片
九:绘制奥运五环

import turtle

p = turtle
p.pensize(3)
p.color("blue")
p.circle(30, 360)
p.pu()
p.goto(60, 0)
p.pd()
p.color("black")
p.circle(30, 360)
p.pu()
p.goto(120, 0)
p.pd()
p.color("red")
p.circle(30, 360)
p.pu()
p.goto(90, -30)
p.pd()
p.color("green")
p.circle(30, 360)
p.pu()
p.goto(30, -30)
p.pd()
p.color("yellow")
p.circle(30, 360)
p.done()


效果:
python内置库turtle绘制有趣图形_第9张图片

你可能感兴趣的:(python)