9.5  Example: Launching a Rocket

Say we want to launch the rocket with a keystroke on 'up

;; The world is one of: 
;; --- false 
;; --- N
;; Interpretation: false says no rocket has been launched; a
;; natural number says the rocket has been flying for that many ticks

(define G 3)
(define WIDTH 100)
(define HEIGHT 300)

;; World  →  true
(define (world-draw w) (rocket-draw w))

;; World  →  true
(define (world-clear w)
  (draw-solid-rect (make-posn 0 0) WIDTH HEIGHT 'white))

;; World  →  World
;; erase old world, draw new one, return new one 
(define (tock w)
  (cond
    [(boolean? w) w]
    [else (local ((define new-w (+ w 1)))
            (draw (world-clear w) (world-draw w) produce new-w))]))

;; KeyEvent World  →  World
;; change the world to a rocket 
(define (react ke w)
  (cond
    [(char? ke) w]
    ;; now we know it’s a symbol 
    [(symbol=? 'up ke) 0]
    [else w]))

;; --- run program run
(start WIDTH HEIGHT) ;; show the canvas
(big-bang .2 false) ;; create the world, start the clock
(on-tick-event tock)
(on-key-event react)