Build a program that uses a turtle in python to simulate the traffic lights. There will be three states in our traffic light: Red, then Green then Yellow and then repeat itself. The light should spend 4 seconds in the Red state, then three seconds in the Green state, and then 2 seconds in the Yellow state.
Answers
Answer:
Friday, August 11, 2017
6 mins read
We’re going to build a program that uses a turtle in python to simulate the traffic lights.
There will be four states in our traffic light: Green, then Green and Orange together, then Orange only, and then Red. The light should spend 3 seconds in the Green state, followed by one second in the Green+Orange state, then one second in the Orange state, and then 2 seconds in the Red state.
import turtle # Allows
turtle.setup(400, 600) # Determine the window size
wn = turtle.Screen() # Creates a playground for turtles
wn.title('traffic light using different turtles') # Set the window title
wn.bgcolor('skyblue') # Set the window background color
tess = turtle.Turtle() # Create a turtle, assign to tess
alex = turtle.Turtle() # Create alex
henry = turtle.Turtle() # Create henry
def draw_housing():
""" Draw a nice housing to hold the traffic lights"""
tess.pensize(3) # Change tess' pen width
tess.color('black', 'white') # Set tess' color
tess.begin_fill() # Tell tess to start filling the color
tess.forward(80) # Tell tess to move forward by 80 units
tess.left(90) # Tell tess to turn left by 90 degrees
tess.forward(200)
tess.circle(40, 180) # Tell tess to draw a semi-circle
Answer: