;; The first three lines of this file were inserted by DrRacket. 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 entry2) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp"))))) ;; 1301 lab 3 ;; open the canvas (start 800 500) ;; Contract: posn -> boolean ;; Purpose: To create a solid blue rectangle for the background, with a position, width, height, and color. (draw-solid-rect (make-posn 2 2) 800 500 'blue) ;; Purpose: To create a structure that represents a house. Corner is a position (posn), x is the width, y is the height, and color is a symbol that represents the color. ;; The first four fields of the structure are for the frame of the house (H). The next four fields (R) are for the right window. (L) is for the left window, and (D) is for the door. (define-struct house (cornerH xH yH colorH cornerR xR yR colorR cornerL xL yL colorL cornerD xD yD colorD)) ;; draw-house: house -> boolean ;; Purpose: to draw solid rectangles which represent a house. (define (draw-house a-house) (and (draw-solid-rect (house-cornerH a-house) (house-xH a-house) (house-yH a-house) (house-colorH a-house)) (draw-solid-rect (house-cornerR a-house) (house-xR a-house) (house-yR a-house) (house-colorR a-house)) (draw-solid-rect (house-cornerL a-house) (house-xL a-house) (house-yL a-house) (house-colorL a-house)) (draw-solid-rect (house-cornerD a-house) (house-xD a-house) (house-yD a-house) (house-colorD a-house)) )) (define house1 (make-house (make-posn 250 250) 300 250 'gray (make-posn 460 275) 60 70 'white (make-posn 280 275) 60 70 'white (make-posn 350 350) 100 200 'brown)) (draw-house house1) ;; The structure represents a sun, which is a solid disk. The center is a position (posn), radius is the radius value of the disk, and color is a symbol that represents the color. (define-struct sun (center radius color)) ;; draw-sun: sun -> boolean ;; Purpose: to draw a solid disk, which represents a sun. (define (draw-sun a-sun) (draw-solid-disk (sun-center a-sun) (sun-radius a-sun) (sun-color a-sun))) ;; clear-sun: sun -> boolean ;; Purpose: to clear a drawing of a sun structure (define (clear-sun a-sun) (clear-solid-disk (sun-center a-sun) (sun-radius a-sun))) ;; draw-and-clear-sun: sun -> boolean ;; Purpose: to draw a sun structure, wait for 1 sec, clear it, then make another sun at a new position. (define (draw-and-clear-sun a-sun) (and (draw-sun a-sun) (sleep-for-a-while 1) (clear-sun a-sun) )) ;; While "and" could have been used in our sun and moon structures to eliminate the need for drawing two disks each time, we tested the use of "and" and found some problems. ;; On one computer, the use of "and" worked fine. On a different computer, the use of "and" caused our program to draw one whole disk first and then noticeably flicker as the second was created. We found this effect distracting and decided to do it differently just in case the problem were to show up on different computers. (define sun1 (make-sun (make-posn 400 100) 50 'yellow)) (draw-and-clear-sun sun1) ;; We use the sun structure to draw a blue solid disk in the same position as the previous yellow disk. This restores the background, our "sky". (define sun2 (make-sun (make-posn 400 100) 50 'blue)) (draw-sun sun2) (define sun3 (make-sun (make-posn 650 200) 50 'yellow)) (draw-and-clear-sun sun3) (define sun4 (make-sun (make-posn 650 200) 50 'blue)) (draw-sun sun4) ;; Sunset (define sun5 (make-sun (make-posn 800 500) 150 'red)) (draw-sun sun5) (define sun6 (make-sun (make-posn 800 500) 110 'orange)) (draw-sun sun6) (define sun7 (make-sun (make-posn 800 500) 70 'yellow)) (draw-sun sun7) (sleep-for-a-while 1) ;; The background color changes because in the cartoon it will be night. (draw-solid-rect (make-posn 2 2) 800 500 'black) (draw-house house1) (sleep-for-a-while 1) ;; Contract: posn -> boolean ;; Purpose: To change the color of the right window and the left window from white to yellow. (draw-solid-rect (make-posn 460 275) 60 70 'yellow) (draw-solid-rect (make-posn 280 275) 60 70 'yellow) (sleep-for-a-while 1) ;; This structure represents a moon, which is made from a solid white disk. The center is a position (posn), radius is the radius value of the disk, and color is a symbol that represents the color. (define-struct moon (center radius color)) ;; draw-moon: moon -> boolean ;; Purpose: to draw a disk, which represents a moon. (define (draw-moon a-moon) (draw-solid-disk (moon-center a-moon) (moon-radius a-moon) (moon-color a-moon))) (define moon1 (make-moon (make-posn 50 450) 50 'white)) (draw-moon moon1) (define moon2 (make-moon (make-posn 75 425) 50 'black)) (draw-moon moon2) (sleep-for-a-while 1) (define moon3 (make-moon (make-posn 50 450) 50 'black)) (draw-moon moon3) (define moon4 (make-moon (make-posn 180 200) 50 'white)) (draw-moon moon4) (define moon5 (make-moon (make-posn 205 175) 50 'black)) (draw-moon moon5) (sleep-for-a-while 1) (define moon6 (make-moon (make-posn 180 200) 50 'black)) (draw-moon moon6) (define moon7 (make-moon (make-posn 400 100) 50 'white)) (draw-moon moon7) (define moon8 (make-moon (make-posn 425 75) 50 'black)) (draw-moon moon8) (sleep-for-a-while 1) ;; Contract: posn -> boolean ;; Purpose: to draw a small number of stars made from solid white disks (draw-solid-disk (make-posn 250 150) 3 'white) (draw-solid-disk (make-posn 550 130) 3 'white) (draw-solid-disk (make-posn 430 190) 3 'white) (draw-solid-disk (make-posn 470 100) 2 'white) (draw-solid-disk (make-posn 280 90) 2 'white) (sleep-for-a-while 1) ;; Contract: posn -> boolean ;; Purpose: to draw two black rectangles and simulate lights being turned off. (draw-solid-rect (make-posn 460 275) 60 70 'black) (draw-solid-rect (make-posn 280 275) 60 70 'black)