]> git.jsancho.org Git - gacela.git/blob - gacela_tetris.lisp
(no commit message)
[gacela.git] / gacela_tetris.lisp
1 (in-package :gacela)
2
3 (setq *zoom* -50)
4
5 (defun tetramine-i ()
6   (let ((color '(1 0 0)))
7     `((,color ,color ,color ,color))))
8
9 (defun tetramine-j ()
10   (let ((color '(1 0.5 0)))
11     `((,color ,color ,color)
12       (nil nil ,color))))
13
14 (defun tetramine-l ()
15   (let ((color '(1 0 1)))
16     `((nil nil ,color)
17       (,color ,color ,color))))
18
19 (defun tetramine-o ()
20   (let ((color '(0 0 1)))
21     `((,color ,color)
22       (,color ,color))))
23
24 (defun tetramine-s ()
25   (let ((color '(0 1 0)))
26     `((nil ,color ,color)
27       (,color ,color nil))))
28
29 (defun tetramine-t ()
30   (let ((color '(0.5 0 0)))
31     `((,color ,color ,color)
32       (nil ,color nil))))
33
34 (defun tetramine-z ()
35   (let ((color '(0 1 1)))
36     `((,color ,color nil)
37       (nil ,color ,color))))
38
39 (defun random-tetramine ()
40   (let ((n (random 7)))
41     (cond ((= n 0) (tetramine-i))
42           ((= n 1) (tetramine-j))
43           ((= n 2) (tetramine-l))
44           ((= n 3) (tetramine-o))
45           ((= n 4) (tetramine-s))
46           ((= n 5) (tetramine-t))
47           ((= n 6) (tetramine-z)))))
48
49 (defun draw-cell (cell)
50   (cond ((null cell) nil)
51         (t (draw-color cell) (draw-square :size 0.9))))
52
53 (defun draw-row (row)
54   (mapcar (lambda (cell) (draw-cell cell) (translate 2 0)) row))
55
56 (defun draw-grid (grid)
57   (mapcar (lambda (row) (draw-row row) (translate (* -2 (length row)) -2)) grid))
58
59 (defun join-rows (source destination &optional (offset 0))
60   (cond ((null source) destination)
61         ((null destination) nil)
62         ((> offset 0) (cons (car destination) (join-rows source (cdr destination) (- offset 1))))
63         (t (cons (or (car source) (car destination))
64                  (join-rows (cdr source) (cdr destination) offset)))))
65
66 (defun join-grids (source destination &optional (x 0) (y 0))
67   (cond ((null source) destination)
68         ((null destination) nil)
69         ((> y 0) (cons (car destination)
70                        (join-grids source (cdr destination) x (- y 1))))
71         (t (cons (join-rows (car source) (car destination) x)
72                  (join-grids (cdr source) (cdr destination) x y)))))
73
74 (defun collide-rows (row1 row2 &optional (offset 0))
75   (cond ((not (or row1 row2)) nil)
76         ((> offset 0) (collide-rows row1 (cdr row2) (- offset 1)))
77         (t (or (and (car row1) (car row2)) (collide-rows (cdr row1) (cdr row2))))))
78
79 (defun collide-grids (grid1 grid2 &optional (x 0) (y 0))
80   (cond ((not (or grid1 grid2)) nil)
81         ((> y 0) (collide-grids grid1 (cdr grid2) x (- y 1)))
82         (t (or (collide-rows (car grid1) (car grid2) x)
83                (collide-grids (cdr grid1) (cdr grid2) x y)))))
84
85 (defun rotate-tetramine (grid)
86   (labels ((rot (grid res)
87                 (cond ((null grid) res)
88                       (t (rot (cdr grid) (mapcar #'cons (car grid) res))))))
89           (rot grid (make-list (length (car grid))))))
90
91 (defun row-completed (row)
92   (cond ((null row) t)
93         (t (and (car row) (row-completed (cdr row))))))
94
95 (defun remove-rows-completed (grid)
96   (let ((res (remove-if (lambda (x) (row-completed x)) grid)))
97     (labels ((fill (grid n)
98                    (cond ((< n 1) grid)
99                          (t (fill (cons (make-list 14) grid) (- n 1))))))
100             (fill res (- 20 (length res))))))
101
102 (let ((tetramine (random-tetramine)) (x 6) (y 0)
103       (next (random-tetramine))
104       (timer (make-timer))
105       (grid (make-list 20 :initial-element (make-list 14)))
106       (texture (load-texture "fondo_tetris.png")))
107   (defun tetramine ()
108     (cond ((eq (timer-state timer) 'stopped) (start-timer timer)))
109
110     (cond ((key? 'right)
111            (cond ((not (collide-grids tetramine grid (+ x 1) y))
112                   (incf x)))))
113     (cond ((key? 'left)
114            (cond ((not (collide-grids tetramine grid (- x 1) y))
115                   (decf x)))))
116     (cond ((< x 0) (setq x 0))
117           ((> (+ x (length (car tetramine))) 14) (setq x (- 14 (length (car tetramine))))))
118
119     (cond ((key-pressed? 'up)
120            (let ((t1 (rotate-tetramine tetramine)))
121              (cond ((not (collide-grids t1 grid x y))
122                     (setq tetramine t1))))))
123
124     (cond ((or (key? 'down) (> (get-time timer) 5000))
125            (cond ((or (collide-grids tetramine grid x (+ y 1))
126                       (> (+ y 1 (length tetramine)) 20))
127                   (setq grid (remove-rows-completed (join-grids tetramine grid x y)))
128                   (setq tetramine next x 6 y 0)
129                   (setq next (random-tetramine)))
130                  (t (incf y) (start-timer timer)))))
131
132     (draw-square :size 1 :texture texture)
133     (translate -25 19)
134     (draw-grid (join-grids tetramine grid x y))
135     (translate 40 40)
136     (draw-grid next)))
137
138 (run-game "Gacela Tetris" (tetramine))