Question: ( define BALLS ' ( ) ) ; inital empty list of balls ;To make multiple balls move, use map the next - ball function

(define BALLS '()) ; inital empty list of balls
;To make multiple balls move, use map the next-ball function over all balls
;then to get their next positions accordingly.
(define (ListOfBall balls) ; how this balls relate to b below???
(cond
[null? balls]
[else (cons (ListOfBall balls))]))
;create a first-ball to start the problem
(define first-ball (ListOfBall (make-ball(3002504-3))))
;Check if ball near edges
(define (touch-top? b)
(<=(+(ball-y b)(ball-dy b)) TOP))
(define (touch-bot? b)
(>=(+(ball-y b)(ball-dy b)) BOT))
(define (touch-rig? b)
(>=(+(ball-x b)(ball-dx b)) RIG))
(define (touch-lef? b)
(<=(+(ball-x b)(ball-dx b)) LEF))
;Bounce the ball of the edges
(define (bounce-top b)(make-ball (ball-x b)(+ TOP 1)(ball-dx b)(-(ball-dy b))))
(define (bounce-bot b)(make-ball (ball-x b)(- BOT 1)(ball-dx b)(-(ball-dy b))))
(define (bounce-lef b)(make-ball (+ LEF 1)(ball-y b)(-(ball-dx b))(ball-dy b)))
(define (bounce-rig b)(make-ball (- RIG 1)(ball-y b)(-(ball-dx b))(ball-dy b)))
;Move the ball
(define (glide b)(make-ball (+(ball-x b)(ball-dx b))(+(ball-y b)(ball-dy b))(ball-dx b)(ball-dy b)))
;Handle mouse click to creat a new ball
(define (handle-mouse b x y me) ;how does this b relate to balls below?
(cond [(equal? me "button-down")
(make-ball x y (-5(random 11))(-5(random 11)))]
[else b]))
; handle-mouse adds a new ball on mouse click
(define (handle-mouse balls x y event)
(cond [(mouse=? event "button-down")
(cons (make-ball x y (-5(random 11))(-5(random 11)))
balls)]
[else balls]))
;To handle key event, when the space bar is pressed, it will make the ListOfBall empty
(define (handle-key lob key) ; how does this lob relate to balls below?
(cond [(key=? key "")
'()]
[else lob]))
; handle-key clears all balls on space press
(define (handle-key balls key)
(cond [(key=? key "")
'()]
[else balls]))
;Render the ball on scene with green background
(define (render-ball b) ; how does this b relate to lob below?
(place-image BALL (ball-x b)(ball-y b)(empty-scene WIDTH HEIGHT "green")))
;render-balls to render image of multiple balls, use map the render-ball function over all balls
;then place them on the background image.
(define (render-balls balls)
(foldr (lambda (ball acc) ;what is difference ball vs balls??
(place-image BALL (ball-x ball)(ball-y ball) acc)) ;How does acc do in this foldrprocedure??
MTS
lob))
; next-balls produces the next list of balls, moving each one
(define (next-balls balls) ; how does this balls relate to b below?
(map next-ball balls)) ; what does procedure of map doing??
;Determine the next state of the ball using current position
(define (next-ball b)
(cond
[(touch-top? b)(bounce-top b)]
[(touch-bot? b)(bounce-bot b)]
[(touch-lef? b)(bounce-lef b)]
[(touch-rig? b)(bounce-rig b)]
[else (glide b)]))
;Main program start here use B1 to initialize
(big-bang B1 ; how to convert B1 to first ball below??
(on-tick next-ball)
(on-draw render-ball)
(on-key handle-key)
(on-mouse handle-mouse))
; main remains the same but now uses BALLS as the initial world state
(define (main first-ball) ;use first-ball to start the program.
(big-bang first-ball
(on-draw render-balls) ;ListOfBall -> Image
(on-tick next-balls) ;ListOfBall -> ListOfBall
(on-mouse handle-mouse) ;ListOfBall Integer Integer MouseEvent -> ListOfBall
(on-key handle-key))) ;ListOfBall KeyEvent -> ListOfBall ;questions:
;1) how does lob relate to balls?
;2) what are difference of lob vs ball vs balls?
;3) How does acc do in this foldrprocedure??
;4) what does map in procedure next-balls doing??

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!