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



此代码在 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)这样的事情在像这样的事件驱动的世界中是要避免的。 相反,考虑自己的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()

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

最新更新