Question: @problem 1 ) ( @htdw Ball ) ;; Constants: ( define WIDTH 6 0 5 ) ( define HEIGHT 5 3 5 ) ( define

@problem 1)
(@htdw Ball)
;; Constants:
(define WIDTH 605)
(define HEIGHT 535)
(define BALL-RADIUS 10)
(define TOP (+0 BALL-RADIUS)) ;these constants define the "inner box"
(define BOT (- HEIGHT 1 BALL-RADIUS)) ;that constrains the center of the ball
(define LEF (+0 BALL-RADIUS)) ;
(define RIG (- WIDTH 1 BALL-RADIUS)) ;
(define BALL (circle BALL-RADIUS "solid" "white"))
(define MTS (rectangle WIDTH HEIGHT "solid" "green"))
;; ===========================================================================
;; ===========================================================================
;; Data definitions:
(@htdd Ball)
(define-struct ball (x y dx dy))
;; Ball is (make-ball Number Number Number Number)
;; interp. (make-ball x y dx dy) is ball
;; - position x, y in screen coordinates
;; - velocity dx, dy in pixels/tick
(define B1(make-ball (/ WIDTH 2)(/ HEIGHT 2)4-3))
(@dd-template-rules compound)
(define (fn-for-ball b)
(...(ball-x b)
(ball-y b)
(ball-dx b)
(ball-dy b)))
;; ===========================================================================
;; ===========================================================================
;; Functions:
(@htdf main)
(@signature Ball -> Ball)
;; start the game, call with (main B1)
;;
(@template-origin htdw-main)
(define (main b)
(big-bang b
(on-draw render-ball) ;Ball -> Image
(on-tick next-ball) ;Ball -> Ball
(on-mouse handle-mouse)));Ball Integer Integer MouseEvent -> Ball
(@htdf render-ball)
(@signature Ball -> Image)
;; place BALL on image at appropriate x, y coordinate
;; !!!
(define (render-ball b) MTS)
(@htdf next-ball)
(@signature Ball -> Ball)
;; produce ball at next x,y; checks bounces off top/right/bottom/left wall
(check-expect (next-ball (make-ball (+ LEF 1) TOP 3-4))
(bounce-top (make-ball (+ LEF 1) TOP 3-4)))
(check-expect (next-ball (make-ball (+ LEF 1) BOT 34))
(bounce-bottom (make-ball (+ LEF 1) BOT 34)))
(check-expect (next-ball (make-ball LEF (+ TOP 1)-34))
(bounce-left (make-ball LEF (+ TOP 1)-34)))
(check-expect (next-ball (make-ball RIG (+ TOP 1)34))
(bounce-right (make-ball RIG (+ TOP 1)34)))
(check-expect (next-ball (make-ball (/ WIDTH 2)(/ HEIGHT 2)34))
(glide (make-ball (/ WIDTH 2)(/ HEIGHT 2)34)))
#;
(define (next-ball b) b)
(@template-origin Number) ;b is treated as atomic
(@template
(define (next-ball b)
(... b)))
(define (next-ball b)
(cond [(touch-top? b)(bounce-top b)]
[(touch-bottom? b)(bounce-bottom b)]
[(touch-right? b)(bounce-right b)]
[(touch-left? b)(bounce-left b)]
[else
(glide b)]))
(@htdf handle-mouse)
(@signature Ball Integer Integer MouseEvent -> Ball)
;; replace ball with new ball on mouse click
;; NOTE: uses random, so testing has to use check-random
(check-random (handle-mouse (make-ball 1234)100200 "button-down")
(make-ball 100200(-5(random 11))(-5(random 11))))
(check-random (handle-mouse (make-ball 1234)100200 "button-up")
(make-ball 1234))
#;
(define (handle-mouse b x y me) b)
(@template-origin MouseEvent)
(@template
(define (handle-mouse b x y me)
(cond [(mouse=? me "button-down")(... b x y)]
[else
(... b x y)])))
(define (handle-mouse b x y me)
(cond [(mouse=? me "button-down")
(make-ball x y (-5(random 11))(-5(random 11)))]
[else b]))
(@htdf touch-
top?)
(@signature Ball -> Boolean)
;; true if ball is going up and edge will hit or pass top edge of box
(check-expect (touch-top? (make-ball LEF (+ TOP 5)3-4)) false)
(check-expect (touch-top? (make-ball LEF (+ TOP 4)3-4)) true)
(check-expect (touch-top? (make-ball LEF (+ TOP 1)3-2)) true)
(check-expect (touch-top? (make-ball LEF (+ TOP 0)32)) false)
#;
(define (touch-top? b) false)
(@template-origin Ball)
(@template
(define (touch-top? b)
(...(ball-x b)
(ball-y b)
(ball-dx b)
(ball-dy b))))
(define (touch-top? b)
(<=(+(ball-y b)(ball-dy b)) TOP))
(@htdf touch-bottom?)
(@signature Ball -> Boolean)
;;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!