]> git.jsancho.org Git - gacela.git/blobdiff - games/asteroids/asteroids.scm
(no commit message)
[gacela.git] / games / asteroids / asteroids.scm
index b18b7f604906ae0838ec127c0c371200597954b3..8f1c91124591f9a9cc2820fd8e27abb79fb0cbac 100644 (file)
@@ -7,16 +7,16 @@
 
 (define-mob (asteroid
             (image (load-texture "Asteroid.png"))
-            (x 0) (y 0) (angle 0)
-            (vx 1) (vy 1))
-  (set! x (+ x vx))
-  (set! y (+ y vy))
+            (x 0) (y 0) (angle 0) (dir 0))
+  (let ((r (degrees-to-radians (- dir))))
+    (set! x (+ x (sin r)))
+    (set! y (+ y (cos r))))
   (set! angle (+ angle 1))
 
-  (cond ((> x max-x) (set! vx -1))
-       ((< x min-x) (set! vx 1)))
-  (cond ((> y max-y) (set! vy -1))
-       ((< y min-y) (set! vy 1)))
+  (cond ((or (> x max-x) (< x min-x))
+        (set! dir (* -1 dir))))
+  (cond ((or (> y max-y) (< y min-y))
+        (set! dir (- 180 dir))))
   
   (translate x y)
   (rotate angle)
         (set! moving #t))
        (else
         (set! moving #f)))
+  (cond ((key-pressed? 'space)
+        (show-mob (make-shot #:x x #:y y #:angle angle))))
 
   (translate x y)
   (rotate angle)
   (draw-texture (if moving ship2 ship1)))
 
 (define-mob (shot (x 0) (y 0) (angle 0))
+  (let ((r (degrees-to-radians (- angle))))
+    (set! x (+ x (* 10 (sin r))))
+    (set! y (+ y (* 10 (cos r))))
+    (cond ((or (> x max-x)
+              (< x min-x)
+              (> y max-y)
+              (< y min-y))
+          (kill-me))))
+
   (translate x y)
   (rotate angle)
   (draw-line 10))
 
-;; (define (move-shots shots)
-;;   (cond ((null? shots) '())
-;;     (else
-;;      (let* ((sh (car shots))
-;;             (x (assoc-ref sh 'x)) (y (assoc-ref sh 'y))
-;;             (angle (assoc-ref sh 'angle))
-;;             (r (degrees-to-radians (- angle))))
-;;        (set! x (+ x (* 10 (sin r))))
-;;        (set! y (+ y (* 10 (cos r))))
-;;        (cond ((and (<= x max-x)
-;;                    (>= x min-x)
-;;                    (<= y max-y)
-;;                    (>= y min-y))
-;;               (cons `((x . ,x) (y . ,y) (angle . ,angle))
-;;                     (move-shots (cdr shots))))
-;;              (else
-;;               (move-shots (cdr shots))))))))
-
-(show-mob (make-asteroid))
+
+(define (init-asteroids n)
+  (cond ((> n 0)
+        (let ((x (- (random (* max-x 2)) max-x))
+              (y (- (random (* max-y 2)) max-y)))
+          (cond ((< (distance-between-points (list x y) '(0 0)) 120)
+                 (init-asteroids n))
+                (else
+                 (let ((angle (random 360)) (dir (- (random 360) 180)))
+                   (show-mob (make-asteroid #:x x #:y y #:angle angle #:dir dir)))
+                 (init-asteroids (- n 1))))))))
+
+
+(init-asteroids 2)
 (show-mob (make-ship))
 
-;; (define (ship-shot s)
-;;   (cond ((key-pressed? 'space)
-;;      `((x . ,(assoc-ref s 'x)) (y . ,(assoc-ref s 'y)) (angle . ,(assoc-ref s 'angle))))
-;;     (else
-;;      #f)))
+(let ((font (load-font "../tetris/lazy.ttf" #:size 20)))
+  (run-game
+   (render-text (format #f "Mobs: ~a" (length (get-active-mobs))) font)))
 
-;; (define (draw-shot sh)
-;;   (to-origin)
-;;   (translate (assoc-ref sh 'x) (assoc-ref sh 'y))
-;;   (rotate (assoc-ref sh 'angle))
-;;   (draw-line 10))
 
-;; (define (move-shots shots)
-;;   (cond ((null? shots) '())
-;;     (else
-;;      (let* ((sh (car shots))
-;;             (x (assoc-ref sh 'x)) (y (assoc-ref sh 'y))
-;;             (angle (assoc-ref sh 'angle))
-;;             (r (degrees-to-radians (- angle))))
-;;        (set! x (+ x (* 10 (sin r))))
-;;        (set! y (+ y (* 10 (cos r))))
-;;        (cond ((and (<= x max-x)
-;;                    (>= x min-x)
-;;                    (<= y max-y)
-;;                    (>= y min-y))
-;;               (cons `((x . ,x) (y . ,y) (angle . ,angle))
-;;                     (move-shots (cdr shots))))
-;;              (else
-;;               (move-shots (cdr shots))))))))
-  
-;; (define (make-asteroids n)
-;;   (define (xy n r)
-;;     (let ((n2 (- (random (* n 2)) n)))
-;;       (cond ((and (< n2 r) (>= n2 0)) r)
-;;         ((and (> n2 (- r)) (< n2 0)) (- r))
-;;         (else n2))))
-
-;;   (cond ((= n 0) '())
-;;     (else
-;;      (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1) (size . 95))
-;;            (make-asteroids (- n 1))))))
+;;   (define (new-game n)
+;;     (set! asteroids (make-asteroids n))
+;;     (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
+;;     (set! shots '()))
+
+;;   (new-game 2)
 
 ;; (define (killed-ship? s a)
 ;;   (cond ((null? a) #f)
                           
 
 ;; (let ((asteroids #f) (ship #f) (shots #f))
-;;   (define (new-game n)
-;;     (set! asteroids (make-asteroids n))
-;;     (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
-;;     (set! shots '()))
-
-;;   (new-game 2)
 
 ;;   (run-game
 ;;    (cond ((killed-ship? ship asteroids)