;; CS101 Spring 2013 ;; Lab 9 ;; (require 2htdp/image) (require 2htdp/universe) (display "\n CS101 Lab 9, Spring 2013") (display "\n Please write your name here\n\n") ;; ;; Description: bouncing ball ;; BEFORE STARTING TO WRITE ANY NEW CODE, RUN THIS FILE. ;; ;; WHEN YOU RUN THIS CODE INITIALLY, YOU WILL SEE ;; A RED BALL MOVING ACROSS A YELLOW WINDOW. THE BALL ;; SHOULD BOUNCE OFF THE TOP AND BOTTOM EDGES OF THE ;; WINDOW AND THEN CONTINUE OFF THE SCENE TO THE ;; RIGHT. ;; READ THROUGH ALL THE CODE BEFORE STARTING TO WRITE THE ;; SOLUTIONS TO THIS LAB. THERE ARE 4 PROBLEMS TO FINISH. ;; IF YOU CHOOSE "SHOW PROGRAM CONTOUR" UNDER THE VIEW ;; MENU, YOU WILL SEE THE PROBLEMS LISTED IN LARGE LETTERS. ; ; ; ; ;; ;; ; ; ; ; ;;; ;;; ; ;; ;;; ;;;; ;;; ; ;; ;;;; ;;; ; ; ; ; ; ;;; ; ; ; ;; ; ;;; ; ; ; ; ; ; ;; ; ;; ; ; ; ; ; ;; ; ; ; ; ; ; ; ;; ; ;; ; ; ; ;; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ;; ; ; ; ; ;;; ; ; ; ; ; ;; ; ; ;;; ;;; ; ;; ;;;; ;; ;; ;; ; ;; ;; ;;;; ; ;;;;;;;;;;;;;;;;;;;Numerical Constants;;;;;;;;;;;;;;;;;;;;;;;;;; (define RADIUS 25) ; number: ball radius (define WIDTH 500) ; number: scene width (define HEIGHT 300) ; number: scene height (define DX 4) ; x increment (if positive, ball moving to right) (define DY -4) ; y increment (if negative, ball moving up) ;;;;;;;;;;;;;;;;;;;Graphical Constants;;;;;;;;;;;;;;;;;;;;;;;;;; (define BALL (circle RADIUS "solid" "red")) (define MT (place-image (rectangle WIDTH HEIGHT "solid" "yellow") (/ WIDTH 2) (/ HEIGHT 2) (empty-scene WIDTH HEIGHT))) ;; A structure named bw (short for bounce-world) is ;; defined below. ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ;;; ;; ; ;;; ; ; ; ;; ; ; ;;; ; ;; ; ; ; ; ;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ;;;; ;;; ; ; ; ; ;;; ;; ;; ;; ;; ; ;; Data Definition (define-struct bw (x y dx dy)) ;; x and y are the coordinates of BALL's center, and ;; dx and dy are numbers that, respectively, represent ;; the change in the horizontal and vertical positions ;; of BALL. The sign (positive or negative) of the dx ;; and dy fields indicate direction. ;; Contracts for the constructor and accessor functions ;; created by define-struct of bw should be written here: ; ; ; ; ; ; ; ; ; ;; ; ;;; ; ; ;;; ;; ;;; ; ;;; ; ; ;; ; ;;; ; ; ; ; ; ; ; ;; ; ; ; ;; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;; ;; ;;; ; ;; ;;;; ; ; ; ; ; ; ; ; ; ; ; ; Problem 1: ; ; Write the contracts for the constructor and accessor functions ; created by (define-struct bw (x y dx dy)) You should write ; 5 contracts under the define-struct statement in the area ; indicated above. ; ;; Example bw: The initial state of the world. If you look at ;; the values of DX and DY in the constant definition section, ;; you should note that the bw-dx of this structure is increasing ;; and the bw-dy is decreasing, so BALL starts moving at about ;; 1/4 of the scene down from the top and against the left edge, ;; moving to the right and up. (define INIT-BW (make-bw RADIUS (/ HEIGHT 4) DX DY)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; The main function calls the big-bang function, sending in ;; the bw to start the animation. ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;; ; ;;;; ; ;;; ;;; ; ;; ;;;; ; ; ; ; ; ; ; ; ; ; ;; ; ;;; ; ; ; ; ;; ; ; ; ; ;; ; ; ; ;; ; ; ; ; ; ; ; ; ; ;;; ; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;; ;; ; ; ; ;;; ; ; ; ; ; ;; ; ;; ;; ;;; ; ; ;; ;; ;; ; ;; ;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ;; ;; Contract: (main bw)-> scene ;; Header: (define main (lambda (w) ...)) ;; Purpose: start the big-bang function ;; Function definition: (THIS FUNCTION IS CALLED BELOW) (define main (lambda (w) (big-bang w (on-tick move-bw) (on-draw draw-ball)))) ;; Clauses used in this big-bang simulation are on-tick and on-draw. The ;; arguments to these clauses are move-bw and draw-ball, defined below: ;;;;;;;;;;;;;;;;;;;;FUNCTIONS CALLED IN BIG-BANG CLAUSES;;;;;;;;;;;;;;;;;;;;;;; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ;;; ; ;; ;;;; ; ;;; ; ;; ; ; ; ;;; ; ; ; ; ; ; ;; ; ;; ; ;; ; ; ; ; ; ;; ; ; ; ;; ; ;;; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;; ;;; ; ; ; ;;; ; ;; ;; ;; ;;; ; ;; ; ; ;; Contract: (move-bw bw) -> bw ;; Header: (define move-bw (lambda (w) ... )) ;; Purpose: changes position and possibly direction of ball ;; in scene by creating new bw world. ;; Function definition: (YOU FINISH THIS FUNCTION FOR PROBLEM 2) (define move-bw (lambda (w) (cond ;; if the bw is at the top edge and moving up or ;; at the bottom edge and moving down, reverse the ;; vertical movement. [(or (and (<= (bw-y w) RADIUS) (< (bw-dy w) 0)) (and (>= (bw-y w) (- HEIGHT RADIUS)) (> (bw-dy w) 0))) (make-bw (bw-x w) (bw-y w) (bw-dx w) (* -1 (bw-dy w)))] ;; WRITE THE SOLUTION TO PROBLEM 2 HERE. ;; if the ball is not at an edge, just move b-w in ;; the current dx and dy directions by adding the dx ;; amount onto the x field and adding the dy amount ;; onto the y field (leave the dx and dy fields un- ;; changed. [else (make-bw (+ (bw-x w) (bw-dx w)) (+ (bw-y w) (bw-dy w)) (bw-dx w) (bw-dy w))]))) ; ; ; ; ;;; ; ; ;; ;; ; ;; ; ; ; ; ; ;;; ;; ;;; ; ;;; ; ; ; ;; ; ;;; ; ; ; ; ; ; ; ; ;; ; ; ; ;; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ;; ; ; ;; ;; ;;; ; ;; ;;;;;;; ; ; ; ; ; ; ; ; Problem 2: ; ; Write the missing clause in the move-bw function above by ; making the ball bounce off the right and left edges of the ; scene. ; ; BE SURE TO WRITE A COMMENT TO DESCRIBE THE ADDITIONAL CODE! ; ; After you have completed this problem successfully, run the ; simulation. The ball should now bounce off every edge of the ; scene. Show this part to a coach or your prof before going ; on. ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ;;; ; ;; ;;; ;; ;;; ; ; ; ; ; ; ;;; ; ;; ; ;;; ;; ; ; ; ;; ; ;; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ; ; ;;; ; ; ; ; ;;; ; ; ; ;; ; ;;; ; ;; ;; ;; ;; ;; ;; ; ; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Contract: (draw-ball bw) -> scene (image) ;; Header: (define draw-ball (lambda (w) ... )) ;; Purpose produces image of ball at given current position ;; on MT (CALLED BY ON-DRAW) ;; CHANGE THIS FUNCTION AS PART OF THE SOLUTION FOR PROBLEM 3, ;; GIVEN BELOW. ;; Function definition: (define draw-ball (lambda (w) (place-image BALL (bw-x w) (bw-y w) MT))) ; ; ; ; ;; ; ; ; ; ; ;;; ;;;; ;;; ; ;;; ; ; ; ; ;; ;;; ; ;; ; ; ; ; ; ;; ; ; ; ;;; ;; ; ; ; ;;; ; ; ; ; ;; ; ;; ; ; ; ;;; ; ; ;; ;; ; ; ;; ; ; ; ; ; ;;; ; ; ; ;; ; ; ;; ;; ; ; ; ; ; ; ; ; ; ;; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ;; ; ; ; ; ; ;;; ;; ;;; ; ;; ; ; ; ;; ;;; ; ;; ; ; ; ; ; ; ; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Contract: (end-bounces? bw) -> boolean ;; Header: (define end-bounces? (lambda (w) ... )) ;; Purpose Stops the simulation when the ball has bounced ;; a certain number of times. ;; WRITE THIS FUNCTION AS PART OF THE SOLUTION TO PROBLEM 3, ;; GIVEN BELOW. ; ; ; ; ; ; ; ; ;;;;;; ; ;; ; ; ; ; ; ;;; ;; ;;; ; ;;; ; ; ;; ; ;;; ; ; ; ; ; ;; ; ;; ; ; ; ;; ; ;; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ;; ;; ;;; ; ;; ;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; Problem 3: This problem involves changes to move-ball and draw-ball, ; as well as the addition of an end-bounces? function. ; ; Part (a) ; ; Create a variable in the global environment called count-bounces ; to count the number of bounces the ball makes. Define count- ; bounces at the top of this program where the constants are defined ; and set its initial value to 0. ; ; Add calls to set! in the move-ball function to add 1 to count- ; bounces every time the ball hits a wall. Remember to use begin ; before calling set! because set! returns void. ; ; Part (b) ; ; After you have successfully written the code in move-ball ; to count the bounces, write a stop-when clause in the invocation ; to big-bang that will consume a function called end-bounces? to ; stop the simulation when count-bounces equals 10 (define 10 as a ; constant called LIMIT at the top of this program, where the other ; constants are defined). Write the function end-bounces? above. ; ; Part (c) ; ; In the draw-ball function, overlay a string version of count- ; bounces on the ball image so the count is displayed inside the ; ball while it is bouncing. Make the text font size 24 and the ; color black. ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Contract: (final-scene bw) -> scene (image) ;; Header: (define final-scene (lambda (w) ... )) ;; Purpose Displays a message after the simulation ends ;; WRITE THIS FUNCTION AS PART OF THE SOLUTION TO PROBLEM 4. ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ;;; ;; ;;; ; ;;; ; ; ; ;; ; ;;; ; ; ; ; ; ; ; ; ;; ; ; ; ;; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ;;;;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ;; ;;; ; ;; ; ; ; ; ; ; ; ; ; ; ; Problem 4: ; ; Write a function called final-scene above to display ; the final scene when the end-bounces? function returns ; true. This final scene should overlay the text message ; shown below on a final call to draw-ball: ; ; Ball done bouncing!! ; ; This function will be the second argument to the ; stop-when clause in big-bang. ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;; ;;; ; ; ; ;; ;; ;;; ; ; ;; ; ; ; ;; ; ; ; ;;; ; ; ; ;; ; ; ;;; ; ; ; ; ; ; ; ; ;; ;; ; ; ; ; ;; ; ; ; ; ; ; ; ;; ;; ; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ;;; ; ;;;;;; ; ; ; ;; ;;; ; ;;;; ; ; ; ;;; ;; ;; ;; ;; ; ; ;; ;; ;; ;; ; ;; ; ;; Initial call to main to start simulation (main INIT-BW) ; ; Make sure you get a coach or your professor to check you off for ; doing this lab and submit the lab9 directory when you are done. ;