1 (set-game-properties! #:title "Gacela Tetris" #:fps 15)
4 (let ((color '(1 0 0)))
5 `((,color ,color ,color ,color))))
8 (let ((color '(1 0.5 0)))
9 `((,color ,color ,color)
13 (let ((color '(1 0 1)))
15 (,color ,color ,color))))
18 (let ((color '(0 0 1)))
23 (let ((color '(0 1 0)))
28 (let ((color '(0.5 0 0)))
29 `((,color ,color ,color)
33 (let ((color '(0 1 1)))
37 (define (random-tetramine)
39 (cond ((= n 0) (tetramine-i))
40 ((= n 1) (tetramine-j))
41 ((= n 2) (tetramine-l))
42 ((= n 3) (tetramine-o))
43 ((= n 4) (tetramine-s))
44 ((= n 5) (tetramine-t))
45 ((= n 6) (tetramine-z)))))
47 (define (draw-cell cell)
48 (cond ((and cell (not (null? cell)))
49 (with-color cell (draw-square #:size 20)))))
51 (define (draw-row row)
52 (for-each (lambda (cell) (draw-cell cell) (translate 23 0)) row))
54 (define (draw-grid grid)
55 (for-each (lambda (row) (draw-row row) (translate (* -23 (length row)) -23)) grid))
57 (define* (join-rows source destination #:optional (offset 0))
58 (cond ((null? source) destination)
59 ((null? destination) '())
60 ((> offset 0) (cons (car destination) (join-rows source (cdr destination) (- offset 1))))
61 (else (cons (or (car source) (car destination))
62 (join-rows (cdr source) (cdr destination) offset)))))
64 (define* (join-grids source destination #:optional (x 0) (y 0))
65 (cond ((null? source) destination)
66 ((null? destination) '())
67 ((> y 0) (cons (car destination)
68 (join-grids source (cdr destination) x (- y 1))))
69 (else (cons (join-rows (car source) (car destination) x)
70 (join-grids (cdr source) (cdr destination) x y)))))
72 (define* (collide-rows row1 row2 #:optional (offset 0))
73 (cond ((or (null? row1) (null? row2)) #f)
74 ((> offset 0) (collide-rows row1 (cdr row2) (- offset 1)))
75 (else (or (and (car row1) (car row2)) (collide-rows (cdr row1) (cdr row2))))))
77 (define* (collide-grids grid1 grid2 #:optional (x 0) (y 0))
78 (cond ((or (null? grid1) (null? grid2)) #f)
79 ((> y 0) (collide-grids grid1 (cdr grid2) x (- y 1)))
80 (else (or (collide-rows (car grid1) (car grid2) x)
81 (collide-grids (cdr grid1) (cdr grid2) x y)))))
83 (define (rotate-tetramine grid)
84 (define (rot grid res)
85 (cond ((null? grid) res)
86 (else (rot (cdr grid) (map cons (car grid) res)))))
87 (rot grid (make-list (length (car grid)))))
89 (define (row-completed row)
90 (cond ((null? row) #t)
91 (else (and (car row) (row-completed (cdr row))))))
93 (define (remove-rows-completed grid)
94 (let ((res (filter (lambda (x) (not (row-completed x))) grid)))
97 (else (fill (cons (make-list 14 #f) grid) (- n 1)))))
98 (inc-points (- (length grid) (length res)))
99 (fill res (- 20 (length res)))))
101 (define get-points #f)
102 (define get-lines #f)
103 (define inc-points #f)
105 (let ((points 0) (lines 0))
116 (define (more-lines-better n)
118 (else (+ n (more-lines-better (- n 1))))))
119 (set! points (+ points (* (more-lines-better l) 10)))
120 (set! lines (+ lines l)))))
123 (define display-game-over #f)
124 (define tetramine #f)
126 (let ((current-tetramine (random-tetramine)) (x 6) (y 0)
127 (next (random-tetramine))
129 (grid (make-list 20 (make-list 14 #f)))
130 (background (load-texture "fondo_tetris.png"))
131 (font (load-font "lazy.ttf" #:size 20))
136 (if game-over (display-game-over) (tetramine))))
138 (set! display-game-over
141 (render-text "Game Over" font #:size 50)))
145 (cond ((eq? (get-state timer) 'stopped) (start-timer timer)))
148 (cond ((not (collide-grids current-tetramine grid (+ x 1) y))
151 (cond ((not (collide-grids current-tetramine grid (- x 1) y))
153 (cond ((< x 0) (set! x 0))
154 ((> (+ x (length (car current-tetramine))) 14) (set! x (- 14 (length (car current-tetramine))))))
156 (cond ((key-pressed? 'up)
157 (let ((t1 (rotate-tetramine current-tetramine)))
158 (cond ((not (collide-grids t1 grid x y))
159 (set! current-tetramine t1))))))
161 (cond ((or (key? 'down) (> (get-time timer) 5000))
162 (cond ((or (collide-grids current-tetramine grid x (+ y 1))
163 (> (+ y 1 (length current-tetramine)) 20))
164 (set! grid (remove-rows-completed (join-grids current-tetramine grid x y)))
165 (set! current-tetramine next)
168 (cond ((collide-grids current-tetramine grid x y) (set! game-over #t)))
169 (set! next (random-tetramine)))
172 (start-timer timer)))))
173 (draw-texture background)
175 (draw-grid (join-grids current-tetramine grid x y))
179 (render-text (format #f "Points: ~a" (get-points)) font)
181 (render-text (format #f "Lines: ~a" (get-lines)) font))))
183 (define (run-gacela-tetris)
184 (let ((frame 0.0) (fps (make-timer)) (update (make-timer)))
189 (set! frame (+ frame 1))
190 (cond ((> (get-time update) 1000)
191 (display (/ frame (/ (get-time fps) 1000.0)))
193 (start-timer update))))))