Advertisement
tokyoedtech

Python Stoplights (Turtle Module / Classes)

Jun 19th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. # Python 3.6 Stoplight Class Demo using the Turtle Module
  2. # by TokyoEdTech
  3. # www.christianthompson.com
  4.  
  5. # Import the turtle module
  6. # Set up the screen
  7. import turtle
  8. wn = turtle.Screen()
  9. wn.title("Stoplights by @TokyoEdTech")
  10. wn.bgcolor("black")
  11.  
  12. # Create the Stoplight class
  13. class Stoplight():
  14.     def __init__(self, x, y):
  15.         self.red_light = turtle.Turtle()
  16.         self.yellow_light = turtle.Turtle()
  17.         self.green_light = turtle.Turtle()
  18.  
  19.         self.red_light.speed(0)
  20.         self.yellow_light.speed(0)
  21.         self.green_light.speed(0)
  22.  
  23.         self.red_light.color("grey")
  24.         self.yellow_light.color("grey")
  25.         self.green_light.color("grey")
  26.  
  27.         self.red_light.shape("circle")
  28.         self.yellow_light.shape("circle")
  29.         self.green_light.shape("circle")
  30.  
  31.         self.red_light.penup()
  32.         self.yellow_light.penup()
  33.         self.green_light.penup()
  34.  
  35.         self.red_light.goto(x, y + 40)
  36.         self.yellow_light.goto(x, y)
  37.         self.green_light.goto(x, y - 40)
  38.  
  39.         self.color = ""
  40.  
  41.         self.pen = turtle.Turtle()
  42.         self.pen.penup()
  43.         self.pen.width(3)
  44.         self.pen.hideturtle()
  45.         self.pen.speed(0)
  46.         self.pen.color("yellow")
  47.         self.pen.goto(x - 30, y + 60)
  48.         self.pen.down()
  49.         self.pen.fd(60)
  50.         self.pen.rt(90)
  51.         self.pen.fd(120)
  52.         self.pen.rt(90)
  53.         self.pen.fd(60)
  54.         self.pen.rt(90)
  55.         self.pen.fd(120)
  56.    
  57.     # Set the color of the stoplight
  58.     def set_color(self, color):
  59.         self.red_light.color("grey")
  60.         self.yellow_light.color("grey")
  61.         self.green_light.color("grey")
  62.  
  63.         if color == "red":
  64.             self.red_light.color("red")
  65.             self.color = "red"
  66.         elif color == "yellow":
  67.             self.yellow_light.color("yellow")
  68.             self.color = "yellow"
  69.         elif color == "green":
  70.             self.green_light.color("green")
  71.             self.color = "green"
  72.         else:
  73.             print("Error: Unknown Color")
  74.  
  75.     # Use a timer to change the stoplight
  76.     def timer(self):
  77.         if self.color == "red":
  78.             self.set_color("green")
  79.             wn.ontimer(self.timer, 2000)
  80.         elif self.color == "yellow":
  81.             self.set_color("red")
  82.             wn.ontimer(self.timer, 2000)
  83.         elif self.color == "green":
  84.             self.set_color("yellow")
  85.             wn.ontimer(self.timer, 1000)
  86.  
  87. # Create the first stoplight instance
  88. stoplight = Stoplight(0, 0)
  89. stoplight.set_color("red")
  90. stoplight.timer()
  91.  
  92. # Create the second stoplight instance
  93. stoplight2 = Stoplight(-100, 0)
  94. stoplight2.set_color("yellow")
  95. stoplight2.timer()
  96.  
  97. # Create the third stoplight instance
  98. stoplight3 = Stoplight(100, 0)
  99. stoplight3.set_color("green")
  100. stoplight3.timer()
  101.  
  102. # Start the main events loop
  103. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement