Session #21
Answers
A world program represents a ball moving horizontally across the screen. The only changing information is the ball's x-coordinate. Define a data definition named BallWorld for the world state, include an interpretation, and write the basic template for a function that consumes a BallWorld.
; BallWorld is Number
; interp. The horizontal X position of a ball
(define (fn-for-bw) ... bw)
```racket ; BallWorld is Number ; interp. The horizontal X position of a ball (define (fn-for-bw) ... bw) ```
; BallWorld is Number
; interp. the x-coordinate of the ball
#;
(define (fn-for-ball-world bw)
(... bw))
; BallWorld is Number ; interp. the x-coordinate of the ball #; (define (fn-for-ball-world bw) (... bw))
Write a big-bang expression for a world program that starts at 0 and uses tick as its on-tick handler, draw as its to-draw handler, and handle-key as its on-key handler.
(define (main ws)
(big-bang ws
(on-tick tick)
(to-draw draw)
(on-key handle-key)))
```racket
(define (main ws)
(big-bang ws
(on-tick tick)
(to-draw draw)
(on-key handle-key)))
```
(big-bang 0
(on-tick tick)
(to-draw draw)
(on-key handle-key))
(big-bang 0 (on-tick tick) (to-draw draw) (on-key handle-key))
Write the signatures and stubs for three handlers: tick updates a WorldState every clock tick, draw renders a WorldState as an Image, and handle-key consumes a WorldState and KeyEvent and produces a WorldState.
(define (tick ws)
(+ ws 1))
(define (draw ws)
(text ws))
(define (handle-key ws ke)
(cond [(string=? "space") (+ ws 10)]
[else ws]))
```racket
(define (tick ws)
(+ ws 1))
(define (draw ws)
(text ws))
(define (handle-key ws ke)
(cond [(string=? "space") (+ ws 10)]
[else ws]))
```
; WorldState -> WorldState
(define (tick ws) ws)
; WorldState -> Image
(define (draw ws) empty-image)
; WorldState KeyEvent -> WorldState
(define (handle-key ws ke) ws)
; WorldState -> WorldState (define (tick ws) ws) ; WorldState -> Image (define (draw ws) empty-image) ; WorldState KeyEvent -> WorldState (define (handle-key ws ke) ws)
A key handler should reset the world to 0 when the space key is pressed and otherwise leave the world unchanged. Write the handler and two check-expect tests: one for the space key and one representative unrelated key.
(define (on-key WS KE)
(cond [(string=? KE "space") 0]
[else WS]))
(check-expect (on-key "K" 10) 10)
(check-expect (on-key "space" 42) 0)
```racket
(define (on-key WS KE)
(cond [(string=? KE "space") 0]
[else WS]))
(check-expect (on-key "K" 10) 10)
(check-expect (on-key "space" 42) 0)
```
(define (handle-key ws ke)
(cond [(key=? ke " ") 0]
[else ws]))
(check-expect (handle-key 25 " ") 0)
(check-expect (handle-key 25 "a") 25)
(define (handle-key ws ke)
(cond [(key=? ke " ") 0]
[else ws]))
(check-expect (handle-key 25 " ") 0)
(check-expect (handle-key 25 "a") 25)
Explain the flow of information in a simple big-bang program after an on-tick event occurs. Describe what the tick handler receives and produces, what happens to that result, and how to-draw is involved.
The on-tick function recieves the WorldState then should return a WorlState. The world program will then pass this to a to-draw function which should return an image which represents the world state
The `on-tick` function recieves the WorldState then should return a WorlState. The world program will then pass this to a to-draw function which should return an image which represents the world state
The on-tick handler receives the current world state and produces a new world state. big-bang stores that returned value as the new current world state. The to-draw handler can then consume that latest world state and produce the Image displayed on the screen.
The on-tick handler receives the current world state and produces a new world state. big-bang stores that returned value as the new current world state. The to-draw handler can then consume that latest world state and produce the Image displayed on the screen.