1 ;;; tetris.el --- implementation of Tetris for Emacs
3 ;; Copyright (C) 1997, 2001-2015 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
'(vector (color :tag
"Shape 1")
81 (color :tag
"Shape 2")
82 (color :tag
"Shape 3")
83 (color :tag
"Shape 4")
84 (color :tag
"Shape 5")
85 (color :tag
"Shape 6")
86 (color :tag
"Shape 7")))
88 (defcustom tetris-x-colors
89 [[0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]]
90 "Vector of colors of the various shapes."
94 (defcustom tetris-buffer-name
"*Tetris*"
95 "Name used for Tetris buffer."
99 (defcustom tetris-buffer-width
30
100 "Width of used portion of buffer."
104 (defcustom tetris-buffer-height
22
105 "Height of used portion of buffer."
109 (defcustom tetris-width
10
110 "Width of playing area."
114 (defcustom tetris-height
20
115 "Height of playing area."
119 (defcustom tetris-top-left-x
3
120 "X position of top left of playing area."
124 (defcustom tetris-top-left-y
1
125 "Y position of top left of playing area."
129 (defvar tetris-next-x
(+ (* 2 tetris-top-left-x
) tetris-width
)
130 "X position of next shape.")
132 (defvar tetris-next-y tetris-top-left-y
133 "Y position of next shape.")
135 (defvar tetris-score-x tetris-next-x
136 "X position of score.")
138 (defvar tetris-score-y
(+ tetris-next-y
6)
139 "Y position of score.")
141 ;; It is not safe to put this in /tmp.
142 ;; Someone could make a symlink in /tmp
143 ;; pointing to a file you don't want to clobber.
144 (defvar tetris-score-file
"tetris-scores"
145 ;; anybody with a well-connected server want to host this?
146 ;(defvar tetris-score-file "/anonymous@ftp.pgt.com:/pub/cgw/tetris-scores"
147 "File for holding high scores.")
149 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 (defvar tetris-blank-options
156 (color-tty color-tty
))
157 (((glyph color-x
) [0 0 0])
158 (color-tty "black"))))
160 (defvar tetris-cell-options
166 (color-tty color-tty
)
168 ;; color information is taken from tetris-x-colors and tetris-tty-colors
171 (defvar tetris-border-options
176 (color-tty color-tty
))
177 (((glyph color-x
) [0.5 0.5 0.5])
178 (color-tty "white"))))
180 (defvar tetris-space-options
185 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187 (defconst tetris-shapes
188 [[[[0 0] [1 0] [0 1] [1 1]]]
190 [[[0 0] [1 0] [2 0] [2 1]]
191 [[1 -
1] [1 0] [1 1] [0 1]]
192 [[0 -
1] [0 0] [1 0] [2 0]]
193 [[1 -
1] [2 -
1] [1 0] [1 1]]]
195 [[[0 0] [1 0] [2 0] [0 1]]
196 [[0 -
1] [1 -
1] [1 0] [1 1]]
197 [[2 -
1] [0 0] [1 0] [2 0]]
198 [[1 -
1] [1 0] [1 1] [2 1]]]
200 [[[0 0] [1 0] [1 1] [2 1]]
201 [[1 0] [0 1] [1 1] [0 2]]]
203 [[[1 0] [2 0] [0 1] [1 1]]
204 [[0 0] [0 1] [1 1] [1 2]]]
206 [[[1 0] [0 1] [1 1] [2 1]]
207 [[1 0] [1 1] [2 1] [1 2]]
208 [[0 1] [1 1] [2 1] [1 2]]
209 [[1 0] [0 1] [1 1] [1 2]]]
211 [[[0 0] [1 0] [2 0] [3 0]]
212 [[1 -
1] [1 0] [1 1] [1 2]]]]
213 "Each shape is described by a vector that contains the coordinates of
214 each one of its four blocks.")
216 ;;the scoring rules were taken from "xtetris". Blocks score differently
217 ;;depending on their rotation
219 (defconst tetris-shape-scores
220 [[6] [6 7 6 7] [6 7 6 7] [6 7] [6 7] [5 5 6 5] [5 8]] )
222 (defconst tetris-shape-dimensions
223 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]])
225 (defconst tetris-blank 7)
227 (defconst tetris-border 8)
229 (defconst tetris-space 9)
231 (defun tetris-default-update-speed-function (_shapes rows)
232 (/ 20.0 (+ 50.0 rows)))
234 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 (defvar tetris-shape 0)
237 (defvar tetris-rot 0)
238 (defvar tetris-next-shape 0)
239 (defvar tetris-n-shapes 0)
240 (defvar tetris-n-rows 0)
241 (defvar tetris-score 0)
242 (defvar tetris-pos-x 0)
243 (defvar tetris-pos-y 0)
244 (defvar tetris-paused nil)
246 (make-variable-buffer-local 'tetris-shape)
247 (make-variable-buffer-local 'tetris-rot)
248 (make-variable-buffer-local 'tetris-next-shape)
249 (make-variable-buffer-local 'tetris-n-shapes)
250 (make-variable-buffer-local 'tetris-n-rows)
251 (make-variable-buffer-local 'tetris-score)
252 (make-variable-buffer-local 'tetris-pos-x)
253 (make-variable-buffer-local 'tetris-pos-y)
254 (make-variable-buffer-local 'tetris-paused)
256 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
258 (defvar tetris-mode-map
259 (let ((map (make-sparse-keymap 'tetris-mode-map)))
260 (define-key map "n" 'tetris-start-game)
261 (define-key map "q" 'tetris-end-game)
262 (define-key map "p" 'tetris-pause-game)
264 (define-key map " " 'tetris-move-bottom)
265 (define-key map [left] 'tetris-move-left)
266 (define-key map [right] 'tetris-move-right)
267 (define-key map [up] 'tetris-rotate-prev)
268 (define-key map [down] 'tetris-rotate-next)
271 (defvar tetris-null-map
272 (let ((map (make-sparse-keymap 'tetris-null-map)))
273 (define-key map "n" 'tetris-start-game)
276 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278 (defun tetris-display-options ()
279 (let ((options (make-vector 256 nil)))
282 (cond ((= c tetris-blank)
283 tetris-blank-options)
284 ((and (>= c 0) (<= c 6))
287 `((((glyph color-x) ,(aref tetris-x-colors c))
288 (color-tty ,(aref tetris-tty-colors c))
291 tetris-border-options)
293 tetris-space-options)
298 (defun tetris-get-tick-period ()
299 (if (boundp 'tetris-update-speed-function)
300 (let ((period (apply tetris-update-speed-function
303 (and (numberp period) period))))
305 (defun tetris-get-shape-cell (block)
306 (aref (aref (aref tetris-shapes
307 tetris-shape) tetris-rot)
310 (defun tetris-shape-width ()
311 (aref (aref tetris-shape-dimensions tetris-shape) 0))
313 (defun tetris-shape-rotations ()
314 (length (aref tetris-shapes tetris-shape)))
316 (defun tetris-draw-score ()
317 (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes)
318 (format "Rows: %05d" tetris-n-rows)
319 (format "Score: %05d" tetris-score))))
321 (let* ((string (aref strings y))
322 (len (length string)))
324 (gamegrid-set-cell (+ tetris-score-x x)
326 (aref string x)))))))
328 (defun tetris-update-score ()
330 (let ((period (tetris-get-tick-period)))
331 (if period (gamegrid-set-timer period))))
333 (defun tetris-new-shape ()
334 (setq tetris-shape tetris-next-shape)
336 (setq tetris-next-shape (random 7))
337 (setq tetris-pos-x (/ (- tetris-width (tetris-shape-width)) 2))
338 (setq tetris-pos-y 0)
339 (if (tetris-test-shape)
342 (tetris-draw-next-shape)
343 (tetris-update-score)))
345 (defun tetris-draw-next-shape ()
348 (gamegrid-set-cell (+ tetris-next-x x)
352 (let ((tetris-shape tetris-next-shape)
354 (gamegrid-set-cell (+ tetris-next-x
355 (aref (tetris-get-shape-cell i) 0))
357 (aref (tetris-get-shape-cell i) 1))
360 (defun tetris-draw-shape ()
362 (let ((c (tetris-get-shape-cell i)))
363 (gamegrid-set-cell (+ tetris-top-left-x
371 (defun tetris-erase-shape ()
373 (let ((c (tetris-get-shape-cell i)))
374 (gamegrid-set-cell (+ tetris-top-left-x
382 (defun tetris-test-shape ()
387 (let* ((c (tetris-get-shape-cell i))
392 (or (>= xx tetris-width)
393 (>= yy tetris-height)
394 (/= (gamegrid-get-cell
395 (+ xx tetris-top-left-x)
396 (+ yy tetris-top-left-y))
400 (defun tetris-full-row (y)
402 (dotimes (x tetris-width)
403 (if (= (gamegrid-get-cell (+ tetris-top-left-x x)
404 (+ tetris-top-left-y y))
409 (defun tetris-shift-row (y)
411 (dotimes (x tetris-width)
412 (gamegrid-set-cell (+ tetris-top-left-x x)
413 (+ tetris-top-left-y y)
415 (dotimes (x tetris-width)
416 (let ((c (gamegrid-get-cell (+ tetris-top-left-x x)
417 (+ tetris-top-left-y y -1))))
418 (gamegrid-set-cell (+ tetris-top-left-x x)
419 (+ tetris-top-left-y y)
422 (defun tetris-shift-down ()
423 (dotimes (y0 tetris-height)
424 (when (tetris-full-row y0)
425 (setq tetris-n-rows (1+ tetris-n-rows))
426 (cl-loop for y from y0 downto 0 do
427 (tetris-shift-row y)))))
429 (defun tetris-draw-border-p ()
430 (or (not (eq gamegrid-display-mode 'glyph))
431 tetris-draw-border-with-glyphs))
433 (defun tetris-init-buffer ()
434 (gamegrid-init-buffer tetris-buffer-width
437 (let ((buffer-read-only nil))
438 (if (tetris-draw-border-p)
439 (cl-loop for y from -1 to tetris-height do
440 (cl-loop for x from -1 to tetris-width do
441 (gamegrid-set-cell (+ tetris-top-left-x x)
442 (+ tetris-top-left-y y)
444 (dotimes (y tetris-height)
445 (dotimes (x tetris-width)
446 (gamegrid-set-cell (+ tetris-top-left-x x)
447 (+ tetris-top-left-y y)
449 (if (tetris-draw-border-p)
450 (cl-loop for y from -1 to 4 do
451 (cl-loop for x from -1 to 4 do
452 (gamegrid-set-cell (+ tetris-next-x x)
456 (defun tetris-reset-game ()
457 (gamegrid-kill-timer)
459 (setq tetris-next-shape (random 7))
470 (defun tetris-shape-done ()
472 (setq tetris-n-shapes (1+ tetris-n-shapes))
475 (aref (aref tetris-shape-scores tetris-shape) tetris-rot)))
476 (tetris-update-score)
479 (defun tetris-update-game (tetris-buffer)
480 "Called on each clock tick.
481 Drops the shape one square, testing for collision."
482 (if (and (not tetris-paused)
483 (eq (current-buffer) tetris-buffer))
486 (setq tetris-pos-y (1+ tetris-pos-y))
487 (setq hit (tetris-test-shape))
489 (setq tetris-pos-y (1- tetris-pos-y)))
492 (tetris-shape-done)))))
494 (defun tetris-move-bottom ()
495 "Drop the shape to the bottom of the playing area."
497 (unless tetris-paused
501 (setq tetris-pos-y (1+ tetris-pos-y))
502 (setq hit (tetris-test-shape)))
503 (setq tetris-pos-y (1- tetris-pos-y))
505 (tetris-shape-done))))
507 (defun tetris-move-left ()
508 "Move the shape one square to the left."
510 (unless tetris-paused
512 (setq tetris-pos-x (1- tetris-pos-x))
513 (if (tetris-test-shape)
514 (setq tetris-pos-x (1+ tetris-pos-x)))
515 (tetris-draw-shape)))
517 (defun tetris-move-right ()
518 "Move the shape one square to the right."
520 (unless tetris-paused
522 (setq tetris-pos-x (1+ tetris-pos-x))
523 (if (tetris-test-shape)
524 (setq tetris-pos-x (1- tetris-pos-x)))
525 (tetris-draw-shape)))
527 (defun tetris-rotate-prev ()
528 "Rotate the shape clockwise."
530 (unless tetris-paused
532 (setq tetris-rot (% (+ 1 tetris-rot)
533 (tetris-shape-rotations)))
534 (if (tetris-test-shape)
535 (setq tetris-rot (% (+ 3 tetris-rot)
536 (tetris-shape-rotations))))
537 (tetris-draw-shape)))
539 (defun tetris-rotate-next ()
540 "Rotate the shape anticlockwise."
542 (unless tetris-paused
544 (setq tetris-rot (% (+ 3 tetris-rot)
545 (tetris-shape-rotations)))
546 (if (tetris-test-shape)
547 (setq tetris-rot (% (+ 1 tetris-rot)
548 (tetris-shape-rotations))))
549 (tetris-draw-shape)))
551 (defun tetris-end-game ()
552 "Terminate the current game."
554 (gamegrid-kill-timer)
555 (use-local-map tetris-null-map)
556 (gamegrid-add-score tetris-score-file tetris-score))
558 (defun tetris-start-game ()
559 "Start a new game of Tetris."
562 (use-local-map tetris-mode-map)
563 (let ((period (or (tetris-get-tick-period)
564 tetris-default-tick-period)))
565 (gamegrid-start-timer period 'tetris-update-game)))
567 (defun tetris-pause-game ()
568 "Pause (or resume) the current game."
570 (setq tetris-paused (not tetris-paused))
571 (message (and tetris-paused "Game paused (press p to resume)")))
573 (defun tetris-active-p ()
574 (eq (current-local-map) tetris-mode-map))
576 (put 'tetris-mode 'mode-class 'special)
578 (define-derived-mode tetris-mode nil "Tetris"
579 "A mode for playing Tetris."
581 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
583 (use-local-map tetris-null-map)
585 (unless (featurep 'emacs)
586 (setq mode-popup-menu
588 ["Start new game" tetris-start-game]
589 ["End game" tetris-end-game
591 ["Pause" tetris-pause-game
592 (and (tetris-active-p) (not tetris-paused))]
593 ["Resume" tetris-pause-game
594 (and (tetris-active-p) tetris-paused)])))
596 (setq show-trailing-whitespace nil)
598 (setq gamegrid-use-glyphs tetris-use-glyphs)
599 (setq gamegrid-use-color tetris-use-color)
601 (gamegrid-init (tetris-display-options)))
605 "Play the Tetris game.
606 Shapes drop from the top of the screen, and the user has to move and
607 rotate the shape to fit in with those at the bottom of the screen so
608 as to form complete rows.
610 tetris-mode keybindings:
612 \\[tetris-start-game] Starts a new game of Tetris
613 \\[tetris-end-game] Terminates the current game
614 \\[tetris-pause-game] Pauses (or resumes) the current game
615 \\[tetris-move-left] Moves the shape one square to the left
616 \\[tetris-move-right] Moves the shape one square to the right
617 \\[tetris-rotate-prev] Rotates the shape clockwise
618 \\[tetris-rotate-next] Rotates the shape anticlockwise
619 \\[tetris-move-bottom] Drops the shape to the bottom of the playing area
624 (select-window (or (get-buffer-window tetris-buffer-name)
626 (switch-to-buffer tetris-buffer-name)
627 (gamegrid-kill-timer)
633 ;;; tetris.el ends here