X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=games%2Fasteroids%2Fasteroids.scm;h=a12c1def86f7a0b0aa43a523d2f9b6218cae1e5d;hb=f3f295df512f5c2a4668f2d8c38977ea7c650911;hp=3a355a63db0d747d3054e25b24a45361a84eed5d;hpb=f52afe4d66f4708213e5823ce4d383e82bedce44;p=gacela.git diff --git a/games/asteroids/asteroids.scm b/games/asteroids/asteroids.scm index 3a355a6..a12c1de 100644 --- a/games/asteroids/asteroids.scm +++ b/games/asteroids/asteroids.scm @@ -1,3 +1,11 @@ +#!/usr/bin/guile \ +-e gacela-script -s +!# + +(use-modules (gacela gacela) + (gacela math)) +(init-gacela) + (set-game-properties! #:title "Gacela Asteroids") (define max-x (/ (assoc-ref (get-game-properties) 'width) 2)) @@ -5,78 +13,142 @@ (define max-y (/ (assoc-ref (get-game-properties) 'height) 2)) (define min-y (- max-y)) -(define draw-asteroid - (let ((asteroid (load-texture "Asteroid.png"))) - (lambda (a) - (to-origin) - (translate (assoc-ref a 'x) (assoc-ref a 'y)) - (rotate (assoc-ref a 'angle)) - (draw-texture asteroid)))) - -(define (move-asteroid a) - (let* ((x (assoc-ref a 'x)) (y (assoc-ref a 'y)) - (angle (assoc-ref a 'angle)) - (vx (assoc-ref a 'vx)) (vy (assoc-ref a 'vy)) - (nx (+ x vx)) (ny (+ y vy))) - (cond ((> nx max-x) (set! vx -1)) - ((< nx min-x) (set! vx 1))) - (cond ((> ny max-y) (set! vy -1)) - ((< ny min-y) (set! vy 1))) - (set! angle (+ angle 1)) - `((x . ,(+ x vx)) (y . ,(+ y vy)) (angle . ,angle) (vx . ,vx) (vy . ,vy)))) - -(define draw-ship - (let ((ship1 (load-texture "Ship1.png")) - (ship2 (load-texture "Ship2.png"))) - (lambda (s) - (to-origin) - (translate (assoc-ref s 'x) (assoc-ref s 'y)) - (rotate (assoc-ref s 'angle)) - (let ((ship (if (assoc-ref s 'moving) ship2 ship1))) - (draw-texture ship))))) - -(define (move-ship s) - (let ((x (assoc-ref s 'x)) (y (assoc-ref s 'y)) - (angle (assoc-ref s 'angle)) - (moving (assoc-ref s 'moving))) - (cond ((key? 'left) (set! angle (+ angle 5))) - ((key? 'right) (set! angle (- angle 5)))) - (cond ((key? 'up) - (let ((r (degrees-to-radians (- angle)))) - (set! x (+ x (* 4 (sin r)))) - (set! y (+ y (* 4 (cos r))))) - (cond ((> x max-x) (set! x min-x)) - ((< x min-x) (set! x max-x))) - (cond ((> y max-y) (set! y min-y)) - ((< y min-y) (set! y max-y))) - (set! moving #t)) - (else - (set! moving #f))) - `((x . ,x) (y . ,y) (angle . ,angle) (moving . ,moving)))) - -(define (ship-shot s) - (cond ((key-released? 'space) - #f))) - -(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) '()) + +;;; Asteroids + +(define-checking-mobs (asteroid-shots x y size) (shot (sx x) (sy y)) + (if (< (distance-between-points (list sx sy) (list x y)) size) 1 0)) + +(define (asteroid-killed? x y size) + (> (apply + (asteroid-shots x y size)) 0)) + +(define-mob (asteroid + (image (load-texture "Asteroid.png")) + (x 0) (y 0) (angle 0) (dir 0) (size 100)) + (cond ((asteroid-killed? x y size) + (kill-me)) + (else + (let ((r (degrees-to-radians (- dir)))) + (set! x (+ x (sin r))) + (set! y (+ y (cos r)))) + (set! angle (+ angle 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) + (draw-texture image)) + + +;;; Ship + +(define-mob (ship + (ship1 (load-texture "Ship1.png")) + (ship2 (load-texture "Ship2.png")) + (x 0) (y 0) (angle 0) + (moving #f)) + (cond ((key? 'left) (set! angle (+ angle 5))) + ((key? 'right) (set! angle (- angle 5)))) + (cond ((key? 'up) + (let ((r (degrees-to-radians (- angle)))) + (set! x (+ x (* 4 (sin r)))) + (set! y (+ y (* 4 (cos r))))) + (cond ((> x max-x) (set! x min-x)) + ((< x min-x) (set! x max-x))) + (cond ((> y max-y) (set! y min-y)) + ((< y min-y) (set! y max-y))) + (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))) + + +;;; Shots + +(define-checking-mobs (impacted-shots x y) (asteroid (ax x) (ay y) (size size)) + (if (< (distance-between-points (list ax ay) (list x y)) size) 1 0)) + +(define (shot-killed? x y) + (> (apply + (impacted-shots x y)) 0)) + +(define-mob (shot (x 0) (y 0) (angle 0)) + (cond ((shot-killed? x y) + (kill-me)) (else - (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1)) (make-asteroids (- n 1)))))) - -(let ((asteroids (make-asteroids 2)) - (ship '((x . 0) (y . 0) (angle . 0) (moving . #f))) - (shots '()) - (run-game - (set! asteroids (map move-asteroid asteroids)) - (set! ship (move-ship ship)) - (let ((shot (ship-shot ship))) - (cond (shot - (set! shots (cons shot shots))))) - (for-each draw-asteroid asteroids) - (draw-ship ship))) + (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)) + + +;;; Game + +(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)) + +(let ((font (load-font "../tetris/lazy.ttf" #:size 20))) + (game + (render-text (format #f "Mobs: ~a" (length (get-active-mobs))) font))) + + +;; (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) +;; (else +;; (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y)) +;; (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y))) +;; (assoc-ref (car a) 'size)) +;; (killed-ship? s (cdr a)))))) + + +;; (let ((asteroids #f) (ship #f) (shots #f)) + +;; (run-game +;; (cond ((killed-ship? ship asteroids) +;; (new-game 2))) +;; (receive (s a) (kill-asteroids shots asteroids) +;; (set! shots s) +;; (set! asteroids a)) +;; (set! asteroids (map move-asteroid asteroids)) +;; (set! ship (move-ship (alist-copy ship))) +;; (let ((shot (ship-shot ship))) +;; (cond (shot +;; (set! shots (cons shot shots))))) +;; (set! shots (move-shots shots)) +;; (for-each draw-asteroid asteroids) +;; (for-each draw-shot shots) +;; (draw-ship ship)))