Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is our solution to Problem Set 3 . ;; For problem set 4 you must improve this program in important ways. Be sure ;;

Below is our solution to Problem Set 3.
;; For problem set 4 you must improve this program in important ways. Be sure
;; not to change the program's behaviour in any other ways than we are asking.
;; Making extra improvements could cause you problems with the autograder.
;;
;; Modify the program so that instead of there being a single ball there
;; can be any number of balls. The mouse button should add a new ball.
;; Pressing space should clear all the balls.
;;
;; Again your change should be systematic and complete. All relevant
;; design elements and tags must be updated. You must name the new
;; world state type ListOfBall.
;;
;; NOTE, if you work systematically, starting from the HtDW recipe, you
;; will decide that you need a new ListOfBall type; but that the Ball
;; type does not change. Therefore you should NOT change your next-ball
;; function. Your render-ball function will also be useful when you
;; work on your place-ball function, although render-ball will have to
;; change some.
;;
(@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 203033))
(place-image BALL 2030 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 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) ; 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 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")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

Which are non projected Teaching aids in advance learning system?

Answered: 1 week ago