My question is when ball disappeared, instantly it comes to center of window<br><br>;; Teachpack: draw.ss<br><br>(define-struct ball (radius x y x-dot y-dot color))<br>;; A Ball is (make-ball Number Number Number Number Number Symbol)<br>
<br>#| Template<br>1. Trivial case:<br>Ball is entirely out of bounds. In that case, simply return true.<br>2. Divide:<br>Draw the ball, erase it, then replace it by a moved copy.<br>3 Conquer:<br>Nothing to do here.<br>
|#<br><br>;; move-ball-until-gone : Ball -> true<br>;; display a ball moving until it disappears from the window<br>;; uses generative recursion<br>(define (move-ball-until-gone b)<br> <br> (cond<br> ((out-of-bounds? b) true )<br>
(else (and<br> (draw-and-clear-ball b)<br> (move-ball-until-gone (move-ball b))))))<br><br>(define DELTA-T 1) ; controls speed of simulation<br>(define DELAY 0.1) ; seconds between updates<br>
<br>;; move-ball : Ball -> Ball<br>;; models the movement of a ball<br>(define (move-ball b)<br> (make-ball (ball-radius b)<br> (+ (ball-x b) (* DELTA-T (ball-x-dot b)))<br> (+ (ball-y b) (* DELTA-T (ball-y-dot b)))<br>
(ball-x-dot b)<br> (ball-y-dot b)<br> (ball-color b)))<br><br>;; drawing window dimensions<br>(define WIDTH 300)<br>(define HEIGHT 300)<br><br>;; draw-and-clear-ball : Ball -> true<br>
;; draws ball, waits a short time, then clears it<br>(define (draw-and-clear-ball b)<br> (and (draw-solid-disk (make-posn (ball-x b) (ball-y b))<br> (ball-radius b)<br> (ball-color b))<br>
(sleep-for-a-while DELAY)<br> (clear-solid-disk (make-posn (ball-x b) (ball-y b))<br> (ball-radius b)<br> (ball-color b))))<br><br>;; out-of-bounds? : Ball -> Boolean<br>
;; determines if the ball is completely outside the drawing window<br>(define (out-of-bounds? b)<br> <br> {local [(define ball-left (- (ball-x b) (ball-radius b)))<br> (define ball-right (+ (ball-x b) (ball-radius b)))<br>
(define ball-top (- (ball-y b) (ball-radius b)))<br> (define ball-bottom (+ (ball-y b) (ball-radius b)))]<br> (or (< ball-right 0) (> ball-left WIDTH)<br> (< ball-bottom 0) (> ball-top HEIGHT))})<br>
<br>;; test the program<br>(start WIDTH HEIGHT)<br>(move-ball-until-gone<br> (make-ball 10 50 50 3 0 'red)) <br><br><br> <br> <br>