我正在使用Pyscripter ide,但也在vscode中使用同样的输出问题。
我从一开始就期待这一点(即第一次迭代)
但它却是这样的
此 Python 脚本使用 Turtle 模块来模拟交通灯。以下是其工作原理的详细说明:
绘图函数:定义了多个函数来绘制交通灯场景的不同元素,例如天空、云、道路、草地、交通灯杆和文本。
draw_sky(x, y):绘制蓝天矩形。
draw_cloud(x, y):在指定位置绘制云。
draw_lawn(x, y):绘制绿色草坪。
draw_road(x, y):绘制一条黑色道路。
draw_road_lines(x, y):在道路上绘制白线来模拟道路标记。
draw_pole(x, y):绘制交通灯的灰色杆。
draw_box(x, y):为交通灯绘制一个黑框。
draw_text(x, y, message):在指定位置写入文本。
draw_traffic_light(x, y, active_light):绘制交通灯本身。它调用其他绘图函数来构建场景。
simulate_traffic_light():此函数连续循环显示交通灯颜色(红色、黄色、绿色),每次更改之间有 2 秒的延迟。
close_window():关闭 Turtle 图形窗口。
设置和主循环:设置尺寸为600x760像素的Turtle窗口,设置标题和背景颜色,隐藏turtle光标,将速度设置为0(最快),监听'c'键关闭窗口,并开始交通灯模拟。
主执行:调用Turtle模块的
mainloop()
函数,该函数使窗口保持打开状态并响应事件。
此脚本演示了如何使用 Turtle 模块在 Python 中创建简单的动画和绘图。 这是代码
import turtle
import time
def draw_filled_circle(color, radius):
turtle.begin_fill()
turtle.fillcolor(color)
turtle.circle(radius)
turtle.end_fill()
def draw_sky(x, y):
turtle.penup()
turtle.goto(x , y)
turtle.goto(x + 300, y - 10)
turtle.pendown()
turtle.color("sky blue")
turtle.begin_fill()
for \_ in range(2):
turtle.forward(600)
turtle.right(90)
turtle.forward(400)
turtle.right(90)
turtle.end_fill()
def draw_cloud(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color("white")
turtle.begin_fill()
for \_ in range(6):
turtle.circle(30, extent=180)
turtle.right(120)
turtle.end_fill()
def draw_lawn(x, y):
turtle.penup()
turtle.goto(x + 300, y)
turtle.pendown()
turtle.color("green")
turtle.begin_fill()
for \_ in range(2):
turtle.forward(600)
turtle.left(90)
turtle.forward(200)
turtle.left(90)
turtle.end_fill()
def draw_road(x, y):
turtle.penup()
turtle.goto(x + 300 , y - 170)
turtle.pendown()
turtle.color("black")
turtle.begin_fill()
for \_ in range(2):
turtle.forward(600)
turtle.left(90)
turtle.forward(200)
turtle.left(90)
turtle.end_fill()
def draw_road_lines(x, y):
turtle.penup()
turtle.goto(x - 300, y - 270)
turtle.setheading(0)
turtle.pendown()
turtle.color("white")
turtle.width(5)
for \_ in range(20):
turtle.forward(20)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.penup()
def draw_pole(x, y):
turtle.goto(x - 8, y - 170)
turtle.pendown()
turtle.color("black")
turtle.begin_fill()
turtle.fillcolor("gray")
turtle.setheading(90)
turtle.forward(150)
turtle.setheading(0)
turtle.forward(20)
turtle.setheading(-90)
turtle.forward(150)
turtle.setheading(180)
turtle.forward(20)
turtle.end_fill()
turtle.penup()
def draw_box(x, y):
turtle.goto(x, y)
turtle.goto(x + 38, y + 85)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor("black")
for \_ in range(2):
turtle.forward(75)
turtle.left(90)
turtle.forward(150)
turtle.left(90)
turtle.end_fill()
turtle.penup()
def draw_text(x, y, message):
turtle.penup()
turtle.goto(x, y + 150)
turtle.color("black")
turtle.write(message, align="center", font=("Calibri", 20, "normal"))
def draw_traffic_light(x, y, active_light):
turtle.penup()
turtle.goto(x, y)
draw_sky(x, y)
draw_lawn(x, y)
draw_cloud(150, 180)
draw_cloud(-50, 330)
draw_cloud(-250, 180)
draw_road(x, y)
draw_road_lines(x, y)
draw_pole(x, y)
draw_box(x, y)
turtle.goto(x + 2, y + 75)
if active_light == "red":
draw_filled_circle("red", 20)
draw_text(x, y, "STOP!")
else:
draw_filled_circle("gray", 20)
turtle.goto(x + 2, y + 30)
if active_light == "yellow":
draw_filled_circle("yellow", 20)
draw_text(x, y, "Get Ready!!")
else:
draw_filled_circle("gray", 20)
turtle.goto(x + 2, y - 15)
if active_light == "green":
draw_filled_circle("green", 20)
draw_text(x, y, "Let's GO !!!")
else:
draw_filled_circle("gray", 20)
def simulate_traffic_light():
while True:
turtle.clear()
draw_traffic_light(0, 0, "red")
time.sleep(2)
turtle.clear()
draw_traffic_light(0, 0, "yellow")
time.sleep(2)
turtle.clear()
draw_traffic_light(0, 0, "green")
time.sleep(2)
def close_window():
turtle.bye()
turtle.setup(width=600, height=760)
turtle.title("Traffic Light Simulation \['c' to quit\]")
turtle.bgcolor("grey")
turtle.hideturtle()
turtle.speed(0)
turtle.onkey(close_window, "c")
turtle.listen()
simulate_traffic_light()
turtle.mainloop()
我在您的代码中看到一些问题:
在较高级别时,交通灯以错误的顺序打开。不是“红 -> 黄 -> 绿 -> 红”,而是“红 -> 绿 -> 黄 -> 红”。
要更改点亮的灯光,您需要重新绘制整个场景。我们只需要用单独的乌龟重新绘制交通灯本身。
您使用的是
while True:
,它在像乌龟这样的事件驱动世界中没有地位。请改用海龟的 ontimer()
方法。
下面是通过上述更改和其他小调整重新思考您的代码:
from turtle import Screen, Turtle
def draw_filled_circle(color, radius):
light.fillcolor(color)
light.begin_fill()
light.circle(radius)
light.end_fill()
def draw_cloud(x, y):
turtle.penup()
turtle.goto(x + 45, y - 30)
turtle.pendown()
turtle.color("white")
turtle.begin_fill()
for _ in range(6):
turtle.circle(30, extent=180)
turtle.right(120)
turtle.end_fill()
def draw_lawn():
width = screen.window_width()
turtle.penup()
turtle.goto(-width/2, 0)
turtle.pendown()
turtle.color("green")
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(190)
turtle.right(90)
turtle.end_fill()
def draw_road():
width = screen.window_width()
turtle.penup()
turtle.goto(-width/2, -190)
turtle.pendown()
turtle.color("black")
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(190)
turtle.right(90)
turtle.end_fill()
def draw_road_lines():
width = screen.window_width()
turtle.penup()
turtle.goto(-width/2, -280)
turtle.setheading(0)
turtle.pendown()
turtle.color("white")
turtle.width(5)
for _ in range(width // 30):
turtle.forward(20)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.penup()
def draw_pole():
turtle.goto(-10, -190)
turtle.pendown()
turtle.color("black", "gray")
turtle.setheading(90)
turtle.begin_fill()
turtle.forward(100)
turtle.right(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(20)
turtle.end_fill()
turtle.penup()
def draw_box():
turtle.goto(37.5, 60)
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
for _ in range(2):
turtle.forward(75)
turtle.left(90)
turtle.forward(150)
turtle.left(90)
turtle.end_fill()
turtle.penup()
def draw_text(x, y, message):
light.penup()
light.goto(x, y + 150)
light.color("black")
light.write(message, align="center", font=("Calibri", 20, "normal"))
def draw_traffic_light():
light.penup()
light.goto(0, 15)
if active_light == "red":
draw_filled_circle("red", 20)
draw_text(0, 75, "STOP!")
else:
draw_filled_circle("gray", 20)
light.sety(-30)
if active_light == "yellow":
draw_filled_circle("yellow", 20)
draw_text(0, 75, "Get Ready To Stop!!")
else:
draw_filled_circle("gray", 20)
light.sety(-75)
if active_light == "green":
draw_filled_circle("green", 20)
draw_text(0, 75, "Let's GO !!!")
else:
draw_filled_circle("gray", 20)
def simulate_traffic_light():
global active_light
light.clear()
draw_traffic_light()
if active_light == 'red':
active_light = 'green'
elif active_light == 'green':
active_light = 'yellow'
elif active_light == 'yellow':
active_light = 'red'
screen.update()
screen.ontimer(simulate_traffic_light, 2000) # milliseconds
screen = Screen()
screen.setup(width=600, height=760)
screen.title("Traffic Light Simulation \[click on window to quit\]")
screen.bgcolor("sky blue")
screen.tracer(False)
turtle = Turtle()
turtle.hideturtle()
draw_lawn()
draw_cloud(150, 180)
draw_cloud(0, 330)
draw_cloud(-150, 180)
draw_road()
draw_road_lines()
draw_pole()
draw_box()
light = Turtle()
light.hideturtle()
active_light = 'red'
simulate_traffic_light()
screen.exitonclick()