文章目录
前言
今天继续来用turtle库画图。
一、画八卦阵
上次画出来八卦图,今天来句需改善一下。八卦阵是一种古代的军事阵法,相传为诸葛亮发明。
from turtle import *
def rset():
pensize(1)
pencolor('black')
penup()
home()
pendown()
def offset(off_set, angle=0, mode='taiji'):
penup()
home()
if mode == 'taiji':
right(90)
fd(off_set)
seth(0)
else:
left(angle)
fd(off_set)
left(90)
pendown()
def taiji(radius, pen_size=2, color='black'):
rset()
pensize(pen_size)
pencolor(color)
offset(radius)
fillcolor('black')
begin_fill()
circle(radius, 180)
circle(radius / 2, 180)
circle(-radius / 2, 180)
end_fill()
circle(-radius, 180)
begin_fill()
fillcolor('white')
penup()
home()
left(90)
fd(radius * 0.7)
right(90)
pendown()
circle(-radius * 0.2)
end_fill()
rset()
penup()
begin_fill()
fillcolor('black')
right(90)
fd(radius * 0.7)
left(90)
pendown()
circle(radius * 0.2)
end_fill()
def bagua(off_set, side, gua_lis):
pensize(side / 2)
for j, k in enumerate(gua_lis):
angl = j * 45
rset()
offset(off_set, angle=angl, mode='bagua')
pencolor('white')
pensize(5)
bk(side / 2)
for i in k:
pencolor('black')
if i == 1:
fd(side)
elif i == 0:
fd(side * (1 / 3))
penup()
fd(side * (1 / 3))
pendown()
fd(side * (1 / 3))
elif isinstance(i, str) and j <= 4:
penup()
fd(side / 2)
pendown()
write(i, align='center', font=('楷体', 15, "bold"))
else:
penup()
fd(side / 2)
right(90)
fd(15)
pendown()
write(i, align='center', font=('楷体', 15, "bold"))
penup()
bk(side / 2)
right(90)
fd(side / 2)
left(90)
bk(side / 2)
pendown()
gua_lis = [(1, 1, 0, '兑'), (0, 0, 0, '坤'), (1, 0, 1, '离'), (0, 1, 1, '巽'), (1, 0, 0, '震'), (0, 0, 1, '艮'),
(0, 1, 0, '坎'), (1, 1, 1, '乾')]
taiji(50)
bagua(70, 30, gua_lis)
hideturtle()
done()
二、冰墩墩
冰墩墩(英文:Bing Dwen Dwen,汉语拼音:bīng dūn dūn),是2022年北京冬季奥运会的吉祥物。 冰墩墩将熊猫形象与富有超能量的冰晶外壳相结合,头部外壳造型取自冰雪运动头盔,装饰彩色光环,整体形象酷似航天员。
import turtle
turtle.title('Python(冰墩墩)')
turtle.speed(40)
# 左手
turtle.penup()
turtle.goto(177, 112)
turtle.pencolor("lightgray")
turtle.pensize(3)
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(80)
turtle.circle(-45, 200)
turtle.circle(-300, 23)
turtle.end_fill()
# 左手内
turtle.penup()
turtle.goto(182, 95)
turtle.pencolor("black")
turtle.pensize(1)
turtle.fillcolor("black")
turtle.begin_fill()
t