python - 用海龟创建一个红绿灯循环,但所有的灯都只进入四个红绿灯中的一个
问题描述
在我的程序中,我试图在四个角落放置 4 个红绿灯,同时使用 turtle 和 python 跟随 2 个计时器,这样一对红绿灯在绿灯处开始,另一对在红灯处开始。我已经正确定位了它,但问题是 4 组灯进入右下角的红绿灯,而不是每组进入单独的一个。下面是我的代码:
import turtle
wn = turtle.Screen()
wn.title("4 Stoplights")
wn.bgcolor("black")
class Stoplight():
def __init__(self, x, y):
self.line = turtle.Turtle()
self.line.penup()
self.line.hideturtle()
self.line.speed(0)
self.line.color("yellow")
self.line.goto(225, 250)
self.line.pendown()
self.line.fd(60)
self.line.rt(90)
self.line.fd(120)
self.line.rt(90)
self.line.fd(60)
self.line.rt(90)
self.line.fd(120)
self.line = turtle.Turtle()
self.line.penup()
self.line.hideturtle()
self.line.speed(0)
self.line.color("yellow")
self.line.goto(225, -120)
self.line.pendown()
self.line.fd(60)
self.line.rt(90)
self.line.fd(120)
self.line.rt(90)
self.line.fd(60)
self.line.rt(90)
self.line.fd(120)
self.line = turtle.Turtle()
self.line.penup()
self.line.hideturtle()
self.line.speed(0)
self.line.color("yellow")
self.line.goto(-270, -120)
self.line.pendown()
self.line.fd(60)
self.line.rt(90)
self.line.fd(120)
self.line.rt(90)
self.line.fd(60)
self.line.rt(90)
self.line.fd(120)
self.line = turtle.Turtle()
self.line.penup()
self.line.hideturtle()
self.line.speed(0)
self.line.color("yellow")
self.line.goto(-270, 250)
self.line.pendown()
self.line.fd(60)
self.line.rt(90)
self.line.fd(120)
self.line.rt(90)
self.line.fd(60)
self.line.rt(90)
self.line.fd(120)
self.color = ""
self.red_light = turtle.Turtle()
self.yellow_light = turtle.Turtle()
self.green_light = turtle.Turtle()
self.red_light.speed(0)
self.yellow_light.speed(0)
self.green_light.speed(0)
self.red_light.color("grey")
self.yellow_light.color("grey")
self.green_light.color("grey")
self.red_light.shape("circle")
self.yellow_light.shape("circle")
self.green_light.shape("arrow")
self.red_light.penup()
self.yellow_light.penup()
self.green_light.penup()
self.red_light.goto(255, 230)
self.yellow_light.goto(255, 190)
self.green_light.goto(255, 150)
self.red_light = turtle.Turtle()
self.yellow_light = turtle.Turtle()
self.green_light = turtle.Turtle()
self.red_light.speed(0)
self.yellow_light.speed(0)
self.green_light.speed(0)
self.red_light.color("grey")
self.yellow_light.color("grey")
self.green_light.color("grey")
self.red_light.shape("circle")
self.yellow_light.shape("circle")
self.green_light.shape("arrow")
self.red_light.penup()
self.yellow_light.penup()
self.green_light.penup()
self.red_light.goto(-240, 230)
self.yellow_light.goto(-240, 190)
self.green_light.goto(-240, 150)
self.red_light = turtle.Turtle()
self.yellow_light = turtle.Turtle()
self.green_light = turtle.Turtle()
self.red_light.speed(0)
self.yellow_light.speed(0)
self.green_light.speed(0)
self.red_light.color("grey")
self.yellow_light.color("grey")
self.green_light.color("grey")
self.red_light.shape("circle")
self.yellow_light.shape("circle")
self.green_light.shape("arrow")
self.red_light.penup()
self.yellow_light.penup()
self.green_light.penup()
self.red_light.goto(-240, -140)
self.yellow_light.goto(-240, -180)
self.green_light.goto(-240, -220)
self.red_light = turtle.Turtle()
self.yellow_light = turtle.Turtle()
self.green_light = turtle.Turtle()
self.red_light.speed(0)
self.yellow_light.speed(0)
self.green_light.speed(0)
self.red_light.color("grey")
self.yellow_light.color("grey")
self.green_light.color("grey")
self.red_light.shape("circle")
self.yellow_light.shape("circle")
self.green_light.shape("arrow")
self.red_light.penup()
self.yellow_light.penup()
self.green_light.penup()
self.red_light.goto(255, -140)
self.yellow_light.goto(255, -180)
self.green_light.goto(255, -220)
def change_color(self, color):
self.red_light.color("grey")
self.yellow_light.color("grey")
self.green_light.color("grey")
if color == "red":
self.red_light.color("red")
self.color = "red"
elif color == "yellow":
self.yellow_light.color("yellow")
self.color = "yellow"
elif color == "green":
self.green_light.color("green")
self.color = "green"
else:
print("Error: Unknown Color {}".format(color))
def change_color2(self, color):
self.red_light.color("grey")
self.yellow_light.color("grey")
self.green_light.color("grey")
if color == "green":
self.red_light.color("green")
self.color = "green"
elif color == "yellow":
self.yellow_light.color("yellow")
self.color = "yellow"
elif color == "red":
self.green_light.color("red")
self.color = "red"
else:
print("Error: Unknown Color {}".format(color))
def timer(self):
if self.color == "yellow":
self.change_color("red")
wn.ontimer(self.timer, 3300)
elif self.color == "green":
self.change_color("yellow")
wn.ontimer(self.timer, 3300)
elif self.color == "red":
self.change_color("green")
wn.ontimer(self.timer, 3300)
def timer2(self):
if self.color == "yellow":
self.change_color("red")
wn.ontimer(self.timer, 2000)
elif self.color == "green":
self.change_color("yellow")
wn.ontimer(self.timer, 2000)
elif self.color == "red":
self.change_color("green")
wn.ontimer(self.timer, 2000)
stoplight = Stoplight(225, 250)
stoplight.change_color("red")
stoplight.timer()
stoplight2 = Stoplight(225, -120)
stoplight2.change_color("red")
stoplight2.timer()
stoplight3 = Stoplight(-270, -120)
stoplight3.change_color2("green")
stoplight3.timer2()
stoplight4 = Stoplight(-270, 250)
stoplight4.change_color2("green")
stoplight4.timer2()
wn.mainloop()
解决方案
推荐阅读
- excel - 即使 Excel 语言设置为阿拉伯语,VBA 也无法设置 RightToLeft
- php - 如何将我的下拉值传递给 CodeIgniter 控制器?
- gitlab - 如何将 Gitlab 页面映射到端口?
- c# - 如何检查一个 int 是否可以被另一个 int 整除?使用c#,以下指定条件
- reactjs - 反应路由不起作用 - 使用 react-router-bootstrap
- python - 如何使用 Python 从分类数据制作列联表?
- wordpress - 如何查找 Wordpress Hook 是否有效
- sql - 在 QLDB 中获取整个分类帐历史记录
- powershell - PowerShell 的 getElementsByTagName 是否支持自定义标签 InnerHTML?
- c# - 如何在C#中按条件设置DataGrid中部分文本的样式