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/>.
29 (eval-when-compile (require 'cl-lib
))
33 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 "Play a game of Tetris."
40 (defcustom tetris-use-glyphs t
41 "Non-nil means use glyphs when available."
45 (defcustom tetris-use-color t
46 "Non-nil means use color when available."
50 (defcustom tetris-draw-border-with-glyphs t
51 "Non-nil means draw a border even when using glyphs."
55 (defcustom tetris-default-tick-period
0.3
56 "The default time taken for a shape to drop one row."
60 (defcustom tetris-update-speed-function
61 'tetris-default-update-speed-function
62 "Function run whenever the Tetris score changes.
63 Called with two arguments: (SHAPES ROWS)
64 SHAPES is the number of shapes which have been dropped.
65 ROWS is the number of rows which have been completed.
67 If the return value is a number, it is used as the timer period."
71 (defcustom tetris-mode-hook nil
72 "Hook run upon starting Tetris."
76 (defcustom tetris-tty-colors
77 ["blue" "white" "yellow" "magenta" "cyan" "green" "red"]
78 "Vector of colors of the various shapes in text mode."
80 :type
(let ((names `("Shape 1" "Shape 2" "Shape 3"
81 "Shape 4" "Shape 5" "Shape 6" "Shape 7"))
88 (mapcar (lambda (color)
92 (setq names
(cdr names
)))
95 (defcustom tetris-x-colors
96 [[0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]]
97 "Vector of colors of the various shapes."
101 (defcustom tetris-buffer-name
"*Tetris*"
102 "Name used for Tetris buffer."
106 (defcustom tetris-buffer-width
30
107 "Width of used portion of buffer."
111 (defcustom tetris-buffer-height
22
112 "Height of used portion of buffer."
116 (defcustom tetris-width
10
117 "Width of playing area."
121 (defcustom tetris-height
20
122 "Height of playing area."
126 (defcustom tetris-top-left-x
3
127 "X position of top left of playing area."
131 (defcustom tetris-top-left-y
1
132 "Y position of top left of playing area."
136 (defvar tetris-next-x
(+ (* 2 tetris-top-left-x
) tetris-width
)
137 "X position of next shape.")
139 (defvar tetris-next-y tetris-top-left-y
140 "Y position of next shape.")
142 (defvar tetris-score-x tetris-next-x
143 "X position of score.")
145 (defvar tetris-score-y
(+ tetris-next-y
6)
146 "Y position of score.")
148 ;; It is not safe to put this in /tmp.
149 ;; Someone could make a symlink in /tmp
150 ;; pointing to a file you don't want to clobber.
151 (defvar tetris-score-file
"tetris-scores"
152 ;; anybody with a well-connected server want to host this?
153 ;(defvar tetris-score-file "/anonymous@ftp.pgt.com:/pub/cgw/tetris-scores"
154 "File for holding high scores.")
156 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 (defvar tetris-blank-options
163 (color-tty color-tty
))
164 (((glyph color-x
) [0 0 0])
165 (color-tty "black"))))
167 (defvar tetris-cell-options
173 (color-tty color-tty
)
175 ;; color information is taken from tetris-x-colors and tetris-tty-colors
178 (defvar tetris-border-options
183 (color-tty color-tty
))
184 (((glyph color-x
) [0.5 0.5 0.5])
185 (color-tty "white"))))
187 (defvar tetris-space-options
192 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 (defconst tetris-shapes
195 [[[[0 0] [1 0] [0 1] [1 1]]]
197 [[[0 0] [1 0] [2 0] [2 1]]
198 [[1 -
1] [1 0] [1 1] [0 1]]
199 [[0 -
1] [0 0] [1 0] [2 0]]
200 [[1 -
1] [2 -
1] [1 0] [1 1]]]
202 [[[0 0] [1 0] [2 0] [0 1]]
203 [[0 -
1] [1 -
1] [1 0] [1 1]]
204 [[2 -
1] [0 0] [1 0] [2 0]]
205 [[1 -
1] [1 0] [1 1] [2 1]]]
207 [[[0 0] [1 0] [1 1] [2 1]]
208 [[1 0] [0 1] [1 1] [0 2]]]
210 [[[1 0] [2 0] [0 1] [1 1]]
211 [[0 0] [0 1] [1 1] [1 2]]]
213 [[[1 0] [0 1] [1 1] [2 1]]
214 [[1 0] [1 1] [2 1] [1 2]]
215 [[0 1] [1 1] [2 1] [1 2]]
216 [[1 0] [0 1] [1 1] [1 2]]]
218 [[[0 0] [1 0] [2 0] [3 0]]
219 [[1 -
1] [1 0] [1 1] [1 2]]]]
220 "Each shape is described by a vector that contains the coordinates of
221 each one of its four blocks.")
223 ;;the scoring rules were taken from "xtetris". Blocks score differently
224 ;;depending on their rotation
226 (defconst tetris-shape-scores
227 [[6] [6 7 6 7] [6 7 6 7] [6 7] [6 7] [5 5 6 5] [5 8]] )
229 (defconst tetris-shape-dimensions
230 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]])
232 (defconst tetris-blank 7)
234 (defconst tetris-border 8)
236 (defconst tetris-space 9)
238 (defun tetris-default-update-speed-function (_shapes rows)
239 (/ 20.0 (+ 50.0 rows)))
241 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 (defvar tetris-shape 0)
244 (defvar tetris-rot 0)
245 (defvar tetris-next-shape 0)
246 (defvar tetris-n-shapes 0)
247 (defvar tetris-n-rows 0)
248 (defvar tetris-score 0)
249 (defvar tetris-pos-x 0)
250 (defvar tetris-pos-y 0)
251 (defvar tetris-paused nil)
253 (make-variable-buffer-local 'tetris-shape)
254 (make-variable-buffer-local 'tetris-rot)
255 (make-variable-buffer-local 'tetris-next-shape)
256 (make-variable-buffer-local 'tetris-n-shapes)
257 (make-variable-buffer-local 'tetris-n-rows)
258 (make-variable-buffer-local 'tetris-score)
259 (make-variable-buffer-local 'tetris-pos-x)
260 (make-variable-buffer-local 'tetris-pos-y)
261 (make-variable-buffer-local 'tetris-paused)
263 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 (defvar tetris-mode-map
266 (let ((map (make-sparse-keymap 'tetris-mode-map)))
267 (define-key map "n" 'tetris-start-game)
268 (define-key map "q" 'tetris-end-game)
269 (define-key map "p" 'tetris-pause-game)
271 (define-key map " " 'tetris-move-bottom)
272 (define-key map [left] 'tetris-move-left)
273 (define-key map [right] 'tetris-move-right)
274 (define-key map [up] 'tetris-rotate-prev)
275 (define-key map [down] 'tetris-rotate-next)
278 (defvar tetris-null-map
279 (let ((map (make-sparse-keymap 'tetris-null-map)))
280 (define-key map "n" 'tetris-start-game)
283 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285 (defun tetris-display-options ()
286 (let ((options (make-vector 256 nil)))
289 (cond ((= c tetris-blank)
290 tetris-blank-options)
291 ((and (>= c 0) (<= c 6))
294 `((((glyph color-x) ,(aref tetris-x-colors c))
295 (color-tty ,(aref tetris-tty-colors c))
298 tetris-border-options)
300 tetris-space-options)
305 (defun tetris-get-tick-period ()
306 (if (boundp 'tetris-update-speed-function)
307 (let ((period (apply tetris-update-speed-function
310 (and (numberp period) period))))
312 (defun tetris-get-shape-cell (block)
313 (aref (aref (aref tetris-shapes
314 tetris-shape) tetris-rot)
317 (defun tetris-shape-width ()
318 (aref (aref tetris-shape-dimensions tetris-shape) 0))
320 (defun tetris-shape-rotations ()
321 (length (aref tetris-shapes tetris-shape)))
323 (defun tetris-draw-score ()
324 (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes)
325 (format "Rows: %05d" tetris-n-rows)
326 (format "Score: %05d" tetris-score))))
328 (let* ((string (aref strings y))
329 (len (length string)))
331 (gamegrid-set-cell (+ tetris-score-x x)
333 (aref string x)))))))
335 (defun tetris-update-score ()
337 (let ((period (tetris-get-tick-period)))
338 (if period (gamegrid-set-timer period))))
340 (defun tetris-new-shape ()
341 (setq tetris-shape tetris-next-shape)
343 (setq tetris-next-shape (random 7))
344 (setq tetris-pos-x (/ (- tetris-width (tetris-shape-width)) 2))
345 (setq tetris-pos-y 0)
346 (if (tetris-test-shape)
349 (tetris-draw-next-shape)
350 (tetris-update-score)))
352 (defun tetris-draw-next-shape ()
355 (gamegrid-set-cell (+ tetris-next-x x)
359 (let ((tetris-shape tetris-next-shape)
361 (gamegrid-set-cell (+ tetris-next-x
362 (aref (tetris-get-shape-cell i) 0))
364 (aref (tetris-get-shape-cell i) 1))
367 (defun tetris-draw-shape ()
369 (let ((c (tetris-get-shape-cell i)))
370 (gamegrid-set-cell (+ tetris-top-left-x
378 (defun tetris-erase-shape ()
380 (let ((c (tetris-get-shape-cell i)))
381 (gamegrid-set-cell (+ tetris-top-left-x
389 (defun tetris-test-shape ()
394 (let* ((c (tetris-get-shape-cell i))
399 (or (>= xx tetris-width)
400 (>= yy tetris-height)
401 (/= (gamegrid-get-cell
402 (+ xx tetris-top-left-x)
403 (+ yy tetris-top-left-y))
407 (defun tetris-full-row (y)
409 (dotimes (x tetris-width)
410 (if (= (gamegrid-get-cell (+ tetris-top-left-x x)
411 (+ tetris-top-left-y y))
416 (defun tetris-shift-row (y)
418 (dotimes (x tetris-width)
419 (gamegrid-set-cell (+ tetris-top-left-x x)
420 (+ tetris-top-left-y y)
422 (dotimes (x tetris-width)
423 (let ((c (gamegrid-get-cell (+ tetris-top-left-x x)
424 (+ tetris-top-left-y y -1))))
425 (gamegrid-set-cell (+ tetris-top-left-x x)
426 (+ tetris-top-left-y y)
429 (defun tetris-shift-down ()
430 (dotimes (y0 tetris-height)
431 (when (tetris-full-row y0)
432 (setq tetris-n-rows (1+ tetris-n-rows))
433 (cl-loop for y from y0 downto 0 do
434 (tetris-shift-row y)))))
436 (defun tetris-draw-border-p ()
437 (or (not (eq gamegrid-display-mode 'glyph))
438 tetris-draw-border-with-glyphs))
440 (defun tetris-init-buffer ()
441 (gamegrid-init-buffer tetris-buffer-width
444 (let ((buffer-read-only nil))
445 (if (tetris-draw-border-p)
446 (cl-loop for y from -1 to tetris-height do
447 (cl-loop for x from -1 to tetris-width do
448 (gamegrid-set-cell (+ tetris-top-left-x x)
449 (+ tetris-top-left-y y)
451 (dotimes (y tetris-height)
452 (dotimes (x tetris-width)
453 (gamegrid-set-cell (+ tetris-top-left-x x)
454 (+ tetris-top-left-y y)
456 (if (tetris-draw-border-p)
457 (cl-loop for y from -1 to 4 do
458 (cl-loop for x from -1 to 4 do
459 (gamegrid-set-cell (+ tetris-next-x x)
463 (defun tetris-reset-game ()
464 (gamegrid-kill-timer)
466 (setq tetris-next-shape (random 7))
477 (defun tetris-shape-done ()
479 (setq tetris-n-shapes (1+ tetris-n-shapes))
482 (aref (aref tetris-shape-scores tetris-shape) tetris-rot)))
483 (tetris-update-score)
486 (defun tetris-update-game (tetris-buffer)
487 "Called on each clock tick.
488 Drops the shape one square, testing for collision."
489 (if (and (not tetris-paused)
490 (eq (current-buffer) tetris-buffer))
493 (setq tetris-pos-y (1+ tetris-pos-y))
494 (setq hit (tetris-test-shape))
496 (setq tetris-pos-y (1- tetris-pos-y)))
499 (tetris-shape-done)))))
501 (defun tetris-move-bottom ()
502 "Drop the shape to the bottom of the playing area."
504 (unless tetris-paused
508 (setq tetris-pos-y (1+ tetris-pos-y))
509 (setq hit (tetris-test-shape)))
510 (setq tetris-pos-y (1- tetris-pos-y))
512 (tetris-shape-done))))
514 (defun tetris-move-left ()
515 "Move the shape one square to the left."
517 (unless tetris-paused
519 (setq tetris-pos-x (1- tetris-pos-x))
520 (if (tetris-test-shape)
521 (setq tetris-pos-x (1+ tetris-pos-x)))
522 (tetris-draw-shape)))
524 (defun tetris-move-right ()
525 "Move the shape one square to the right."
527 (unless tetris-paused
529 (setq tetris-pos-x (1+ tetris-pos-x))
530 (if (tetris-test-shape)
531 (setq tetris-pos-x (1- tetris-pos-x)))
532 (tetris-draw-shape)))
534 (defun tetris-rotate-prev ()
535 "Rotate the shape clockwise."
537 (unless tetris-paused
539 (setq tetris-rot (% (+ 1 tetris-rot)
540 (tetris-shape-rotations)))
541 (if (tetris-test-shape)
542 (setq tetris-rot (% (+ 3 tetris-rot)
543 (tetris-shape-rotations))))
544 (tetris-draw-shape)))
546 (defun tetris-rotate-next ()
547 "Rotate the shape anticlockwise."
549 (unless tetris-paused
551 (setq tetris-rot (% (+ 3 tetris-rot)
552 (tetris-shape-rotations)))
553 (if (tetris-test-shape)
554 (setq tetris-rot (% (+ 1 tetris-rot)
555 (tetris-shape-rotations))))
556 (tetris-draw-shape)))
558 (defun tetris-end-game ()
559 "Terminate the current game."
561 (gamegrid-kill-timer)
562 (use-local-map tetris-null-map)
563 (gamegrid-add-score tetris-score-file tetris-score))
565 (defun tetris-start-game ()
566 "Start a new game of Tetris."
569 (use-local-map tetris-mode-map)
570 (let ((period (or (tetris-get-tick-period)
571 tetris-default-tick-period)))
572 (gamegrid-start-timer period 'tetris-update-game)))
574 (defun tetris-pause-game ()
575 "Pause (or resume) the current game."
577 (setq tetris-paused (not tetris-paused))
578 (message (and tetris-paused "Game paused (press p to resume)")))
580 (defun tetris-active-p ()
581 (eq (current-local-map) tetris-mode-map))
583 (put 'tetris-mode 'mode-class 'special)
585 (define-derived-mode tetris-mode nil "Tetris"
586 "A mode for playing Tetris."
588 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
590 (use-local-map tetris-null-map)
592 (unless (featurep 'emacs)
593 (setq mode-popup-menu
595 ["Start new game" tetris-start-game]
596 ["End game" tetris-end-game
598 ["Pause" tetris-pause-game
599 (and (tetris-active-p) (not tetris-paused))]
600 ["Resume" tetris-pause-game
601 (and (tetris-active-p) tetris-paused)])))
603 (setq show-trailing-whitespace nil)
605 (setq gamegrid-use-glyphs tetris-use-glyphs)
606 (setq gamegrid-use-color tetris-use-color)
608 (gamegrid-init (tetris-display-options)))
612 "Play the Tetris game.
613 Shapes drop from the top of the screen, and the user has to move and
614 rotate the shape to fit in with those at the bottom of the screen so
615 as to form complete rows.
617 tetris-mode keybindings:
619 \\[tetris-start-game] Starts a new game of Tetris
620 \\[tetris-end-game] Terminates the current game
621 \\[tetris-pause-game] Pauses (or resumes) the current game
622 \\[tetris-move-left] Moves the shape one square to the left
623 \\[tetris-move-right] Moves the shape one square to the right
624 \\[tetris-rotate-prev] Rotates the shape clockwise
625 \\[tetris-rotate-next] Rotates the shape anticlockwise
626 \\[tetris-move-bottom] Drops the shape to the bottom of the playing area
631 (select-window (or (get-buffer-window tetris-buffer-name)
633 (switch-to-buffer tetris-buffer-name)
634 (gamegrid-kill-timer)
642 ;;; tetris.el ends here