]> git.jsancho.org Git - gacela.git/blob - games/asteroids/asteroids.scm
a0268f8e69a71ba76f5b083b73eea5e90882e10a
[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-mob (asteroid
9              (image (load-texture "Asteroid.png"))
10              (x 0) (y 0) (angle 0)
11              (vx 1) (vy 1))
12   (set! x (+ x vx))
13   (set! y (+ y vy))
14   (cond ((> x max-x) (set! vx -1))
15         ((< x min-x) (set! vx 1)))
16   (cond ((> y max-y) (set! vy -1))
17         ((< y min-y) (set! vy 1)))
18   
19   (translate x y)
20   (rotate angle)
21   (draw-texture image))
22
23 (show-mob (make-asteroid))
24
25 ;; (define (move-asteroid a)
26 ;;   (let ((x (assoc-ref a 'x)) (y (assoc-ref a 'y))
27 ;;      (angle (assoc-ref a 'angle))
28 ;;      (vx (assoc-ref a 'vx)) (vy (assoc-ref a 'vy)))
29 ;;     (set! x (+ x vx))
30 ;;     (set! y (+ y vy))
31 ;;     (cond ((> x max-x) (set! vx -1))
32 ;;        ((< x min-x) (set! vx 1)))
33 ;;     (cond ((> y max-y) (set! vy -1))
34 ;;        ((< y min-y) (set! vy 1)))
35
36 ;;     (assoc-multiple-set! a 'x x 'y y 'angle (+ angle 1) 'vx vx 'vy vy)))
37
38 ;; (define draw-ship
39 ;;   (let ((ship1 (load-texture "Ship1.png"))
40 ;;      (ship2 (load-texture "Ship2.png")))
41 ;;     (lambda (s)
42 ;;       (to-origin)
43 ;;       (translate (assoc-ref s 'x) (assoc-ref s 'y))
44 ;;       (rotate (assoc-ref s 'angle))
45 ;;       (let ((ship (if (assoc-ref s 'moving) ship2 ship1)))
46 ;;      (draw-texture ship)))))
47
48 ;; (define (move-ship ship)
49 ;;   (let* ((s ship)
50 ;;       (x (assoc-ref s 'x)) (y (assoc-ref s 'y))
51 ;;       (angle (assoc-ref s 'angle))
52 ;;       (moving (assoc-ref s 'moving)))
53 ;;     (cond ((key? 'left) (set! angle (+ angle 5)))
54 ;;        ((key? 'right) (set! angle (- angle 5))))
55 ;;     (cond ((key? 'up)
56 ;;         (let ((r (degrees-to-radians (- angle))))
57 ;;           (set! x (+ x (* 4 (sin r))))
58 ;;           (set! y (+ y (* 4 (cos r)))))
59 ;;         (cond ((> x max-x) (set! x min-x))
60 ;;               ((< x min-x) (set! x max-x)))
61 ;;         (cond ((> y max-y) (set! y min-y))
62 ;;               ((< y min-y) (set! y max-y)))
63 ;;         (set! moving #t))
64 ;;        (else
65 ;;         (set! moving #f)))
66
67 ;;     (assoc-multiple-set! s 'x x 'y y 'angle angle 'moving moving)))
68
69 ;; (define (ship-shot s)
70 ;;   (cond ((key-pressed? 'space)
71 ;;       `((x . ,(assoc-ref s 'x)) (y . ,(assoc-ref s 'y)) (angle . ,(assoc-ref s 'angle))))
72 ;;      (else
73 ;;       #f)))
74
75 ;; (define (draw-shot sh)
76 ;;   (to-origin)
77 ;;   (translate (assoc-ref sh 'x) (assoc-ref sh 'y))
78 ;;   (rotate (assoc-ref sh 'angle))
79 ;;   (draw-line 10))
80
81 ;; (define (move-shots shots)
82 ;;   (cond ((null? shots) '())
83 ;;      (else
84 ;;       (let* ((sh (car shots))
85 ;;              (x (assoc-ref sh 'x)) (y (assoc-ref sh 'y))
86 ;;              (angle (assoc-ref sh 'angle))
87 ;;              (r (degrees-to-radians (- angle))))
88 ;;         (set! x (+ x (* 10 (sin r))))
89 ;;         (set! y (+ y (* 10 (cos r))))
90 ;;         (cond ((and (<= x max-x)
91 ;;                     (>= x min-x)
92 ;;                     (<= y max-y)
93 ;;                     (>= y min-y))
94 ;;                (cons `((x . ,x) (y . ,y) (angle . ,angle))
95 ;;                      (move-shots (cdr shots))))
96 ;;               (else
97 ;;                (move-shots (cdr shots))))))))
98   
99 ;; (define (make-asteroids n)
100 ;;   (define (xy n r)
101 ;;     (let ((n2 (- (random (* n 2)) n)))
102 ;;       (cond ((and (< n2 r) (>= n2 0)) r)
103 ;;          ((and (> n2 (- r)) (< n2 0)) (- r))
104 ;;          (else n2))))
105
106 ;;   (cond ((= n 0) '())
107 ;;      (else
108 ;;       (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1) (size . 95))
109 ;;             (make-asteroids (- n 1))))))
110
111 ;; (define (killed-ship? s a)
112 ;;   (cond ((null? a) #f)
113 ;;      (else
114 ;;       (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y))
115 ;;                                       (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y)))
116 ;;              (assoc-ref (car a) 'size))
117 ;;           (killed-ship? s (cdr a))))))
118
119 ;; (define (kill-asteroids s a)
120 ;;   (define (f1 s1 a)
121 ;;     (cond ((null? a)
122 ;;         (values a #f))
123 ;;        (else
124 ;;         (let ((a1 (car a)))
125 ;;           (cond ((< (distance-between-points (list (assoc-ref s1 'x) (assoc-ref s1 'y))
126 ;;                                              (list (assoc-ref a1 'x) (assoc-ref a1 'y)))
127 ;;                     (assoc-ref a1 'size))
128 ;;                  (values (cdr a) #t))
129 ;;                 (else
130 ;;                  (receive (an k) (f1 s1 (cdr a))
131 ;;                           (values (cons a1 an) k))))))))
132
133 ;;   (cond ((null? s)
134 ;;       (values s a))
135 ;;      (else
136 ;;       (let ((s1 (car s)))
137 ;;         (receive (an k) (f1 s1 a)
138 ;;                  (cond (k
139 ;;                         (kill-asteroids (cdr s) an))
140 ;;                        (else
141 ;;                         (receive (sn an) (kill-asteroids (cdr s) an)
142 ;;                                  (values (cons s1 sn) an)))))))))
143                            
144
145 ;; (let ((asteroids #f) (ship #f) (shots #f))
146 ;;   (define (new-game n)
147 ;;     (set! asteroids (make-asteroids n))
148 ;;     (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
149 ;;     (set! shots '()))
150
151 ;;   (new-game 2)
152
153 ;;   (run-game
154 ;;    (cond ((killed-ship? ship asteroids)
155 ;;        (new-game 2)))
156 ;;    (receive (s a) (kill-asteroids shots asteroids)
157 ;;          (set! shots s)
158 ;;          (set! asteroids a))
159 ;;    (set! asteroids (map move-asteroid asteroids))
160 ;;    (set! ship (move-ship (alist-copy ship)))
161 ;;    (let ((shot (ship-shot ship)))
162 ;;      (cond (shot
163 ;;          (set! shots (cons shot shots)))))
164 ;;    (set! shots (move-shots shots))
165 ;;    (for-each draw-asteroid asteroids)
166 ;;    (for-each draw-shot shots)
167 ;;    (draw-ship ship)))