首页 > 解决方案 > 用海龟创建一个红绿灯循环,但所有的灯都只进入四个红绿灯中的一个

问题描述

在我的程序中,我试图在四个角落放置 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()

标签: pythonloops

解决方案


推荐阅读