用python海龟画个会倒计时的红绿灯

直接上代码吧

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"))这是图上写入文字的用法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值