]> git.jsancho.org Git - gacela.git/blobdiff - gacela_tetris.scm
(no commit message)
[gacela.git] / gacela_tetris.scm
index b8c3b57f75ccd1030e853bfaf7dd2246f1bd4686..d27a08608fd0bb0393b22e83d585d8476d56a958 100644 (file)
@@ -1,4 +1,4 @@
-(set-game-properties #:title "Gacela Tetris" #:fps 15)
+(set-game-properties! #:title "Gacela Tetris" #:fps 15)
 
 (define (tetramine-i)
   (let ((color '(1 0 0)))
@@ -45,8 +45,8 @@
          ((= n 6) (tetramine-z)))))
 
 (define (draw-cell cell)
-  (cond ((null? cell) #f)
-       (else (with-color cell (draw-square #:size 20)))))
+  (cond ((and cell (not (null? cell)))
+        (with-color cell (draw-square #:size 20)))))
 
 (define (draw-row row)
   (for-each (lambda (cell) (draw-cell cell) (translate 23 0)) row))
                    (join-grids (cdr source) (cdr destination) x y)))))
 
 (define* (collide-rows row1 row2 #:optional (offset 0))
-  (cond ((not (or row1 row2)) #f)
+  (cond ((or (null? row1) (null? row2)) #f)
        ((> offset 0) (collide-rows row1 (cdr row2) (- offset 1)))
        (else (or (and (car row1) (car row2)) (collide-rows (cdr row1) (cdr row2))))))
 
 (define* (collide-grids grid1 grid2 #:optional (x 0) (y 0))
-  (cond ((not (or grid1 grid2)) nil)
+  (cond ((or (null? grid1) (null? grid2)) #f)
        ((> y 0) (collide-grids grid1 (cdr grid2) x (- y 1)))
        (else (or (collide-rows (car grid1) (car grid2) x)
                  (collide-grids (cdr grid1) (cdr grid2) x y)))))
@@ -83,7 +83,7 @@
 (define (rotate-tetramine grid)
   (define (rot grid res)
     (cond ((null? grid) res)
-         (else (rot (cdr grid) (mapcar #'cons (car grid) res)))))
+         (else (rot (cdr grid) (map cons (car grid) res)))))
   (rot grid (make-list (length (car grid)))))
 
 (define (row-completed row)
        (else (and (car row) (row-completed (cdr row))))))
 
 (define (remove-rows-completed grid)
-  (let ((res (remove-if (lambda (x) (row-completed x)) grid)))
-    (inc-points (- (length grid) (length res)))
+  (let ((res (filter (lambda (x) (not (row-completed x))) grid)))
     (define (fill grid n)
       (cond ((< n 1) grid)
-           (else (fill (cons (make-list 14) grid) (- n 1)))))
+           (else (fill (cons (make-list 14 #f) grid) (- n 1)))))
+    (inc-points (- (length grid) (length res)))
     (fill res (- 20 (length res)))))
 
 (define get-points #f)
          (set! lines (+ lines l)))))
 
 (define game #f)
-(define game-over #f)
+(define display-game-over #f)
 (define tetramine #f)
 
 (let ((current-tetramine (random-tetramine)) (x 6) (y 0)
       (next (random-tetramine))
       (timer (make-timer))
-      (grid (make-list 20 (make-list 14)))
+      (grid (make-list 20 (make-list 14 #f)))
       (background (load-texture "fondo_tetris.png"))
       (font (load-font "lazy.ttf" #:size 20))
       (game-over #f))
 
   (set! game
        (lambda ()
-         (if game-over (game-over) (tetramine))))
+         (if game-over (display-game-over) (tetramine))))
 
-  (set! game-over
+  (set! display-game-over
        (lambda ()
          (translate -100 0)
          (render-text "Game Over" font #:size 50)))
 
   (set! tetramine
        (lambda ()
-         (cond ((eq? (timer-state timer) 'stopped) (start-timer timer)))
+         (cond ((eq? (get-state timer) 'stopped) (start-timer timer)))
 
          (cond ((key? 'right)
                 (cond ((not (collide-grids current-tetramine grid (+ x 1) y))
                 (cond ((or (collide-grids current-tetramine grid x (+ y 1))
                            (> (+ y 1 (length current-tetramine)) 20))
                        (set! grid (remove-rows-completed (join-grids current-tetramine grid x y)))
-                       (set! current-tetramine next x 6 y 0)
-                       (cond ((collide-grids current-tetramine grid x y) (set! game-over t)))
+                       (set! current-tetramine next)
+                       (set! x 6)
+                       (set! y 0)
+                       (cond ((collide-grids current-tetramine grid x y) (set! game-over #t)))
                        (set! next (random-tetramine)))
                       (else
                        (set! y (+ y 1))
          (translate 440 440)
          (draw-grid next)
          (translate -40 -100)
-         (render-text (format nil "Points: ~d" (get-points)) font)
+         (render-text (format #f "Points: ~a" (get-points)) font)
          (translate 0 -30)
-         (render-text (format nil "Lines: ~d" (get-lines)) font))))
-
-(define (run-gacela-tetris)
-  (let ((frame 0.0) (fps (make-timer)) (update (make-timer)))
-    (start-timer update)
-    (start-timer fps)
-    (run-game
-     (game)
-     (set! frame (+ frame 1))
-     (cond ((> (get-time update) 1000)
-           (display (/ frame (/ (get-time fps) 1000.0)))
-           (newline)
-           (start-timer update))))
-    (quit-game)))
+         (render-text (format #f "Lines: ~a" (get-lines)) font))))
+
+(let ((frame 0.0) (fps (make-timer)) (update (make-timer)))
+  (start-timer update)
+  (start-timer fps)
+  (run-game
+   (game)
+   (set! frame (+ frame 1))
+   (cond ((> (get-time update) 1000)
+         (display (/ frame (/ (get-time fps) 1000.0)))
+         (newline)
+         (start-timer update)))))