注:本文非标准教程,仅是总结个人学习过程,可能存在纰漏,如有错误之处欢迎留言告知,非常感谢
来自:中国大学mooc 《Python语言程序设计》北京理工大学 嵩天、黄天羽、礼欣
所需掌握的知识:
- turtle库的使用: https://blog.csdn.net/weixin_43866567/article/details/89202831
- time库和random库的使用: https://blog.csdn.net/zhouheng2018/article/details/79829430
- 函数的调用
import turtle, time
def drawGap(): #绘制数码管间隔
turtle.penup()
turtle.fd(5)
def drawLine(draw): #绘制单段数码管
drawGap()
turtle.pendown() if draw else turtle.penup() #ture 就执行pendown() False 就 penup()
turtle.fd(40)
drawGap()
turtle.right(90)
def drawDigit(d): #根据数字绘制七段数码管
drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,2,6,8] else drawLine(False)
turtle.left(90)
drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)
turtle.left(180)
turtle.penup()
turtle.fd(20)
def drawDate(date):
turtle.pencolor("grey")
for i in date:
if i == '-':
turtle.write('年',font=("Arial", 30, "normal")) #字体 字号大小
turtle.pencolor("purple")
turtle.fd(40)
elif i == '=':
turtle.write('月',font=("Arial", 30, "normal"))
turtle.pencolor("cyan")
turtle.fd(40)
elif i == '+':
turtle.write('日',font=("Arial", 30, "normal"))
else:
drawDigit(eval(i))
def main():
turtle.setup(800, 350, 200, 200)
turtle.penup()
turtle.fd(-350)
turtle.pensize(5)
# drawDate('2019-4=05+')
drawDate(time.strftime('%Y-%m=%d+',time.gmtime()))
turtle.hideturtle()
turtle.done()
main()
- 函数的递归
#KochDrawV2.py
import turtle
def koch(size, n):
if n == 0:
turtle.fd(size)
else:
for angle in [0, 60, -120, 60]:
turtle.left(angle)
koch(size/3, n-1)
def main():
turtle.setup(600,600)
turtle.penup()
turtle.goto(-200, 100)
turtle.pendown()
turtle.pencolor("cyan")
turtle.pensize(2)
level = 3 # 3阶科赫雪花,阶数
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.hideturtle()
main()