Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #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")))))
4 (define-struct ball (radius x y delta-x delta-y color))
6 ;A ball is a structure
7 ;(make-ball number number number number number symbol)
9 ;draw-and-clear-ball : ball -> true
10 (define (draw-and-clear-ball a-ball)
11 (and (draw-solid-disk (make-posn (ball-x a-ball)
12 (ball-y a-ball))
13 (ball-radius a-ball)
14 (ball-color a-ball))
15 (sleep-for-a-while DELAY)
16 (clear-solid-disk (make-posn (ball-x a-ball)
17 (ball-y a-ball))
18 (ball-radius a-ball)
19 (ball-color a-ball))))
21 ;move-ball : ball -> ball
22 (define (move-ball a-ball)
23 (make-ball (ball-radius a-ball)
24 (+ (ball-x a-ball) (ball-delta-x a-ball))
25 (+ (ball-y a-ball) (ball-delta-y a-ball))
26 (ball-delta-x a-ball)
27 (ball-delta-y a-ball)
28 (ball-color a-ball)))
30 ;out-of-bounds? : ball -> boolean
31 (define (out-of-bounds? a-ball)
32 (not (and (<= 0 (ball-x a-ball) WIDTH)
33 (<= 0 (ball-y a-ball) HEIGHT))))
35 ;move-until-out : ball -> true
36 ;Move the ball until it is out of bounds.
38 (define (move-until-out a-ball)
39 (cond
40 [(out-of-bounds? a-ball) true]
41 [(draw-and-clear-ball a-ball) (move-until-out (move-ball a-ball))]))
43 (define VOLLEYBALL (make-ball 20 0 0 5 2 'black))
44 (define BEACHBALL (make-ball 50 25 25 3 2 'green))
45 (define BASEBALL (make-ball 10 470 470 -10 -14 'blue))
46 (define TENNISBALL (make-ball 8 470 20 -6 8 'yellow))
48 (define LISTOFBALLS (list VOLLEYBALL
49 BEACHBALL
50 BASEBALL
51 TENNISBALL))
54 ;move-balls : (listof balls) -> true
55 ;Moves alob (listof balls).
57 (define (move-balls alob)
58 (andmap move-until-out alob))
60 ;move-simultaneous : (listof balls) -> true
61 ;Moves alob simultaneously.
63 (define (move-simultaneous alob)
64 (cond
65 [(andmap out-of-bounds? alob) true]
66 [else (and (andmap draw-and-clear-ball alob)
67 (move-simultaneous (map move-ball alob)))]))
69 (define WIDTH 500)
70 (define HEIGHT 500)
71 (define DELAY 1)
72 (start WIDTH HEIGHT)
73 ;(move-balls LISTOFBALLS)
74 (move-simultaneous LISTOFBALLS)
75 (stop)