在屏幕中心建立一个宽为600,高为400的绘图窗口,在绘图窗口中从坐标(10,50)开始画一个边长为80的正方形,要求边为绿色,画笔宽度为4。
import turtle as t
# 在屏幕中心建立一个宽为600,高为400的绘图窗口,
t.setup(600,400,200,200)
# 在绘图窗口中从坐标(10,50)开始画一个边长为80的正方形,要求边为绿色,画笔宽度为4。
t.color("green")
t.pensize(4)
t.speed(1)
t.penup()
t.goto(10,50)
#画笔执行四次同样的动作:绘制长度为100的直线、向右转90度。
t.pendown()
t.forward(80)
t.right(90)
t.forward(80)
t.right(90)
t.forward(80)
t.right(90)
t.forward(80)
t.right(90)
#结束绘图
t.done()
在屏幕中心建立一个宽为屏幕宽度1/2,高为屏幕高度1/2的绘图窗口,以坐标(200,50)为起点绘一个半径为75的圆的内接六边形,并以蓝色为填充。移动画笔至坐标原点,向左侧画一个圆,半径为60,画笔颜色为红色。
import turtle as t
# 在屏幕中心建立一个宽为屏幕宽度1/2,高为屏幕高度1/2的绘图窗口,
t.setup(width=0.5, height=0.5)
t.pensize(3)
t.speed(2)
# 以坐标(200,50)为起点绘一个半径为75的圆的内接六边形,并以蓝色为填充
t.penup()
t.goto(200,50)
t.pendown()
t.begin_fill()
t.color("blue")
t.circle(75,steps=6)
t.end_fill()
# 移动画笔至坐标原点
t.penup()
t.home()
t.pendown()
# 向左侧画一个圆,半径为60,画笔颜色为红色。
t.color("red")
t.circle(60)#向左侧画一个圆
t.done()
在屏幕中心建立一个宽为屏幕宽度1/2,高为屏幕高度1/2的绘图窗口,在绘图窗口点绘5个边长为50的五角星,填充色为红色。绘图位置自己确定。
import turtle as t
# 在屏幕中心建立一个宽为屏幕宽度1/2,高为屏幕高度1/2的绘图窗口
t.setup(width=0.5, height=0.5)
# 在绘图窗口点绘5个边长为50的五角星,填充色为红色。绘图位置自己确定。
t.speed(1)
t.pencolor('red')
t.fillcolor('yellow')
t.begin_fill()
k = 50
for j in range(5):
t.penup()
t.home()
t.forward(k)
t.pendown()
for i in range(5):#这个语句是循环语句,可以控制循环部分执行多次。
t.forward(50)
t.right(144)
t.end_fill()
k = k + 10
t.penup()
t.done()
学习以上内容后,请尝试用小海龟在计算机屏幕上绘出你自己设计的图案。
# 画太阳花
# coding=utf-8
import turtle
import time
# 同时设置pencolor=color1, fillcolor=color2
turtle.color("red", "yellow")
turtle.begin_fill()
for _ in range(50):
turtle.forward(200)
turtle.left(170)
turtle.end_fill()
turtle.mainloop()
#绘制科赫雪花
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.pensize(2)
level = 3 #3阶科赫雪花,阶数
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.hideturtle()
turtle.done()
main()
# 时钟
# 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()