1 (set-game-properties! #:title "Gacela Asteroids")
3 (define max-x (/ (assoc-ref (get-game-properties) 'width) 2))
4 (define min-x (- max-x))
5 (define max-y (/ (assoc-ref (get-game-properties) 'height) 2))
6 (define min-y (- max-y))
9 (let ((asteroid (load-texture "Asteroid.png")))
12 (translate (assoc-ref a 'x) (assoc-ref a 'y))
13 (rotate (assoc-ref a 'angle))
14 (draw-texture asteroid))))
16 (define (move-asteroid a)
17 (let* ((x (assoc-ref a 'x)) (y (assoc-ref a 'y))
18 (angle (assoc-ref a 'angle))
19 (vx (assoc-ref a 'vx)) (vy (assoc-ref a 'vy)))
22 (cond ((> x max-x) (set! vx -1))
23 ((< x min-x) (set! vx 1)))
24 (cond ((> y max-y) (set! vy -1))
25 ((< y min-y) (set! vy 1)))
27 (assoc-multiple-set! a 'x x 'y y 'angle (+ angle 1) 'vx vx 'vy vy)))
30 (let ((ship1 (load-texture "Ship1.png"))
31 (ship2 (load-texture "Ship2.png")))
34 (translate (assoc-ref s 'x) (assoc-ref s 'y))
35 (rotate (assoc-ref s 'angle))
36 (let ((ship (if (assoc-ref s 'moving) ship2 ship1)))
37 (draw-texture ship)))))
40 (let ((x (assoc-ref s 'x)) (y (assoc-ref s 'y))
41 (angle (assoc-ref s 'angle))
42 (moving (assoc-ref s 'moving)))
43 (cond ((key? 'left) (set! angle (+ angle 5)))
44 ((key? 'right) (set! angle (- angle 5))))
46 (let ((r (degrees-to-radians (- angle))))
47 (set! x (+ x (* 4 (sin r))))
48 (set! y (+ y (* 4 (cos r)))))
49 (cond ((> x max-x) (set! x min-x))
50 ((< x min-x) (set! x max-x)))
51 (cond ((> y max-y) (set! y min-y))
52 ((< y min-y) (set! y max-y)))
57 (assoc-multiple-set! s 'x x 'y y 'angle angle 'moving moving)))
60 (cond ((key-pressed? 'space)
61 `((x . ,(assoc-ref s 'x)) (y . ,(assoc-ref s 'y)) (angle . ,(assoc-ref s 'angle))))
65 (define (draw-shot sh)
67 (translate (assoc-ref sh 'x) (assoc-ref sh 'y))
68 (rotate (assoc-ref sh 'angle))
71 (define (move-shots shots)
72 (cond ((null? shots) '())
74 (let* ((sh (car shots))
75 (x (assoc-ref sh 'x)) (y (assoc-ref sh 'y))
76 (angle (assoc-ref sh 'angle))
77 (r (degrees-to-radians (- angle))))
78 (set! x (+ x (* 10 (sin r))))
79 (set! y (+ y (* 10 (cos r))))
80 (cond ((and (<= x max-x)
84 (cons `((x . ,x) (y . ,y) (angle . ,angle))
85 (move-shots (cdr shots))))
87 (move-shots (cdr shots))))))))
89 (define (make-asteroids n)
91 (let ((n2 (- (random (* n 2)) n)))
92 (cond ((and (< n2 r) (>= n2 0)) r)
93 ((and (> n2 (- r)) (< n2 0)) (- r))
98 (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1) (size . 95))
99 (make-asteroids (- n 1))))))
101 (define (killed-ship? s a)
104 (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y))
105 (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y)))
106 (assoc-ref (car a) 'size))
107 (killed-ship? s (cdr a))))))
109 (define (kill-asteroids s a)
115 (cond ((< (distance-between-points (list (assoc-ref s1 'x) (assoc-ref s1 'y))
116 (list (assoc-ref a1 'x) (assoc-ref a1 'y)))
117 (assoc-ref a1 'size))
120 (receive (an k) (f1 s1 (cdr a))
121 (values (cons a1 an) k))))))))
127 (receive (an k) (f1 s1 a)
129 (kill-asteroids (cdr s) an))
131 (receive (sn an) (kill-asteroids (cdr s) an)
132 (values (cons s1 sn) an)))))))))
135 (let ((asteroids #f) (ship #f) (shots #f))
137 (set! asteroids (make-asteroids n))
138 (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
144 (cond ((killed-ship? ship asteroids)
146 (receive (s a) (kill-asteroids shots asteroids)
149 (set! asteroids (map move-asteroid asteroids))
150 (set! ship (move-ship ship))
151 (let ((shot (ship-shot ship)))
153 (set! shots (cons shot shots)))))
154 (set! shots (move-shots shots))
155 (for-each draw-asteroid asteroids)
156 (for-each draw-shot shots)