1 ;;; tetris.el --- implementation of Tetris for Emacs
3 ;; Copyright (C) 1997 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 2, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
36 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 "Play a game of tetris."
43 (defcustom tetris-use-glyphs t
44 "*Non-nil means use glyphs when available."
48 (defcustom tetris-use-color t
49 "*Non-nil means use color when available."
53 (defcustom tetris-draw-border-with-glyphs t
54 "*Non-nil means draw a border even when using glyphs."
58 (defcustom tetris-default-tick-period
0.3
59 "*The default time taken for a shape to drop one row."
63 (defcustom tetris-update-speed-function
64 'tetris-default-update-speed-function
65 "Function run whenever the Tetris score changes
66 Called with two arguments: (SHAPES ROWS)
67 SHAPES is the number of shapes which have been dropped
68 ROWS is the number of rows which have been completed
70 If the return value is a number, it is used as the timer period."
74 (defcustom tetris-mode-hook nil
75 "Hook run upon starting Tetris."
79 (defcustom tetris-tty-colors
80 [nil
"blue" "white" "yellow" "magenta" "cyan" "green" "red"]
81 "Vector of colors of the various shapes in text mode
82 Element 0 is ignored."
84 :type
(let ((names `("Shape 1" "Shape 2" "Shape 3"
85 "Shape 4" "Shape 5" "Shape 6" "Shape 7"))
86 (result `(vector (const nil
))))
92 (mapcar (lambda (color)
96 (setq names
(cdr names
)))
99 (defcustom tetris-x-colors
100 [nil
[0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]]
101 "Vector of colors of the various shapes
102 Element 0 is ignored."
106 (defcustom tetris-buffer-name
"*Tetris*"
107 "Name used for Tetris buffer."
111 (defcustom tetris-buffer-width
30
112 "Width of used portion of buffer."
116 (defcustom tetris-buffer-height
22
117 "Height of used portion of buffer."
121 (defcustom tetris-width
10
122 "Width of playing area."
126 (defcustom tetris-height
20
127 "Height of playing area."
131 (defcustom tetris-top-left-x
3
132 "X position of top left of playing area."
136 (defcustom tetris-top-left-y
1
137 "Y position of top left of playing area."
141 (defvar tetris-next-x
(+ (* 2 tetris-top-left-x
) tetris-width
)
142 "X position of next shape.")
144 (defvar tetris-next-y tetris-top-left-y
145 "Y position of next shape.")
147 (defvar tetris-score-x tetris-next-x
148 "X position of score.")
150 (defvar tetris-score-y
(+ tetris-next-y
6)
151 "Y position of score.")
153 ;; It is not safe to put this in /tmp.
154 ;; Someone could make a symlink in /tmp
155 ;; pointing to a file you don't want to clobber.
156 (defvar tetris-score-file
"tetris-scores"
157 ;; anybody with a well-connected server want to host this?
158 ;(defvar tetris-score-file "/anonymous@ftp.pgt.com:/pub/cgw/tetris-scores"
159 "File for holding high scores.")
161 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
163 (defvar tetris-blank-options
168 (color-tty color-tty
))
169 (((glyph color-x
) [0 0 0])
170 (color-tty "black"))))
172 (defvar tetris-cell-options
178 (color-tty color-tty
)
180 ;; color information is taken from tetris-x-colors and tetris-tty-colors
183 (defvar tetris-border-options
188 (color-tty color-tty
))
189 (((glyph color-x
) [0.5 0.5 0.5])
190 (color-tty "white"))))
192 (defvar tetris-space-options
197 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 (defconst tetris-shapes
200 [[[[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]]
201 [[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]]
202 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]
203 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
205 [[[2 2 2 0] [0 2 0 0] [2 0 0 0] [2 2 0 0]]
206 [[0 0 2 0] [0 2 0 0] [2 2 2 0] [2 0 0 0]]
207 [[0 0 0 0] [2 2 0 0] [0 0 0 0] [2 0 0 0]]
208 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
210 [[[3 3 3 0] [3 3 0 0] [0 0 3 0] [3 0 0 0]]
211 [[3 0 0 0] [0 3 0 0] [3 3 3 0] [3 0 0 0]]
212 [[0 0 0 0] [0 3 0 0] [0 0 0 0] [3 3 0 0]]
213 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
215 [[[4 4 0 0] [0 4 0 0] [4 4 0 0] [0 4 0 0]]
216 [[0 4 4 0] [4 4 0 0] [0 4 4 0] [4 4 0 0]]
217 [[0 0 0 0] [4 0 0 0] [0 0 0 0] [4 0 0 0]]
218 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
220 [[[0 5 5 0] [5 0 0 0] [0 5 5 0] [5 0 0 0]]
221 [[5 5 0 0] [5 5 0 0] [5 5 0 0] [5 5 0 0]]
222 [[0 0 0 0] [0 5 0 0] [0 0 0 0] [0 5 0 0]]
223 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
225 [[[0 6 0 0] [6 0 0 0] [6 6 6 0] [0 6 0 0]]
226 [[6 6 6 0] [6 6 0 0] [0 6 0 0] [6 6 0 0]]
227 [[0 0 0 0] [6 0 0 0] [0 0 0 0] [0 6 0 0]]
228 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
230 [[[7 7 7 7] [7 0 0 0] [7 7 7 7] [7 0 0 0]]
231 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]
232 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]
233 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]]])
235 ;;the scoring rules were taken from "xtetris". Blocks score differently
236 ;;depending on their rotation
238 (defconst tetris-shape-scores
239 [ [6 6 6 6] [6 7 6 7] [6 7 6 7] [6 7 6 7] [6 7 6 7] [5 5 6 5] [5 8 5 8]] )
241 (defconst tetris-shape-dimensions
242 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]])
244 (defconst tetris-blank
0)
246 (defconst tetris-border
8)
248 (defconst tetris-space
9)
250 (defun tetris-default-update-speed-function (shapes rows
)
251 (/ 20.0 (+ 50.0 rows
)))
253 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 (defvar tetris-shape
0)
256 (defvar tetris-rot
0)
257 (defvar tetris-next-shape
0)
258 (defvar tetris-n-shapes
0)
259 (defvar tetris-n-rows
0)
260 (defvar tetris-score
0)
261 (defvar tetris-pos-x
0)
262 (defvar tetris-pos-y
0)
263 (defvar tetris-paused nil
)
265 (make-variable-buffer-local 'tetris-shape
)
266 (make-variable-buffer-local 'tetris-rot
)
267 (make-variable-buffer-local 'tetris-next-shape
)
268 (make-variable-buffer-local 'tetris-n-shapes
)
269 (make-variable-buffer-local 'tetris-n-rows
)
270 (make-variable-buffer-local 'tetris-score
)
271 (make-variable-buffer-local 'tetris-pos-x
)
272 (make-variable-buffer-local 'tetris-pos-y
)
273 (make-variable-buffer-local 'tetris-paused
)
275 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 (defvar tetris-mode-map
278 (make-sparse-keymap 'tetris-mode-map
))
280 (define-key tetris-mode-map
"n" 'tetris-start-game
)
281 (define-key tetris-mode-map
"q" 'tetris-end-game
)
282 (define-key tetris-mode-map
"p" 'tetris-pause-game
)
284 (define-key tetris-mode-map
" " 'tetris-move-bottom
)
285 (define-key tetris-mode-map
[left] 'tetris-move-left)
286 (define-key tetris-mode-map [right] 'tetris-move-right)
287 (define-key tetris-mode-map [up] 'tetris-rotate-prev)
288 (define-key tetris-mode-map [down] 'tetris-rotate-next)
290 (defvar tetris-null-map
291 (make-sparse-keymap 'tetris-null-map))
293 (define-key tetris-null-map "n" 'tetris-start-game)
295 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 (defun tetris-display-options ()
298 (let ((options (make-vector 256 nil)))
299 (loop for c from 0 to 255 do
301 (cond ((= c tetris-blank)
302 tetris-blank-options)
303 ((and (>= c 1) (<= c 7))
306 `((((glyph color-x) ,(aref tetris-x-colors c))
307 (color-tty ,(aref tetris-tty-colors c))
310 tetris-border-options)
312 tetris-space-options)
317 (defun tetris-get-tick-period ()
318 (if (boundp 'tetris-update-speed-function)
319 (let ((period (apply tetris-update-speed-function
322 (and (numberp period) period))))
324 (defun tetris-get-shape-cell (x y)
325 (aref (aref (aref (aref tetris-shapes
331 (defun tetris-shape-width ()
332 (aref (aref tetris-shape-dimensions tetris-shape)
335 (defun tetris-shape-height ()
336 (aref (aref tetris-shape-dimensions tetris-shape)
337 (- 1 (% tetris-rot 2))))
339 (defun tetris-draw-score ()
340 (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes)
341 (format "Rows: %05d" tetris-n-rows)
342 (format "Score: %05d" tetris-score))))
343 (loop for y from 0 to 2 do
344 (let* ((string (aref strings y))
345 (len (length string)))
346 (loop for x from 0 to (1- len) do
347 (gamegrid-set-cell (+ tetris-score-x x)
349 (aref string x)))))))
351 (defun tetris-update-score ()
353 (let ((period (tetris-get-tick-period)))
354 (if period (gamegrid-set-timer period))))
356 (defun tetris-new-shape ()
357 (setq tetris-shape tetris-next-shape)
359 (setq tetris-next-shape (random 7))
360 (setq tetris-pos-x (/ (- tetris-width (tetris-shape-width)) 2))
361 (setq tetris-pos-y 0)
362 (if (tetris-test-shape)
365 (tetris-draw-next-shape)
366 (tetris-update-score))
368 (defun tetris-draw-next-shape ()
369 (loop for y from 0 to 3 do
370 (loop for x from 0 to 3 do
371 (gamegrid-set-cell (+ tetris-next-x x)
373 (let ((tetris-shape tetris-next-shape)
375 (tetris-get-shape-cell x y))))))
377 (defun tetris-draw-shape ()
378 (loop for y from 0 to (1- (tetris-shape-height)) do
379 (loop for x from 0 to (1- (tetris-shape-width)) do
380 (let ((c (tetris-get-shape-cell x y)))
381 (if (/= c tetris-blank)
382 (gamegrid-set-cell (+ tetris-top-left-x
390 (defun tetris-erase-shape ()
391 (loop for y from 0 to (1- (tetris-shape-height)) do
392 (loop for x from 0 to (1- (tetris-shape-width)) do
393 (let ((c (tetris-get-shape-cell x y))
394 (px (+ tetris-top-left-x tetris-pos-x x))
395 (py (+ tetris-top-left-y tetris-pos-y y)))
396 (if (/= c tetris-blank)
397 (gamegrid-set-cell px py tetris-blank))))))
399 (defun tetris-test-shape ()
401 (loop for y from 0 to (1- (tetris-shape-height)) do
402 (loop for x from 0 to (1- (tetris-shape-width)) do
405 (let* ((c (tetris-get-shape-cell x y))
406 (xx (+ tetris-pos-x x))
407 (yy (+ tetris-pos-y y))
408 (px (+ tetris-top-left-x xx))
409 (py (+ tetris-top-left-y yy)))
410 (and (/= c tetris-blank)
411 (or (>= xx tetris-width)
412 (>= yy tetris-height)
413 (/= (gamegrid-get-cell px py)
417 (defun tetris-full-row (y)
419 (loop for x from 0 to (1- tetris-width) do
420 (if (= (gamegrid-get-cell (+ tetris-top-left-x x)
421 (+ tetris-top-left-y y))
426 (defun tetris-shift-row (y)
428 (loop for x from 0 to (1- tetris-width) do
429 (gamegrid-set-cell (+ tetris-top-left-x x)
430 (+ tetris-top-left-y y)
432 (loop for x from 0 to (1- tetris-width) do
433 (let ((c (gamegrid-get-cell (+ tetris-top-left-x x)
434 (+ tetris-top-left-y y -1))))
435 (gamegrid-set-cell (+ tetris-top-left-x x)
436 (+ tetris-top-left-y y)
439 (defun tetris-shift-down ()
440 (loop for y0 from 0 to (1- tetris-height) do
441 (if (tetris-full-row y0)
442 (progn (setq tetris-n-rows (1+ tetris-n-rows))
443 (loop for y from y0 downto 0 do
444 (tetris-shift-row y))))))
446 (defun tetris-draw-border-p ()
447 (or (not (eq gamegrid-display-mode 'glyph))
448 tetris-draw-border-with-glyphs))
450 (defun tetris-init-buffer ()
451 (gamegrid-init-buffer tetris-buffer-width
454 (let ((buffer-read-only nil))
455 (if (tetris-draw-border-p)
456 (loop for y from -1 to tetris-height do
457 (loop for x from -1 to tetris-width do
458 (gamegrid-set-cell (+ tetris-top-left-x x)
459 (+ tetris-top-left-y y)
461 (loop for y from 0 to (1- tetris-height) do
462 (loop for x from 0 to (1- tetris-width) do
463 (gamegrid-set-cell (+ tetris-top-left-x x)
464 (+ tetris-top-left-y y)
466 (if (tetris-draw-border-p)
467 (loop for y from -1 to 4 do
468 (loop for x from -1 to 4 do
469 (gamegrid-set-cell (+ tetris-next-x x)
473 (defun tetris-reset-game ()
474 (gamegrid-kill-timer)
476 (setq tetris-next-shape (random 7))
487 (defun tetris-shape-done ()
489 (setq tetris-n-shapes (1+ tetris-n-shapes))
492 (aref (aref tetris-shape-scores tetris-shape) tetris-rot)))
493 (tetris-update-score)
496 (defun tetris-update-game (tetris-buffer)
497 "Called on each clock tick.
498 Drops the shape one square, testing for collision."
499 (if (and (not tetris-paused)
500 (eq (current-buffer) tetris-buffer))
503 (setq tetris-pos-y (1+ tetris-pos-y))
504 (setq hit (tetris-test-shape))
506 (setq tetris-pos-y (1- tetris-pos-y)))
509 (tetris-shape-done)))))
511 (defun tetris-move-bottom ()
512 "Drops the shape to the bottom of the playing area"
517 (setq tetris-pos-y (1+ tetris-pos-y))
518 (setq hit (tetris-test-shape)))
519 (setq tetris-pos-y (1- tetris-pos-y))
521 (tetris-shape-done)))
523 (defun tetris-move-left ()
524 "Moves the shape one square to the left"
526 (unless (= tetris-pos-x 0)
528 (setq tetris-pos-x (1- tetris-pos-x))
529 (if (tetris-test-shape)
530 (setq tetris-pos-x (1+ tetris-pos-x)))
531 (tetris-draw-shape)))
533 (defun tetris-move-right ()
534 "Moves the shape one square to the right"
536 (unless (= (+ tetris-pos-x (tetris-shape-width))
539 (setq tetris-pos-x (1+ tetris-pos-x))
540 (if (tetris-test-shape)
541 (setq tetris-pos-x (1- tetris-pos-x)))
542 (tetris-draw-shape)))
544 (defun tetris-rotate-prev ()
545 "Rotates the shape clockwise"
548 (setq tetris-rot (% (+ 1 tetris-rot) 4))
549 (if (tetris-test-shape)
550 (setq tetris-rot (% (+ 3 tetris-rot) 4)))
553 (defun tetris-rotate-next ()
554 "Rotates the shape anticlockwise"
557 (setq tetris-rot (% (+ 3 tetris-rot) 4))
558 (if (tetris-test-shape)
559 (setq tetris-rot (% (+ 1 tetris-rot) 4)))
562 (defun tetris-end-game ()
563 "Terminates the current game"
565 (gamegrid-kill-timer)
566 (use-local-map tetris-null-map)
567 (gamegrid-add-score tetris-score-file tetris-score))
569 (defun tetris-start-game ()
570 "Starts a new game of Tetris"
573 (use-local-map tetris-mode-map)
574 (let ((period (or (tetris-get-tick-period)
575 tetris-default-tick-period)))
576 (gamegrid-start-timer period 'tetris-update-game)))
578 (defun tetris-pause-game ()
579 "Pauses (or resumes) the current game"
581 (setq tetris-paused (not tetris-paused))
582 (message (and tetris-paused "Game paused (press p to resume)")))
584 (defun tetris-active-p ()
585 (eq (current-local-map) tetris-mode-map))
587 (put 'tetris-mode 'mode-class 'special)
589 (defun tetris-mode ()
590 "A mode for playing Tetris.
592 tetris-mode keybindings:
595 (kill-all-local-variables)
597 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
599 (use-local-map tetris-null-map)
601 (setq major-mode 'tetris-mode)
602 (setq mode-name "Tetris")
604 (setq mode-popup-menu
606 ["Start new game" tetris-start-game]
607 ["End game" tetris-end-game
609 ["Pause" tetris-pause-game
610 (and (tetris-active-p) (not tetris-paused))]
611 ["Resume" tetris-pause-game
612 (and (tetris-active-p) tetris-paused)]))
614 (setq gamegrid-use-glyphs tetris-use-glyphs)
615 (setq gamegrid-use-color tetris-use-color)
617 (gamegrid-init (tetris-display-options))
619 (run-hooks 'tetris-mode-hook))
623 "Play the Tetris game.
624 Shapes drop from the top of the screen, and the user has to move and
625 rotate the shape to fit in with those at the bottom of the screen so
626 as to form complete rows.
628 tetris-mode keybindings:
630 \\[tetris-start-game] Starts a new game of Tetris
631 \\[tetris-end-game] Terminates the current game
632 \\[tetris-pause-game] Pauses (or resumes) the current game
633 \\[tetris-move-left] Moves the shape one square to the left
634 \\[tetris-move-right] Moves the shape one square to the right
635 \\[tetris-rotate-prev] Rotates the shape clockwise
636 \\[tetris-rotate-next] Rotates the shape anticlockwise
637 \\[tetris-move-bottom] Drops the shape to the bottom of the playing area
642 (switch-to-buffer tetris-buffer-name)
643 (gamegrid-kill-timer)
649 ;;; tetris.el ends here