Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Any suggestions on the following prompt and code would be much

Any suggestions on the following prompt and code would be much

Computer Science

Any suggestions on the following prompt and code would be much...

Any suggestions on the following prompt and code would be much appreciated.

This is my prompt:

Modify the below code so that the traffic light changes automatically from color to color (right now the color changes when you press the space)  The sequence should repeat indefinitely.  Use a timer.

 

Here is my code:

 

#Tess becomes a traffic light
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 house 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)
   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 cicle
tess.shape("circle")
tess.shapesize(3)
tess.fillcolor("green")

#A traffic light is a kind of state machine with three states,
#Green, orange, Red.  We number these state 0, 1, 2
#When the machine changes state, we change tess' position and her fill color

#This variable holds the current state of the machine
state_num = 0

def advance_state_machine():
   global state_num
#Transition from state 0 to state 1
   if state_num == 0:
       tess.forward (70)
       tess.fillcolor("orange")
       state_num = 1
#Transition from state 1 to state 2
   elif state_num == 1:
       tess.forward(70)
       tess.fillcolor("red")
       state_num = 2
#Transition from state 2 to state 0
   else:
       tess.back (140)
       tess.fillcolor ("green")
       state_num = 0

#Bind the even handler to the space key.
wn.onkey (advance_state_machine, "space")

#listen for events
wn.listen()
wn.mainloop()

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Related Questions