]> git.jsancho.org Git - gacela.git/blob - games/asteroids/asteroids.scm
27265f7a57716b069702a8f24370bfe6b35cb566
[gacela.git] / games / asteroids / asteroids.scm
1 (set-game-properties! #:title "Gacela Asteroids")
2
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))
7
8 (define draw-asteroid
9   (let ((asteroid (load-texture "Asteroid.png")))
10     (lambda (a)
11       (to-origin)
12       (translate (assoc-ref a 'x) (assoc-ref a 'y))
13       (rotate (assoc-ref a 'angle))
14       (draw-texture asteroid))))
15
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)))
20     (set! x (+ x vx))
21     (set! y (+ y 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)))
26
27     (assoc-multiple-set! a 'x x 'y y 'angle (+ angle 1) 'vx vx 'vy vy)))
28
29 (define draw-ship
30   (let ((ship1 (load-texture "Ship1.png"))
31         (ship2 (load-texture "Ship2.png")))
32     (lambda (s)
33       (to-origin)
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)))))
38
39 (define (move-ship s)
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))))
45     (cond ((key? 'up)
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)))
53            (set! moving #t))
54           (else
55            (set! moving #f)))
56
57     (assoc-multiple-set! s 'x x 'y y 'angle angle 'moving moving)))
58
59 (define (ship-shot s)
60   (cond ((key-pressed? 'space)
61          `((x . ,(assoc-ref s 'x)) (y . ,(assoc-ref s 'y)) (angle . ,(assoc-ref s 'angle))))
62         (else
63          #f)))
64
65 (define (draw-shot sh)
66   (to-origin)
67   (translate (assoc-ref sh 'x) (assoc-ref sh 'y))
68   (rotate (assoc-ref sh 'angle))
69   (draw-line 10))
70
71 (define (move-shots shots)
72   (cond ((null? shots) '())
73         (else
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)
81                        (>= x min-x)
82                        (<= y max-y)
83                        (>= y min-y))
84                   (cons `((x . ,x) (y . ,y) (angle . ,angle))
85                         (move-shots (cdr shots))))
86                  (else
87                   (move-shots (cdr shots))))))))
88   
89 (define (make-asteroids n)
90   (define (xy n r)
91     (let ((n2 (- (random (* n 2)) n)))
92       (cond ((and (< n2 r) (>= n2 0)) r)
93             ((and (> n2 (- r)) (< n2 0)) (- r))
94             (else n2))))
95
96   (cond ((= n 0) '())
97         (else
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))))))
100
101 (define (killed-ship? s a)
102   (cond ((null? a) #f)
103         (else
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))))))
108
109 (define (kill-asteroids s a)
110   (define (f1 s1 a)
111     (cond ((null? a)
112            (values a #f))
113           (else
114            (let ((a1 (car 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))
118                     (values (cdr a) #t))
119                    (else
120                     (receive (an k) (f1 s1 (cdr a))
121                              (values (cons a1 an) k))))))))
122
123   (cond ((null? s)
124          (values s a))
125         (else
126          (let ((s1 (car s)))
127            (receive (an k) (f1 s1 a)
128                     (cond (k
129                            (kill-asteroids (cdr s) an))
130                           (else
131                            (receive (sn an) (kill-asteroids (cdr s) an)
132                                     (values (cons s1 sn) an)))))))))
133                            
134
135 (let ((asteroids #f) (ship #f) (shots #f))
136   (define (new-game n)
137     (set! asteroids (make-asteroids n))
138     (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
139     (set! shots '()))
140
141   (new-game 2)
142
143   (run-game
144    (cond ((killed-ship? ship asteroids)
145           (new-game 2)))
146    (receive (s a) (kill-asteroids shots asteroids)
147             (set! shots s)
148             (set! asteroids a))
149    (set! asteroids (map move-asteroid asteroids))
150    (set! ship (move-ship ship))
151    (let ((shot (ship-shot ship)))
152      (cond (shot
153             (set! shots (cons shot shots)))))
154    (set! shots (move-shots shots))
155    (for-each draw-asteroid asteroids)
156    (for-each draw-shot shots)
157    (draw-ship ship)))