#reader(lib "htdp-beginner-reader.ss" "lang")((modname bubble-2c) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
(require (lib "world.ss" "moby" "stub"))
(define WIDTH 320)
(define HEIGHT 480)
(define-struct vel (x y))
(define target (make-posn 200 175 (random WIDTH) (random HEIGHT)))
(define-struct world (posn r vel))
(define initial-w
(make-world (make-posn 200 120 (quotient WIDTH 2) (quotient HEIGHT 2))
10
(make-vel 0 0)))
(define (tick w)
(make-world (posn+vel (world-posn w) (world-vel w))
(- (world-r w) 1/3)
(world-vel w)))
(define (tilt w azimuth pitch roll)
(make-world (world-posn w)
(world-r w)
(make-vel roll (- pitch))))
(define (render w)
(place-image/posn (circle 30 "solid" "red")
target
(place-image/posn (circle (world-r w) "solid" "blue")
(world-posn w)
(empty-scene WIDTH HEIGHT))))
(define (collide? w)
(< (distance (world-posn w) target)
(world-r w)))
(define (game-ends? w)
(or (<= (world-r w) 1) (collide? w)))
(define (posn+vel a-posn a-vel)
(make-posn (clamp (+ (posn-x a-posn) (vel-x a-vel))
0 WIDTH)
(clamp (+ (posn-y a-posn) (vel-y a-vel))
0 HEIGHT)))
(define (clamp x a b)
(cond [(> x b) b]
[(< x a) a]
[else x]))
(define (distance posn-1 posn-2)
(sqrt
(+ (sqr (- (posn-x posn-1) (posn-x posn-2)))
(sqr (- (posn-y posn-1) (posn-y posn-2))))))
(define (place-image/posn an-image a-posn a-scene)
(place-image an-image (posn-x a-posn) (posn-y a-posn) a-scene))
(big-bang WIDTH HEIGHT 1/20 initial-w)
(on-redraw render)
(stop-when game-ends?)
(on-tilt tilt)