X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;ds=sidebyside;f=games%2Fasteroids%2Fasteroids.scm;h=a12c1def86f7a0b0aa43a523d2f9b6218cae1e5d;hb=2eee3eb546a25305d548fcb331769be84fd3a38f;hp=0ce6832165538e33c6b4392da6fafb823059c407;hpb=0277721b10a05850563424de2053013bc1f0ca6c;p=gacela.git diff --git a/games/asteroids/asteroids.scm b/games/asteroids/asteroids.scm index 0ce6832..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,71 +13,92 @@ (define max-y (/ (assoc-ref (get-game-properties) 'height) 2)) (define min-y (- max-y)) -(define-macro (asteroid-killed?) - `(> (apply + - (map-mobs - (lambda (s) (if (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y)) (list x y)) size) 1 0)) - 'shot)) - 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)) - (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)))) + (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)))))) - (if (asteroid-killed?) (kill-me)) (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)))) + ((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))) + (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)))) + (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)) - (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)))) + (cond ((shot-killed? x y) + (kill-me)) + (else + (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)) @@ -86,7 +115,7 @@ (show-mob (make-ship)) (let ((font (load-font "../tetris/lazy.ttf" #:size 20))) - (run-game + (game (render-text (format #f "Mobs: ~a" (length (get-active-mobs))) font)))