;; 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 blob-world) (read-case-sensitive #t) (teachpacks ((lib "world.ss" "teachpack" "htdp") (lib "testing.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "world.ss" "teachpack" "htdp") (lib "testing.ss" "teachpack" "htdp"))))) ;; blob-world.ss ;; A Blob is (make-blob Posn Symbol) ;; Interpretation: a Blob is a disk at a given location and color (define-struct blob (loc color)) ;; examples of blobs (define blob1 (make-blob (make-posn 30 40) 'red)) (define blob2 (make-blob (make-posn 130 40) 'blue)) ;; draw an image of a blob on the given scene ;; draw-blob: Blob Scene -> Scene (define (draw-blob a-blob a-scene) (place-image (circle 10 'solid (blob-color a-blob)) (posn-x (blob-loc a-blob)) (posn-y (blob-loc a-blob)) a-scene)) ;; a visual test to draw the two blobs (draw-blob blob1 (draw-blob blob2 (empty-scene 200 100))) ;; move the given posn by the given (dx, dy) ;; move-posn: Posn Number Number -> Posn (define (move-posn p dx dy) (make-posn (+ (posn-x p) dx) (+ (posn-y p) dy))) ;; tests for the function move-posn (check-expect (move-posn (make-posn 100 100) 20 40) (make-posn 120 140)) ;; move the given blob by the given (dx, dy) ;; move-blob: Blob Number Number -> Blob (define (move-blob a-blob dx dy) (make-blob (move-posn (blob-loc a-blob) dx dy) (blob-color a-blob))) ;; tests for the function move-blob (check-expect (move-blob blob1 10 20) (make-blob (make-posn 40 60) 'red)) ;;---------------------- BREAK -------------------------------------- ;; The world consists of one blob ;; 'on-redraw' Draw the blob on a yellow rectangle 200 by 100 ;; 'on-tick' On tick of the clock the blob moves 3 pixels to the left ;; 'on-key-event' Keep the blob stays within bounds using the right arrow key ;; On each hit of the right arrow key the blob moves 3 pixels to the right ;; 'stop-world The world ends when the blob gets out of bounds. ;; We work on each part one at a time: ;;------------------------------------------------------------------------ ;; (on-redraw draw-world) ;; ;; draw the blob on a yellow background ;; draw-world: Blob -> Scene (define (draw-world a-blob) (draw-blob a-blob ;; draw the blob (place-image (rectangle 200 100 'solid 'yellow) ;; on a rectangle 100 50 ;; centered pinhole (empty-scene 200 100)))) ;; base is empty scene ;; a visual test for the draw=world function (draw-world blob2) ;;------------------------------------------------------------------------ ;; (on-tick-event tock) ;; ;; produce a blob moved 3 pixels to the left ;; tock: Blob -> Blob (define (tock a-blob) (move-blob a-blob -3 0)) ;; test for the function tock (check-expect (tock blob1) (make-blob (make-posn 27 40) 'red)) ;;------------------------------------------------------------------------ ;; Run the world with just the tick events ;; ;(big-bang 200 100 0.1 blob2) ; ;(on-redraw draw-world) ;(on-tick-event tock) ;;------------------------------------------------------------------------ ;; (on-key-event back ke) ;; ;; if right arrow is hit, move the blob right 3 pixels ;; back: Blob KeyEvent -> Blob (define (back a-blob ke) (cond [(char? ke) a-blob] [(symbol? ke) (cond [(symbol=? ke 'right) (move-blob a-blob 3 0)] [else a-blob])])) ;; tests for the function back (check-expect (back blob1 'right) (make-blob (make-posn 33 40) 'red)) (check-expect (back blob1 'left) blob1) (check-expect (back blob1 #\x) blob1) ;;------------------------------------------------------------------------ ;; Run the world with the tick events and key events ;; ;(big-bang 200 100 0.3 blob2) ; ;(on-redraw draw-world) ;(on-tick-event tock) ;(on-key-event back) ;;------------------------------------------------------------------------ ;; (stop-when out-of-bounds?) ;; ;; is the blob out of bound on the left? ;; out-of-bounds?: Blob -> Boolean (define (out-of-bounds? a-blob) (< (posn-x (blob-loc a-blob)) 0)) ;; tests for the function out-of-bounds? (check-expect (out-of-bounds? (make-blob (make-posn 33 40) 'red)) false) (check-expect (out-of-bounds? (make-blob (make-posn -2 40) 'blue)) true) ;;------------------------------------------------------------------------- ;; Run the world with the tick events and key events and the world end test ;; (big-bang 200 100 0.3 blob1) (on-redraw draw-world) (on-tick-event tock) (on-key-event back) (stop-when out-of-bounds?)