;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname 15.6.1) (read-case-sensitive #t) (teachpacks ((lib "testing.ss" "teachpack" "htdp") (lib "371-tiles.ss" "installed-teachpacks") (lib "371-sb-world.ss" "installed-teachpacks"))) (htdp-settings #8(#t constructor repeating-decimal #f #t none #f ((lib "testing.ss" "teachpack" "htdp") (lib "371-tiles.ss" "installed-teachpacks") (lib "371-sb-world.ss" "installed-teachpacks"))))) ; Worked exercise 15.6.1 ; An animation of a picture that moves up, down, left, and right ; in response to the corresponding arrow keys ; Model: a posn representing the current location of the picture ; Handlers needed: redraw and key ; Useful constants: (define WIDTH 300) (define HEIGHT 300) (define BACKGROUND (empty-scene WIDTH HEIGHT)) (define DOT (circle 3 "solid" "blue")) ; Redraw handler: ; show-picture: posn -> scene (define (show-picture where) ; where a posn (make-posn 27 149) ; (posn-x where) a number(x) 27 ; (posn-y where) a number(y) 149 ; DOT an image ; BACKGROUND a scene ; right answer a scene (place-image DOT 27 149 BACKGROUND) (place-image DOT (posn-x where) (posn-y where) BACKGROUND) ) (check-expect (show-picture (make-posn 15 12)) (place-image DOT 15 12 BACKGROUND)) (check-expect (show-picture (make-posn 27 149)) (place-image DOT 27 149 BACKGROUND)) ; 15.5.4 add-posns : posn posn -> posn ; Copied this so I can use it in handle-key (define (add-posns here there) ; here posn ; there posn ; (posn-x here) num ; (posn-y here) num ; (posn-x there) num ; (posn-y there) num (make-posn (+ (posn-x here) (posn-x there)) (+ (posn-y here) (posn-y there))) ) (check-expect (add-posns (make-posn 0 0) (make-posn 3 -2)) (make-posn 3 -2)) (check-expect (add-posns (make-posn 1 -2) (make-posn 2 7)) (make-posn 3 5)) ; Key handler: ; handle-key: posn symbol-or-char -> posn\ (define (handle-key where key) ; where a posn (make-posn 12 19) ; key a symbol or char various ; (posn-x where) a number 12 ; (posn-y where) a number 19 (cond [(char? key) where] [(symbol? key) ; (cond [(symbol=? key 'right) ; ; right answer a posn (make-posn 13 19) ; (add-posn (make-posn 1 0) where)] ; [(symbol=? key 'left) ; ; right answer a posn (make-posn 11 19) ; (add-posn (make-posn -1 0) where)] ; [(symbol=? key 'up) ; ; right answer a posn (make-posn 12 18) ; (add-posn (make-posn 0 -1) where)] ; [(symbol=? key 'down) ; ; right answer a posn (make-posn 12 20) ; (add-posn (make-posn 0 1) where)] ; [else where] ; ) ; or alternatively, (add-posns where (cond [(symbol=? key 'right) (make-posn 1 0)] [(symbol=? key 'left) (make-posn -1 0)] [(symbol=? key 'up) (make-posn 0 -1)] [(symbol=? key 'down) (make-posn 0 1)] [else (make-posn 0 0)])) ] )) (check-expect (handle-key (make-posn 12 19) #\e) (make-posn 12 19)) (check-expect (handle-key (make-posn 12 19) 'left) (make-posn 11 19)) (check-expect (handle-key (make-posn 12 19) 'right) (make-posn 13 19)) (check-expect (handle-key (make-posn 12 19) 'up) (make-posn 12 18)) (check-expect (handle-key (make-posn 12 19) 'down) (make-posn 12 20)) (check-expect (handle-key (make-posn 12 19) 'home) (make-posn 12 19)) (generate-report) (run-animation WIDTH HEIGHT (make-posn (quotient WIDTH 2) (quotient HEIGHT 2)) 1 (on-redraw show-picture) (on-key handle-key))