Merge changes from emacs-23 branch
[emacs.git] / lisp / play / tetris.el
blob24d1c3f2417d27be1da08d1b94839a13b9ea8dd1
1 ;;; tetris.el --- implementation of Tetris for Emacs
3 ;; Copyright (C) 1997, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Glynn Clements <glynn@sensei.co.uk>
6 ;; Version: 2.01
7 ;; Created: 1997-08-13
8 ;; Keywords: games
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile
30 (require 'cl))
32 (require 'gamegrid)
34 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 (defgroup tetris nil
37 "Play a game of Tetris."
38 :prefix "tetris-"
39 :group 'games)
41 (defcustom tetris-use-glyphs t
42 "Non-nil means use glyphs when available."
43 :group 'tetris
44 :type 'boolean)
46 (defcustom tetris-use-color t
47 "Non-nil means use color when available."
48 :group 'tetris
49 :type 'boolean)
51 (defcustom tetris-draw-border-with-glyphs t
52 "Non-nil means draw a border even when using glyphs."
53 :group 'tetris
54 :type 'boolean)
56 (defcustom tetris-default-tick-period 0.3
57 "The default time taken for a shape to drop one row."
58 :group 'tetris
59 :type 'number)
61 (defcustom tetris-update-speed-function
62 'tetris-default-update-speed-function
63 "Function run whenever the Tetris score changes.
64 Called with two arguments: (SHAPES ROWS)
65 SHAPES is the number of shapes which have been dropped.
66 ROWS is the number of rows which have been completed.
68 If the return value is a number, it is used as the timer period."
69 :group 'tetris
70 :type 'function)
72 (defcustom tetris-mode-hook nil
73 "Hook run upon starting Tetris."
74 :group 'tetris
75 :type 'hook)
77 (defcustom tetris-tty-colors
78 ["blue" "white" "yellow" "magenta" "cyan" "green" "red"]
79 "Vector of colors of the various shapes in text mode."
80 :group 'tetris
81 :type (let ((names `("Shape 1" "Shape 2" "Shape 3"
82 "Shape 4" "Shape 5" "Shape 6" "Shape 7"))
83 (result nil))
84 (while names
85 (add-to-list 'result
86 (cons 'choice
87 (cons :tag
88 (cons (car names)
89 (mapcar (lambda (color)
90 (list 'const color))
91 (defined-colors)))))
93 (setq names (cdr names)))
94 result))
96 (defcustom tetris-x-colors
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."
99 :group 'tetris
100 :type 'sexp)
102 (defcustom tetris-buffer-name "*Tetris*"
103 "Name used for Tetris buffer."
104 :group 'tetris
105 :type 'string)
107 (defcustom tetris-buffer-width 30
108 "Width of used portion of buffer."
109 :group 'tetris
110 :type 'number)
112 (defcustom tetris-buffer-height 22
113 "Height of used portion of buffer."
114 :group 'tetris
115 :type 'number)
117 (defcustom tetris-width 10
118 "Width of playing area."
119 :group 'tetris
120 :type 'number)
122 (defcustom tetris-height 20
123 "Height of playing area."
124 :group 'tetris
125 :type 'number)
127 (defcustom tetris-top-left-x 3
128 "X position of top left of playing area."
129 :group 'tetris
130 :type 'number)
132 (defcustom tetris-top-left-y 1
133 "Y position of top left of playing area."
134 :group 'tetris
135 :type 'number)
137 (defvar tetris-next-x (+ (* 2 tetris-top-left-x) tetris-width)
138 "X position of next shape.")
140 (defvar tetris-next-y tetris-top-left-y
141 "Y position of next shape.")
143 (defvar tetris-score-x tetris-next-x
144 "X position of score.")
146 (defvar tetris-score-y (+ tetris-next-y 6)
147 "Y position of score.")
149 ;; It is not safe to put this in /tmp.
150 ;; Someone could make a symlink in /tmp
151 ;; pointing to a file you don't want to clobber.
152 (defvar tetris-score-file "tetris-scores"
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.")
157 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 (defvar tetris-blank-options
160 '(((glyph colorize)
161 (t ?\040))
162 ((color-x color-x)
163 (mono-x grid-x)
164 (color-tty color-tty))
165 (((glyph color-x) [0 0 0])
166 (color-tty "black"))))
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)
175 (mono-tty mono-tty))
176 ;; color information is taken from tetris-x-colors and tetris-tty-colors
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"))))
188 (defvar tetris-space-options
189 '(((t ?\040))
191 nil))
193 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 (defconst tetris-shapes
196 [[[[0 0] [1 0] [0 1] [1 1]]]
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]]]
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]]]
208 [[[0 0] [1 0] [1 1] [2 1]]
209 [[1 0] [0 1] [1 1] [0 2]]]
211 [[[1 0] [2 0] [0 1] [1 1]]
212 [[0 0] [0 1] [1 1] [1 2]]]
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]]
217 [[1 0] [0 1] [1 1] [1 2]]]
219 [[[0 0] [1 0] [2 0] [3 0]]
220 [[1 -1] [1 0] [1 1] [1 2]]]]
221 "Each shape is described by a vector that contains the coordinates of
222 each one of its four blocks.")
224 ;;the scoring rules were taken from "xtetris". Blocks score differently
225 ;;depending on their rotation
227 (defconst tetris-shape-scores
228 [[6] [6 7 6 7] [6 7 6 7] [6 7] [6 7] [5 5 6 5] [5 8]] )
230 (defconst tetris-shape-dimensions
231 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]])
233 (defconst tetris-blank 7)
235 (defconst tetris-border 8)
237 (defconst tetris-space 9)
239 (defun tetris-default-update-speed-function (_shapes rows)
240 (/ 20.0 (+ 50.0 rows)))
242 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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)
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)
264 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 (defvar tetris-mode-map
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)
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))
279 (defvar tetris-null-map
280 (let ((map (make-sparse-keymap 'tetris-null-map)))
281 (define-key map "n" 'tetris-start-game)
282 map))
284 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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)
292 ((and (>= c 0) (<= c 6))
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)
303 '(nil nil nil)))))
304 options))
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))))
313 (defun tetris-get-shape-cell (block)
314 (aref (aref (aref tetris-shapes
315 tetris-shape) tetris-rot)
316 block))
318 (defun tetris-shape-width ()
319 (aref (aref tetris-shape-dimensions tetris-shape) 0))
321 (defun tetris-shape-rotations ()
322 (length (aref tetris-shapes tetris-shape)))
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)))))))
336 (defun tetris-update-score ()
337 (tetris-draw-score)
338 (let ((period (tetris-get-tick-period)))
339 (if period (gamegrid-set-timer period))))
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)
349 (tetris-draw-shape)
350 (tetris-draw-next-shape)
351 (tetris-update-score)))
353 (defun tetris-draw-next-shape ()
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))))
368 (defun tetris-draw-shape ()
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))))
379 (defun tetris-erase-shape ()
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
383 tetris-pos-x
384 (aref c 0))
385 (+ tetris-top-left-y
386 tetris-pos-y
387 (aref c 1))
388 tetris-blank))))
390 (defun tetris-test-shape ()
391 (let ((hit nil))
392 (loop for i from 0 to 3 do
393 (unless hit
394 (setq hit
395 (let* ((c (tetris-get-shape-cell i))
396 (xx (+ tetris-pos-x
397 (aref c 0)))
398 (yy (+ tetris-pos-y
399 (aref c 1))))
400 (or (>= xx tetris-width)
401 (>= yy tetris-height)
402 (/= (gamegrid-get-cell
403 (+ xx tetris-top-left-x)
404 (+ yy tetris-top-left-y))
405 tetris-blank))))))
406 hit))
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))
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)))))
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))))))
437 (defun tetris-draw-border-p ()
438 (or (not (eq gamegrid-display-mode 'glyph))
439 tetris-draw-border-with-glyphs))
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))))))
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))
478 (defun tetris-shape-done ()
479 (tetris-shift-down)
480 (setq tetris-n-shapes (1+ tetris-n-shapes))
481 (setq tetris-score
482 (+ tetris-score
483 (aref (aref tetris-shape-scores tetris-shape) tetris-rot)))
484 (tetris-update-score)
485 (tetris-new-shape))
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)))))
502 (defun tetris-move-bottom ()
503 "Drop the shape to the bottom of the playing area."
504 (interactive)
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))))
515 (defun tetris-move-left ()
516 "Move the shape one square to the left."
517 (interactive)
518 (unless tetris-paused
519 (tetris-erase-shape)
520 (setq tetris-pos-x (1- tetris-pos-x))
521 (if (tetris-test-shape)
522 (setq tetris-pos-x (1+ tetris-pos-x)))
523 (tetris-draw-shape)))
525 (defun tetris-move-right ()
526 "Move the shape one square to the right."
527 (interactive)
528 (unless tetris-paused
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)))
535 (defun tetris-rotate-prev ()
536 "Rotate the shape clockwise."
537 (interactive)
538 (unless tetris-paused
539 (tetris-erase-shape)
540 (setq tetris-rot (% (+ 1 tetris-rot)
541 (tetris-shape-rotations)))
542 (if (tetris-test-shape)
543 (setq tetris-rot (% (+ 3 tetris-rot)
544 (tetris-shape-rotations))))
545 (tetris-draw-shape)))
547 (defun tetris-rotate-next ()
548 "Rotate the shape anticlockwise."
549 (interactive)
550 (unless tetris-paused
551 (tetris-erase-shape)
552 (setq tetris-rot (% (+ 3 tetris-rot)
553 (tetris-shape-rotations)))
554 (if (tetris-test-shape)
555 (setq tetris-rot (% (+ 1 tetris-rot)
556 (tetris-shape-rotations))))
557 (tetris-draw-shape)))
559 (defun tetris-end-game ()
560 "Terminate the current game."
561 (interactive)
562 (gamegrid-kill-timer)
563 (use-local-map tetris-null-map)
564 (gamegrid-add-score tetris-score-file tetris-score))
566 (defun tetris-start-game ()
567 "Start a new game of Tetris."
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)))
575 (defun tetris-pause-game ()
576 "Pause (or resume) the current game."
577 (interactive)
578 (setq tetris-paused (not tetris-paused))
579 (message (and tetris-paused "Game paused (press p to resume)")))
581 (defun tetris-active-p ()
582 (eq (current-local-map) tetris-mode-map))
584 (put 'tetris-mode 'mode-class 'special)
586 (define-derived-mode tetris-mode nil "Tetris"
587 "A mode for playing Tetris."
589 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
591 (use-local-map tetris-null-map)
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)])))
604 (setq show-trailing-whitespace nil)
606 (setq gamegrid-use-glyphs tetris-use-glyphs)
607 (setq gamegrid-use-color tetris-use-color)
609 (gamegrid-init (tetris-display-options)))
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.
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
630 (interactive)
632 (select-window (or (get-buffer-window tetris-buffer-name)
633 (selected-window)))
634 (switch-to-buffer tetris-buffer-name)
635 (gamegrid-kill-timer)
636 (tetris-mode)
637 (tetris-start-game))
639 (random t)
641 (provide 'tetris)
643 ;;; tetris.el ends here