Commit | Line | Data |
---|---|---|
6e44da43 | 1 | ;;; tetris.el --- implementation of Tetris for Emacs |
3d4ae13e | 2 | |
acaf905b | 3 | ;; Copyright (C) 1997, 2001-2012 Free Software Foundation, Inc. |
3d4ae13e RS |
4 | |
5 | ;; Author: Glynn Clements <glynn@sensei.co.uk> | |
6 | ;; Version: 2.01 | |
7 | ;; Created: 1997-08-13 | |
8 | ;; Keywords: games | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
b1fc2b50 | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
3d4ae13e | 13 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
3d4ae13e RS |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
3d4ae13e RS |
24 | |
25 | ;;; Commentary: | |
26 | ||
6e44da43 PJ |
27 | ;;; Code: |
28 | ||
3d4ae13e RS |
29 | (eval-when-compile |
30 | (require 'cl)) | |
31 | ||
32 | (require 'gamegrid) | |
33 | ||
34 | ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
35 | ||
67ec1c1a | 36 | (defgroup tetris nil |
fcc93746 | 37 | "Play a game of Tetris." |
67ec1c1a RS |
38 | :prefix "tetris-" |
39 | :group 'games) | |
40 | ||
41 | (defcustom tetris-use-glyphs t | |
67d110f1 | 42 | "Non-nil means use glyphs when available." |
67ec1c1a RS |
43 | :group 'tetris |
44 | :type 'boolean) | |
45 | ||
46 | (defcustom tetris-use-color t | |
67d110f1 | 47 | "Non-nil means use color when available." |
67ec1c1a RS |
48 | :group 'tetris |
49 | :type 'boolean) | |
50 | ||
51 | (defcustom tetris-draw-border-with-glyphs t | |
67d110f1 | 52 | "Non-nil means draw a border even when using glyphs." |
67ec1c1a RS |
53 | :group 'tetris |
54 | :type 'boolean) | |
55 | ||
56 | (defcustom tetris-default-tick-period 0.3 | |
67d110f1 | 57 | "The default time taken for a shape to drop one row." |
67ec1c1a RS |
58 | :group 'tetris |
59 | :type 'number) | |
60 | ||
61 | (defcustom tetris-update-speed-function | |
3d4ae13e | 62 | 'tetris-default-update-speed-function |
fcc93746 | 63 | "Function run whenever the Tetris score changes. |
3d4ae13e | 64 | Called with two arguments: (SHAPES ROWS) |
fcc93746 JB |
65 | SHAPES is the number of shapes which have been dropped. |
66 | ROWS is the number of rows which have been completed. | |
3d4ae13e | 67 | |
67ec1c1a RS |
68 | If the return value is a number, it is used as the timer period." |
69 | :group 'tetris | |
70 | :type 'function) | |
3d4ae13e | 71 | |
67ec1c1a RS |
72 | (defcustom tetris-mode-hook nil |
73 | "Hook run upon starting Tetris." | |
74 | :group 'tetris | |
75 | :type 'hook) | |
3d4ae13e | 76 | |
67ec1c1a | 77 | (defcustom tetris-tty-colors |
195e19e4 LH |
78 | ["blue" "white" "yellow" "magenta" "cyan" "green" "red"] |
79 | "Vector of colors of the various shapes in text mode." | |
67ec1c1a RS |
80 | :group 'tetris |
81 | :type (let ((names `("Shape 1" "Shape 2" "Shape 3" | |
82 | "Shape 4" "Shape 5" "Shape 6" "Shape 7")) | |
195e19e4 | 83 | (result nil)) |
67ec1c1a | 84 | (while names |
adcce7d5 SS |
85 | (add-to-list 'result |
86 | (cons 'choice | |
87 | (cons :tag | |
88 | (cons (car names) | |
67ec1c1a RS |
89 | (mapcar (lambda (color) |
90 | (list 'const color)) | |
91 | (defined-colors))))) | |
92 | t) | |
93 | (setq names (cdr names))) | |
94 | result)) | |
95 | ||
96 | (defcustom tetris-x-colors | |
195e19e4 LH |
97 | [[0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]] |
98 | "Vector of colors of the various shapes." | |
67ec1c1a RS |
99 | :group 'tetris |
100 | :type 'sexp) | |
101 | ||
102 | (defcustom tetris-buffer-name "*Tetris*" | |
103 | "Name used for Tetris buffer." | |
104 | :group 'tetris | |
105 | :type 'string) | |
106 | ||
107 | (defcustom tetris-buffer-width 30 | |
108 | "Width of used portion of buffer." | |
109 | :group 'tetris | |
110 | :type 'number) | |
111 | ||
112 | (defcustom tetris-buffer-height 22 | |
113 | "Height of used portion of buffer." | |
114 | :group 'tetris | |
115 | :type 'number) | |
116 | ||
117 | (defcustom tetris-width 10 | |
118 | "Width of playing area." | |
119 | :group 'tetris | |
120 | :type 'number) | |
121 | ||
122 | (defcustom tetris-height 20 | |
123 | "Height of playing area." | |
124 | :group 'tetris | |
125 | :type 'number) | |
126 | ||
127 | (defcustom tetris-top-left-x 3 | |
128 | "X position of top left of playing area." | |
129 | :group 'tetris | |
130 | :type 'number) | |
131 | ||
132 | (defcustom tetris-top-left-y 1 | |
133 | "Y position of top left of playing area." | |
134 | :group 'tetris | |
135 | :type 'number) | |
3d4ae13e RS |
136 | |
137 | (defvar tetris-next-x (+ (* 2 tetris-top-left-x) tetris-width) | |
138 | "X position of next shape.") | |
139 | ||
140 | (defvar tetris-next-y tetris-top-left-y | |
141 | "Y position of next shape.") | |
142 | ||
143 | (defvar tetris-score-x tetris-next-x | |
144 | "X position of score.") | |
145 | ||
146 | (defvar tetris-score-y (+ tetris-next-y 6) | |
147 | "Y position of score.") | |
148 | ||
9caf26fe | 149 | ;; It is not safe to put this in /tmp. |
adcce7d5 | 150 | ;; Someone could make a symlink in /tmp |
9caf26fe | 151 | ;; pointing to a file you don't want to clobber. |
76f1b321 | 152 | (defvar tetris-score-file "tetris-scores" |
3d4ae13e RS |
153 | ;; anybody with a well-connected server want to host this? |
154 | ;(defvar tetris-score-file "/anonymous@ftp.pgt.com:/pub/cgw/tetris-scores" | |
155 | "File for holding high scores.") | |
156 | ||
157 | ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
158 | ||
3d4ae13e RS |
159 | (defvar tetris-blank-options |
160 | '(((glyph colorize) | |
161 | (t ?\040)) | |
162 | ((color-x color-x) | |
163 | (mono-x grid-x) | |
ba8cb9c5 | 164 | (color-tty color-tty)) |
3d4ae13e | 165 | (((glyph color-x) [0 0 0]) |
ba8cb9c5 | 166 | (color-tty "black")))) |
3d4ae13e RS |
167 | |
168 | (defvar tetris-cell-options | |
169 | '(((glyph colorize) | |
170 | (emacs-tty ?O) | |
171 | (t ?\040)) | |
172 | ((color-x color-x) | |
173 | (mono-x mono-x) | |
174 | (color-tty color-tty) | |
ba8cb9c5 | 175 | (mono-tty mono-tty)) |
3d4ae13e RS |
176 | ;; color information is taken from tetris-x-colors and tetris-tty-colors |
177 | )) | |
178 | ||
ba8cb9c5 FP |
179 | (defvar tetris-border-options |
180 | '(((glyph colorize) | |
181 | (t ?\+)) | |
182 | ((color-x color-x) | |
183 | (mono-x grid-x) | |
184 | (color-tty color-tty)) | |
185 | (((glyph color-x) [0.5 0.5 0.5]) | |
186 | (color-tty "white")))) | |
187 | ||
3d4ae13e RS |
188 | (defvar tetris-space-options |
189 | '(((t ?\040)) | |
190 | nil | |
191 | nil)) | |
192 | ||
193 | ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
194 | ||
195 | (defconst tetris-shapes | |
121656e9 JB |
196 | [[[[0 0] [1 0] [0 1] [1 1]]] |
197 | ||
198 | [[[0 0] [1 0] [2 0] [2 1]] | |
199 | [[1 -1] [1 0] [1 1] [0 1]] | |
200 | [[0 -1] [0 0] [1 0] [2 0]] | |
201 | [[1 -1] [2 -1] [1 0] [1 1]]] | |
202 | ||
203 | [[[0 0] [1 0] [2 0] [0 1]] | |
204 | [[0 -1] [1 -1] [1 0] [1 1]] | |
205 | [[2 -1] [0 0] [1 0] [2 0]] | |
206 | [[1 -1] [1 0] [1 1] [2 1]]] | |
207 | ||
208 | [[[0 0] [1 0] [1 1] [2 1]] | |
195e19e4 | 209 | [[1 0] [0 1] [1 1] [0 2]]] |
121656e9 JB |
210 | |
211 | [[[1 0] [2 0] [0 1] [1 1]] | |
212 | [[0 0] [0 1] [1 1] [1 2]]] | |
213 | ||
214 | [[[1 0] [0 1] [1 1] [2 1]] | |
215 | [[1 0] [1 1] [2 1] [1 2]] | |
216 | [[0 1] [1 1] [2 1] [1 2]] | |
195e19e4 | 217 | [[1 0] [0 1] [1 1] [1 2]]] |
121656e9 | 218 | |
195e19e4 LH |
219 | [[[0 0] [1 0] [2 0] [3 0]] |
220 | [[1 -1] [1 0] [1 1] [1 2]]]] | |
121656e9 | 221 | "Each shape is described by a vector that contains the coordinates of |
195e19e4 | 222 | each one of its four blocks.") |
3d4ae13e | 223 | |
adcce7d5 | 224 | ;;the scoring rules were taken from "xtetris". Blocks score differently |
3d4ae13e RS |
225 | ;;depending on their rotation |
226 | ||
adcce7d5 | 227 | (defconst tetris-shape-scores |
195e19e4 | 228 | [[6] [6 7 6 7] [6 7 6 7] [6 7] [6 7] [5 5 6 5] [5 8]] ) |
3d4ae13e RS |
229 | |
230 | (defconst tetris-shape-dimensions | |
231 | [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]]) | |
232 | ||
195e19e4 | 233 | (defconst tetris-blank 7) |
3d4ae13e RS |
234 | |
235 | (defconst tetris-border 8) | |
236 | ||
237 | (defconst tetris-space 9) | |
238 | ||
121656e9 | 239 | (defun tetris-default-update-speed-function (_shapes rows) |
3d4ae13e RS |
240 | (/ 20.0 (+ 50.0 rows))) |
241 | ||
242 | ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
243 | ||
244 | (defvar tetris-shape 0) | |
245 | (defvar tetris-rot 0) | |
246 | (defvar tetris-next-shape 0) | |
247 | (defvar tetris-n-shapes 0) | |
248 | (defvar tetris-n-rows 0) | |
249 | (defvar tetris-score 0) | |
250 | (defvar tetris-pos-x 0) | |
251 | (defvar tetris-pos-y 0) | |
252 | (defvar tetris-paused nil) | |
253 | ||
254 | (make-variable-buffer-local 'tetris-shape) | |
255 | (make-variable-buffer-local 'tetris-rot) | |
256 | (make-variable-buffer-local 'tetris-next-shape) | |
257 | (make-variable-buffer-local 'tetris-n-shapes) | |
258 | (make-variable-buffer-local 'tetris-n-rows) | |
259 | (make-variable-buffer-local 'tetris-score) | |
260 | (make-variable-buffer-local 'tetris-pos-x) | |
261 | (make-variable-buffer-local 'tetris-pos-y) | |
262 | (make-variable-buffer-local 'tetris-paused) | |
263 | ||
264 | ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
265 | ||
266 | (defvar tetris-mode-map | |
fcc93746 JB |
267 | (let ((map (make-sparse-keymap 'tetris-mode-map))) |
268 | (define-key map "n" 'tetris-start-game) | |
269 | (define-key map "q" 'tetris-end-game) | |
270 | (define-key map "p" 'tetris-pause-game) | |
271 | ||
272 | (define-key map " " 'tetris-move-bottom) | |
273 | (define-key map [left] 'tetris-move-left) | |
274 | (define-key map [right] 'tetris-move-right) | |
275 | (define-key map [up] 'tetris-rotate-prev) | |
276 | (define-key map [down] 'tetris-rotate-next) | |
277 | map)) | |
3d4ae13e RS |
278 | |
279 | (defvar tetris-null-map | |
fcc93746 JB |
280 | (let ((map (make-sparse-keymap 'tetris-null-map))) |
281 | (define-key map "n" 'tetris-start-game) | |
282 | map)) | |
3d4ae13e RS |
283 | |
284 | ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
285 | ||
286 | (defun tetris-display-options () | |
287 | (let ((options (make-vector 256 nil))) | |
288 | (loop for c from 0 to 255 do | |
289 | (aset options c | |
290 | (cond ((= c tetris-blank) | |
291 | tetris-blank-options) | |
195e19e4 | 292 | ((and (>= c 0) (<= c 6)) |
3d4ae13e RS |
293 | (append |
294 | tetris-cell-options | |
295 | `((((glyph color-x) ,(aref tetris-x-colors c)) | |
296 | (color-tty ,(aref tetris-tty-colors c)) | |
297 | (t nil))))) | |
298 | ((= c tetris-border) | |
299 | tetris-border-options) | |
300 | ((= c tetris-space) | |
301 | tetris-space-options) | |
302 | (t | |
303 | '(nil nil nil))))) | |
304 | options)) | |
305 | ||
306 | (defun tetris-get-tick-period () | |
307 | (if (boundp 'tetris-update-speed-function) | |
308 | (let ((period (apply tetris-update-speed-function | |
309 | tetris-n-shapes | |
310 | tetris-n-rows nil))) | |
311 | (and (numberp period) period)))) | |
312 | ||
195e19e4 LH |
313 | (defun tetris-get-shape-cell (block) |
314 | (aref (aref (aref tetris-shapes | |
315 | tetris-shape) tetris-rot) | |
316 | block)) | |
3d4ae13e RS |
317 | |
318 | (defun tetris-shape-width () | |
195e19e4 | 319 | (aref (aref tetris-shape-dimensions tetris-shape) 0)) |
3d4ae13e | 320 | |
195e19e4 LH |
321 | (defun tetris-shape-rotations () |
322 | (length (aref tetris-shapes tetris-shape))) | |
3d4ae13e RS |
323 | |
324 | (defun tetris-draw-score () | |
325 | (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes) | |
326 | (format "Rows: %05d" tetris-n-rows) | |
327 | (format "Score: %05d" tetris-score)))) | |
328 | (loop for y from 0 to 2 do | |
329 | (let* ((string (aref strings y)) | |
330 | (len (length string))) | |
331 | (loop for x from 0 to (1- len) do | |
332 | (gamegrid-set-cell (+ tetris-score-x x) | |
333 | (+ tetris-score-y y) | |
334 | (aref string x))))))) | |
335 | ||
336 | (defun tetris-update-score () | |
337 | (tetris-draw-score) | |
338 | (let ((period (tetris-get-tick-period))) | |
339 | (if period (gamegrid-set-timer period)))) | |
340 | ||
341 | (defun tetris-new-shape () | |
342 | (setq tetris-shape tetris-next-shape) | |
343 | (setq tetris-rot 0) | |
344 | (setq tetris-next-shape (random 7)) | |
345 | (setq tetris-pos-x (/ (- tetris-width (tetris-shape-width)) 2)) | |
346 | (setq tetris-pos-y 0) | |
347 | (if (tetris-test-shape) | |
348 | (tetris-end-game) | |
d46b9408 CY |
349 | (tetris-draw-shape) |
350 | (tetris-draw-next-shape) | |
351 | (tetris-update-score))) | |
3d4ae13e RS |
352 | |
353 | (defun tetris-draw-next-shape () | |
195e19e4 LH |
354 | (loop for x from 0 to 3 do |
355 | (loop for y from 0 to 3 do | |
356 | (gamegrid-set-cell (+ tetris-next-x x) | |
357 | (+ tetris-next-y y) | |
358 | tetris-blank))) | |
359 | (loop for i from 0 to 3 do | |
360 | (let ((tetris-shape tetris-next-shape) | |
361 | (tetris-rot 0)) | |
362 | (gamegrid-set-cell (+ tetris-next-x | |
363 | (aref (tetris-get-shape-cell i) 0)) | |
364 | (+ tetris-next-y | |
365 | (aref (tetris-get-shape-cell i) 1)) | |
366 | tetris-shape)))) | |
3d4ae13e RS |
367 | |
368 | (defun tetris-draw-shape () | |
195e19e4 LH |
369 | (loop for i from 0 to 3 do |
370 | (let ((c (tetris-get-shape-cell i))) | |
371 | (gamegrid-set-cell (+ tetris-top-left-x | |
372 | tetris-pos-x | |
373 | (aref c 0)) | |
374 | (+ tetris-top-left-y | |
375 | tetris-pos-y | |
376 | (aref c 1)) | |
377 | tetris-shape)))) | |
3d4ae13e RS |
378 | |
379 | (defun tetris-erase-shape () | |
195e19e4 LH |
380 | (loop for i from 0 to 3 do |
381 | (let ((c (tetris-get-shape-cell i))) | |
382 | (gamegrid-set-cell (+ tetris-top-left-x | |
121656e9 | 383 | tetris-pos-x |
195e19e4 LH |
384 | (aref c 0)) |
385 | (+ tetris-top-left-y | |
121656e9 | 386 | tetris-pos-y |
195e19e4 LH |
387 | (aref c 1)) |
388 | tetris-blank)))) | |
3d4ae13e RS |
389 | |
390 | (defun tetris-test-shape () | |
391 | (let ((hit nil)) | |
195e19e4 LH |
392 | (loop for i from 0 to 3 do |
393 | (unless hit | |
394 | (setq hit | |
395 | (let* ((c (tetris-get-shape-cell i)) | |
121656e9 | 396 | (xx (+ tetris-pos-x |
195e19e4 | 397 | (aref c 0))) |
121656e9 | 398 | (yy (+ tetris-pos-y |
195e19e4 LH |
399 | (aref c 1)))) |
400 | (or (>= xx tetris-width) | |
401 | (>= yy tetris-height) | |
121656e9 JB |
402 | (/= (gamegrid-get-cell |
403 | (+ xx tetris-top-left-x) | |
195e19e4 LH |
404 | (+ yy tetris-top-left-y)) |
405 | tetris-blank)))))) | |
3d4ae13e RS |
406 | hit)) |
407 | ||
408 | (defun tetris-full-row (y) | |
409 | (let ((full t)) | |
410 | (loop for x from 0 to (1- tetris-width) do | |
411 | (if (= (gamegrid-get-cell (+ tetris-top-left-x x) | |
412 | (+ tetris-top-left-y y)) | |
413 | tetris-blank) | |
414 | (setq full nil))) | |
415 | full)) | |
416 | ||
417 | (defun tetris-shift-row (y) | |
418 | (if (= y 0) | |
419 | (loop for x from 0 to (1- tetris-width) do | |
420 | (gamegrid-set-cell (+ tetris-top-left-x x) | |
421 | (+ tetris-top-left-y y) | |
422 | tetris-blank)) | |
423 | (loop for x from 0 to (1- tetris-width) do | |
424 | (let ((c (gamegrid-get-cell (+ tetris-top-left-x x) | |
425 | (+ tetris-top-left-y y -1)))) | |
426 | (gamegrid-set-cell (+ tetris-top-left-x x) | |
427 | (+ tetris-top-left-y y) | |
428 | c))))) | |
429 | ||
430 | (defun tetris-shift-down () | |
431 | (loop for y0 from 0 to (1- tetris-height) do | |
432 | (if (tetris-full-row y0) | |
433 | (progn (setq tetris-n-rows (1+ tetris-n-rows)) | |
434 | (loop for y from y0 downto 0 do | |
435 | (tetris-shift-row y)))))) | |
436 | ||
437 | (defun tetris-draw-border-p () | |
438 | (or (not (eq gamegrid-display-mode 'glyph)) | |
439 | tetris-draw-border-with-glyphs)) | |
440 | ||
441 | (defun tetris-init-buffer () | |
442 | (gamegrid-init-buffer tetris-buffer-width | |
443 | tetris-buffer-height | |
444 | tetris-space) | |
445 | (let ((buffer-read-only nil)) | |
446 | (if (tetris-draw-border-p) | |
447 | (loop for y from -1 to tetris-height do | |
448 | (loop for x from -1 to tetris-width do | |
449 | (gamegrid-set-cell (+ tetris-top-left-x x) | |
450 | (+ tetris-top-left-y y) | |
451 | tetris-border)))) | |
452 | (loop for y from 0 to (1- tetris-height) do | |
453 | (loop for x from 0 to (1- tetris-width) do | |
454 | (gamegrid-set-cell (+ tetris-top-left-x x) | |
455 | (+ tetris-top-left-y y) | |
456 | tetris-blank))) | |
457 | (if (tetris-draw-border-p) | |
458 | (loop for y from -1 to 4 do | |
459 | (loop for x from -1 to 4 do | |
460 | (gamegrid-set-cell (+ tetris-next-x x) | |
461 | (+ tetris-next-y y) | |
462 | tetris-border)))))) | |
463 | ||
464 | (defun tetris-reset-game () | |
465 | (gamegrid-kill-timer) | |
466 | (tetris-init-buffer) | |
467 | (setq tetris-next-shape (random 7)) | |
468 | (setq tetris-shape 0 | |
469 | tetris-rot 0 | |
470 | tetris-pos-x 0 | |
471 | tetris-pos-y 0 | |
472 | tetris-n-shapes 0 | |
473 | tetris-n-rows 0 | |
474 | tetris-score 0 | |
475 | tetris-paused nil) | |
476 | (tetris-new-shape)) | |
477 | ||
478 | (defun tetris-shape-done () | |
479 | (tetris-shift-down) | |
480 | (setq tetris-n-shapes (1+ tetris-n-shapes)) | |
481 | (setq tetris-score | |
adcce7d5 | 482 | (+ tetris-score |
3d4ae13e RS |
483 | (aref (aref tetris-shape-scores tetris-shape) tetris-rot))) |
484 | (tetris-update-score) | |
485 | (tetris-new-shape)) | |
486 | ||
487 | (defun tetris-update-game (tetris-buffer) | |
488 | "Called on each clock tick. | |
489 | Drops the shape one square, testing for collision." | |
490 | (if (and (not tetris-paused) | |
491 | (eq (current-buffer) tetris-buffer)) | |
492 | (let (hit) | |
493 | (tetris-erase-shape) | |
494 | (setq tetris-pos-y (1+ tetris-pos-y)) | |
495 | (setq hit (tetris-test-shape)) | |
496 | (if hit | |
497 | (setq tetris-pos-y (1- tetris-pos-y))) | |
498 | (tetris-draw-shape) | |
499 | (if hit | |
500 | (tetris-shape-done))))) | |
501 | ||
502 | (defun tetris-move-bottom () | |
fcc93746 | 503 | "Drop the shape to the bottom of the playing area." |
3d4ae13e | 504 | (interactive) |
195e19e4 LH |
505 | (unless tetris-paused |
506 | (let ((hit nil)) | |
507 | (tetris-erase-shape) | |
508 | (while (not hit) | |
509 | (setq tetris-pos-y (1+ tetris-pos-y)) | |
510 | (setq hit (tetris-test-shape))) | |
511 | (setq tetris-pos-y (1- tetris-pos-y)) | |
512 | (tetris-draw-shape) | |
513 | (tetris-shape-done)))) | |
3d4ae13e RS |
514 | |
515 | (defun tetris-move-left () | |
fcc93746 | 516 | "Move the shape one square to the left." |
3d4ae13e | 517 | (interactive) |
195e19e4 | 518 | (unless tetris-paused |
3d4ae13e RS |
519 | (tetris-erase-shape) |
520 | (setq tetris-pos-x (1- tetris-pos-x)) | |
521 | (if (tetris-test-shape) | |
195e19e4 | 522 | (setq tetris-pos-x (1+ tetris-pos-x))) |
3d4ae13e RS |
523 | (tetris-draw-shape))) |
524 | ||
525 | (defun tetris-move-right () | |
fcc93746 | 526 | "Move the shape one square to the right." |
3d4ae13e | 527 | (interactive) |
195e19e4 | 528 | (unless tetris-paused |
3d4ae13e RS |
529 | (tetris-erase-shape) |
530 | (setq tetris-pos-x (1+ tetris-pos-x)) | |
531 | (if (tetris-test-shape) | |
532 | (setq tetris-pos-x (1- tetris-pos-x))) | |
533 | (tetris-draw-shape))) | |
534 | ||
535 | (defun tetris-rotate-prev () | |
fcc93746 | 536 | "Rotate the shape clockwise." |
3d4ae13e | 537 | (interactive) |
195e19e4 LH |
538 | (unless tetris-paused |
539 | (tetris-erase-shape) | |
121656e9 | 540 | (setq tetris-rot (% (+ 1 tetris-rot) |
195e19e4 LH |
541 | (tetris-shape-rotations))) |
542 | (if (tetris-test-shape) | |
121656e9 | 543 | (setq tetris-rot (% (+ 3 tetris-rot) |
195e19e4 LH |
544 | (tetris-shape-rotations)))) |
545 | (tetris-draw-shape))) | |
3d4ae13e RS |
546 | |
547 | (defun tetris-rotate-next () | |
fcc93746 | 548 | "Rotate the shape anticlockwise." |
3d4ae13e | 549 | (interactive) |
195e19e4 | 550 | (unless tetris-paused |
297a6dca | 551 | (tetris-erase-shape) |
195e19e4 LH |
552 | (setq tetris-rot (% (+ 3 tetris-rot) |
553 | (tetris-shape-rotations))) | |
297a6dca | 554 | (if (tetris-test-shape) |
195e19e4 LH |
555 | (setq tetris-rot (% (+ 1 tetris-rot) |
556 | (tetris-shape-rotations)))) | |
557 | (tetris-draw-shape))) | |
3d4ae13e RS |
558 | |
559 | (defun tetris-end-game () | |
fcc93746 | 560 | "Terminate the current game." |
3d4ae13e RS |
561 | (interactive) |
562 | (gamegrid-kill-timer) | |
563 | (use-local-map tetris-null-map) | |
564 | (gamegrid-add-score tetris-score-file tetris-score)) | |
565 | ||
566 | (defun tetris-start-game () | |
fcc93746 | 567 | "Start a new game of Tetris." |
3d4ae13e RS |
568 | (interactive) |
569 | (tetris-reset-game) | |
570 | (use-local-map tetris-mode-map) | |
571 | (let ((period (or (tetris-get-tick-period) | |
572 | tetris-default-tick-period))) | |
573 | (gamegrid-start-timer period 'tetris-update-game))) | |
574 | ||
575 | (defun tetris-pause-game () | |
fcc93746 | 576 | "Pause (or resume) the current game." |
3d4ae13e RS |
577 | (interactive) |
578 | (setq tetris-paused (not tetris-paused)) | |
579 | (message (and tetris-paused "Game paused (press p to resume)"))) | |
580 | ||
581 | (defun tetris-active-p () | |
582 | (eq (current-local-map) tetris-mode-map)) | |
583 | ||
584 | (put 'tetris-mode 'mode-class 'special) | |
585 | ||
fcc93746 JB |
586 | (define-derived-mode tetris-mode nil "Tetris" |
587 | "A mode for playing Tetris." | |
3d4ae13e | 588 | |
3d4ae13e RS |
589 | (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t) |
590 | ||
591 | (use-local-map tetris-null-map) | |
592 | ||
2b632b16 RS |
593 | (unless (featurep 'emacs) |
594 | (setq mode-popup-menu | |
595 | '("Tetris Commands" | |
596 | ["Start new game" tetris-start-game] | |
597 | ["End game" tetris-end-game | |
598 | (tetris-active-p)] | |
599 | ["Pause" tetris-pause-game | |
600 | (and (tetris-active-p) (not tetris-paused))] | |
601 | ["Resume" tetris-pause-game | |
602 | (and (tetris-active-p) tetris-paused)]))) | |
3d4ae13e | 603 | |
fcc93746 JB |
604 | (setq show-trailing-whitespace nil) |
605 | ||
3d4ae13e RS |
606 | (setq gamegrid-use-glyphs tetris-use-glyphs) |
607 | (setq gamegrid-use-color tetris-use-color) | |
608 | ||
fcc93746 | 609 | (gamegrid-init (tetris-display-options))) |
3d4ae13e RS |
610 | |
611 | ;;;###autoload | |
612 | (defun tetris () | |
613 | "Play the Tetris game. | |
614 | Shapes drop from the top of the screen, and the user has to move and | |
615 | rotate the shape to fit in with those at the bottom of the screen so | |
616 | as to form complete rows. | |
617 | ||
618 | tetris-mode keybindings: | |
619 | \\<tetris-mode-map> | |
620 | \\[tetris-start-game] Starts a new game of Tetris | |
621 | \\[tetris-end-game] Terminates the current game | |
622 | \\[tetris-pause-game] Pauses (or resumes) the current game | |
623 | \\[tetris-move-left] Moves the shape one square to the left | |
624 | \\[tetris-move-right] Moves the shape one square to the right | |
625 | \\[tetris-rotate-prev] Rotates the shape clockwise | |
626 | \\[tetris-rotate-next] Rotates the shape anticlockwise | |
627 | \\[tetris-move-bottom] Drops the shape to the bottom of the playing area | |
628 | ||
629 | " | |
630 | (interactive) | |
631 | ||
fcc93746 JB |
632 | (select-window (or (get-buffer-window tetris-buffer-name) |
633 | (selected-window))) | |
3d4ae13e RS |
634 | (switch-to-buffer tetris-buffer-name) |
635 | (gamegrid-kill-timer) | |
636 | (tetris-mode) | |
637 | (tetris-start-game)) | |
638 | ||
d2fe6685 GM |
639 | (random t) |
640 | ||
3d4ae13e RS |
641 | (provide 'tetris) |
642 | ||
643 | ;;; tetris.el ends here |