X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=games%2Fasteroids%2Fasteroids.scm;h=d684e07d7e3fb272cefc7359355c3f3406369465;hb=e562aad852e11b0ac7f9d94f65bff8b984240d02;hp=3a355a63db0d747d3054e25b24a45361a84eed5d;hpb=f52afe4d66f4708213e5823ce4d383e82bedce44;p=gacela.git diff --git a/games/asteroids/asteroids.scm b/games/asteroids/asteroids.scm index 3a355a6..d684e07 100644 --- a/games/asteroids/asteroids.scm +++ b/games/asteroids/asteroids.scm @@ -16,14 +16,15 @@ (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)))) + (vx (assoc-ref a 'vx)) (vy (assoc-ref a 'vy))) + (set! x (+ x vx)) + (set! y (+ y vy)) + (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))) + + (assoc-multiple-set! a 'x x 'y y 'angle (+ angle 1) 'vx vx 'vy vy))) (define draw-ship (let ((ship1 (load-texture "Ship1.png")) @@ -52,12 +53,39 @@ (set! moving #t)) (else (set! moving #f))) - `((x . ,x) (y . ,y) (angle . ,angle) (moving . ,moving)))) + + (assoc-multiple-set! s 'x x 'y y 'angle angle 'moving moving))) (define (ship-shot s) - (cond ((key-released? 'space) + (cond ((key-pressed? 'space) + `((x . ,(assoc-ref s 'x)) (y . ,(assoc-ref s 'y)) (angle . ,(assoc-ref s 'angle)))) + (else #f))) +(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))) @@ -67,16 +95,34 @@ (cond ((= n 0) '()) (else - (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1)) (make-asteroids (- n 1)))))) + (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1) (size . 95)) + (make-asteroids (- n 1)))))) + +(define (killed-ship? s a) + (cond ((null? a) #f) + (else + (or (< (sqrt (+ (expt (- (assoc-ref s 'x) (assoc-ref (car a) 'x)) 2) + (expt (- (assoc-ref s 'y) (assoc-ref (car a) 'y)) 2))) + (assoc-ref (car a) 'size)) + (killed-ship? s (cdr a)))))) + +(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) -(let ((asteroids (make-asteroids 2)) - (ship '((x . 0) (y . 0) (angle . 0) (moving . #f))) - (shots '()) (run-game + (cond ((killed-ship? ship asteroids) + (new-game 2))) (set! asteroids (map move-asteroid asteroids)) (set! ship (move-ship 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)))