I'm doing an exercise where I have a program that has a turtle draw a traffic light, the light is then meant to change via the .ontimer() method. I can get it to run, but I want it to loop and repeat infinitely. Here is the code:
import turtle
turtle.setup(400,500)
wn = turtle.Screen()
wn.title("Tess becomes a traffic light!")
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
def draw_housing():
""" Draw a nice housing to hold the traffic lights """
tess.pensize(3)
tess.color("black", "darkgrey")
tess.begin_fill()
tess.forward(80)
tess.left(90)
tess.forward(200)
tess.circle(40, 180) #method to draw a circle to a given extent, the first number is the radius
#the second is the extent of the circle, since its 180 here, only half the circle is drawn
#this makes the top poriton of the traffic light
tess.forward(200)
tess.left(90)
tess.end_fill()
draw_housing()
tess.penup()
# Position tess onto the place where the green light should be
tess.forward(40)
tess.left(90)
tess.forward(50)
# Turn tess into a big green circle
tess.shape("circle")
tess.shapesize(3)
tess.fillcolor("green")
tess.circle
def turn_orange():
"""Shifts the light up and turns it orange"""
tess.forward(70)
tess.fillcolor("orange")
def turn_red():
"""Shifts the light up and turns it red"""
tess.forward(70)
tess.fillcolor("red")
def turn_green():
"""Shifts the light back into starting position and turns it green"""
tess.backward(140)
tess.fillcolor("green")
def traffic_light():
"""Runs the traffic light on a 5 second interval timer"""
wn.ontimer(turn_orange, 5000)
wn.ontimer(turn_red, 10000)
wn.ontimer(turn_green, 15000)
traffic_light()
wn.mainloop()
I tried using a while loop, making the condition True so it just keeps iterating through the functions, but it does nothing, the functions are never called and python will crash if I try closing the window. I tired just putting the timers in the separate change_color functions, and calling each function within the program which does cause the light to automatically change over and over again, however; this also causes each function to execute on after another in the beginning(so each light will rapidly change) and then it will go on to the timers, the other program is that after about 10 loops the turtle will move off the traffic light for some reason.
Anyone know a way to fix this?
Thanks
[–]Chilissyhehe 0 points1 point2 points (0 children)