]> git.jsancho.org Git - gacela.git/blob - games/asteroids/asteroids.scm
d684e07d7e3fb272cefc7359355c3f3406369465
[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 (< (sqrt (+ (expt (- (assoc-ref s 'x) (assoc-ref (car a) 'x)) 2)
105                          (expt (- (assoc-ref s 'y) (assoc-ref (car a) 'y)) 2)))
106                 (assoc-ref (car a) 'size))
107              (killed-ship? s (cdr a))))))
108
109 (let ((asteroids #f) (ship #f) (shots #f))
110   (define (new-game n)
111     (set! asteroids (make-asteroids n))
112     (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
113     (set! shots '()))
114
115   (new-game 2)
116
117   (run-game
118    (cond ((killed-ship? ship asteroids)
119           (new-game 2)))
120    (set! asteroids (map move-asteroid asteroids))
121    (set! ship (move-ship ship))
122    (let ((shot (ship-shot ship)))
123      (cond (shot
124             (set! shots (cons shot shots)))))
125    (set! shots (move-shots shots))
126    (for-each draw-asteroid asteroids)
127    (for-each draw-shot shots)
128    (draw-ship ship)))