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 (< (sqrt (+ (expt (- (assoc-ref s 'x) (assoc-ref (car a) 'x)) 2)
105 (expt (- (assoc-ref s 'y) (assoc-ref (car a) 'y)) 2)))
106 (assoc-ref (car a) 'size))
107 (killed-ship? s (cdr a))))))
109 (define (kill-asteroids s a)
110 (cond ((or (null? s) (null? a)) (values s a))
112 (let ((s1 (car s)) (a1 (car a)))
113 (cond ((or (< (sqrt (+ (expt (- (assoc-ref
115 (let ((asteroids #f) (ship #f) (shots #f))
117 (set! asteroids (make-asteroids n))
118 (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
124 (cond ((killed-ship? ship asteroids)
126 (set! asteroids (map move-asteroid asteroids))
127 (set! ship (move-ship ship))
128 (let ((shot (ship-shot ship)))
130 (set! shots (cons shot shots)))))
131 (set! shots (move-shots shots))
132 (for-each draw-asteroid asteroids)
133 (for-each draw-shot shots)