Mercurial > emacs
annotate lisp/play/snake.el @ 89337:6026c8e5664f
(Fset_fontset_font): Fix indentation of docstring.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Tue, 07 Jan 2003 01:28:01 +0000 |
parents | dc17128932c4 |
children | 93a9551db080 |
rev | line source |
---|---|
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
22489
diff
changeset
|
1 ;;; snake.el --- implementation of Snake for Emacs |
22488 | 2 |
3 ;; Copyright (C) 1997 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Glynn Clements <glynn@sensei.co.uk> | |
6 ;; Created: 1997-09-10 | |
7 ;; Keywords: games | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 | |
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
22489
diff
changeset
|
28 ;;; Code: |
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
22489
diff
changeset
|
29 |
22488 | 30 (eval-when-compile |
31 (require 'cl)) | |
32 | |
33 (require 'gamegrid) | |
34 | |
35 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
36 | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
37 (defvar snake-use-glyphs-flag t |
22488 | 38 "Non-nil means use glyphs when available.") |
39 | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
40 (defvar snake-use-color-flag t |
22488 | 41 "Non-nil means use color when available.") |
42 | |
43 (defvar snake-buffer-name "*Snake*" | |
44 "Name used for Snake buffer.") | |
45 | |
46 (defvar snake-buffer-width 30 | |
47 "Width of used portion of buffer.") | |
48 | |
49 (defvar snake-buffer-height 22 | |
50 "Height of used portion of buffer.") | |
51 | |
52 (defvar snake-width 30 | |
53 "Width of playing area.") | |
54 | |
55 (defvar snake-height 20 | |
56 "Height of playing area.") | |
57 | |
58 (defvar snake-initial-length 5 | |
59 "Initial length of snake.") | |
60 | |
61 (defvar snake-initial-x 10 | |
62 "Initial X position of snake.") | |
63 | |
64 (defvar snake-initial-y 10 | |
65 "Initial Y position of snake.") | |
66 | |
67 (defvar snake-initial-velocity-x 1 | |
68 "Initial X velocity of snake.") | |
69 | |
70 (defvar snake-initial-velocity-y 0 | |
71 "Initial Y velocity of snake.") | |
72 | |
73 (defvar snake-tick-period 0.2 | |
74 "The default time taken for the snake to advance one square.") | |
75 | |
76 (defvar snake-mode-hook nil | |
77 "Hook run upon starting Snake.") | |
78 | |
79 (defvar snake-score-x 0 | |
80 "X position of score.") | |
81 | |
82 (defvar snake-score-y snake-height | |
83 "Y position of score.") | |
84 | |
39499
54c9c11abe9e
(snake-score-file): Use temporary-file-directory
Eli Zaretskii <eliz@gnu.org>
parents:
38425
diff
changeset
|
85 (defvar snake-score-file (concat temporary-file-directory "snake-scores") |
22488 | 86 "File for holding high scores.") |
87 | |
88 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
89 | |
90 (defvar snake-blank-options | |
91 '(((glyph colorize) | |
92 (t ?\040)) | |
93 ((color-x color-x) | |
94 (mono-x grid-x) | |
95 (color-tty color-tty)) | |
96 (((glyph color-x) [0 0 0]) | |
97 (color-tty "black")))) | |
98 | |
99 (defvar snake-snake-options | |
100 '(((glyph colorize) | |
101 (emacs-tty ?O) | |
102 (t ?\040)) | |
103 ((color-x color-x) | |
104 (mono-x mono-x) | |
105 (color-tty color-tty) | |
106 (mono-tty mono-tty)) | |
107 (((glyph color-x) [1 1 0]) | |
108 (color-tty "yellow")))) | |
109 | |
110 (defvar snake-dot-options | |
111 '(((glyph colorize) | |
112 (t ?\*)) | |
113 ((color-x color-x) | |
114 (mono-x grid-x) | |
115 (color-tty color-tty)) | |
116 (((glyph color-x) [1 0 0]) | |
117 (color-tty "red")))) | |
118 | |
119 (defvar snake-border-options | |
120 '(((glyph colorize) | |
121 (t ?\+)) | |
122 ((color-x color-x) | |
123 (mono-x grid-x)) | |
124 (((glyph color-x) [0.5 0.5 0.5]) | |
125 (color-tty "white")))) | |
126 | |
127 (defvar snake-space-options | |
128 '(((t ?\040)) | |
129 nil | |
130 nil)) | |
131 | |
132 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
133 | |
134 (defconst snake-blank 0) | |
135 (defconst snake-snake 1) | |
136 (defconst snake-dot 2) | |
137 (defconst snake-border 3) | |
138 (defconst snake-space 4) | |
139 | |
140 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
141 | |
142 (defvar snake-length 0) | |
143 (defvar snake-velocity-x 1) | |
144 (defvar snake-velocity-y 0) | |
145 (defvar snake-positions nil) | |
146 (defvar snake-cycle 0) | |
147 (defvar snake-score 0) | |
148 (defvar snake-paused nil) | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
149 (defvar snake-moved-p nil) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
150 (defvar snake-velocity-queue nil |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
151 "This queue stores the velocities requested too quickly by user. |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
152 They will take effect one at a time at each clock-interval. |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
153 This is necessary for proper behavior. |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
154 |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
155 For instance, if you are moving right, you press up and then left, you |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
156 want the snake to move up just once before starting to move left. If |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
157 we implemented all your keystrokes immediately, the snake would |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
158 effectively never move up. Thus, we need to move it up for one turn |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
159 and then start moving it leftwards.") |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
160 |
22488 | 161 |
162 (make-variable-buffer-local 'snake-length) | |
163 (make-variable-buffer-local 'snake-velocity-x) | |
164 (make-variable-buffer-local 'snake-velocity-y) | |
165 (make-variable-buffer-local 'snake-positions) | |
166 (make-variable-buffer-local 'snake-cycle) | |
167 (make-variable-buffer-local 'snake-score) | |
168 (make-variable-buffer-local 'snake-paused) | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
169 (make-variable-buffer-local 'snake-moved-p) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
170 (make-variable-buffer-local 'snake-velocity-queue) |
22488 | 171 |
172 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
173 | |
174 (defvar snake-mode-map | |
175 (make-sparse-keymap 'snake-mode-map)) | |
176 | |
177 (define-key snake-mode-map "n" 'snake-start-game) | |
178 (define-key snake-mode-map "q" 'snake-end-game) | |
179 (define-key snake-mode-map "p" 'snake-pause-game) | |
180 | |
181 (define-key snake-mode-map [left] 'snake-move-left) | |
182 (define-key snake-mode-map [right] 'snake-move-right) | |
183 (define-key snake-mode-map [up] 'snake-move-up) | |
184 (define-key snake-mode-map [down] 'snake-move-down) | |
185 | |
186 (defvar snake-null-map | |
187 (make-sparse-keymap 'snake-null-map)) | |
188 | |
189 (define-key snake-null-map "n" 'snake-start-game) | |
190 | |
191 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
192 | |
193 (defun snake-display-options () | |
194 (let ((options (make-vector 256 nil))) | |
195 (loop for c from 0 to 255 do | |
196 (aset options c | |
197 (cond ((= c snake-blank) | |
198 snake-blank-options) | |
199 ((= c snake-snake) | |
200 snake-snake-options) | |
201 ((= c snake-dot) | |
202 snake-dot-options) | |
203 ((= c snake-border) | |
204 snake-border-options) | |
205 ((= c snake-space) | |
206 snake-space-options) | |
207 (t | |
208 '(nil nil nil))))) | |
209 options)) | |
210 | |
211 (defun snake-update-score () | |
212 (let* ((string (format "Score: %05d" snake-score)) | |
213 (len (length string))) | |
214 (loop for x from 0 to (1- len) do | |
215 (gamegrid-set-cell (+ snake-score-x x) | |
216 snake-score-y | |
217 (aref string x))))) | |
218 | |
219 (defun snake-init-buffer () | |
220 (gamegrid-init-buffer snake-buffer-width | |
221 snake-buffer-height | |
222 snake-space) | |
223 (let ((buffer-read-only nil)) | |
224 (loop for y from 0 to (1- snake-height) do | |
225 (loop for x from 0 to (1- snake-width) do | |
226 (gamegrid-set-cell x y snake-border))) | |
227 (loop for y from 1 to (- snake-height 2) do | |
228 (loop for x from 1 to (- snake-width 2) do | |
229 (gamegrid-set-cell x y snake-blank))))) | |
230 | |
231 (defun snake-reset-game () | |
232 (gamegrid-kill-timer) | |
233 (snake-init-buffer) | |
234 (setq snake-length snake-initial-length | |
235 snake-velocity-x snake-initial-velocity-x | |
236 snake-velocity-y snake-initial-velocity-y | |
237 snake-positions nil | |
238 snake-cycle 1 | |
239 snake-score 0 | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
240 snake-paused nil |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
241 snake-moved-p nil |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
242 snake-velocity-queue nil) |
22488 | 243 (let ((x snake-initial-x) |
244 (y snake-initial-y)) | |
245 (dotimes (i snake-length) | |
246 (gamegrid-set-cell x y snake-snake) | |
247 (setq snake-positions (cons (vector x y) snake-positions)) | |
248 (incf x snake-velocity-x) | |
249 (incf y snake-velocity-y))) | |
250 (snake-update-score)) | |
251 | |
252 (defun snake-update-game (snake-buffer) | |
253 "Called on each clock tick. | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
254 Advances the snake one square, testing for collision. |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
255 Argument SNAKE-BUFFER is the name of the buffer." |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
256 (when (and (not snake-paused) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
257 (eq (current-buffer) snake-buffer)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
258 (snake-update-velocity) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
259 (let* ((pos (car snake-positions)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
260 (x (+ (aref pos 0) snake-velocity-x)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
261 (y (+ (aref pos 1) snake-velocity-y)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
262 (c (gamegrid-get-cell x y))) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
263 (if (or (= c snake-border) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
264 (= c snake-snake)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
265 (snake-end-game) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
266 (cond ((= c snake-dot) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
267 (incf snake-length) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
268 (incf snake-score) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
269 (snake-update-score)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
270 (t |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
271 (let* ((last-cons (nthcdr (- snake-length 2) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
272 snake-positions)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
273 (tail-pos (cadr last-cons)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
274 (x0 (aref tail-pos 0)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
275 (y0 (aref tail-pos 1))) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
276 (gamegrid-set-cell x0 y0 |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
277 (if (= (% snake-cycle 5) 0) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
278 snake-dot |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
279 snake-blank)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
280 (incf snake-cycle) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
281 (setcdr last-cons nil)))) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
282 (gamegrid-set-cell x y snake-snake) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
283 (setq snake-positions |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
284 (cons (vector x y) snake-positions)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
285 (setq snake-moved-p nil))))) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
286 |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
287 (defun snake-update-velocity () |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
288 (unless snake-moved-p |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
289 (if snake-velocity-queue |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
290 (let ((new-vel (car (last snake-velocity-queue)))) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
291 (setq snake-velocity-x (car new-vel) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
292 snake-velocity-y (cadr new-vel)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
293 (setq snake-velocity-queue |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
294 (nreverse (cdr (nreverse snake-velocity-queue)))))) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
295 (setq snake-moved-p t))) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
296 |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
297 (defun snake-final-x-velocity () |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
298 (or (caar snake-velocity-queue) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
299 snake-velocity-x)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
300 |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
301 (defun snake-final-y-velocity () |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
302 (or (cadr (car snake-velocity-queue)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
303 snake-velocity-y)) |
22488 | 304 |
305 (defun snake-move-left () | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
306 "Make the snake move left." |
22488 | 307 (interactive) |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
308 (when (zerop (snake-final-x-velocity)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
309 (push '(-1 0) snake-velocity-queue))) |
22488 | 310 |
311 (defun snake-move-right () | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
312 "Make the snake move right." |
22488 | 313 (interactive) |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
314 (when (zerop (snake-final-x-velocity)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
315 (push '(1 0) snake-velocity-queue))) |
22488 | 316 |
317 (defun snake-move-up () | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
318 "Make the snake move up." |
22488 | 319 (interactive) |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
320 (when (zerop (snake-final-y-velocity)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
321 (push '(0 -1) snake-velocity-queue))) |
22488 | 322 |
323 (defun snake-move-down () | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
324 "Make the snake move down." |
22488 | 325 (interactive) |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
326 (when (zerop (snake-final-y-velocity)) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
327 (push '(0 1) snake-velocity-queue))) |
22488 | 328 |
329 (defun snake-end-game () | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
330 "Terminate the current game." |
22488 | 331 (interactive) |
332 (gamegrid-kill-timer) | |
333 (use-local-map snake-null-map) | |
334 (gamegrid-add-score snake-score-file snake-score)) | |
335 | |
336 (defun snake-start-game () | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
337 "Start a new game of Snake." |
22488 | 338 (interactive) |
339 (snake-reset-game) | |
340 (use-local-map snake-mode-map) | |
341 (gamegrid-start-timer snake-tick-period 'snake-update-game)) | |
342 | |
343 (defun snake-pause-game () | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
344 "Pause (or resume) the current game." |
22488 | 345 (interactive) |
346 (setq snake-paused (not snake-paused)) | |
347 (message (and snake-paused "Game paused (press p to resume)"))) | |
348 | |
349 (defun snake-active-p () | |
350 (eq (current-local-map) snake-mode-map)) | |
351 | |
352 (put 'snake-mode 'mode-class 'special) | |
353 | |
354 (defun snake-mode () | |
355 "A mode for playing Snake. | |
356 | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
357 Snake mode keybindings: |
22488 | 358 \\{snake-mode-map} |
359 " | |
360 (kill-all-local-variables) | |
361 | |
362 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t) | |
363 | |
364 (use-local-map snake-null-map) | |
365 | |
366 (setq major-mode 'snake-mode) | |
367 (setq mode-name "Snake") | |
368 | |
369 (setq mode-popup-menu | |
370 '("Snake Commands" | |
371 ["Start new game" snake-start-game] | |
372 ["End game" snake-end-game | |
373 (snake-active-p)] | |
374 ["Pause" snake-pause-game | |
375 (and (snake-active-p) (not snake-paused))] | |
376 ["Resume" snake-pause-game | |
377 (and (snake-active-p) snake-paused)])) | |
378 | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
379 (setq gamegrid-use-glyphs snake-use-glyphs-flag) |
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
380 (setq gamegrid-use-color snake-use-color-flag) |
22488 | 381 |
382 (gamegrid-init (snake-display-options)) | |
383 | |
384 (run-hooks 'snake-mode-hook)) | |
385 | |
386 ;;;###autoload | |
387 (defun snake () | |
388 "Play the Snake game. | |
389 Move the snake around without colliding with its tail or with the border. | |
390 | |
391 Eating dots causes the snake to get longer. | |
392 | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
393 Snake mode keybindings: |
22488 | 394 \\<snake-mode-map> |
395 \\[snake-start-game] Starts a new game of Snake | |
396 \\[snake-end-game] Terminates the current game | |
397 \\[snake-pause-game] Pauses (or resumes) the current game | |
398 \\[snake-move-left] Makes the snake move left | |
399 \\[snake-move-right] Makes the snake move right | |
400 \\[snake-move-up] Makes the snake move up | |
43464
dc17128932c4
(snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents:
41485
diff
changeset
|
401 \\[snake-move-down] Makes the snake move down" |
22488 | 402 (interactive) |
403 | |
404 (switch-to-buffer snake-buffer-name) | |
405 (gamegrid-kill-timer) | |
406 (snake-mode) | |
407 (snake-start-game)) | |
408 | |
409 (provide 'snake) | |
410 | |
411 ;;; snake.el ends here |