]> 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 (update-asteroid! a #:key x y angle vx vy)
9   (cond (x (assoc-set! a 'x x)))
10   (cond (y (assoc-set! a 'y y)))
11   (cond (x (assoc-set! a 'x x)))
12   (cond (x (assoc-set! a 'x x)))
13   (cond (x (assoc-set! a 'x x)))
14
15 (define draw-asteroid
16   (let ((asteroid (load-texture "Asteroid.png")))
17     (lambda (a)
18       (to-origin)
19       (translate (assoc-ref a 'x) (assoc-ref a 'y))
20       (rotate (assoc-ref a 'angle))
21       (draw-texture asteroid))))
22
23 (define (move-asteroid a)
24   (let* ((x (assoc-ref a 'x)) (y (assoc-ref a 'y))
25          (angle (assoc-ref a 'angle))
26          (vx (assoc-ref a 'vx)) (vy (assoc-ref a 'vy)))
27     (set! x (+ x vx))
28     (set! y (+ y vy))
29     (cond ((> x max-x) (set! vx -1))
30           ((< x min-x) (set! vx 1)))
31     (cond ((> y max-y) (set! vy -1))
32           ((< y min-y) (set! vy 1)))
33
34     (assoc-set! a 'x x)
35     (assoc-set! a 'y y)
36     (assoc-set! a 'angle (+ angle 1))
37     (assoc-set! a 'vx vx)
38     (assoc-set! a 'vy vy)
39     a))
40
41 (define draw-ship
42   (let ((ship1 (load-texture "Ship1.png"))
43         (ship2 (load-texture "Ship2.png")))
44     (lambda (s)
45       (to-origin)
46       (translate (assoc-ref s 'x) (assoc-ref s 'y))
47       (rotate (assoc-ref s 'angle))
48       (let ((ship (if (assoc-ref s 'moving) ship2 ship1)))
49         (draw-texture ship)))))
50
51 (define (move-ship s)
52   (let ((x (assoc-ref s 'x)) (y (assoc-ref s 'y))
53         (angle (assoc-ref s 'angle))
54         (moving (assoc-ref s 'moving)))
55     (cond ((key? 'left) (set! angle (+ angle 5)))
56           ((key? 'right) (set! angle (- angle 5))))
57     (cond ((key? 'up)
58            (let ((r (degrees-to-radians (- angle))))
59              (set! x (+ x (* 4 (sin r))))
60              (set! y (+ y (* 4 (cos r)))))
61            (cond ((> x max-x) (set! x min-x))
62                  ((< x min-x) (set! x max-x)))
63            (cond ((> y max-y) (set! y min-y))
64                  ((< y min-y) (set! y max-y)))
65            (set! moving #t))
66           (else
67            (set! moving #f)))
68
69     (assoc-set! s 'x x)
70     (assoc-set! s 'y y)
71     `((x . ,x) (y . ,y) (angle . ,angle) (moving . ,moving))))
72
73 (define (ship-shot s)
74   (cond ((key-pressed? 'space)
75          `((x . ,(assoc-ref s 'x)) (y . ,(assoc-ref s 'y)) (angle . ,(assoc-ref s 'angle))))
76         (else
77          #f)))
78
79 (define (draw-shot sh)
80   (to-origin)
81   (translate (assoc-ref sh 'x) (assoc-ref sh 'y))
82   (rotate (assoc-ref sh 'angle))
83   (draw-line 10))
84
85 (define (move-shots shots)
86   (cond ((null? shots) '())
87         (else
88          (let* ((sh (car shots))
89                 (x (assoc-ref sh 'x)) (y (assoc-ref sh 'y))
90                 (angle (assoc-ref sh 'angle))
91                 (r (degrees-to-radians (- angle))))
92            (set! x (+ x (* 10 (sin r))))
93            (set! y (+ y (* 10 (cos r))))
94            (cond ((and (<= x max-x)
95                        (>= x min-x)
96                        (<= y max-y)
97                        (>= y min-y))
98                   (cons `((x . ,x) (y . ,y) (angle . ,angle))
99                         (move-shots (cdr shots))))
100                  (else
101                   (move-shots (cdr shots))))))))
102   
103 (define (make-asteroids n)
104   (define (xy n r)
105     (let ((n2 (- (random (* n 2)) n)))
106       (cond ((and (< n2 r) (>= n2 0)) r)
107             ((and (> n2 (- r)) (< n2 0)) (- r))
108             (else n2))))
109
110   (cond ((= n 0) '())
111         (else
112          (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1)) (make-asteroids (- n 1))))))
113
114 (define (killed-ship? ship asteroids)
115   #f)
116
117 (let ((asteroids #f) (ship #f) (shots #f))
118   (define (new-game n)
119     (set! asteroids (make-asteroids n))
120     (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
121     (set! shots '()))
122
123   (new-game 2)
124
125   (run-game
126    (cond ((killed-ship? ship asteroids)
127           (new-game 2)))
128    (set! asteroids (map move-asteroid asteroids))
129    (set! ship (move-ship ship))
130    (let ((shot (ship-shot ship)))
131      (cond (shot
132             (set! shots (cons shot shots)))))
133    (set! shots (move-shots shots))
134    (for-each draw-asteroid asteroids)
135    (for-each draw-shot shots)
136    (draw-ship ship)))