;; Example of how to program the world ;;------------------------------------ (define-struct my-world (loc radius)) ;; MyWorld = (make-my-world Posn Number) ;; Interpretation: ;; The world consists of a red circle ;; with the centre at loc and the specified radius (define world0 (make-my-world (make-posn 100 100) 20)) (define world1 (make-my-world (make-posn 10 90) 4)) ;; define how the worlds changes on each tick ;; World -> World (define (tick-fcn w) (cond [(<= (my-world-radius w) 5) (make-my-world (my-world-loc w) 5)] [else (make-my-world (my-world-loc w) (- (my-world-radius w) 1))])) ;;; tests for tick (check-expect (tick-fcn world0) (make-my-world (make-posn 100 100) 19)) (check-expect (tick-fcn world1) (make-my-world (make-posn 10 90) 5)) ;; helper function to produce posn at a position changed by dx dy ;; move-posn: Posn Number Number -> Posn (define (move-posn p dx dy) (make-posn (+ dx (posn-x p)) (+ dy (posn-y p)))) (check-expect (move-posn (make-posn 20 20) 3 5) (make-posn 23 25)) ;; define how the world responds to key events ;; World KeyEvent -> World (define (ke-fcn w ke) (cond [(char? ke) w] ;; ignore characters [(symbol=? ke 'left) (make-my-world (move-posn (my-world-loc w) -3 0) (my-world-radius w))] [(symbol=? ke 'right) (make-my-world (move-posn (my-world-loc w) 3 0) (my-world-radius w))] [else w])) (check-expect (ke-fcn world0 "left") world0) (check-expect (ke-fcn world0 'left) (make-my-world (make-posn 97 100) 20)) (check-expect (ke-fcn world0 'right) (make-my-world (make-posn 100 103) 20)) (check-expect (ke-fcn world0 'up) world0) ;; define how the world responds to muse clicks ;; World Number Number MouseEvent -> World (define (mouse-fcn w mpx mpy mpe) (cond [(symbol=? mpe 'button-down) w] [(symbol=? mpe 'button-up) (make-my-world (make-posn mpx mpy) (my-world-radius w))] [(symbol=? mpe 'drag) (make-my-world (make-posn mpx mpy) (my-world-radius w))] [(symbol=? mpe 'move) w] [(symbol=? mpe 'enter) w] [(symbol=? mpe 'leave) w])) (check-expect (mouse-fcn world0 50 40 'button-down) world0) (check-expect (mouse-fcn world0 50 40 'button-up) (make-my-world (make-posn 50 40) 20)) (check-expect (mouse-fcn world0 50 40 'drag) (make-my-world (make-posn 50 40) 20)) (check-expect (mouse-fcn world0 50 40 'move) world0) (check-expect (mouse-fcn world0 50 40 'enter) world0) (check-expect (mouse-fcn world0 50 40 'leave) world0) ;; A MouseEvent is one of: ;; - 'button-down ;; - 'button-up ;; - 'drag ;; - 'move ;; - 'enter ;; - 'leave ;; draw the current world onto the given scene ;; draw-fcn: World Image -> Image (define (draw-fcn w scene) (place-image (circle (my-world-radius w) 'solid 'red) (posn-x (my-world-loc w)) (posn-y (my-world-loc w)) scene)) ;; verify that you get the expected drawing (draw-fcn world0 (empty-scene 200 200)) (draw-fcn world1 (empty-scene 200 200)) ;;------------ END OF THE STUDENT CODE ------------------------- ;; --- run program run --- (define WIDTH 200) (define HEIGHT 200) ;; World -> Scene ;; place the image of the world in an empty scene (define (world-scene w) (draw-fcn w (empty-scene WIDTH HEIGHT))) ;; run the program with one tick per second (big-bang WIDTH HEIGHT 1 world0) (on-redraw world-scene) (on-tick-event tick-fcn) (on-key-event ke-fcn) (on-mouse-event mouse-fcn) ;; on-mouse-event : (World Number Number MouseEvent -> World) -> true ;; (on-mouse-event clack) means that DrScheme must call clack ;; on the current world, the current x and y coordinates of the mouse, ;; and and a (representation of the) mouse event for every action ;; of the mouse the programmer (user of the computer) makes; ;; it uses the result as the next world