Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Exercise 51.
- ; world constants
- (define scaleTL 100) ; Single control point, scale of traffic light, default 100
- (define wHeight (* scaleTL 2.8))
- (define wLength (* scaleTL 1))
- (define inactiveLightRadius (* scaleTL 0.45))
- (define activeLightRadius (* scaleTL 0.40))
- (define backgroundColor "black")
- (define inactiveLightColor "dimGray")
- (define Background (place-image (rectangle wLength wHeight "solid" backgroundColor)
- (/ wLength 2)
- (/ wHeight 2)
- (empty-scene wLength wHeight)))
- ;---------------------
- ; A TrafficLight (TL) is one of the following Strings:
- ; – "red"
- ; – "green"
- ; – "yellow"
- ; interpretation the three strings represent the three
- ; possible states that a traffic light may assume
- ;---------------------
- ; traffic-light-next
- ; TrafficLight -> TrafficLight
- ; yields the next state given current state s
- (check-expect (traffic-light-next "green") "yellow")
- (check-expect (traffic-light-next "yellow") "red")
- (check-expect (traffic-light-next "red") "green")
- (define (traffic-light-next TL)
- (cond
- [(string=? "red" TL) "green"]
- [(string=? "green" TL) "yellow"]
- [(string=? "yellow" TL) "red"]))
- ; inactiveLight
- ; draws inactive light
- (define inactiveLight (circle inactiveLightRadius "solid" inactiveLightColor))
- ; activeLight
- ; TL -> Image
- ; draws current active light
- (define (activeLight TL)
- (circle activeLightRadius "solid" TL))
- ;light-positions - used by place-images function
- (define topPos (make-posn ( / wLength 2)( * 1/6 wHeight)))
- (define centerPos (make-posn ( / wLength 2)( * 3/6 wHeight)))
- (define bottomPos (make-posn ( / wLength 2)( * 5/6 wHeight)))
- ; activeLightPosition
- ; TL -> Number
- (define (activeLightPosition TL)
- (cond ((string=? TL "green") topPos)
- ((string=? TL "yellow") centerPos)
- ((string=? TL "red") bottomPos)))
- ; render
- ; TrafficLight -> Image
- ; draws TrafficLight depending on current light
- (define (render TL)
- (place-images (list (activeLight TL)
- inactiveLight
- inactiveLight
- inactiveLight)
- (list (activeLightPosition TL)
- topPos
- centerPos
- bottomPos)
- Background))
- ;tock
- ; TL -> TL
- ; changes TL to next state
- (define (tock TL)
- (traffic-light-next TL))
- ; main big-bang function
- ; TL -> TL
- (define (main TL)
- (big-bang TL
- [to-draw render]
- [on-tick tock]
- ))
- ;Launch
- (main "red")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement