(define max-y (/ (assoc-ref (get-game-properties) 'height) 2))
(define min-y (- max-y))
-(define (update-asteroid! a #:key x y angle vx vy)
- (cond (x (assoc-set! a 'x x)))
- (cond (y (assoc-set! a 'y y)))
- (cond (x (assoc-set! a 'x x)))
- (cond (x (assoc-set! a 'x x)))
- (cond (x (assoc-set! a 'x x)))
-
(define draw-asteroid
(let ((asteroid (load-texture "Asteroid.png")))
(lambda (a)
(cond ((> y max-y) (set! vy -1))
((< y min-y) (set! vy 1)))
- (assoc-set! a 'x x)
- (assoc-set! a 'y y)
- (assoc-set! a 'angle (+ angle 1))
- (assoc-set! a 'vx vx)
- (assoc-set! a 'vy vy)
- a))
+ (assoc-multiple-set! a 'x x 'y y 'angle (+ angle 1) 'vx vx 'vy vy)))
(define draw-ship
(let ((ship1 (load-texture "Ship1.png"))
(else
(set! moving #f)))
- (assoc-set! s 'x x)
- (assoc-set! s 'y y)
- `((x . ,x) (y . ,y) (angle . ,angle) (moving . ,moving))))
+ (assoc-multiple-set! s 'x x 'y y 'angle angle 'moving moving)))
(define (ship-shot s)
(cond ((key-pressed? 'space)
(cond ((= n 0) '())
(else
- (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1)) (make-asteroids (- n 1))))))
+ (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1) (size . 95))
+ (make-asteroids (- n 1))))))
-(define (killed-ship? ship asteroids)
- #f)
+(define (killed-ship? s a)
+ (cond ((null? a) #f)
+ (else
+ (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y))
+ (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y)))
+ (assoc-ref (car a) 'size))
+ (killed-ship? s (cdr a))))))
+
+(define (kill-asteroids s a)
+ (define (f1 s1 a)
+ (cond ((null? a)
+ (values a #f))
+ (else
+ (let ((a1 (car a)))
+ (cond ((< (distance-between-points (list (assoc-ref s1 'x) (assoc-ref s1 'y))
+ (list (assoc-ref a1 'x) (assoc-ref a1 'y)))
+ (assoc-ref a1 'size))
+ (values (cdr a) #t))
+ (else
+ (receive (an k) (f1 s1 (cdr a))
+ (values (cons a1 an) k))))))))
+
+ (cond ((null? s)
+ (values s a))
+ (else
+ (let ((s1 (car s)))
+ (receive (an k) (f1 s1 a)
+ (cond (k
+ (kill-asteroids (cdr s) an))
+ (else
+ (receive (sn an) (kill-asteroids (cdr s) an)
+ (values (cons s1 sn) an)))))))))
+
(let ((asteroids #f) (ship #f) (shots #f))
(define (new-game n)
(run-game
(cond ((killed-ship? ship asteroids)
(new-game 2)))
+ (receive (s a) (kill-asteroids shots asteroids)
+ (set! shots s)
+ (set! asteroids a))
(set! asteroids (map move-asteroid asteroids))
(set! ship (move-ship ship))
(let ((shot (ship-shot ship)))