(define WIDTH 150) (define HEIGHT 180) (define RADIUS 10) (define DX 5) (define DY 5) ; A colored-posn comprises two numbers (x and y) and a string (color). (define-struct colored-posn [x y color]) ; Functions that come "for free" with this: ; make-colored-posn : num num string -> colored-posn ; colored-posn-x : colored-posn -> num ; colored-posn-y : colored-posn -> num ; colored-posn-color : colored-posn -> string ; colored-posn? : object -> boolean ; Useful auxiliary functions ; draw-ball-at-colored-posn : colored-posn -> image (define (draw-ball-at-colored-posn where) (overlay/xy (empty-scene WIDTH HEIGHT) (colored-posn-x where) (colored-posn-y where) (circle RADIUS "solid" (colored-posn-color where)))) "Examples of draw-ball-at-colored-posn:" (draw-ball-at-colored-posn (make-colored-posn 50 10 "blue")) "should be an empty scene with a blue ball at (50,10)" ; move-colored-posn : colored-posn num num -> posn (define (move-colored-posn old dx dy) (make-colored-posn (+ (colored-posn-x old) dx) (+ (colored-posn-y old) dy) (colored-posn-color old))) "Example of move-posn:" (move-colored-posn (make-colored-posn 30 20 "blue") -2 9) "should be" (make-colored-posn 28 29 "blue") ; switch-color : colored-posn -> colored-posn ; switches between green & red; other colors are left unchanged. (define (switch-color old) (make-colored-posn (colored-posn-x old) (colored-posn-y old) (cond [(string=? (colored-posn-color old) "red") "green"] [(string=? (colored-posn-color old) "green") "red"] [else (colored-posn-color old)]))) "Examples of switch-color:" (switch-color (make-colored-posn 5 10 "green")) "should be" (make-colored-posn 5 10 "red") (switch-color (make-colored-posn 5 10 "red")) "should be" (make-colored-posn 5 10 "green") (switch-color (make-colored-posn 5 10 "purple")) "should be" (make-colored-posn 5 10 "purple") ; A world is a colored-posn. ; Its display is a ball of that color at those coordinates ; show-world : world -> image (define (show-world where) (draw-ball-at-colored-posn where)) "Example of show-world:" (show-world (make-colored-posn 30 50 "blue")) "should be" (overlay/xy (empty-scene WIDTH HEIGHT) 30 50 (circle RADIUS "solid" "blue")) ; move-right : colored-posn -> colored-posn (define (move-right p) (move-colored-posn p DX 0)) "examples of move-right:" (move-right (make-colored-posn 5 7 "orange")) "should be" (make-colored-posn 10 7 "orange") ; move-left : colored-posn -> colored-posn (define (move-left p) (move-colored-posn p (- DX) 0)) "example of move-left:" (move-left (make-colored-posn 29 4 "purple")) "should be" (make-colored-posn 24 47 "purple") ; move-up : colored-posn -> colored-posn (define (move-up p) (move-colored-posn p 0 (- DY))) "example of move-up:" (move-up (make-colored-posn 29 47 "green")) "should be" (make-colored-posn 29 42 "green") ; move-down : colored-posn -> colored-posn (define (move-down p) (move-colored-posn p 0 DY)) "example of move-down:" (move-down (make-colored-posn 29 47 "black")) "should be" (make-colored-posn 29 52 "black") ; The next world is moved up, down, left, right, or nowhere depending on what key the user hit. ; handle-key : world -> world (define (handle-key key where) (cond [(not (symbol? key)) where] ; handle letters, numbers, etc. [(symbol=? key 'right) (move-right where)] [(symbol=? key 'left) (move-left where)] [(symbol=? key 'up) (move-up where)] [(symbol=? key 'down) (move-down where)] [else where])) "Examples of handle-key:" (handle-key 'right (make-colored-posn 29 47 "pink")) "should be" (make-colored-posn 34 47 "pink") (handle-key 'left (make-colored-posn 29 47 "red")) "should be" (make-colored-posn 24 47 "red") (handle-key 'up (make-colored-posn 29 47 "yellow")) "should be" (make-colored-posn 29 42 "yellow") (handle-key 'down (make-colored-posn 29 47 "blue")) "should be" (make-colored-posn 29 52 "blue") (big-bang WIDTH HEIGHT 1 (make-colored-posn 50 50 "red")) (on-redraw-event show-world) (on-key-event handle-key) (on-tick-event switch-color) ; note that switch-color has the right contract (world -> world) ; to be a tick-handler, even though it doesn't happen to be named "handle-tick".