]> git.jsancho.org Git - gacela.git/blob - games/asteroids/asteroids.scm
0ce6832165538e33c6b4392da6fafb823059c407
[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-macro (asteroid-killed?)
9   `(> (apply +
10              (map-mobs
11               (lambda (s) (if (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y)) (list x y)) size) 1 0))
12               'shot))
13       0))
14
15 (define-mob (asteroid
16              (image (load-texture "Asteroid.png"))
17              (x 0) (y 0) (angle 0) (dir 0) (size 100))
18   (let ((r (degrees-to-radians (- dir))))
19     (set! x (+ x (sin r)))
20     (set! y (+ y (cos r))))
21   (set! angle (+ angle 1))
22
23   (cond ((or (> x max-x) (< x min-x))
24          (set! dir (* -1 dir))))
25   (cond ((or (> y max-y) (< y min-y))
26          (set! dir (- 180 dir))))
27   
28   (if (asteroid-killed?) (kill-me))
29   (translate x y)
30   (rotate angle)
31   (draw-texture image))
32
33 (define-mob (ship
34              (ship1 (load-texture "Ship1.png"))
35              (ship2 (load-texture "Ship2.png"))
36              (x 0) (y 0) (angle 0)
37              (moving #f))
38   (cond ((key? 'left) (set! angle (+ angle 5)))
39         ((key? 'right) (set! angle (- angle 5))))
40   (cond ((key? 'up)
41          (let ((r (degrees-to-radians (- angle))))
42            (set! x (+ x (* 4 (sin r))))
43            (set! y (+ y (* 4 (cos r)))))
44          (cond ((> x max-x) (set! x min-x))
45                ((< x min-x) (set! x max-x)))
46          (cond ((> y max-y) (set! y min-y))
47                ((< y min-y) (set! y max-y)))
48          (set! moving #t))
49         (else
50          (set! moving #f)))
51   (cond ((key-pressed? 'space)
52          (show-mob (make-shot #:x x #:y y #:angle angle))))
53
54   (translate x y)
55   (rotate angle)
56   (draw-texture (if moving ship2 ship1)))
57
58 (define-mob (shot (x 0) (y 0) (angle 0))
59   (let ((r (degrees-to-radians (- angle))))
60     (set! x (+ x (* 10 (sin r))))
61     (set! y (+ y (* 10 (cos r))))
62     (cond ((or (> x max-x)
63                (< x min-x)
64                (> y max-y)
65                (< y min-y))
66            (kill-me))))
67
68   (translate x y)
69   (rotate angle)
70   (draw-line 10))
71
72
73 (define (init-asteroids n)
74   (cond ((> n 0)
75          (let ((x (- (random (* max-x 2)) max-x))
76                (y (- (random (* max-y 2)) max-y)))
77            (cond ((< (distance-between-points (list x y) '(0 0)) 120)
78                   (init-asteroids n))
79                  (else
80                   (let ((angle (random 360)) (dir (- (random 360) 180)))
81                     (show-mob (make-asteroid #:x x #:y y #:angle angle #:dir dir)))
82                   (init-asteroids (- n 1))))))))
83
84
85 (init-asteroids 2)
86 (show-mob (make-ship))
87      
88 (let ((font (load-font "../tetris/lazy.ttf" #:size 20)))
89   (run-game
90    (render-text (format #f "Mobs: ~a" (length (get-active-mobs))) font)))
91
92
93 ;;   (define (new-game n)
94 ;;     (set! asteroids (make-asteroids n))
95 ;;     (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
96 ;;     (set! shots '()))
97
98 ;;   (new-game 2)
99
100 ;; (define (killed-ship? s a)
101 ;;   (cond ((null? a) #f)
102 ;;      (else
103 ;;       (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y))
104 ;;                                       (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y)))
105 ;;              (assoc-ref (car a) 'size))
106 ;;           (killed-ship? s (cdr a))))))
107
108
109 ;; (let ((asteroids #f) (ship #f) (shots #f))
110
111 ;;   (run-game
112 ;;    (cond ((killed-ship? ship asteroids)
113 ;;        (new-game 2)))
114 ;;    (receive (s a) (kill-asteroids shots asteroids)
115 ;;          (set! shots s)
116 ;;          (set! asteroids a))
117 ;;    (set! asteroids (map move-asteroid asteroids))
118 ;;    (set! ship (move-ship (alist-copy ship)))
119 ;;    (let ((shot (ship-shot ship)))
120 ;;      (cond (shot
121 ;;          (set! shots (cons shot shots)))))
122 ;;    (set! shots (move-shots shots))
123 ;;    (for-each draw-asteroid asteroids)
124 ;;    (for-each draw-shot shots)
125 ;;    (draw-ship ship)))