Advertisement
crummy

Untitled

Oct 31st, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.16 KB | None | 0 0
  1. import time as time
  2. from graphics import *
  3.  
  4. class Button:
  5.    
  6.     def __init__(self, win, center, width, height, label):
  7.  
  8.         w,h = width/2.0, height/2.0
  9.         x,y = center.getX(), center.getY()
  10.         self.xmax, self.xmin = x+w, x-w
  11.         self.ymax, self.ymin = y+h, y-h
  12.         p5 = Point(self.xmin, self.ymin)
  13.         p6 = Point(self.xmax, self.ymax)
  14.         self.rect = Rectangle(p5, p6)
  15.         self.rect.setFill("lightgrey")
  16.         self.rect.draw(win)
  17.         self.label = Text(center, label)
  18.         self.label.draw(win)
  19.         self.deactivate()
  20.  
  21.  
  22.     def clicked(self, p):
  23.         "Returns true if button active and p is inside"
  24.         if p is None:
  25.             return False
  26.         return(self.active and self.xmin <= p.getX() <= self.xmax and self.ymin <= p.getY() <= self.ymax)
  27.  
  28.     def getLabel(self):
  29.         "Returns the label string on this button."
  30.         return self.label.getText()
  31.    
  32.     def activate(self):
  33.         "sets this button to 'active'."
  34.         self.label.setFill("black")
  35.         self.rect.setWidth(2)
  36.         self.active = True
  37.  
  38.  
  39.     def deactivate(self):
  40.         "sets this button to 'inactive'."
  41.         self.label.setFill("darkgrey")
  42.         self.rect.setWidth(1)
  43.         self.active = False
  44.  
  45.  
  46.  
  47.  
  48. def main():
  49.  
  50.     print("Sara Heedy, Chemical Engineering" )
  51.    
  52.     # window information
  53.  
  54.     win = GraphWin("Traffic Lights", 800, 600)
  55.     win.setCoords(0.0, 0.0, 4.0, 6.0)
  56.  
  57.     # Start and Quit Buttons
  58.  
  59.     startButton = Button(win, Point(0.50, 0.50), 0.5, 0.5, "Start")
  60.     startButton.activate()
  61.     quitButton = Button(win, Point(1.50, 0.50), 0.5, 0.5, "Quit")
  62.  
  63.  
  64.     # Entry Times and Boxes
  65.  
  66.     Text(Point(3.25, 5.75), "Enter Red Time (s)").draw(win)
  67.     red = Entry(Point(3.25, 5.25), 6)
  68.     red.setText("1.0")
  69.     red.draw(win)
  70.     redr = Rectangle(Point(3.00, 5.00), Point(3.50, 5.50))
  71.     redr.setOutline("red")
  72.     redr.draw(win)
  73.  
  74.  
  75.     Text(Point(3.25, 4.75), "Enter Green Time (s)").draw(win)
  76.     green = Entry(Point(3.25, 4.25), 6)
  77.     green.setText("1.0")
  78.     green.draw(win)
  79.     greenr = Rectangle(Point(3.00, 4.00), Point(3.50, 4.50))
  80.     greenr.setOutline("green")
  81.     greenr.draw(win)
  82.  
  83.  
  84.     Text(Point(3.25, 3.75), "Enter Yellow Time (s)").draw(win)
  85.     yellow = Entry(Point(3.25, 3.25), 6)
  86.     yellow.setText("1.0")
  87.     yellow.draw(win)
  88.     yellowr = Rectangle(Point(3.00, 3.00), Point(3.50, 3.50))
  89.     yellowr.setOutline("yellow")
  90.     yellowr.draw(win)
  91.  
  92.  
  93.  
  94.     # Pedestrian
  95.  
  96.     Text(Point(3.25, 2.75), "Enter Pedestrian, 1 or 0").draw(win)
  97.     ped = Entry(Point(3.25, 2.25), 6)
  98.     ped.setText("0.0")
  99.     ped.draw(win)
  100.     pedr = Rectangle(Point(3.00, 2.00), Point(3.50, 2.50))
  101.     pedr.setOutline("cyan")
  102.     pedr.draw(win)
  103.  
  104.  
  105.  
  106.     # Begin clicks for Traffic Lights
  107.  
  108.     # Traffic Light Title
  109.     title = Text(Point(1.5,5.5),"Traffic Lights")
  110.     title.setStyle("bold")
  111.     title.setTextColor("darkgreen")
  112.     title.setSize(18)
  113.     title.draw(win)
  114.    
  115.    
  116.     # Traffic Light 1
  117.     message = Text(Point(1.5,5.1), "Click 2 times to make your first traffic box")
  118.     message.draw(win)
  119.  
  120.     p1 = win.getMouse()
  121.     p1.draw(win)
  122.     p2 = win.getMouse()
  123.     p2.draw(win)
  124.     box1 = Rectangle(p1,p2)
  125.     box1.setOutline("black")
  126.     box1.setFill("grey")
  127.     box1.draw(win)
  128.  
  129.     message.setText("Click 3 times to make traffic lights")
  130.  
  131.     a1 = win.getMouse()
  132.     circ1 = Circle(a1,.2)
  133.     circ1.setOutline("black")
  134.     circ1.setFill("red")
  135.     circ1.draw(win)
  136.  
  137.     a2 = win.getMouse()
  138.     circ2 = Circle(a2,.2)
  139.     circ2.setOutline("black")
  140.     circ2.setFill("yellow")
  141.     circ2.draw(win)
  142.  
  143.     a3 = win.getMouse()
  144.     circ3 = Circle(a3,.2)
  145.     circ3.setOutline("black")
  146.     circ3.setFill("green")
  147.     circ3.draw(win)
  148.  
  149.  
  150.  
  151.     # Traffic Light 2
  152.     message.setText("Click 2 times to make second traffic box")
  153.  
  154.  
  155.     p3 = win.getMouse()
  156.     p3.draw(win)
  157.     p4 = win.getMouse()
  158.     p4.draw(win)
  159.     box2 = Rectangle(p3,p4)
  160.     box2.setOutline("black")
  161.     box2.setFill("grey")
  162.     box2.draw(win)
  163.  
  164.  
  165.     message.setText("Click 3 times to make traffic lights")
  166.  
  167.     a4 = win.getMouse()
  168.     circ4 = Circle(a4,.2)
  169.     circ4.setOutline("black")
  170.     circ4.setFill("red")
  171.     circ4.draw(win)
  172.  
  173.     a5 = win.getMouse()
  174.     circ5 = Circle(a5,.2)
  175.     circ5.setOutline("black")
  176.     circ5.setFill("yellow")
  177.     circ5.draw(win)
  178.  
  179.     a6 = win.getMouse()
  180.     circ6 = Circle(a6,.2)
  181.     circ6.setOutline("black")
  182.     circ6.setFill("green")
  183.     circ6.draw(win)
  184.  
  185.  
  186.     # Both traffic lights are made
  187.     message.setText("Both traffic lights are made, please set conditions and click 'Start'.")
  188.     circ1.setFill("black")
  189.     circ2.setFill("black")
  190.     circ3.setFill("black")
  191.     circ4.setFill("black")
  192.     circ5.setFill("black")
  193.     circ6.setFill("black")
  194.    
  195.  
  196.  
  197.     # Loop
  198.     pt = win.getMouse()
  199.     quitButton.activate()
  200.     while not quitButton.clicked(pt):
  201.         pt = win.checkMouse()
  202.         if startButton.clicked(pt):
  203.             message.setText("Traffic program has started.")
  204.  
  205.  
  206.         while eval(ped.getText()) == 1.0:  # Check pedestrians
  207.             circ1.setFill("red")
  208.             circ4.setFill("red")
  209.             ped.setText("0.0")
  210.             time.sleep(eval(red.getText()))
  211.             break
  212.  
  213.  
  214.  
  215.     # Red and green light combo, check which is greater time interval
  216.  
  217.         if eval(red.getText()) <= eval(green.getText()):
  218.             circ3.setFill("black")
  219.             circ2.setFill("black")
  220.             circ4.setFill("black")
  221.             circ1.setFill("red")
  222.             circ6.setFill("green")
  223.             time.sleep(eval(green.getText()))
  224.         else:
  225.             circ3.setFill("black")
  226.             circ2.setFill("black")
  227.             circ4.setFill("black")
  228.             circ1.setFill("red")      
  229.             circ6.setFill("green")
  230.             time.sleep(eval(red.getText()))
  231.        
  232.     # Red and yellow light combo, check which is greater time interval
  233.  
  234.         if eval(red.getText()) <= eval(yellow.getText()):
  235.             circ6.setFill("black")
  236.             circ5.setFill("yellow")
  237.             time.sleep(eval(yellow.getText()))
  238.         else:
  239.             circ6.setFill("black")
  240.             circ5.setFill("yellow")
  241.             time.sleep(eval(red.getText()))
  242.        
  243.  
  244.     # Red and green light combo, check which is greater time interval
  245.    
  246.         if eval(red.getText()) <= eval(green.getText()):
  247.             circ5.setFill("black")
  248.             circ1.setFill("black")
  249.             circ4.setFill("red")
  250.             circ3.setFill("green")
  251.             time.sleep(eval(green.getText()))
  252.         else:
  253.             circ5.setFill("black")
  254.             circ1.setFill("black")
  255.             circ4.setFill("red")
  256.             circ3.setFill("green")
  257.             time.sleep(eval(red.getText()))
  258.  
  259.            
  260.    
  261.     # Red and yellow light combo, check which is greater time interval
  262.    
  263.         if eval(red.getText()) <= eval(yellow.getText()):
  264.             circ3.setFill("black")
  265.             circ2.setFill("yellow")
  266.             time.sleep(eval(yellow.getText()))
  267.  
  268.         else:
  269.             circ3.setFill("black")
  270.             circ2.setFill("yellow")
  271.             time.sleep(eval(red.getText()))
  272.  
  273.  
  274. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement