]> git.jsancho.org Git - gacela.git/blob - games/asteroids/asteroids.scm
756dd73d4e5bf7dd14e4c83af5b8138ca0aa8f8e
[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-mob (ship
26              (ship1 (load-texture "Ship1.png"))
27              (ship2 (load-texture "Ship2.png"))
28              (x 0) (y 0) (angle 0)
29              (moving #f))
30   (cond ((key? 'left) (set! angle (+ angle 5)))
31         ((key? 'right) (set! angle (- angle 5))))
32   (cond ((key? 'up)
33          (let ((r (degrees-to-radians (- angle))))
34            (set! x (+ x (* 4 (sin r))))
35            (set! y (+ y (* 4 (cos r)))))
36          (cond ((> x max-x) (set! x min-x))
37                ((< x min-x) (set! x max-x)))
38          (cond ((> y max-y) (set! y min-y))
39                ((< y min-y) (set! y max-y)))
40          (set! moving #t))
41         (else
42          (set! moving #f)))
43   (cond ((key-pressed? 'space)
44          (show-mob (make-shot #:x x #:y y #:angle angle))))
45
46   (translate x y)
47   (rotate angle)
48   (draw-texture (if moving ship2 ship1)))
49
50 (define-mob (shot (x 0) (y 0) (angle 0))
51   (let ((r (degrees-to-radians (- angle))))
52     (set! x (+ x (* 10 (sin r))))
53     (set! y (+ y (* 10 (cos r))))
54     (cond ((or (> x max-x)
55                (< x min-x)
56                (> y max-y)
57                (< y min-y))
58            (kill-me))))
59
60   (translate x y)
61   (rotate angle)
62   (draw-line 10))
63
64
65 (define (init-asteroids n)
66   (cond ((> n 0)
67          (let ((x (- (random (* max-x 2)) max-x))
68                (y (- (random (* max-y 2)) max-y)))
69            (cond ((< (distance-between-points (list x y) '(0 0)) 120)
70                   (init-asteroids n))
71                  (else
72                   (let ((angle (random 360)) (dir (- (random 360) 180)))
73                     (show-mob (make-asteroid #:x x #:y y #:angle angle #:dir dir)))
74                   (init-asteroids (- n 1))))))))
75
76
77 (init-asteroids 2)
78 (show-mob (make-ship))
79
80 ;;   (define (new-game n)
81 ;;     (set! asteroids (make-asteroids n))
82 ;;     (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
83 ;;     (set! shots '()))
84
85 ;;   (new-game 2)
86
87 ;; (define (killed-ship? s a)
88 ;;   (cond ((null? a) #f)
89 ;;      (else
90 ;;       (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y))
91 ;;                                       (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y)))
92 ;;              (assoc-ref (car a) 'size))
93 ;;           (killed-ship? s (cdr a))))))
94
95 ;; (define (kill-asteroids s a)
96 ;;   (define (f1 s1 a)
97 ;;     (cond ((null? a)
98 ;;         (values a #f))
99 ;;        (else
100 ;;         (let ((a1 (car a)))
101 ;;           (cond ((< (distance-between-points (list (assoc-ref s1 'x) (assoc-ref s1 'y))
102 ;;                                              (list (assoc-ref a1 'x) (assoc-ref a1 'y)))
103 ;;                     (assoc-ref a1 'size))
104 ;;                  (values (cdr a) #t))
105 ;;                 (else
106 ;;                  (receive (an k) (f1 s1 (cdr a))
107 ;;                           (values (cons a1 an) k))))))))
108
109 ;;   (cond ((null? s)
110 ;;       (values s a))
111 ;;      (else
112 ;;       (let ((s1 (car s)))
113 ;;         (receive (an k) (f1 s1 a)
114 ;;                  (cond (k
115 ;;                         (kill-asteroids (cdr s) an))
116 ;;                        (else
117 ;;                         (receive (sn an) (kill-asteroids (cdr s) an)
118 ;;                                  (values (cons s1 sn) an)))))))))
119                            
120
121 ;; (let ((asteroids #f) (ship #f) (shots #f))
122
123 ;;   (run-game
124 ;;    (cond ((killed-ship? ship asteroids)
125 ;;        (new-game 2)))
126 ;;    (receive (s a) (kill-asteroids shots asteroids)
127 ;;          (set! shots s)
128 ;;          (set! asteroids a))
129 ;;    (set! asteroids (map move-asteroid asteroids))
130 ;;    (set! ship (move-ship (alist-copy ship)))
131 ;;    (let ((shot (ship-shot ship)))
132 ;;      (cond (shot
133 ;;          (set! shots (cons shot shots)))))
134 ;;    (set! shots (move-shots shots))
135 ;;    (for-each draw-asteroid asteroids)
136 ;;    (for-each draw-shot shots)
137 ;;    (draw-ship ship)))