Mercurial > emacs
annotate lisp/play/tetris.el @ 74563:da4c1968d862
Fix spurious open-paren-in-column-0 in a comment.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Sun, 10 Dec 2006 22:00:27 +0000 |
parents | f7702c5f335d |
children | e3694f1cb928 6588c6259dfb |
rev | line source |
---|---|
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
22490
diff
changeset
|
1 ;;; tetris.el --- implementation of Tetris for Emacs |
22490 | 2 |
74509 | 3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, |
68634
836785857446
Update copyright notices of all files in the lisp/play directory.
Romain Francoise <romain@orebokech.com>
parents:
64701
diff
changeset
|
4 ;; 2006 Free Software Foundation, Inc. |
22490 | 5 |
6 ;; Author: Glynn Clements <glynn@sensei.co.uk> | |
7 ;; Version: 2.01 | |
8 ;; Created: 1997-08-13 | |
9 ;; Keywords: games | |
10 | |
11 ;; This file is part of GNU Emacs. | |
12 | |
13 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
14 ;; it under the terms of the GNU General Public License as published by | |
15 ;; the Free Software Foundation; either version 2, or (at your option) | |
16 ;; any later version. | |
17 | |
18 ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 ;; GNU General Public License for more details. | |
22 | |
23 ;; You should have received a copy of the GNU General Public License | |
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 ;; Boston, MA 02110-1301, USA. | |
22490 | 27 |
28 ;;; Commentary: | |
29 | |
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
22490
diff
changeset
|
30 ;;; Code: |
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
22490
diff
changeset
|
31 |
22490 | 32 (eval-when-compile |
33 (require 'cl)) | |
34 | |
35 (require 'gamegrid) | |
36 | |
37 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
38 | |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
39 (defgroup tetris nil |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
40 "Play a game of tetris." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
41 :prefix "tetris-" |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
42 :group 'games) |
22490 | 43 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
44 (defcustom tetris-use-glyphs t |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
45 "*Non-nil means use glyphs when available." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
46 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
47 :type 'boolean) |
22490 | 48 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
49 (defcustom tetris-use-color t |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
50 "*Non-nil means use color when available." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
51 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
52 :type 'boolean) |
22490 | 53 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
54 (defcustom tetris-draw-border-with-glyphs t |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
55 "*Non-nil means draw a border even when using glyphs." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
56 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
57 :type 'boolean) |
22490 | 58 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
59 (defcustom tetris-default-tick-period 0.3 |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
60 "*The default time taken for a shape to drop one row." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
61 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
62 :type 'number) |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
63 |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
64 (defcustom tetris-update-speed-function |
22490 | 65 'tetris-default-update-speed-function |
66 "Function run whenever the Tetris score changes | |
67 Called with two arguments: (SHAPES ROWS) | |
68 SHAPES is the number of shapes which have been dropped | |
69 ROWS is the number of rows which have been completed | |
70 | |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
71 If the return value is a number, it is used as the timer period." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
72 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
73 :type 'function) |
22490 | 74 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
75 (defcustom tetris-mode-hook nil |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
76 "Hook run upon starting Tetris." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
77 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
78 :type 'hook) |
22490 | 79 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
80 (defcustom tetris-tty-colors |
22490 | 81 [nil "blue" "white" "yellow" "magenta" "cyan" "green" "red"] |
82 "Vector of colors of the various shapes in text mode | |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
83 Element 0 is ignored." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
84 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
85 :type (let ((names `("Shape 1" "Shape 2" "Shape 3" |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
86 "Shape 4" "Shape 5" "Shape 6" "Shape 7")) |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
87 (result `(vector (const nil)))) |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
88 (while names |
43655
1ae60187a39d
fixed parens in the last patch
Sam Steingold <sds@gnu.org>
parents:
43653
diff
changeset
|
89 (add-to-list 'result |
1ae60187a39d
fixed parens in the last patch
Sam Steingold <sds@gnu.org>
parents:
43653
diff
changeset
|
90 (cons 'choice |
1ae60187a39d
fixed parens in the last patch
Sam Steingold <sds@gnu.org>
parents:
43653
diff
changeset
|
91 (cons :tag |
1ae60187a39d
fixed parens in the last patch
Sam Steingold <sds@gnu.org>
parents:
43653
diff
changeset
|
92 (cons (car names) |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
93 (mapcar (lambda (color) |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
94 (list 'const color)) |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
95 (defined-colors))))) |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
96 t) |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
97 (setq names (cdr names))) |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
98 result)) |
22490 | 99 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
100 (defcustom tetris-x-colors |
22490 | 101 [nil [0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]] |
102 "Vector of colors of the various shapes | |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
103 Element 0 is ignored." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
104 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
105 :type 'sexp) |
22490 | 106 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
107 (defcustom tetris-buffer-name "*Tetris*" |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
108 "Name used for Tetris buffer." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
109 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
110 :type 'string) |
22490 | 111 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
112 (defcustom tetris-buffer-width 30 |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
113 "Width of used portion of buffer." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
114 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
115 :type 'number) |
22490 | 116 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
117 (defcustom tetris-buffer-height 22 |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
118 "Height of used portion of buffer." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
119 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
120 :type 'number) |
22490 | 121 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
122 (defcustom tetris-width 10 |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
123 "Width of playing area." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
124 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
125 :type 'number) |
22490 | 126 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
127 (defcustom tetris-height 20 |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
128 "Height of playing area." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
129 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
130 :type 'number) |
22490 | 131 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
132 (defcustom tetris-top-left-x 3 |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
133 "X position of top left of playing area." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
134 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
135 :type 'number) |
22490 | 136 |
42921
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
137 (defcustom tetris-top-left-y 1 |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
138 "Y position of top left of playing area." |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
139 :group 'tetris |
6267794dc181
(tetris): New defgroup.
Richard M. Stallman <rms@gnu.org>
parents:
41487
diff
changeset
|
140 :type 'number) |
22490 | 141 |
142 (defvar tetris-next-x (+ (* 2 tetris-top-left-x) tetris-width) | |
143 "X position of next shape.") | |
144 | |
145 (defvar tetris-next-y tetris-top-left-y | |
146 "Y position of next shape.") | |
147 | |
148 (defvar tetris-score-x tetris-next-x | |
149 "X position of score.") | |
150 | |
151 (defvar tetris-score-y (+ tetris-next-y 6) | |
152 "Y position of score.") | |
153 | |
43653
dd7b3395a514
(tetris-score-file): Put in home dir, not in /tmp.
Richard M. Stallman <rms@gnu.org>
parents:
42921
diff
changeset
|
154 ;; It is not safe to put this in /tmp. |
43655
1ae60187a39d
fixed parens in the last patch
Sam Steingold <sds@gnu.org>
parents:
43653
diff
changeset
|
155 ;; Someone could make a symlink in /tmp |
43653
dd7b3395a514
(tetris-score-file): Put in home dir, not in /tmp.
Richard M. Stallman <rms@gnu.org>
parents:
42921
diff
changeset
|
156 ;; pointing to a file you don't want to clobber. |
44485
2fd94b88c732
(tetris-score-file): Likewise.
Colin Walters <walters@gnu.org>
parents:
43655
diff
changeset
|
157 (defvar tetris-score-file "tetris-scores" |
22490 | 158 ;; anybody with a well-connected server want to host this? |
159 ;(defvar tetris-score-file "/anonymous@ftp.pgt.com:/pub/cgw/tetris-scores" | |
160 "File for holding high scores.") | |
161 | |
162 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
163 | |
164 (defvar tetris-blank-options | |
165 '(((glyph colorize) | |
166 (t ?\040)) | |
167 ((color-x color-x) | |
168 (mono-x grid-x) | |
47461
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
169 (color-tty color-tty)) |
22490 | 170 (((glyph color-x) [0 0 0]) |
47461
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
171 (color-tty "black")))) |
22490 | 172 |
173 (defvar tetris-cell-options | |
174 '(((glyph colorize) | |
175 (emacs-tty ?O) | |
176 (t ?\040)) | |
177 ((color-x color-x) | |
178 (mono-x mono-x) | |
179 (color-tty color-tty) | |
47461
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
180 (mono-tty mono-tty)) |
22490 | 181 ;; color information is taken from tetris-x-colors and tetris-tty-colors |
182 )) | |
183 | |
47461
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
184 (defvar tetris-border-options |
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
185 '(((glyph colorize) |
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
186 (t ?\+)) |
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
187 ((color-x color-x) |
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
188 (mono-x grid-x) |
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
189 (color-tty color-tty)) |
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
190 (((glyph color-x) [0.5 0.5 0.5]) |
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
191 (color-tty "white")))) |
b2bce4f54bce
(tetris-blank-options, tetris-cell-options):
Francesco Potortì <pot@gnu.org>
parents:
44485
diff
changeset
|
192 |
22490 | 193 (defvar tetris-space-options |
194 '(((t ?\040)) | |
195 nil | |
196 nil)) | |
197 | |
198 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
199 | |
200 (defconst tetris-shapes | |
201 [[[[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]] | |
202 [[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]] | |
203 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] | |
204 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] | |
205 | |
206 [[[2 2 2 0] [0 2 0 0] [2 0 0 0] [2 2 0 0]] | |
207 [[0 0 2 0] [0 2 0 0] [2 2 2 0] [2 0 0 0]] | |
208 [[0 0 0 0] [2 2 0 0] [0 0 0 0] [2 0 0 0]] | |
209 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] | |
210 | |
211 [[[3 3 3 0] [3 3 0 0] [0 0 3 0] [3 0 0 0]] | |
212 [[3 0 0 0] [0 3 0 0] [3 3 3 0] [3 0 0 0]] | |
213 [[0 0 0 0] [0 3 0 0] [0 0 0 0] [3 3 0 0]] | |
214 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] | |
215 | |
216 [[[4 4 0 0] [0 4 0 0] [4 4 0 0] [0 4 0 0]] | |
217 [[0 4 4 0] [4 4 0 0] [0 4 4 0] [4 4 0 0]] | |
218 [[0 0 0 0] [4 0 0 0] [0 0 0 0] [4 0 0 0]] | |
219 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] | |
220 | |
221 [[[0 5 5 0] [5 0 0 0] [0 5 5 0] [5 0 0 0]] | |
222 [[5 5 0 0] [5 5 0 0] [5 5 0 0] [5 5 0 0]] | |
223 [[0 0 0 0] [0 5 0 0] [0 0 0 0] [0 5 0 0]] | |
224 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] | |
225 | |
226 [[[0 6 0 0] [6 0 0 0] [6 6 6 0] [0 6 0 0]] | |
227 [[6 6 6 0] [6 6 0 0] [0 6 0 0] [6 6 0 0]] | |
228 [[0 0 0 0] [6 0 0 0] [0 0 0 0] [0 6 0 0]] | |
229 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] | |
230 | |
231 [[[7 7 7 7] [7 0 0 0] [7 7 7 7] [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]] | |
234 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]]]) | |
235 | |
43655
1ae60187a39d
fixed parens in the last patch
Sam Steingold <sds@gnu.org>
parents:
43653
diff
changeset
|
236 ;;the scoring rules were taken from "xtetris". Blocks score differently |
22490 | 237 ;;depending on their rotation |
238 | |
43655
1ae60187a39d
fixed parens in the last patch
Sam Steingold <sds@gnu.org>
parents:
43653
diff
changeset
|
239 (defconst tetris-shape-scores |
22490 | 240 [ [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 | |
242 (defconst tetris-shape-dimensions | |
243 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]]) | |
244 | |
245 (defconst tetris-blank 0) | |
246 | |
247 (defconst tetris-border 8) | |
248 | |
249 (defconst tetris-space 9) | |
250 | |
251 (defun tetris-default-update-speed-function (shapes rows) | |
252 (/ 20.0 (+ 50.0 rows))) | |
253 | |
254 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
255 | |
256 (defvar tetris-shape 0) | |
257 (defvar tetris-rot 0) | |
258 (defvar tetris-next-shape 0) | |
259 (defvar tetris-n-shapes 0) | |
260 (defvar tetris-n-rows 0) | |
261 (defvar tetris-score 0) | |
262 (defvar tetris-pos-x 0) | |
263 (defvar tetris-pos-y 0) | |
264 (defvar tetris-paused nil) | |
265 | |
266 (make-variable-buffer-local 'tetris-shape) | |
267 (make-variable-buffer-local 'tetris-rot) | |
268 (make-variable-buffer-local 'tetris-next-shape) | |
269 (make-variable-buffer-local 'tetris-n-shapes) | |
270 (make-variable-buffer-local 'tetris-n-rows) | |
271 (make-variable-buffer-local 'tetris-score) | |
272 (make-variable-buffer-local 'tetris-pos-x) | |
273 (make-variable-buffer-local 'tetris-pos-y) | |
274 (make-variable-buffer-local 'tetris-paused) | |
275 | |
276 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
277 | |
278 (defvar tetris-mode-map | |
279 (make-sparse-keymap 'tetris-mode-map)) | |
280 | |
281 (define-key tetris-mode-map "n" 'tetris-start-game) | |
282 (define-key tetris-mode-map "q" 'tetris-end-game) | |
283 (define-key tetris-mode-map "p" 'tetris-pause-game) | |
284 | |
285 (define-key tetris-mode-map " " 'tetris-move-bottom) | |
286 (define-key tetris-mode-map [left] 'tetris-move-left) | |
287 (define-key tetris-mode-map [right] 'tetris-move-right) | |
288 (define-key tetris-mode-map [up] 'tetris-rotate-prev) | |
289 (define-key tetris-mode-map [down] 'tetris-rotate-next) | |
290 | |
291 (defvar tetris-null-map | |
292 (make-sparse-keymap 'tetris-null-map)) | |
293 | |
294 (define-key tetris-null-map "n" 'tetris-start-game) | |
295 | |
296 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
297 | |
298 (defun tetris-display-options () | |
299 (let ((options (make-vector 256 nil))) | |
300 (loop for c from 0 to 255 do | |
301 (aset options c | |
302 (cond ((= c tetris-blank) | |
303 tetris-blank-options) | |
304 ((and (>= c 1) (<= c 7)) | |
305 (append | |
306 tetris-cell-options | |
307 `((((glyph color-x) ,(aref tetris-x-colors c)) | |
308 (color-tty ,(aref tetris-tty-colors c)) | |
309 (t nil))))) | |
310 ((= c tetris-border) | |
311 tetris-border-options) | |
312 ((= c tetris-space) | |
313 tetris-space-options) | |
314 (t | |
315 '(nil nil nil))))) | |
316 options)) | |
317 | |
318 (defun tetris-get-tick-period () | |
319 (if (boundp 'tetris-update-speed-function) | |
320 (let ((period (apply tetris-update-speed-function | |
321 tetris-n-shapes | |
322 tetris-n-rows nil))) | |
323 (and (numberp period) period)))) | |
324 | |
325 (defun tetris-get-shape-cell (x y) | |
326 (aref (aref (aref (aref tetris-shapes | |
327 tetris-shape) | |
328 y) | |
329 tetris-rot) | |
330 x)) | |
331 | |
332 (defun tetris-shape-width () | |
333 (aref (aref tetris-shape-dimensions tetris-shape) | |
334 (% tetris-rot 2))) | |
335 | |
336 (defun tetris-shape-height () | |
337 (aref (aref tetris-shape-dimensions tetris-shape) | |
338 (- 1 (% tetris-rot 2)))) | |
339 | |
340 (defun tetris-draw-score () | |
341 (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes) | |
342 (format "Rows: %05d" tetris-n-rows) | |
343 (format "Score: %05d" tetris-score)))) | |
344 (loop for y from 0 to 2 do | |
345 (let* ((string (aref strings y)) | |
346 (len (length string))) | |
347 (loop for x from 0 to (1- len) do | |
348 (gamegrid-set-cell (+ tetris-score-x x) | |
349 (+ tetris-score-y y) | |
350 (aref string x))))))) | |
351 | |
352 (defun tetris-update-score () | |
353 (tetris-draw-score) | |
354 (let ((period (tetris-get-tick-period))) | |
355 (if period (gamegrid-set-timer period)))) | |
356 | |
357 (defun tetris-new-shape () | |
358 (setq tetris-shape tetris-next-shape) | |
359 (setq tetris-rot 0) | |
360 (setq tetris-next-shape (random 7)) | |
361 (setq tetris-pos-x (/ (- tetris-width (tetris-shape-width)) 2)) | |
362 (setq tetris-pos-y 0) | |
363 (if (tetris-test-shape) | |
364 (tetris-end-game) | |
365 (tetris-draw-shape)) | |
366 (tetris-draw-next-shape) | |
367 (tetris-update-score)) | |
368 | |
369 (defun tetris-draw-next-shape () | |
370 (loop for y from 0 to 3 do | |
371 (loop for x from 0 to 3 do | |
372 (gamegrid-set-cell (+ tetris-next-x x) | |
373 (+ tetris-next-y y) | |
374 (let ((tetris-shape tetris-next-shape) | |
375 (tetris-rot 0)) | |
376 (tetris-get-shape-cell x y)))))) | |
377 | |
378 (defun tetris-draw-shape () | |
379 (loop for y from 0 to (1- (tetris-shape-height)) do | |
380 (loop for x from 0 to (1- (tetris-shape-width)) do | |
381 (let ((c (tetris-get-shape-cell x y))) | |
382 (if (/= c tetris-blank) | |
383 (gamegrid-set-cell (+ tetris-top-left-x | |
384 tetris-pos-x | |
385 x) | |
386 (+ tetris-top-left-y | |
387 tetris-pos-y | |
388 y) | |
389 c)))))) | |
390 | |
391 (defun tetris-erase-shape () | |
392 (loop for y from 0 to (1- (tetris-shape-height)) do | |
393 (loop for x from 0 to (1- (tetris-shape-width)) do | |
394 (let ((c (tetris-get-shape-cell x y)) | |
395 (px (+ tetris-top-left-x tetris-pos-x x)) | |
396 (py (+ tetris-top-left-y tetris-pos-y y))) | |
397 (if (/= c tetris-blank) | |
398 (gamegrid-set-cell px py tetris-blank)))))) | |
399 | |
400 (defun tetris-test-shape () | |
401 (let ((hit nil)) | |
402 (loop for y from 0 to (1- (tetris-shape-height)) do | |
403 (loop for x from 0 to (1- (tetris-shape-width)) do | |
404 (unless hit | |
405 (setq hit | |
406 (let* ((c (tetris-get-shape-cell x y)) | |
407 (xx (+ tetris-pos-x x)) | |
408 (yy (+ tetris-pos-y y)) | |
409 (px (+ tetris-top-left-x xx)) | |
410 (py (+ tetris-top-left-y yy))) | |
411 (and (/= c tetris-blank) | |
412 (or (>= xx tetris-width) | |
413 (>= yy tetris-height) | |
414 (/= (gamegrid-get-cell px py) | |
415 tetris-blank)))))))) | |
416 hit)) | |
417 | |
418 (defun tetris-full-row (y) | |
419 (let ((full t)) | |
420 (loop for x from 0 to (1- tetris-width) do | |
421 (if (= (gamegrid-get-cell (+ tetris-top-left-x x) | |
422 (+ tetris-top-left-y y)) | |
423 tetris-blank) | |
424 (setq full nil))) | |
425 full)) | |
426 | |
427 (defun tetris-shift-row (y) | |
428 (if (= y 0) | |
429 (loop for x from 0 to (1- tetris-width) do | |
430 (gamegrid-set-cell (+ tetris-top-left-x x) | |
431 (+ tetris-top-left-y y) | |
432 tetris-blank)) | |
433 (loop for x from 0 to (1- tetris-width) do | |
434 (let ((c (gamegrid-get-cell (+ tetris-top-left-x x) | |
435 (+ tetris-top-left-y y -1)))) | |
436 (gamegrid-set-cell (+ tetris-top-left-x x) | |
437 (+ tetris-top-left-y y) | |
438 c))))) | |
439 | |
440 (defun tetris-shift-down () | |
441 (loop for y0 from 0 to (1- tetris-height) do | |
442 (if (tetris-full-row y0) | |
443 (progn (setq tetris-n-rows (1+ tetris-n-rows)) | |
444 (loop for y from y0 downto 0 do | |
445 (tetris-shift-row y)))))) | |
446 | |
447 (defun tetris-draw-border-p () | |
448 (or (not (eq gamegrid-display-mode 'glyph)) | |
449 tetris-draw-border-with-glyphs)) | |
450 | |
451 (defun tetris-init-buffer () | |
452 (gamegrid-init-buffer tetris-buffer-width | |
453 tetris-buffer-height | |
454 tetris-space) | |
455 (let ((buffer-read-only nil)) | |
456 (if (tetris-draw-border-p) | |
457 (loop for y from -1 to tetris-height do | |
458 (loop for x from -1 to tetris-width do | |
459 (gamegrid-set-cell (+ tetris-top-left-x x) | |
460 (+ tetris-top-left-y y) | |
461 tetris-border)))) | |
462 (loop for y from 0 to (1- tetris-height) do | |
463 (loop for x from 0 to (1- tetris-width) do | |
464 (gamegrid-set-cell (+ tetris-top-left-x x) | |
465 (+ tetris-top-left-y y) | |
466 tetris-blank))) | |
467 (if (tetris-draw-border-p) | |
468 (loop for y from -1 to 4 do | |
469 (loop for x from -1 to 4 do | |
470 (gamegrid-set-cell (+ tetris-next-x x) | |
471 (+ tetris-next-y y) | |
472 tetris-border)))))) | |
473 | |
474 (defun tetris-reset-game () | |
475 (gamegrid-kill-timer) | |
476 (tetris-init-buffer) | |
477 (setq tetris-next-shape (random 7)) | |
478 (setq tetris-shape 0 | |
479 tetris-rot 0 | |
480 tetris-pos-x 0 | |
481 tetris-pos-y 0 | |
482 tetris-n-shapes 0 | |
483 tetris-n-rows 0 | |
484 tetris-score 0 | |
485 tetris-paused nil) | |
486 (tetris-new-shape)) | |
487 | |
488 (defun tetris-shape-done () | |
489 (tetris-shift-down) | |
490 (setq tetris-n-shapes (1+ tetris-n-shapes)) | |
491 (setq tetris-score | |
43655
1ae60187a39d
fixed parens in the last patch
Sam Steingold <sds@gnu.org>
parents:
43653
diff
changeset
|
492 (+ tetris-score |
22490 | 493 (aref (aref tetris-shape-scores tetris-shape) tetris-rot))) |
494 (tetris-update-score) | |
495 (tetris-new-shape)) | |
496 | |
497 (defun tetris-update-game (tetris-buffer) | |
498 "Called on each clock tick. | |
499 Drops the shape one square, testing for collision." | |
500 (if (and (not tetris-paused) | |
501 (eq (current-buffer) tetris-buffer)) | |
502 (let (hit) | |
503 (tetris-erase-shape) | |
504 (setq tetris-pos-y (1+ tetris-pos-y)) | |
505 (setq hit (tetris-test-shape)) | |
506 (if hit | |
507 (setq tetris-pos-y (1- tetris-pos-y))) | |
508 (tetris-draw-shape) | |
509 (if hit | |
510 (tetris-shape-done))))) | |
511 | |
512 (defun tetris-move-bottom () | |
513 "Drops the shape to the bottom of the playing area" | |
514 (interactive) | |
515 (let ((hit nil)) | |
516 (tetris-erase-shape) | |
517 (while (not hit) | |
518 (setq tetris-pos-y (1+ tetris-pos-y)) | |
519 (setq hit (tetris-test-shape))) | |
520 (setq tetris-pos-y (1- tetris-pos-y)) | |
521 (tetris-draw-shape) | |
522 (tetris-shape-done))) | |
523 | |
524 (defun tetris-move-left () | |
525 "Moves the shape one square to the left" | |
526 (interactive) | |
527 (unless (= tetris-pos-x 0) | |
528 (tetris-erase-shape) | |
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))) | |
533 | |
534 (defun tetris-move-right () | |
535 "Moves the shape one square to the right" | |
536 (interactive) | |
537 (unless (= (+ tetris-pos-x (tetris-shape-width)) | |
538 tetris-width) | |
539 (tetris-erase-shape) | |
540 (setq tetris-pos-x (1+ tetris-pos-x)) | |
541 (if (tetris-test-shape) | |
542 (setq tetris-pos-x (1- tetris-pos-x))) | |
543 (tetris-draw-shape))) | |
544 | |
545 (defun tetris-rotate-prev () | |
546 "Rotates the shape clockwise" | |
547 (interactive) | |
548 (tetris-erase-shape) | |
549 (setq tetris-rot (% (+ 1 tetris-rot) 4)) | |
550 (if (tetris-test-shape) | |
551 (setq tetris-rot (% (+ 3 tetris-rot) 4))) | |
552 (tetris-draw-shape)) | |
553 | |
554 (defun tetris-rotate-next () | |
555 "Rotates the shape anticlockwise" | |
556 (interactive) | |
557 (tetris-erase-shape) | |
558 (setq tetris-rot (% (+ 3 tetris-rot) 4)) | |
559 (if (tetris-test-shape) | |
560 (setq tetris-rot (% (+ 1 tetris-rot) 4))) | |
561 (tetris-draw-shape)) | |
562 | |
563 (defun tetris-end-game () | |
564 "Terminates the current game" | |
565 (interactive) | |
566 (gamegrid-kill-timer) | |
567 (use-local-map tetris-null-map) | |
568 (gamegrid-add-score tetris-score-file tetris-score)) | |
569 | |
570 (defun tetris-start-game () | |
571 "Starts a new game of Tetris" | |
572 (interactive) | |
573 (tetris-reset-game) | |
574 (use-local-map tetris-mode-map) | |
575 (let ((period (or (tetris-get-tick-period) | |
576 tetris-default-tick-period))) | |
577 (gamegrid-start-timer period 'tetris-update-game))) | |
578 | |
579 (defun tetris-pause-game () | |
580 "Pauses (or resumes) the current game" | |
581 (interactive) | |
582 (setq tetris-paused (not tetris-paused)) | |
583 (message (and tetris-paused "Game paused (press p to resume)"))) | |
584 | |
585 (defun tetris-active-p () | |
586 (eq (current-local-map) tetris-mode-map)) | |
587 | |
588 (put 'tetris-mode 'mode-class 'special) | |
589 | |
590 (defun tetris-mode () | |
591 "A mode for playing Tetris. | |
592 | |
593 tetris-mode keybindings: | |
594 \\{tetris-mode-map} | |
595 " | |
596 (kill-all-local-variables) | |
597 | |
598 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t) | |
599 | |
600 (use-local-map tetris-null-map) | |
601 | |
602 (setq major-mode 'tetris-mode) | |
603 (setq mode-name "Tetris") | |
604 | |
64392
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
605 (unless (featurep 'emacs) |
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
606 (setq mode-popup-menu |
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
607 '("Tetris Commands" |
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
608 ["Start new game" tetris-start-game] |
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
609 ["End game" tetris-end-game |
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
610 (tetris-active-p)] |
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
611 ["Pause" tetris-pause-game |
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
612 (and (tetris-active-p) (not tetris-paused))] |
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
613 ["Resume" tetris-pause-game |
9f028db3b769
(tetris-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
614 (and (tetris-active-p) tetris-paused)]))) |
22490 | 615 |
616 (setq gamegrid-use-glyphs tetris-use-glyphs) | |
617 (setq gamegrid-use-color tetris-use-color) | |
618 | |
619 (gamegrid-init (tetris-display-options)) | |
620 | |
62770 | 621 (run-mode-hooks 'tetris-mode-hook)) |
22490 | 622 |
623 ;;;###autoload | |
624 (defun tetris () | |
625 "Play the Tetris game. | |
626 Shapes drop from the top of the screen, and the user has to move and | |
627 rotate the shape to fit in with those at the bottom of the screen so | |
628 as to form complete rows. | |
629 | |
630 tetris-mode keybindings: | |
631 \\<tetris-mode-map> | |
632 \\[tetris-start-game] Starts a new game of Tetris | |
633 \\[tetris-end-game] Terminates the current game | |
634 \\[tetris-pause-game] Pauses (or resumes) the current game | |
635 \\[tetris-move-left] Moves the shape one square to the left | |
636 \\[tetris-move-right] Moves the shape one square to the right | |
637 \\[tetris-rotate-prev] Rotates the shape clockwise | |
638 \\[tetris-rotate-next] Rotates the shape anticlockwise | |
639 \\[tetris-move-bottom] Drops the shape to the bottom of the playing area | |
640 | |
641 " | |
642 (interactive) | |
643 | |
644 (switch-to-buffer tetris-buffer-name) | |
645 (gamegrid-kill-timer) | |
646 (tetris-mode) | |
647 (tetris-start-game)) | |
648 | |
649 (provide 'tetris) | |
650 | |
52401 | 651 ;;; arch-tag: fb780d53-3ff0-49f0-8e19-f7f13cf2d49e |
22490 | 652 ;;; tetris.el ends here |