Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me to complete this BSL DrRacket code, its simply a code of htdw that produce a ball and bounce around the screen and

Please help me to complete this BSL DrRacket code, its simply a code of htdw that produce a ball and bounce around the screen and then when I clicked the mouse the ball will change its location, but the problem its to improve the code so that if I clicked the mouse there will be a new ball instead of one ball there will be a lot of balls, I think I need to use the self reference and reference to finish the problem but I have a problem where to put the template of the self ref and ref in the handle-mouse function. Also with the handle key when its pressed all the balls should be disappear, I don't know if I need to have the same function or not.

(@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 (check-expect (render-ball (make-ball 20 30 3 3)) (place-image BALL 20 30 MTS)) (check-expect (render-ball (make-ball (- WIDTH 4) (- HEIGHT 5) -2 -3)) (place-image BALL (- WIDTH 4) (- HEIGHT 5) MTS)) #; (define (render-ball b) MTS)

(@template-origin Ball)

(@template (define (render-ball b) (... (ball-x b) (ball-y b) (ball-dx b) (ball-dy b))))

(define (render-ball b) (place-image BALL (ball-x b) (ball-y 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 3 4)) (bounce-bottom (make-ball (+ LEF 1) BOT 3 4))) (check-expect (next-ball (make-ball LEF (+ TOP 1) -3 4)) (bounce-left (make-ball LEF (+ TOP 1) -3 4))) (check-expect (next-ball (make-ball RIG (+ TOP 1) 3 4)) (bounce-right (make-ball RIG (+ TOP 1) 3 4))) (check-expect (next-ball (make-ball (/ WIDTH 2) (/ HEIGHT 2) 3 4)) (glide (make-ball (/ WIDTH 2) (/ HEIGHT 2) 3 4)))

#; (define (next-ball b) b)

(@template-origin Number) ; because 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 1 2 3 4) 100 200 "button-down") (make-ball 100 200 (- 5 (random 11)) (- 5 (random 11)))) (check-random (handle-mouse (make-ball 1 2 3 4) 100 200 "button-up") (make-ball 1 2 3 4)) #; (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]))

;;ListOfBall is one-of ; - empty ; - (cons ball ListOfBall) ; interp. a list of balls

(define LOB1 empty) (define LOB2 (cons (make-ball 1 2 3 4) empty))

;; Template rules used ; - atomic-distinct : empty ; - compound : (cons Ball ListOfBall) ; - refference (first lob) is ball ; - self-refference (rest lob) is ListOfBall

(define (fn-for-lob lob) (cond [(empty? lob) (...)] [else (... (fn-for-Ball (first lob)) (fn-for-lob (rest lob)))]))

(@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) 3 2)) 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) ;; true if ball is going down and edge will hit or pass bottom edge of box (check-expect (touch-bottom? (make-ball LEF (- BOT 3) 3 2)) false) (check-expect (touch-bottom? (make-ball LEF (- BOT 2) 3 2)) true) (check-expect (touch-bottom? (make-ball LEF (- BOT 0) 3 2)) true) (check-expect (touch-bottom? (make-ball LEF (- BOT 0) 3 -2)) false) #; (define (touch-bottom? b) false)

(@template-origin Ball)

(@template (define (touch-bottom? b) (... (ball-x b) (ball-y b) (ball-dx b) (ball-dy b))))

(define (touch-bottom? b) (>= (+ (ball-y b) (ball-dy b)) BOT))

(@htdf touch-left?) (@signature Ball -> Boolean) ;; true if ball is going left and edge will hit or pass left edge of box (check-expect (touch-left? (make-ball (+ LEF 6) TOP -5 2)) false) (check-expect (touch-left? (make-ball (+ LEF 5) TOP -5 2)) true) (check-expect (touch-left? (make-ball (+ LEF 0) TOP -5 2)) true) (check-expect (touch-left? (make-ball (+ LEF 0) TOP 3 2)) false) #; (define (touch-left? b) false)

(@template-origin Ball)

(@template (define (touch-left? b) (... (ball-x b) (ball-y b) (ball-dx b) (ball-dy b))))

(define (touch-left? b) (<= (+ (ball-x b) (ball-dx b)) LEF))

(@htdf touch-right?) (@signature Ball -> Boolean) ;; true if ball is going right and edge will hit or pass right edge of box (check-expect (touch-right? (make-ball (- RIG 6) TOP 5 2)) false) (check-expect (touch-right? (make-ball (- RIG 5) TOP 5 2)) true) (check-expect (touch-right? (make-ball (- RIG 0) TOP 5 2)) true) (check-expect (touch-right? (make-ball (- RIG 0) TOP -3 2)) false) #; (define (touch-right? b) false)

(@template-origin Ball)

(@template (define (touch-right? b) (... (ball-x b) (ball-y b) (ball-dx b) (ball-dy b))))

(define (touch-right? b) (>= (+ (ball-x b) (ball-dx b)) RIG))

(@htdf bounce-top) (@signature Ball -> Ball) ;; produce a ball with top edge 1 pixel off top of box, moving down ;; CONSTRAINT: assume ball is close to top edge and moving up (check-expect (bounce-top (make-ball (+ LEF 1) (+ TOP 3) 2 -4)) (make-ball (+ LEF 1) (+ TOP 1) 2 4)) (check-expect (bounce-top (make-ball (+ LEF 2) (+ TOP 6) 3 -7)) (make-ball (+ LEF 2) (+ TOP 1) 3 7)) #; (define (bounce-top b) b)

(@template-origin Ball)

(@template (define (bounce-top b) (... (ball-x b) (ball-y b) (ball-dx b) (ball-dy b))))

(define (bounce-top b) (make-ball (ball-x b) (+ TOP 1) (ball-dx b) (- (ball-dy b))))

(@htdf bounce-bottom) (@signature Ball -> Ball) ;; produce a ball with bottom edge 1 pixel off bottom of box, moving up ;; CONSTRAINT: assume ball is close to bottom edge and moving down (check-expect (bounce-bottom (make-ball (+ LEF 1) (- BOT 3) 2 4)) (make-ball (+ LEF 1) (- BOT 1) 2 -4)) (check-expect (bounce-bottom (make-ball (+ LEF 2) (- BOT 6) 3 7)) (make-ball (+ LEF 2) (- BOT 1) 3 -7)) #; (define (bounce-bottom b) b)

(@template-origin Ball)

(@template (define (bounce-bottom b) (... (ball-x b) (ball-y b) (ball-dx b) (ball-dy b))))

(define (bounce-bottom b) (make-ball (ball-x b) (- BOT 1) (ball-dx b) (- (ball-dy b))))

(@htdf bounce-left) (@signature Ball -> Ball) ;; produce a ball with left edge 1 pixel off left of box, moving right ;; CONSTRAINT: assume ball is close to left edge and moving left (check-expect (bounce-left (make-ball (+ LEF 3) (+ TOP 2) -4 4)) (make-ball (+ LEF 1) (+ TOP 2) 4 4)) (check-expect (bounce-left (make-ball (+ LEF 5) (+ TOP 2) -8 4)) (make-ball (+ LEF 1) (+ TOP 2) 8 4)) #; (define (bounce-left b) b)

(@template-origin Ball)

(@template (define (bounce-left b) (... (ball-x b) (ball-y b) (ball-dx b) (ball-dy b))))

(define (bounce-left b) (make-ball (+ LEF 1) (ball-y b) (- (ball-dx b)) (ball-dy b) ))

(@htdf bounce-right) (@signature Ball -> Ball) ;; produce a ball with right edge 1 pixel off right of box, moving left ;; CONSTRAINT: assume ball is close to right edge and moving right (check-expect (bounce-right (make-ball (- RIG 3) (+ TOP 1) 4 4)) (make-ball (- RIG 1) (+ TOP 1) -4 4)) (check-expect (bounce-right (make-ball (- RIG 5) (+ TOP 1) 8 4)) (make-ball (- RIG 1) (+ TOP 1) -8 4)) #; (define (bounce-right b) b)

(@template-origin Ball)

(@template (define (bounce-right b) (... (ball-x b) (ball-y b) (ball-dx b) (ball-dy b))))

(define (bounce-right b) (make-ball (- RIG 1) (ball-y b) (- (ball-dx b)) (ball-dy b)))

(@htdf glide) (@signature Ball -> Ball) ;; move ball by dx dy ;; CONSTRAINT: ball is not touching or about to touch any edge of the box (check-expect (glide (make-ball 100 200 2 3)) (make-ball 102 203 2 3)) (check-expect (glide (make-ball 50 220 -3 -2)) (make-ball 47 218 -3 -2))

;(define (glide b) b)

(@template-origin Ball)

(@template (define (glide b) (... (ball-x b) (ball-y b) (ball-dx b) (ball-dy b))))

(define (glide b) (make-ball (+ (ball-x b) (ball-dx b)) (+ (ball-y b) (ball-dy b)) (ball-dx b) (ball-dy b)))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions