.Dynamically changing the clock tick rate in big-b

2019-09-17 10:41发布

问题:

The on-tick clause includes an option to change the clock tick rate. I have included my code to dynamically change the value depending on the world state value. The code is not working and I can't understand why. Another issue - how are world programs debugged? The "step" option doesn't work.

; physical constants 

(define HEIGHT 300)
(define WIDTH  100)

(define YDELTA 3)

; graphical constants 
(define BACKG  (empty-scene WIDTH HEIGHT))
(define ROCKET (rectangle 5 30 "solid" "red"))
(define ROCKET-CENTER (/ (image-height ROCKET) 2))
(define ROCKET-XPOS 10)

; LRCD -> LRCD
(define (main1 s)
  (big-bang s
    [to-draw show]
    [on-key launch]
    [on-tick fly (clock-rate s)]))

; LRCD -> Image
; renders the state as a resting or flying rocket
(define (show x)
  (cond
    [(string? x) (rocket-ht HEIGHT)]
    [(<= -3 x -1)
     (place-image (text (number->string x) 20 "red")
                  ROCKET-XPOS (* 3/4 WIDTH)
                  (rocket-ht HEIGHT))]
    [(>= x 0)
     (rocket-ht x)]))

; LRCD -> image
; positions the rocket at correct height
(define (rocket-ht ht)
  (place-image ROCKET ROCKET-XPOS (- ht ROCKET-CENTER) BACKG))

; LRCD KeyEvent -> LRCD
; starts the count-down when space bar is pressed, 
; if the rocket is still resting 
(define (launch x ke)
  (cond
    [(string? x) (if (string=? ke " ") -3 x)]
    [else x]))

; LRCD -> LRCD
; raises the rocket by YDELTA,
;  if it is moving already 
(define (fly x)
  (cond
    [(string? x) x]
    [(<= -3 x -1) (if (= x -1) HEIGHT (add1 x))]
    [else (- x YDELTA)]))

(define (clock-rate s)
  (cond
    [(number? s) (if (< s 0) 1 1/28)]
    [else 1/28]))
标签: scheme racket