]> code.delx.au - gnu-emacs/blob - lisp/play/tetris.el
(tetris-mode): Remove make-local-hook.
[gnu-emacs] / lisp / play / tetris.el
1 ;;; tetris.el --- implementation of Tetris for Emacs
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4
5 ;; Author: Glynn Clements <glynn@sensei.co.uk>
6 ;; Version: 2.01
7 ;; Created: 1997-08-13
8 ;; Keywords: games
9
10 ;; This file is part of GNU Emacs.
11
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)
15 ;; any later version.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32 (require 'cl))
33
34 (require 'gamegrid)
35
36 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37
38 (defvar tetris-use-glyphs t
39 "Non-nil means use glyphs when available.")
40
41 (defvar tetris-use-color t
42 "Non-nil means use color when available.")
43
44 (defvar tetris-draw-border-with-glyphs t
45 "Non-nil means draw a border even when using glyphs.")
46
47 (defvar tetris-default-tick-period 0.3
48 "The default time taken for a shape to drop one row.")
49
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
56
57 If the return value is a number, it is used as the timer period.")
58
59 (defvar tetris-mode-hook nil
60 "Hook run upon starting Tetris.")
61
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.")
66
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.")
71
72 (defvar tetris-buffer-name "*Tetris*"
73 "Name used for Tetris buffer.")
74
75 (defvar tetris-buffer-width 30
76 "Width of used portion of buffer.")
77
78 (defvar tetris-buffer-height 22
79 "Height of used portion of buffer.")
80
81 (defvar tetris-width 10
82 "Width of playing area.")
83
84 (defvar tetris-height 20
85 "Height of playing area.")
86
87 (defvar tetris-top-left-x 3
88 "X position of top left of playing area.")
89
90 (defvar tetris-top-left-y 1
91 "Y position of top left of playing area.")
92
93 (defvar tetris-next-x (+ (* 2 tetris-top-left-x) tetris-width)
94 "X position of next shape.")
95
96 (defvar tetris-next-y tetris-top-left-y
97 "Y position of next shape.")
98
99 (defvar tetris-score-x tetris-next-x
100 "X position of score.")
101
102 (defvar tetris-score-y (+ tetris-next-y 6)
103 "Y position of score.")
104
105 (defvar tetris-score-file (concat temporary-file-directory "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.")
109
110 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
111
112 (defvar tetris-border-options
113 '(((glyph colorize)
114 (t ?\+))
115 ((color-x color-x)
116 (mono-x grid-x)
117 (t nil))
118 (((glyph color-x) [0.5 0.5 0.5])
119 (t nil))))
120
121 (defvar tetris-blank-options
122 '(((glyph colorize)
123 (t ?\040))
124 ((color-x color-x)
125 (mono-x grid-x)
126 (color-tty color-tty)
127 (t nil))
128 (((glyph color-x) [0 0 0])
129 (color-tty "black")
130 (t nil))))
131
132 (defvar tetris-cell-options
133 '(((glyph colorize)
134 (emacs-tty ?O)
135 (t ?\040))
136 ((color-x color-x)
137 (mono-x mono-x)
138 (color-tty color-tty)
139 (mono-tty mono-tty)
140 (t nil))
141 ;; color information is taken from tetris-x-colors and tetris-tty-colors
142 ))
143
144 (defvar tetris-space-options
145 '(((t ?\040))
146 nil
147 nil))
148
149 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150
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]]]
156
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]]]
161
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]]]
166
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]]]
171
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]]]
176
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]]]
181
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]]]])
186
187 ;;the scoring rules were taken from "xtetris". Blocks score differently
188 ;;depending on their rotation
189
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]] )
192
193 (defconst tetris-shape-dimensions
194 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]])
195
196 (defconst tetris-blank 0)
197
198 (defconst tetris-border 8)
199
200 (defconst tetris-space 9)
201
202 (defun tetris-default-update-speed-function (shapes rows)
203 (/ 20.0 (+ 50.0 rows)))
204
205 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
206
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)
216
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)
226
227 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
228
229 (defvar tetris-mode-map
230 (make-sparse-keymap 'tetris-mode-map))
231
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)
235
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)
241
242 (defvar tetris-null-map
243 (make-sparse-keymap 'tetris-null-map))
244
245 (define-key tetris-null-map "n" 'tetris-start-game)
246
247 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248
249 (defun tetris-display-options ()
250 (let ((options (make-vector 256 nil)))
251 (loop for c from 0 to 255 do
252 (aset options c
253 (cond ((= c tetris-blank)
254 tetris-blank-options)
255 ((and (>= c 1) (<= c 7))
256 (append
257 tetris-cell-options
258 `((((glyph color-x) ,(aref tetris-x-colors c))
259 (color-tty ,(aref tetris-tty-colors c))
260 (t nil)))))
261 ((= c tetris-border)
262 tetris-border-options)
263 ((= c tetris-space)
264 tetris-space-options)
265 (t
266 '(nil nil nil)))))
267 options))
268
269 (defun tetris-get-tick-period ()
270 (if (boundp 'tetris-update-speed-function)
271 (let ((period (apply tetris-update-speed-function
272 tetris-n-shapes
273 tetris-n-rows nil)))
274 (and (numberp period) period))))
275
276 (defun tetris-get-shape-cell (x y)
277 (aref (aref (aref (aref tetris-shapes
278 tetris-shape)
279 y)
280 tetris-rot)
281 x))
282
283 (defun tetris-shape-width ()
284 (aref (aref tetris-shape-dimensions tetris-shape)
285 (% tetris-rot 2)))
286
287 (defun tetris-shape-height ()
288 (aref (aref tetris-shape-dimensions tetris-shape)
289 (- 1 (% tetris-rot 2))))
290
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)
300 (+ tetris-score-y y)
301 (aref string x)))))))
302
303 (defun tetris-update-score ()
304 (tetris-draw-score)
305 (let ((period (tetris-get-tick-period)))
306 (if period (gamegrid-set-timer period))))
307
308 (defun tetris-new-shape ()
309 (setq tetris-shape tetris-next-shape)
310 (setq tetris-rot 0)
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)
315 (tetris-end-game)
316 (tetris-draw-shape))
317 (tetris-draw-next-shape)
318 (tetris-update-score))
319
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)
324 (+ tetris-next-y y)
325 (let ((tetris-shape tetris-next-shape)
326 (tetris-rot 0))
327 (tetris-get-shape-cell x y))))))
328
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
335 tetris-pos-x
336 x)
337 (+ tetris-top-left-y
338 tetris-pos-y
339 y)
340 c))))))
341
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))))))
350
351 (defun tetris-test-shape ()
352 (let ((hit nil))
353 (loop for y from 0 to (1- (tetris-shape-height)) do
354 (loop for x from 0 to (1- (tetris-shape-width)) do
355 (unless hit
356 (setq hit
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)
366 tetris-blank))))))))
367 hit))
368
369 (defun tetris-full-row (y)
370 (let ((full t))
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))
374 tetris-blank)
375 (setq full nil)))
376 full))
377
378 (defun tetris-shift-row (y)
379 (if (= y 0)
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)
383 tetris-blank))
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)
389 c)))))
390
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))))))
397
398 (defun tetris-draw-border-p ()
399 (or (not (eq gamegrid-display-mode 'glyph))
400 tetris-draw-border-with-glyphs))
401
402 (defun tetris-init-buffer ()
403 (gamegrid-init-buffer tetris-buffer-width
404 tetris-buffer-height
405 tetris-space)
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)
412 tetris-border))))
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)
417 tetris-blank)))
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)
422 (+ tetris-next-y y)
423 tetris-border))))))
424
425 (defun tetris-reset-game ()
426 (gamegrid-kill-timer)
427 (tetris-init-buffer)
428 (setq tetris-next-shape (random 7))
429 (setq tetris-shape 0
430 tetris-rot 0
431 tetris-pos-x 0
432 tetris-pos-y 0
433 tetris-n-shapes 0
434 tetris-n-rows 0
435 tetris-score 0
436 tetris-paused nil)
437 (tetris-new-shape))
438
439 (defun tetris-shape-done ()
440 (tetris-shift-down)
441 (setq tetris-n-shapes (1+ tetris-n-shapes))
442 (setq tetris-score
443 (+ tetris-score
444 (aref (aref tetris-shape-scores tetris-shape) tetris-rot)))
445 (tetris-update-score)
446 (tetris-new-shape))
447
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))
453 (let (hit)
454 (tetris-erase-shape)
455 (setq tetris-pos-y (1+ tetris-pos-y))
456 (setq hit (tetris-test-shape))
457 (if hit
458 (setq tetris-pos-y (1- tetris-pos-y)))
459 (tetris-draw-shape)
460 (if hit
461 (tetris-shape-done)))))
462
463 (defun tetris-move-bottom ()
464 "Drops the shape to the bottom of the playing area"
465 (interactive)
466 (let ((hit nil))
467 (tetris-erase-shape)
468 (while (not hit)
469 (setq tetris-pos-y (1+ tetris-pos-y))
470 (setq hit (tetris-test-shape)))
471 (setq tetris-pos-y (1- tetris-pos-y))
472 (tetris-draw-shape)
473 (tetris-shape-done)))
474
475 (defun tetris-move-left ()
476 "Moves the shape one square to the left"
477 (interactive)
478 (unless (= tetris-pos-x 0)
479 (tetris-erase-shape)
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)))
484
485 (defun tetris-move-right ()
486 "Moves the shape one square to the right"
487 (interactive)
488 (unless (= (+ tetris-pos-x (tetris-shape-width))
489 tetris-width)
490 (tetris-erase-shape)
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)))
495
496 (defun tetris-rotate-prev ()
497 "Rotates the shape clockwise"
498 (interactive)
499 (tetris-erase-shape)
500 (setq tetris-rot (% (+ 1 tetris-rot) 4))
501 (if (tetris-test-shape)
502 (setq tetris-rot (% (+ 3 tetris-rot) 4)))
503 (tetris-draw-shape))
504
505 (defun tetris-rotate-next ()
506 "Rotates the shape anticlockwise"
507 (interactive)
508 (tetris-erase-shape)
509 (setq tetris-rot (% (+ 3 tetris-rot) 4))
510 (if (tetris-test-shape)
511 (setq tetris-rot (% (+ 1 tetris-rot) 4)))
512 (tetris-draw-shape))
513
514 (defun tetris-end-game ()
515 "Terminates the current game"
516 (interactive)
517 (gamegrid-kill-timer)
518 (use-local-map tetris-null-map)
519 (gamegrid-add-score tetris-score-file tetris-score))
520
521 (defun tetris-start-game ()
522 "Starts a new game of Tetris"
523 (interactive)
524 (tetris-reset-game)
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)))
529
530 (defun tetris-pause-game ()
531 "Pauses (or resumes) the current game"
532 (interactive)
533 (setq tetris-paused (not tetris-paused))
534 (message (and tetris-paused "Game paused (press p to resume)")))
535
536 (defun tetris-active-p ()
537 (eq (current-local-map) tetris-mode-map))
538
539 (put 'tetris-mode 'mode-class 'special)
540
541 (defun tetris-mode ()
542 "A mode for playing Tetris.
543
544 tetris-mode keybindings:
545 \\{tetris-mode-map}
546 "
547 (kill-all-local-variables)
548
549 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
550
551 (use-local-map tetris-null-map)
552
553 (setq major-mode 'tetris-mode)
554 (setq mode-name "Tetris")
555
556 (setq mode-popup-menu
557 '("Tetris Commands"
558 ["Start new game" tetris-start-game]
559 ["End game" tetris-end-game
560 (tetris-active-p)]
561 ["Pause" tetris-pause-game
562 (and (tetris-active-p) (not tetris-paused))]
563 ["Resume" tetris-pause-game
564 (and (tetris-active-p) tetris-paused)]))
565
566 (setq gamegrid-use-glyphs tetris-use-glyphs)
567 (setq gamegrid-use-color tetris-use-color)
568
569 (gamegrid-init (tetris-display-options))
570
571 (run-hooks 'tetris-mode-hook))
572
573 ;;;###autoload
574 (defun tetris ()
575 "Play the Tetris game.
576 Shapes drop from the top of the screen, and the user has to move and
577 rotate the shape to fit in with those at the bottom of the screen so
578 as to form complete rows.
579
580 tetris-mode keybindings:
581 \\<tetris-mode-map>
582 \\[tetris-start-game] Starts a new game of Tetris
583 \\[tetris-end-game] Terminates the current game
584 \\[tetris-pause-game] Pauses (or resumes) the current game
585 \\[tetris-move-left] Moves the shape one square to the left
586 \\[tetris-move-right] Moves the shape one square to the right
587 \\[tetris-rotate-prev] Rotates the shape clockwise
588 \\[tetris-rotate-next] Rotates the shape anticlockwise
589 \\[tetris-move-bottom] Drops the shape to the bottom of the playing area
590
591 "
592 (interactive)
593
594 (switch-to-buffer tetris-buffer-name)
595 (gamegrid-kill-timer)
596 (tetris-mode)
597 (tetris-start-game))
598
599 (provide 'tetris)
600
601 ;;; tetris.el ends here