annotate lisp/play/tetris.el @ 41109:0bcf2d7f0112

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