]> git.jsancho.org Git - gacela.git/blob - games/asteroids/asteroids.scm
(no commit message)
[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) (dir 0))
11   (let ((r (degrees-to-radians (- dir))))
12     (set! x (+ x (sin r)))
13     (set! y (+ y (cos r))))
14   (set! angle (+ angle 1))
15
16   (cond ((or (> x max-x) (< x min-x))
17          (set! dir (* -1 dir))))
18   (cond ((or (> y max-y) (< y min-y))
19          (set! dir (- 180 dir))))
20   
21   (translate x y)
22   (rotate angle)
23   (draw-texture image))
24
25 ;; (define (killed-ship? s a)
26 ;;   (cond ((null? a) #f)
27 ;;      (else
28 ;;       (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y))
29 ;;                                       (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y)))
30 ;;              (assoc-ref (car a) 'size))
31 ;;           (killed-ship? s (cdr a))))))
32
33 (define-macro (killed-ship?)
34   `(
35     (map-mobs
36      (lambda (m)
37        
38 (define-mob (ship
39              (ship1 (load-texture "Ship1.png"))
40              (ship2 (load-texture "Ship2.png"))
41              (x 0) (y 0) (angle 0)
42              (moving #f))
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   (cond ((key-pressed? 'space)
57          (show-mob (make-shot #:x x #:y y #:angle angle))))
58
59   (translate x y)
60   (rotate angle)
61   (draw-texture (if moving ship2 ship1)))
62
63 (define-mob (shot (x 0) (y 0) (angle 0))
64   (let ((r (degrees-to-radians (- angle))))
65     (set! x (+ x (* 10 (sin r))))
66     (set! y (+ y (* 10 (cos r))))
67     (cond ((or (> x max-x)
68                (< x min-x)
69                (> y max-y)
70                (< y min-y))
71            (kill-me))))
72
73   (translate x y)
74   (rotate angle)
75   (draw-line 10))
76
77
78 (define (init-asteroids n)
79   (cond ((> n 0)
80          (let ((x (- (random (* max-x 2)) max-x))
81                (y (- (random (* max-y 2)) max-y)))
82            (cond ((< (distance-between-points (list x y) '(0 0)) 120)
83                   (init-asteroids n))
84                  (else
85                   (let ((angle (random 360)) (dir (- (random 360) 180)))
86                     (show-mob (make-asteroid #:x x #:y y #:angle angle #:dir dir)))
87                   (init-asteroids (- n 1))))))))
88
89
90 (init-asteroids 2)
91 (show-mob (make-ship))
92      
93 (let ((font (load-font "../tetris/lazy.ttf" #:size 20)))
94   (run-game
95    (render-text (format #f "Mobs: ~a" (length (get-active-mobs))) font)))
96
97
98 ;;   (define (new-game n)
99 ;;     (set! asteroids (make-asteroids n))
100 ;;     (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
101 ;;     (set! shots '()))
102
103 ;;   (new-game 2)
104
105 ;; (define (killed-ship? s a)
106 ;;   (cond ((null? a) #f)
107 ;;      (else
108 ;;       (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y))
109 ;;                                       (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y)))
110 ;;              (assoc-ref (car a) 'size))
111 ;;           (killed-ship? s (cdr a))))))
112
113 ;; (define (kill-asteroids s a)
114 ;;   (define (f1 s1 a)
115 ;;     (cond ((null? a)
116 ;;         (values a #f))
117 ;;        (else
118 ;;         (let ((a1 (car a)))
119 ;;           (cond ((< (distance-between-points (list (assoc-ref s1 'x) (assoc-ref s1 'y))
120 ;;                                              (list (assoc-ref a1 'x) (assoc-ref a1 'y)))
121 ;;                     (assoc-ref a1 'size))
122 ;;                  (values (cdr a) #t))
123 ;;                 (else
124 ;;                  (receive (an k) (f1 s1 (cdr a))
125 ;;                           (values (cons a1 an) k))))))))
126
127 ;;   (cond ((null? s)
128 ;;       (values s a))
129 ;;      (else
130 ;;       (let ((s1 (car s)))
131 ;;         (receive (an k) (f1 s1 a)
132 ;;                  (cond (k
133 ;;                         (kill-asteroids (cdr s) an))
134 ;;                        (else
135 ;;                         (receive (sn an) (kill-asteroids (cdr s) an)
136 ;;                                  (values (cons s1 sn) an)))))))))
137                            
138
139 ;; (let ((asteroids #f) (ship #f) (shots #f))
140
141 ;;   (run-game
142 ;;    (cond ((killed-ship? ship asteroids)
143 ;;        (new-game 2)))
144 ;;    (receive (s a) (kill-asteroids shots asteroids)
145 ;;          (set! shots s)
146 ;;          (set! asteroids a))
147 ;;    (set! asteroids (map move-asteroid asteroids))
148 ;;    (set! ship (move-ship (alist-copy ship)))
149 ;;    (let ((shot (ship-shot ship)))
150 ;;      (cond (shot
151 ;;          (set! shots (cons shot shots)))))
152 ;;    (set! shots (move-shots shots))
153 ;;    (for-each draw-asteroid asteroids)
154 ;;    (for-each draw-shot shots)
155 ;;    (draw-ship ship)))