;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname |25.1|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) (define-struct ball (radius x y delta-x delta-y color)) ;A ball is a structure ;(make-ball number number number number number symbol) ;draw-and-clear-ball : ball -> true (define (draw-and-clear-ball a-ball) (and (draw-solid-disk (make-posn (ball-x a-ball) (ball-y a-ball)) (ball-radius a-ball) (ball-color a-ball)) (sleep-for-a-while DELAY) (clear-solid-disk (make-posn (ball-x a-ball) (ball-y a-ball)) (ball-radius a-ball) (ball-color a-ball)))) ;move-ball : ball -> ball (define (move-ball a-ball) (make-ball (ball-radius a-ball) (+ (ball-x a-ball) (ball-delta-x a-ball)) (+ (ball-y a-ball) (ball-delta-y a-ball)) (ball-delta-x a-ball) (ball-delta-y a-ball) (ball-color a-ball))) ;out-of-bounds? : ball -> boolean (define (out-of-bounds? a-ball) (not (and (<= 0 (ball-x a-ball) WIDTH) (<= 0 (ball-y a-ball) HEIGHT)))) ;move-until-out : ball -> true ;Move the ball until it is out of bounds. (define (move-until-out a-ball) (cond [(out-of-bounds? a-ball) true] [(draw-and-clear-ball a-ball) (move-until-out (move-ball a-ball))])) (define VOLLEYBALL (make-ball 20 0 0 5 2 'black)) (define BEACHBALL (make-ball 50 25 25 3 2 'green)) (define BASEBALL (make-ball 10 470 470 -10 -14 'blue)) (define TENNISBALL (make-ball 8 470 20 -6 8 'yellow)) (define LISTOFBALLS (list VOLLEYBALL BEACHBALL BASEBALL TENNISBALL)) ;move-balls : (listof balls) -> true ;Moves alob (listof balls). (define (move-balls alob) (andmap move-until-out alob)) ;move-simultaneous : (listof balls) -> true ;Moves alob simultaneously. (define (move-simultaneous alob) (cond [(andmap out-of-bounds? alob) true] [else (and (andmap draw-and-clear-ball alob) (move-simultaneous (map move-ball alob)))])) (define WIDTH 500) (define HEIGHT 500) (define DELAY 1) (start WIDTH HEIGHT) ;(move-balls LISTOFBALLS) (move-simultaneous LISTOFBALLS) (stop)