【题目】:
1、不同花色的同切圆。
【源码】:
import turtle
turtle.pensize(4)
turtle.pencolor("red")
turtle.circle(200)
turtle.begin_fill()
turtle.color("yellow")
turtle.circle(180)
turtle.end_fill()
turtle.pensize(4)
turtle.pencolor("blue")
turtle.circle(160)
turtle.begin_fill()
turtle.color("green")
turtle.circle(140)
turtle.end_fill()
turtle.pensize(4)
turtle.pencolor("purple")
turtle.circle(120)
turtle.begin_fill()
turtle.color("red")
turtle.circle(100)
turtle.end_fill()
turtle.pensize(4)
turtle.pencolor("orange")
turtle.circle(80)
turtle.begin_fill()
turtle.color("yellow")
turtle.circle(60)
turtle.end_fill()
【运行结果】:
【题目】:
2、实现标靶。
【源码】:
import turtle
turtle.speed(0)#速度
turtle.setup(1000,1000,0,0)#坐标位置
turtle.pensize(20)#画笔粗细
turtle.begin_fill()
turtle.color("red")#画笔颜色
turtle.circle(30)
turtle.end_fill()
turtle.pencolor("black")#画笔颜色
turtle.write(10, font=("宋体", 10, "bold"))
for i in range(9):
turtle.penup()#抬起画笔
turtle.seth(-90)#改变画笔绘制方向
turtle.fd(20)
turtle.pendown()#放下画笔
if i==0:
turtle.pencolor("green")#画笔颜色
elif i==1:
turtle.pencolor("yellow")#画笔颜色
elif i==2:
turtle.pencolor("red")#画笔颜色
elif i==3:
turtle.pencolor("blue")#画笔颜色
elif i==4:
turtle.pencolor("purple")#画笔颜色
elif i==5:
turtle.pencolor("red")#画笔颜色
elif i==6:
turtle.pencolor("yellow")#画笔颜色
elif i==7:
turtle.pencolor("blue")#画笔颜色
elif i==8:
turtle.pencolor("purple")#画笔颜色
else:
turtle.pencolor("green")#画笔颜色
turtle.seth(0)#改变画笔绘制方向
turtle.circle(20+(i+1)*20)
turtle.pencolor("black")#画笔颜色
turtle.write(9-i, font=("宋体", 10, "bold"))
turtle.ht() #隐藏画笔
【运行结果】:
【题目】:
3、输出以下图形(迷宫):
【源码】:
import turtle
turtle.speed(0)#速度
N=int(input("输入:"))
for i in range(N):
turtle.left(90)#改变画笔绘制方向
turtle.fd(5+2*i)
【运行结果】:
【题目】:
4、进度条程序。
【源码】:
print("\t\t注册进行下载\n")
user_name=input("请输入用户名(以“_”开头,3-30个字符):")
password=input("请输入密码(由下划线、数字和字母共同组成,8—16个字符):")
if user_name[0]!='_':
print("用户名请使用下划线开头!")
elif 3>len(user_name) or 30len(password) or 16{}]|".format(c,a,b))
time.sleep(0.2)
print("-----------执行结束-----------")
【运行结果】:
【题目】:
5、画笑脸程序。
【源码】:
import turtle
def arc(sa,ea,x,y,r):
turtle.penup()
turtle.goto(x,y)
turtle.setheading(0)
turtle.left(sa)
turtle.fd(r)
turtle.pendown()
turtle.left(90)
turtle.circle(r,(ea-sa))
return turtle.position()
turtle.hideturtle()
#画脸
turtle.speed(0)
turtle.setup(900,600,200,200)
turtle.pensize(5)
turtle.right(90)
turtle.penup()
turtle.fd(100)
turtle.left(90)
turtle.pendown()
turtle.begin_fill()
turtle.pencolor("black")
turtle.circle(150)
turtle.fillcolor("yellow")
turtle.end_fill()
#画嘴
turtle.penup()
turtle.goto(77,20)
turtle.pencolor("brown")
turtle.goto(0,50)
turtle.right(30)
turtle.fd(110)
turtle.right(90)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor("brown")
turtle.circle(-97,160)
turtle.goto(92,-3)
turtle.end_fill()
turtle.penup()
turtle.goto(77,-25)
#画牙齿
turtle.pencolor("white")
turtle.begin_fill()
turtle.fillcolor("white")
turtle.goto(77,-24)
turtle.goto(-81,29)
turtle.goto(-70,43)
turtle.goto(77,-8)
turtle.end_fill()
turtle.penup()
turtle.goto(0,-100)
turtle.setheading(0)
turtle.pendown()
#画左边眼泪
turtle.left(90)
turtle.penup()
turtle.fd(150)
turtle.right(60)
turtle.fd(-150)
turtle.pendown()
turtle.left(20)
turtle.pencolor("purple")
turtle.fd(150)
turtle.right(180)
position1=turtle.position()
turtle.begin_fill()
turtle.fillcolor("blue")
turtle.fd(150)
turtle.right(20)
turtle.left(270)
turtle.circle(-150,18)
turtle.right(52)
turtle.fd(110)
position2=turtle.position()
turtle.goto(-33,90)
turtle.end_fill()
#画右边眼泪
turtle.penup()
turtle.goto(0,0)
turtle.setheading(0)
turtle.left(90)
turtle.fd(50)
turtle.right(150)
turtle.fd(150)
turtle.left(150)
turtle.fd(100)
turtle.pendown()
turtle.begin_fill()
turtle.fd(-100)
turtle.fillcolor("blue")
turtle.right(60)
turtle.circle(150,15)
turtle.left(45)
turtle.fd(66)
turtle.goto(77,20)
turtle.end_fill()
#画眼睛
turtle.penup()
turtle.pencolor("brown")
turtle.goto(-65,75)
turtle.setheading(0)
turtle.left(27)
turtle.fd(38)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor("brown")
turtle.left(90)
turtle.circle(38,86)
turtle.goto(position2[0],position2[1])
turtle.goto(position1[0],position1[1])
turtle.end_fill()
#画手
turtle.pencolor("gray")
turtle.begin_fill()
turtle.fillcolor("brown")
#第一个手指
arc(-110,10,110,-40,30)
turtle.circle(300,35)
turtle.circle(13,120)
turtle.setheading(-50)
turtle.fd(20)
turtle.setheading(130)
#第二个手指
turtle.circle(200,15)
turtle.circle(12,180)
turtle.fd(40)
turtle.setheading(137)
#第三个手指
turtle.circle(200,16)
turtle.circle(12,160)
turtle.setheading(-35)
turtle.fd(45)
turtle.setheading(140)
#第四个手指
turtle.circle(200,13)
turtle.circle(11,160)
turtle.setheading(-35)
turtle.fd(40)
turtle.setheading(145)
#第五个手指
turtle.circle(200,9)
turtle.circle(10,180)
turtle.setheading(-31)
turtle.fd(50)
#画最后手腕的部分
turtle.setheading(-45)
turtle.pensize(7)
turtle.right(5)
turtle.circle(180,35)
turtle.end_fill()
turtle.begin_fill()
turtle.setheading(-77)
turtle.pensize(5)
turtle.fd(50)
turtle.left(-270)
turtle.fd(7)
turtle.pencolor("gray")
turtle.circle(30,180)
turtle.end_fill()
turtle.done()
【运行结果】:
【题目】:
6、实现钟表程序。
【源码】:
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
【运行结果】: