# 蟒蛇绘制
#引入trutle库
import turtle
# turtle.goto() 以海龟为(0,0)点,直接调转画布任何位置
turtle.setup(650,350,200,200) # setup设置trutle画布的[长,宽,startx,starty]
turtle.penup() # 抬起画笔
turtle.fd(-250) # 向海龟方向前进-250个像素(倒退)也可以用 .bk(250)也表示倒退
turtle.pendown() # 放下画笔
turtle.pensize(25) # 将画笔加粗到25个像素
turtle.pencolor("purple") # 设置画笔颜色,也可以使用RGB表示(1,1,1)或者(255,255,255)
turtle.seth(-40) # 只改变海龟的朝向,不移动,是绝对方向的坐标
# turtle.left() 或者 turtle.rigth() 都是以海龟头朝向的位置向左或右偏向的角度
for i in range(4):
turtle.circle(40,80) # 40是半径,80是角度,40是海龟左侧40的距离为圆心,旋转80°
turtle.circle(-40,80)# -40是距离海龟右测40的距离为圆心,旋转80°
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40* 2/3)
turtle.done()
实例七段数码管
import turtle as t
import time
def drawGap():
t.penup()
t.fd(5)
def drawLine(draw):
drawGap()
t.pendown() if draw else t.penup()
t.fd(40)
drawGap()
t.right(90)
def drawDigit(digit):
drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if digit in [0,2,6,8] else drawLine(False)
t.left(90)
drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False)
drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False)
t.left(180)
t.penup()
t.fd(20)
def drawDate(date):
t.pencolor("red")
for i in date:
if i == '-':
t.write("年",font=("Arial",18,"normal"))
t.pencolor("green")
t.fd(40)
elif i == '=':
t.write("月",font=("Arial",18,"normal"))
elif i == '+':
t.write("日",font=("Arial",18,"normal"))
else:
drawDigit(eval(i))
for i in date:
drawDigit(eval(i))
def main():
t.setup(1200,350,200,200)
t.penup()
t.fd(-500)
t.pensize(5)
drawDate(time.strftime("%Y-%m=%d+",time.gmtime()))
drawDate('20181010')
t.hideturtle()
t.done()
main()
# 科赫雪花
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 mian():
turtle.setup(800,600,200,200)
turtle.penup()
turtle.goto(-200,100)
turtle.pendown()
turtle.pensize(2)
level = 3
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.hideturtle()
mian()
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 mian():
turtle.setup(800,600,200,200)
turtle.penup()
turtle.goto(-200,100)
turtle.pendown()
turtle.pensize(2)
level = 3
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.hideturtle()
mian()