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>
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/>.
34 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 "Play a game of Tetris."
41 (defcustom tetris-use-glyphs t
42 "Non-nil means use glyphs when available."
46 (defcustom tetris-use-color t
47 "Non-nil means use color when available."
51 (defcustom tetris-draw-border-with-glyphs t
52 "Non-nil means draw a border even when using glyphs."
56 (defcustom tetris-default-tick-period
0.3
57 "The default time taken for a shape to drop one row."
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."
72 (defcustom tetris-mode-hook nil
73 "Hook run upon starting Tetris."
77 (defcustom tetris-tty-colors
78 ["blue" "white" "yellow" "magenta" "cyan" "green" "red"]
79 "Vector of colors of the various shapes in text mode."
81 :type
(let ((names `("Shape 1" "Shape 2" "Shape 3"
82 "Shape 4" "Shape 5" "Shape 6" "Shape 7"))
89 (mapcar (lambda (color)
93 (setq names
(cdr names
)))
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."
102 (defcustom tetris-buffer-name
"*Tetris*"
103 "Name used for Tetris buffer."
107 (defcustom tetris-buffer-width
30
108 "Width of used portion of buffer."
112 (defcustom tetris-buffer-height
22
113 "Height of used portion of buffer."
117 (defcustom tetris-width
10
118 "Width of playing area."
122 (defcustom tetris-height
20
123 "Height of playing area."
127 (defcustom tetris-top-left-x
3
128 "X position of top left of playing area."
132 (defcustom tetris-top-left-y
1
133 "Y position of top left of playing area."
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
164 (color-tty color-tty
))
165 (((glyph color-x
) [0 0 0])
166 (color-tty "black"))))
168 (defvar tetris-cell-options
174 (color-tty color-tty
)
176 ;; color information is taken from tetris-x-colors and tetris-tty-colors
179 (defvar tetris-border-options
184 (color-tty color-tty
))
185 (((glyph color-x
) [0.5 0.5 0.5])
186 (color-tty "white"))))
188 (defvar tetris-space-options
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)
279 (defvar tetris-null-map
280 (let ((map (make-sparse-keymap 'tetris-null-map)))
281 (define-key map "n" 'tetris-start-game)
284 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 (defun tetris-display-options ()
287 (let ((options (make-vector 256 nil)))
288 (loop for c from 0 to 255 do
290 (cond ((= c tetris-blank)
291 tetris-blank-options)
292 ((and (>= c 0) (<= c 6))
295 `((((glyph color-x) ,(aref tetris-x-colors c))
296 (color-tty ,(aref tetris-tty-colors c))
299 tetris-border-options)
301 tetris-space-options)
306 (defun tetris-get-tick-period ()
307 (if (boundp 'tetris-update-speed-function)
308 (let ((period (apply tetris-update-speed-function
311 (and (numberp period) period))))
313 (defun tetris-get-shape-cell (block)
314 (aref (aref (aref tetris-shapes
315 tetris-shape) tetris-rot)
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)
334 (aref string x)))))))
336 (defun tetris-update-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)
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)
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)
359 (loop for i from 0 to 3 do
360 (let ((tetris-shape tetris-next-shape)
362 (gamegrid-set-cell (+ tetris-next-x
363 (aref (tetris-get-shape-cell i) 0))
365 (aref (tetris-get-shape-cell i) 1))
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
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
390 (defun tetris-test-shape ()
392 (loop for i from 0 to 3 do
395 (let* ((c (tetris-get-shape-cell i))
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))
408 (defun tetris-full-row (y)
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))
417 (defun tetris-shift-row (y)
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)
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)
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
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)
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)
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)
464 (defun tetris-reset-game ()
465 (gamegrid-kill-timer)
467 (setq tetris-next-shape (random 7))
478 (defun tetris-shape-done ()
480 (setq tetris-n-shapes (1+ tetris-n-shapes))
483 (aref (aref tetris-shape-scores tetris-shape) tetris-rot)))
484 (tetris-update-score)
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))
494 (setq tetris-pos-y (1+ tetris-pos-y))
495 (setq hit (tetris-test-shape))
497 (setq tetris-pos-y (1- tetris-pos-y)))
500 (tetris-shape-done)))))
502 (defun tetris-move-bottom ()
503 "Drop the shape to the bottom of the playing area."
505 (unless tetris-paused
509 (setq tetris-pos-y (1+ tetris-pos-y))
510 (setq hit (tetris-test-shape)))
511 (setq tetris-pos-y (1- tetris-pos-y))
513 (tetris-shape-done))))
515 (defun tetris-move-left ()
516 "Move the shape one square to the left."
518 (unless tetris-paused
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."
528 (unless tetris-paused
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."
538 (unless tetris-paused
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."
550 (unless tetris-paused
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."
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."
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."
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
596 ["Start new game" tetris-start-game]
597 ["End game" tetris-end-game
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)))
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:
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
632 (select-window (or (get-buffer-window tetris-buffer-name)
634 (switch-to-buffer tetris-buffer-name)
635 (gamegrid-kill-timer)
643 ;;; tetris.el ends here