(define-struct school (name tuition)) ;; School is (make-school String Natural) ;; interp. name is the school's name, tuition is international student's tuition in USD
;; School -> Image ;; produce the bar for a single school in the bar chart (check-expect (make-bar (make-school"S1"8000)) (overlay/align"center""bottom" (rotate90 (text"S1" FONT-SIZE FONT-COLOR)) (rectangle BAR-WIDTH (*8000 Y-SCALE) "outline""black") (rectangle BAR-WIDTH (*8000 Y-SCALE) "solid" BAR-COLOR)))
(define-struct bear (x y r)) ;; Bear is (make-bear Number[0,WIDTH] Number[0,HEIGHT] Integer) ;; interp. (make-bear x y r) is the state of a bear, where ;; x is the x coordinate in pixels, ;; y is the y coordinate in pixels, and ;; r is the angle of rotation in degrees
与此同时写上它的例子:
1 2
(define B1 (make-bear000)) ; bear in the upper left corner (define B2 (make-bear (/ WIDTH 2) (/ HEIGHT 2) 90)) ; sideways bear in the middle
作为一个结构,我们需要在函数模板中提到它所有的字段:
1 2 3 4 5 6 7 8
#; (define (fn-for-bear b) (... (bear-x b) ; Number[0,WIDTH] (bear-y b) ; Number[0,HEIGHT] (bear-r b))) ; Integer
;; Took Template from Bear w/ added atomic parameter (define (render-bear-on b img) (place-image (rotate (modulo (bear-r b) 360) BEAR-IMG) (bear-x b) (bear-y b) img))
最后就是处理鼠标点击的函数handle-mouse,它的签名是ListOfBear Integer Integer MouseEvent -> ListOfBear,目的是On mouse-click, adds a bear with 0 rotation to the list at the x, y location。
查阅文档后知道我们只需要响应"button-down"事件即可(借助(mouse=? me "button-down"))。鼠标点击时,x和y是鼠标的坐标,而我们需要在这个位置添加一只熊,所以可以直接使用make-bear来创建一只熊,然后将其添加到列表中。
1 2 3 4 5 6 7 8 9 10 11 12
;; ListOfBear Integer Integer MouseEvent -> ListOfBear ;; On mouse-click, adds a bear with 0 rotation to the list at the x, y location (check-expect (handle-mouse empty 54"button-down") (cons (make-bear540) empty)) (check-expect (handle-mouse empty 54"move") empty)
;(define (handle-mouse lob x y mev) empty)
;; Templated according to MouseEvent large enumeration. (define (handle-mouse lob x y mev) (cond [(mouse=? mev "button-down") (cons (make-bear x y 0) lob)] [else lob]))
(define WIDTH 600) ; width of the scene (define HEIGHT 700) ; height of the scene
(define SPEED 3) ; speed of rotation
(define MTS (empty-scene WIDTH HEIGHT)) ; the empty scene
(define BEAR-IMG .)
;; ================= ;; Data definitions:
(define-struct bear (x y r)) ;; Bear is (make-bear Number[0,WIDTH] Number[0,HEIGHT] Integer) ;; interp. (make-bear x y r) is the state of a bear, where ;; x is the x coordinate in pixels, ;; y is the y coordinate in pixels, and ;; r is the angle of rotation in degrees
(define B1 (make-bear000)) ; bear in the upper left corner (define B2 (make-bear (/ WIDTH 2) (/ HEIGHT 2) 90)) ; sideways bear in the middle
#; (define (fn-for-bear b) (... (bear-x b) ; Number[0,WIDTH] (bear-y b) ; Number[0,HEIGHT] (bear-r b))) ; Integer
;; Took Template from Bear w/ added atomic parameter (define (render-bear-on b img) (place-image (rotate (modulo (bear-r b) 360) BEAR-IMG) (bear-x b) (bear-y b) img))
;; ListOfBear Integer Integer MouseEvent -> ListOfBear ;; On mouse-click, adds a bear with 0 rotation to the list at the x, y location (check-expect (handle-mouse empty 54"button-down") (cons (make-bear540) empty)) (check-expect (handle-mouse empty 54"move") empty)
;(define (handle-mouse lob x y mev) empty)
;; Templated according to MouseEvent large enumeration. (define (handle-mouse lob x y mev) (cond [(mouse=? mev "button-down") (cons (make-bear x y 0) lob)] [else lob]))