1 (set-game-properties! #:title "Gacela Asteroids")
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))
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)))
16 (let ((asteroid (load-texture "Asteroid.png")))
19 (translate (assoc-ref a 'x) (assoc-ref a 'y))
20 (rotate (assoc-ref a 'angle))
21 (draw-texture asteroid))))
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)))
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)))
36 (assoc-set! a 'angle (+ angle 1))
42 (let ((ship1 (load-texture "Ship1.png"))
43 (ship2 (load-texture "Ship2.png")))
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)))))
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))))
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)))
71 `((x . ,x) (y . ,y) (angle . ,angle) (moving . ,moving))))
74 (cond ((key-pressed? 'space)
75 `((x . ,(assoc-ref s 'x)) (y . ,(assoc-ref s 'y)) (angle . ,(assoc-ref s 'angle))))
79 (define (draw-shot sh)
81 (translate (assoc-ref sh 'x) (assoc-ref sh 'y))
82 (rotate (assoc-ref sh 'angle))
85 (define (move-shots shots)
86 (cond ((null? shots) '())
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)
98 (cons `((x . ,x) (y . ,y) (angle . ,angle))
99 (move-shots (cdr shots))))
101 (move-shots (cdr shots))))))))
103 (define (make-asteroids n)
105 (let ((n2 (- (random (* n 2)) n)))
106 (cond ((and (< n2 r) (>= n2 0)) r)
107 ((and (> n2 (- r)) (< n2 0)) (- r))
112 (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1)) (make-asteroids (- n 1))))))
114 (define (killed-ship? ship asteroids)
117 (let ((asteroids #f) (ship #f) (shots #f))
119 (set! asteroids (make-asteroids n))
120 (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
126 (cond ((killed-ship? ship asteroids)
128 (set! asteroids (map move-asteroid asteroids))
129 (set! ship (move-ship ship))
130 (let ((shot (ship-shot ship)))
132 (set! shots (cons shot shots)))))
133 (set! shots (move-shots shots))
134 (for-each draw-asteroid asteroids)
135 (for-each draw-shot shots)