;; 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 entry4) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp"))))) ; CSci 1301 Lab 4 ;;Here is our structure definition for a disk: (define-struct disk (x y radius color)) ;;X, Y and radius are numbers, and color is a symbol. (define head (make-disk 150 100 50 'yellow)) (define ball (make-disk 240 380 50 'orange)) (define mouth (make-disk 150 125 15 'black)) ;;We created the head, ball, and mouth for our cartoon by using the disk structure. Now we are going to create a function in order to draw the disks. (define (draw-a-disk a-disk) (draw-solid-disk (make-posn (disk-x a-disk) (disk-y a-disk)) (disk-radius a-disk) (disk-color a-disk))) (start 500 500) (draw-a-disk head) (draw-a-disk ball) ;(draw-a-disk mouth) ;;We are going to define a function to create rectangles for the rest of our cartoon. (define (rectangle x y w h color) (draw-solid-rect (make-posn x y) w h color)) (rectangle 125 150 50 150 'blue) (rectangle 75 175 150 30 'blue) (rectangle 135 300 30 100 'green) (rectangle 125 400 50 30 'red) ;;Because we want our cartoon to move, we need to define functions that will clear the existing shapes and draw new ones in new positions. Here are our functions for moving the shapes delta units to the right. Keep in mind, the translate disk function consumes structures while the translate-rectangle function does not. (define (translate-disk a-disk delta) (draw-solid-disk (make-posn (+ (disk-x a-disk) delta) (disk-y a-disk)) (disk-radius a-disk) (disk-color a-disk))) (define (translate-rectangle x y w h color delta) (rectangle (+ x delta) y w h color)) ;;Here are our functions to clear shapes. (define (clear-a-disk a-disk) (clear-solid-disk (make-posn (disk-x a-disk) (disk-y a-disk)) (disk-radius a-disk) (disk-color a-disk))) ;(draw-a-disk mouth) (define (clear-rectangle x y w h color) (clear-solid-rect (make-posn x y) w h color)) ;;Here are our functions for drawing a shape and clearing it after a certain number of seconds (.25 and 1). (define (draw-and-clear-disk a-disk) (and (draw-a-disk a-disk) (sleep-for-a-while .25) (clear-a-disk a-disk))) (define (draw-and-clear-rectangle x y w h color) (and (rectangle x y w h color) (sleep-for-a-while 1) (clear-rectangle x y w h color))) ;;Finally, here are our functions for moving the shapes. (define (move-rectangle delta x y w h color) (and (draw-and-clear-rectangle x y w h color) (translate-rectangle x y w h color delta))) (define (move-disk delta a-disk) (cond [(draw-and-clear-disk a-disk) (translate-disk a-disk delta)] [else a-disk])) ;;Now, we will try to move the shapes in our cartoon. (move-rectangle 10 125 400 50 30 'red) (move-rectangle 10 135 300 30 100 'green) (move-rectangle 10 135 400 50 30 'red) ;;Here, we are going to define each new position of the ball as it moves, each as a new structure since we were unable to determine how to keep one structure moving (using the expression on the bottom of p. 74). (define ball1 (make-disk 290 380 50 'orange)) (define ball2 (make-disk 340 380 50 'orange)) (define ball3 (make-disk 390 380 50 'orange)) (define ball4 (make-disk 440 380 50 'orange)) (define ball5 (make-disk 490 380 50 'orange)) ;;We added another shape to our cartoon here. (draw-a-disk mouth) ;;Now we are going to move the ball. (move-disk 50 ball) (move-disk 50 ball1) (move-disk 50 ball2) (move-disk 50 ball3) (move-disk 50 ball4) (move-disk 60 ball5)