直接上代码吧
import turtle
import time
turtle.setup(400,400)
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.speed(0)
#柱子
t1.color('black','grey')
t1.begin_fill()
t1.pu()
t1.goto(200,0)
t1.pd()
t1.seth(90)
t1.forward(300)
t1.seth(0)
t1.forward(50)
t1.seth(-90)
t1.forward(300)
t1.seth(180)
t1.forward(50)
t1.end_fill()
t1.begin_fill()
t1.seth(135)
t1.forward(30)
t1.seth(90)
t1.forward(300)
t1.goto(200,300)
t1.goto(200,0)
t1.end_fill()
#三个灯背景黑框
t1.begin_fill()
t1.pu()
t1.goto(240,250)
t1.pd()
t1.color('black')
t1.seth(45)
t1.circle(20,steps=4)
t1.end_fill()
t1.begin_fill()
t1.pu()
t1.goto(240,220)
t1.pd()
t1.circle(20,steps=4)
t1.end_fill()
t1.begin_fill()
t1.pu()
t1.goto(240,190)
t1.pd()
t1.circle(20,steps=4)
t1.end_fill()
#红灯
while True:
t1.pu()
t1.goto(226,263)
t1.pd()
t1.dot(20,'red')
t2.hideturtle()
t1.hideturtle()
time.sleep(2)
t2.pu()
t2.goto(223,255)
t2.pd()
t2.color('red')
t_time = 3
while t_time > 0:
t1.dot(20, 'black')
t2.clear()
t2.write(str(t_time), font=("Arial", 10, "normal"))
t2.hideturtle()
t1.hideturtle()
time.sleep(1)
t_time = t_time - 1
t1.dot(20, 'red')
t2.clear()
t1.dot(20, 'black')
#黄灯
t1.pu()
t1.goto(226,233)
t1.pd()
t1.dot(20,'yellow')
t_time1 = 3
time.sleep(1)
t2.pu()
t2.goto(223,225)
t2.pd()
t2.color('yellow')
while t_time1 > 0:
t1.dot(20, 'black')
t2.hideturtle()
t1.hideturtle()
time.sleep(1)
t_time1 = t_time1 - 1
t1.dot(20, 'yellow')
t2.clear()
t1.dot(20, 'black')
#绿灯
t1.pu()
t1.goto(226,203)
t1.pd()
t1.dot(20,'green')
time.sleep(2)
t2.pu()
t2.goto(223,195)
t2.pd()
t2.color('green')
t_time = 3
while t_time > 0:
t1.dot(20, 'black')
t2.clear()
t2.write(str(t_time), font=("Arial", 10, "normal"))
t2.hideturtle()
t1.hideturtle()
time.sleep(1)
t_time = t_time - 1
t1.dot(20, 'green')
t2.clear()
t1.dot(20, 'black')
t2.hideturtle()
t1.hideturtle()
turtle.done()
主要三个知识点
1、用t1=turtle.Turtle()
t2=turtle.Turtle()
可以实现一个画布同时画图。
2、time.sleep前面经常要加上隐藏海龟的,turtle.hideturtle,要不然经常有个箭号很讨厌。
3、turtle.write(str(t_time), font=("Arial", 10, "normal"))这是图上写入文字的用法。