14. Essentials

  • (check-with good-model?)

  • (stop-when game-over? win-draw-handler)

check-with

The check-with clause verifies that each handler returns a valid model. You need to provide the function good-model? : Any -> Boolean. Write it yourself or use a built-in function like number?.

Example:

(define (good-model? radius)
  (and (number? radius)
       (>= radius 0)))

stop-when

The stop-when clause stops the animation when your game-over? function returns true. The last image shown is given by calling win-draw-handler: model -> image.

One typical is is to have your win draw handler call your regular draw handler and overlay text (like “Game Over”) on the result.

In spite of the name I give it, win-draw-handler could draw a “lose” screen as well.

Example:

(define (game-over? radius)
  (>= radius 200))
(define (win-draw-handler radius)
  (overlay (text "The End" 36 "black") 
           (draw-handler radius)))

stop-with

The book mentions stop-with. We won’t use that. Instead, change your model to something “impossible” to indicate that you should stop. (For example, use a negative radius… but don’t forget to change your good-model? function.)

Teacher reasoning:

  1. WeScheme does not support stop-with.
  2. I have had problems in Typed Racket using stop-with.
Last modified August 18, 2023: 2022-2023 End State (7352e87)