有没有办法在我的红绿灯代码下方添加一个计数器?

Is there a way to add a counter below my traffic light code?

此代码在 turtle.screen() 上显示交通灯。我正在尝试在我的代码中添加一个倒计时计时器,它应该显示在交通灯下方的乌龟屏幕上,而不是 python 终端上。

import turtle
import time
wn = turtle.Screen()
wn.title("traffic lights")
wn.bgcolor("black")
#box
pen = turtle.Turtle()
pen.color("yellow")
pen.width(3)
pen.hideturtle()
pen.penup()
pen.goto(-30, 60)
pen.pendown()
pen.fd(60)
pen.rt(90)
pen.fd(120)
pen.rt(90)
pen.fd(60)
pen.rt(90)
pen.fd(120)

#red
red_light = turtle.Turtle()
red_light.shape("circle")
red_light.color("grey")
red_light.penup()
red_light.goto(0,40)

#yellow
yellow_light= turtle.Turtle()
yellow_light.shape("circle")
yellow_light.color("grey")
yellow_light.penup()
yellow_light.goto(0,0)

#green
green_light= turtle.Turtle()
green_light.shape("circle")
green_light.color("grey")
green_light.penup()
green_light.goto(0,-40)

while True:
    yellow_light.color("grey")
    red_light.color("red")
    time.sleep(4)

    red_light.color("grey")
    green_light.color("green")
    time.sleep(3)

    green_light.color("grey")
    yellow_light.color("yellow")
    time.sleep(2)

在哪里添加计数器代码,以便在龟屏上显示

添加此类功能的一个困难是该程序构建不正确。 while True:time.sleep(4) 之类的东西在 turtle 这样的事件驱动世界中是要避免的。相反,请考虑 turtle 自己的 ontimer() 方法,它做得更好并且可以很好地处理事件。

首先要做的是修复代码以使用状态机和ontimer()改变灯光:

def state_machine():
    if red_light.pencolor() == 'red':
        red_light.color('grey')
        green_light.color('green')
        screen.ontimer(state_machine, 3000)

    elif green_light.pencolor() == 'green':
        green_light.color('grey')
        yellow_light.color('yellow')
        screen.ontimer(state_machine, 2000)

    elif yellow_light.pencolor() == 'yellow':
        yellow_light.color('grey')
        red_light.color('red')
        screen.ontimer(state_machine, 4000)

为此,其中一盏灯必须开始点亮,而不是灰色。一旦成功,我们也可以添加一个倒数计时器:

def countdown(seconds):
    count.clear()
    count.write(seconds, align='center', font=FONT)

    seconds -= 1

    if seconds > 0:
        screen.ontimer(lambda s=seconds: countdown(s), 1000)

并把它们放在一起:

from turtle import Screen, Turtle

FONT = ('Arial', 18, 'normal')

screen = Screen()
screen.title("Traffic Lights")
screen.bgcolor('black')

# box
pen = Turtle(visible=False)
pen.color('yellow')
pen.width(3)
pen.penup()
pen.goto(-30, 60)
pen.pendown()

for _ in range(2):
    pen.fd(60)
    pen.rt(90)
    pen.fd(120)
    pen.rt(90)

# count down timer
count = Turtle(visible=False)
count.color('white')
count.penup()
count.sety(-100)

# red light
red_light = Turtle('circle')
red_light.color('grey')
red_light.penup()
red_light.sety(40)

# yellow light
yellow_light = Turtle('circle')
yellow_light.color('yellow')  # initially on
yellow_light.penup()

# green light
green_light = Turtle('circle')
green_light.color('grey')
green_light.penup()
green_light.sety(-40)

def countdown(seconds):
    count.clear()
    count.write(seconds, align='center', font=FONT)

    seconds -= 1

    if seconds > 0:
        screen.ontimer(lambda s=seconds: countdown(s), 1000)

def state_machine():
    if red_light.pencolor() == 'red':
        red_light.color('grey')
        green_light.color('green')
        countdown(3)
        screen.ontimer(state_machine, 3000)

    elif green_light.pencolor() == 'green':
        green_light.color('grey')
        yellow_light.color('yellow')
        countdown(2)
        screen.ontimer(state_machine, 2000)

    elif yellow_light.pencolor() == 'yellow':
        yellow_light.color('grey')
        red_light.color('red')
        countdown(4)
        screen.ontimer(state_machine, 4000)

state_machine()

screen.exitonclick()

此方法的另一个优点是,当您退出原始代码时,通常会收到十行错误消息,因为 window 关闭与您的 sleep() 调用不同步。在上面修改后的代码中,关闭 window 干净地退出,没有错误消息,因为一切都在同步。