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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 (defvar tetris-use-glyphs t
39 "Non-nil means use glyphs when available.")
41 (defvar tetris-use-color t
42 "Non-nil means use color when available.")
44 (defvar tetris-draw-border-with-glyphs t
45 "Non-nil means draw a border even when using glyphs.")
47 (defvar tetris-default-tick-period 0.3
48 "The default time taken for a shape to drop one row.")
50 (defvar tetris-update-speed-function
51 'tetris-default-update-speed-function
52 "Function run whenever the Tetris score changes
53 Called with two arguments: (SHAPES ROWS)
54 SHAPES is the number of shapes which have been dropped
55 ROWS is the number of rows which have been completed
57 If the return value is a number, it is used as the timer period.")
59 (defvar tetris-mode-hook nil
60 "Hook run upon starting Tetris.")
62 (defvar tetris-tty-colors
63 [nil "blue" "white" "yellow" "magenta" "cyan" "green" "red"]
64 "Vector of colors of the various shapes in text mode
65 Element 0 is ignored.")
67 (defvar tetris-x-colors
68 [nil [0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]]
69 "Vector of colors of the various shapes
70 Element 0 is ignored.")
72 (defvar tetris-buffer-name "*Tetris*"
73 "Name used for Tetris buffer.")
75 (defvar tetris-buffer-width 30
76 "Width of used portion of buffer.")
78 (defvar tetris-buffer-height 22
79 "Height of used portion of buffer.")
81 (defvar tetris-width 10
82 "Width of playing area.")
84 (defvar tetris-height 20
85 "Height of playing area.")
87 (defvar tetris-top-left-x 3
88 "X position of top left of playing area.")
90 (defvar tetris-top-left-y 1
91 "Y position of top left of playing area.")
93 (defvar tetris-next-x (+ (* 2 tetris-top-left-x) tetris-width)
94 "X position of next shape.")
96 (defvar tetris-next-y tetris-top-left-y
97 "Y position of next shape.")
99 (defvar tetris-score-x tetris-next-x
100 "X position of score.")
102 (defvar tetris-score-y (+ tetris-next-y 6)
103 "Y position of score.")
105 (defvar tetris-score-file "/tmp/tetris-scores"
106 ;; anybody with a well-connected server want to host this?
107 ;(defvar tetris-score-file "/anonymous@ftp.pgt.com:/pub/cgw/tetris-scores"
108 "File for holding high scores.")
110 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
112 (defvar tetris-border-options
118 (((glyph color-x) [0.5 0.5 0.5])
121 (defvar tetris-blank-options
126 (color-tty color-tty)
128 (((glyph color-x) [0 0 0])
132 (defvar tetris-cell-options
138 (color-tty color-tty)
141 ;; color information is taken from tetris-x-colors and tetris-tty-colors
144 (defvar tetris-space-options
149 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 (defconst tetris-shapes
152 [[[[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]]
153 [[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]]
154 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]
155 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
157 [[[2 2 2 0] [0 2 0 0] [2 0 0 0] [2 2 0 0]]
158 [[0 0 2 0] [0 2 0 0] [2 2 2 0] [2 0 0 0]]
159 [[0 0 0 0] [2 2 0 0] [0 0 0 0] [2 0 0 0]]
160 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
162 [[[3 3 3 0] [3 3 0 0] [0 0 3 0] [3 0 0 0]]
163 [[3 0 0 0] [0 3 0 0] [3 3 3 0] [3 0 0 0]]
164 [[0 0 0 0] [0 3 0 0] [0 0 0 0] [3 3 0 0]]
165 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
167 [[[4 4 0 0] [0 4 0 0] [4 4 0 0] [0 4 0 0]]
168 [[0 4 4 0] [4 4 0 0] [0 4 4 0] [4 4 0 0]]
169 [[0 0 0 0] [4 0 0 0] [0 0 0 0] [4 0 0 0]]
170 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
172 [[[0 5 5 0] [5 0 0 0] [0 5 5 0] [5 0 0 0]]
173 [[5 5 0 0] [5 5 0 0] [5 5 0 0] [5 5 0 0]]
174 [[0 0 0 0] [0 5 0 0] [0 0 0 0] [0 5 0 0]]
175 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
177 [[[0 6 0 0] [6 0 0 0] [6 6 6 0] [0 6 0 0]]
178 [[6 6 6 0] [6 6 0 0] [0 6 0 0] [6 6 0 0]]
179 [[0 0 0 0] [6 0 0 0] [0 0 0 0] [0 6 0 0]]
180 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
182 [[[7 7 7 7] [7 0 0 0] [7 7 7 7] [7 0 0 0]]
183 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]
184 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]
185 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]]])
187 ;;the scoring rules were taken from "xtetris". Blocks score differently
188 ;;depending on their rotation
190 (defconst tetris-shape-scores
191 [ [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]] )
193 (defconst tetris-shape-dimensions
194 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]])
196 (defconst tetris-blank 0)
198 (defconst tetris-border 8)
200 (defconst tetris-space 9)
202 (defun tetris-default-update-speed-function (shapes rows)
203 (/ 20.0 (+ 50.0 rows)))
205 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207 (defvar tetris-shape 0)
208 (defvar tetris-rot 0)
209 (defvar tetris-next-shape 0)
210 (defvar tetris-n-shapes 0)
211 (defvar tetris-n-rows 0)
212 (defvar tetris-score 0)
213 (defvar tetris-pos-x 0)
214 (defvar tetris-pos-y 0)
215 (defvar tetris-paused nil)
217 (make-variable-buffer-local 'tetris-shape)
218 (make-variable-buffer-local 'tetris-rot)
219 (make-variable-buffer-local 'tetris-next-shape)
220 (make-variable-buffer-local 'tetris-n-shapes)
221 (make-variable-buffer-local 'tetris-n-rows)
222 (make-variable-buffer-local 'tetris-score)
223 (make-variable-buffer-local 'tetris-pos-x)
224 (make-variable-buffer-local 'tetris-pos-y)
225 (make-variable-buffer-local 'tetris-paused)
227 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
229 (defvar tetris-mode-map
230 (make-sparse-keymap 'tetris-mode-map))
232 (define-key tetris-mode-map "n" 'tetris-start-game)
233 (define-key tetris-mode-map "q" 'tetris-end-game)
234 (define-key tetris-mode-map "p" 'tetris-pause-game)
236 (define-key tetris-mode-map " " 'tetris-move-bottom)
237 (define-key tetris-mode-map [left] 'tetris-move-left)
238 (define-key tetris-mode-map [right] 'tetris-move-right)
239 (define-key tetris-mode-map [up] 'tetris-rotate-prev)
240 (define-key tetris-mode-map [down] 'tetris-rotate-next)
242 (defvar tetris-null-map
243 (make-sparse-keymap 'tetris-null-map))
245 (define-key tetris-null-map "n" 'tetris-start-game)
247 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
249 (defun tetris-display-options ()
250 (let ((options (make-vector 256 nil)))
251 (loop for c from 0 to 255 do
253 (cond ((= c tetris-blank)
254 tetris-blank-options)
255 ((and (>= c 1) (<= c 7))
258 `((((glyph color-x) ,(aref tetris-x-colors c))
259 (color-tty ,(aref tetris-tty-colors c))
262 tetris-border-options)
264 tetris-space-options)
269 (defun tetris-get-tick-period ()
270 (if (boundp 'tetris-update-speed-function)
271 (let ((period (apply tetris-update-speed-function
274 (and (numberp period) period))))
276 (defun tetris-get-shape-cell (x y)
277 (aref (aref (aref (aref tetris-shapes
283 (defun tetris-shape-width ()
284 (aref (aref tetris-shape-dimensions tetris-shape)
287 (defun tetris-shape-height ()
288 (aref (aref tetris-shape-dimensions tetris-shape)
289 (- 1 (% tetris-rot 2))))
291 (defun tetris-draw-score ()
292 (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes)
293 (format "Rows: %05d" tetris-n-rows)
294 (format "Score: %05d" tetris-score))))
295 (loop for y from 0 to 2 do
296 (let* ((string (aref strings y))
297 (len (length string)))
298 (loop for x from 0 to (1- len) do
299 (gamegrid-set-cell (+ tetris-score-x x)
301 (aref string x)))))))
303 (defun tetris-update-score ()
305 (let ((period (tetris-get-tick-period)))
306 (if period (gamegrid-set-timer period))))
308 (defun tetris-new-shape ()
309 (setq tetris-shape tetris-next-shape)
311 (setq tetris-next-shape (random 7))
312 (setq tetris-pos-x (/ (- tetris-width (tetris-shape-width)) 2))
313 (setq tetris-pos-y 0)
314 (if (tetris-test-shape)
317 (tetris-draw-next-shape)
318 (tetris-update-score))
320 (defun tetris-draw-next-shape ()
321 (loop for y from 0 to 3 do
322 (loop for x from 0 to 3 do
323 (gamegrid-set-cell (+ tetris-next-x x)
325 (let ((tetris-shape tetris-next-shape)
327 (tetris-get-shape-cell x y))))))
329 (defun tetris-draw-shape ()
330 (loop for y from 0 to (1- (tetris-shape-height)) do
331 (loop for x from 0 to (1- (tetris-shape-width)) do
332 (let ((c (tetris-get-shape-cell x y)))
333 (if (/= c tetris-blank)
334 (gamegrid-set-cell (+ tetris-top-left-x
342 (defun tetris-erase-shape ()
343 (loop for y from 0 to (1- (tetris-shape-height)) do
344 (loop for x from 0 to (1- (tetris-shape-width)) do
345 (let ((c (tetris-get-shape-cell x y))
346 (px (+ tetris-top-left-x tetris-pos-x x))
347 (py (+ tetris-top-left-y tetris-pos-y y)))
348 (if (/= c tetris-blank)
349 (gamegrid-set-cell px py tetris-blank))))))
351 (defun tetris-test-shape ()
353 (loop for y from 0 to (1- (tetris-shape-height)) do
354 (loop for x from 0 to (1- (tetris-shape-width)) do
357 (let* ((c (tetris-get-shape-cell x y))
358 (xx (+ tetris-pos-x x))
359 (yy (+ tetris-pos-y y))
360 (px (+ tetris-top-left-x xx))
361 (py (+ tetris-top-left-y yy)))
362 (and (/= c tetris-blank)
363 (or (>= xx tetris-width)
364 (>= yy tetris-height)
365 (/= (gamegrid-get-cell px py)
369 (defun tetris-full-row (y)
371 (loop for x from 0 to (1- tetris-width) do
372 (if (= (gamegrid-get-cell (+ tetris-top-left-x x)
373 (+ tetris-top-left-y y))
378 (defun tetris-shift-row (y)
380 (loop for x from 0 to (1- tetris-width) do
381 (gamegrid-set-cell (+ tetris-top-left-x x)
382 (+ tetris-top-left-y y)
384 (loop for x from 0 to (1- tetris-width) do
385 (let ((c (gamegrid-get-cell (+ tetris-top-left-x x)
386 (+ tetris-top-left-y y -1))))
387 (gamegrid-set-cell (+ tetris-top-left-x x)
388 (+ tetris-top-left-y y)
391 (defun tetris-shift-down ()
392 (loop for y0 from 0 to (1- tetris-height) do
393 (if (tetris-full-row y0)
394 (progn (setq tetris-n-rows (1+ tetris-n-rows))
395 (loop for y from y0 downto 0 do
396 (tetris-shift-row y))))))
398 (defun tetris-draw-border-p ()
399 (or (not (eq gamegrid-display-mode 'glyph))
400 tetris-draw-border-with-glyphs))
402 (defun tetris-init-buffer ()
403 (gamegrid-init-buffer tetris-buffer-width
406 (let ((buffer-read-only nil))
407 (if (tetris-draw-border-p)
408 (loop for y from -1 to tetris-height do
409 (loop for x from -1 to tetris-width do
410 (gamegrid-set-cell (+ tetris-top-left-x x)
411 (+ tetris-top-left-y y)
413 (loop for y from 0 to (1- tetris-height) do
414 (loop for x from 0 to (1- tetris-width) do
415 (gamegrid-set-cell (+ tetris-top-left-x x)
416 (+ tetris-top-left-y y)
418 (if (tetris-draw-border-p)
419 (loop for y from -1 to 4 do
420 (loop for x from -1 to 4 do
421 (gamegrid-set-cell (+ tetris-next-x x)
425 (defun tetris-reset-game ()
426 (gamegrid-kill-timer)
428 (setq tetris-next-shape (random 7))
439 (defun tetris-shape-done ()
441 (setq tetris-n-shapes (1+ tetris-n-shapes))
444 (aref (aref tetris-shape-scores tetris-shape) tetris-rot)))
445 (tetris-update-score)
448 (defun tetris-update-game (tetris-buffer)
449 "Called on each clock tick.
450 Drops the shape one square, testing for collision."
451 (if (and (not tetris-paused)
452 (eq (current-buffer) tetris-buffer))
455 (setq tetris-pos-y (1+ tetris-pos-y))
456 (setq hit (tetris-test-shape))
458 (setq tetris-pos-y (1- tetris-pos-y)))
461 (tetris-shape-done)))))
463 (defun tetris-move-bottom ()
464 "Drops the shape to the bottom of the playing area"
469 (setq tetris-pos-y (1+ tetris-pos-y))
470 (setq hit (tetris-test-shape)))
471 (setq tetris-pos-y (1- tetris-pos-y))
473 (tetris-shape-done)))
475 (defun tetris-move-left ()
476 "Moves the shape one square to the left"
478 (unless (= tetris-pos-x 0)
480 (setq tetris-pos-x (1- tetris-pos-x))
481 (if (tetris-test-shape)
482 (setq tetris-pos-x (1+ tetris-pos-x)))
483 (tetris-draw-shape)))
485 (defun tetris-move-right ()
486 "Moves the shape one square to the right"
488 (unless (= (+ tetris-pos-x (tetris-shape-width))
491 (setq tetris-pos-x (1+ tetris-pos-x))
492 (if (tetris-test-shape)
493 (setq tetris-pos-x (1- tetris-pos-x)))
494 (tetris-draw-shape)))
496 (defun tetris-rotate-prev ()
497 "Rotates the shape clockwise"
500 (setq tetris-rot (% (+ 1 tetris-rot) 4))
501 (if (tetris-test-shape)
502 (setq tetris-rot (% (+ 3 tetris-rot) 4)))
505 (defun tetris-rotate-next ()
506 "Rotates the shape anticlockwise"
509 (setq tetris-rot (% (+ 3 tetris-rot) 4))
510 (if (tetris-test-shape)
511 (setq tetris-rot (% (+ 1 tetris-rot) 4)))
514 (defun tetris-end-game ()
515 "Terminates the current game"
517 (gamegrid-kill-timer)
518 (use-local-map tetris-null-map)
519 (gamegrid-add-score tetris-score-file tetris-score))
521 (defun tetris-start-game ()
522 "Starts a new game of Tetris"
525 (use-local-map tetris-mode-map)
526 (let ((period (or (tetris-get-tick-period)
527 tetris-default-tick-period)))
528 (gamegrid-start-timer period 'tetris-update-game)))
530 (defun tetris-pause-game ()
531 "Pauses (or resumes) the current game"
533 (setq tetris-paused (not tetris-paused))
534 (message (and tetris-paused "Game paused (press p to resume)")))
536 (defun tetris-active-p ()
537 (eq (current-local-map) tetris-mode-map))
539 (put 'tetris-mode 'mode-class 'special)
541 (defun tetris-mode ()
542 "A mode for playing Tetris.
544 tetris-mode keybindings:
547 (kill-all-local-variables)
549 (make-local-hook 'kill-buffer-hook)
550 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
552 (use-local-map tetris-null-map)
554 (setq major-mode 'tetris-mode)
555 (setq mode-name "Tetris")
557 (setq mode-popup-menu
559 ["Start new game" tetris-start-game]
560 ["End game" tetris-end-game
562 ["Pause" tetris-pause-game
563 (and (tetris-active-p) (not tetris-paused))]
564 ["Resume" tetris-pause-game
565 (and (tetris-active-p) tetris-paused)]))
567 (setq gamegrid-use-glyphs tetris-use-glyphs)
568 (setq gamegrid-use-color tetris-use-color)
570 (gamegrid-init (tetris-display-options))
572 (run-hooks 'tetris-mode-hook))
576 "Play the Tetris game.
577 Shapes drop from the top of the screen, and the user has to move and
578 rotate the shape to fit in with those at the bottom of the screen so
579 as to form complete rows.
581 tetris-mode keybindings:
583 \\[tetris-start-game] Starts a new game of Tetris
584 \\[tetris-end-game] Terminates the current game
585 \\[tetris-pause-game] Pauses (or resumes) the current game
586 \\[tetris-move-left] Moves the shape one square to the left
587 \\[tetris-move-right] Moves the shape one square to the right
588 \\[tetris-rotate-prev] Rotates the shape clockwise
589 \\[tetris-rotate-next] Rotates the shape anticlockwise
590 \\[tetris-move-bottom] Drops the shape to the bottom of the playing area
595 (switch-to-buffer tetris-buffer-name)
596 (gamegrid-kill-timer)
602 ;;; tetris.el ends here