Advertisement
Guest User

Untitled

a guest
Jan 8th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. ;Exercise 51.
  2.  
  3. ; world constants
  4. (define scaleTL 100) ; Single control point, scale of traffic light, default 100
  5. (define wHeight (* scaleTL 2.8))
  6. (define wLength (* scaleTL 1))
  7. (define inactiveLightRadius (* scaleTL 0.45))
  8. (define activeLightRadius (* scaleTL 0.40))
  9.  
  10. (define backgroundColor "black")
  11. (define inactiveLightColor "dimGray")
  12. (define Background (place-image (rectangle wLength wHeight "solid" backgroundColor)
  13. (/ wLength 2)
  14. (/ wHeight 2)
  15. (empty-scene wLength wHeight)))
  16.  
  17.  
  18. ;---------------------
  19.  
  20. ; A TrafficLight (TL) is one of the following Strings:
  21. ; – "red"
  22. ; – "green"
  23. ; – "yellow"
  24. ; interpretation the three strings represent the three
  25. ; possible states that a traffic light may assume
  26.  
  27. ;---------------------
  28.  
  29. ; traffic-light-next
  30. ; TrafficLight -> TrafficLight
  31. ; yields the next state given current state s
  32. (check-expect (traffic-light-next "green") "yellow")
  33. (check-expect (traffic-light-next "yellow") "red")
  34. (check-expect (traffic-light-next "red") "green")
  35. (define (traffic-light-next TL)
  36. (cond
  37. [(string=? "red" TL) "green"]
  38. [(string=? "green" TL) "yellow"]
  39. [(string=? "yellow" TL) "red"]))
  40.  
  41. ; inactiveLight
  42. ; draws inactive light
  43. (define inactiveLight (circle inactiveLightRadius "solid" inactiveLightColor))
  44.  
  45. ; activeLight
  46. ; TL -> Image
  47. ; draws current active light
  48. (define (activeLight TL)
  49. (circle activeLightRadius "solid" TL))
  50.  
  51. ;light-positions - used by place-images function
  52. (define topPos (make-posn ( / wLength 2)( * 1/6 wHeight)))
  53. (define centerPos (make-posn ( / wLength 2)( * 3/6 wHeight)))
  54. (define bottomPos (make-posn ( / wLength 2)( * 5/6 wHeight)))
  55.  
  56. ; activeLightPosition
  57. ; TL -> Number
  58. (define (activeLightPosition TL)
  59. (cond ((string=? TL "green") topPos)
  60. ((string=? TL "yellow") centerPos)
  61. ((string=? TL "red") bottomPos)))
  62.  
  63. ; render
  64. ; TrafficLight -> Image
  65. ; draws TrafficLight depending on current light
  66. (define (render TL)
  67. (place-images (list (activeLight TL)
  68. inactiveLight
  69. inactiveLight
  70. inactiveLight)
  71. (list (activeLightPosition TL)
  72. topPos
  73. centerPos
  74. bottomPos)
  75. Background))
  76.  
  77. ;tock
  78. ; TL -> TL
  79. ; changes TL to next state
  80. (define (tock TL)
  81. (traffic-light-next TL))
  82.  
  83.  
  84. ; main big-bang function
  85. ; TL -> TL
  86. (define (main TL)
  87. (big-bang TL
  88. [to-draw render]
  89. [on-tick tock]
  90. ))
  91.  
  92. ;Launch
  93. (main "red")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement