Add custom groups for VC backends, for consistency with vc-bzr.
[emacs.git] / lisp / play / snake.el
blobdb54039c237c9d36227ca147e4c2bf9c33fe3be7
1 ;;; snake.el --- implementation of Snake for Emacs
3 ;; Copyright (C) 1997, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Glynn Clements <glynn@sensei.co.uk>
6 ;; Created: 1997-09-10
7 ;; Keywords: games
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile
29 (require 'cl))
31 (require 'gamegrid)
33 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 (defvar snake-use-glyphs-flag t
36 "Non-nil means use glyphs when available.")
38 (defvar snake-use-color-flag t
39 "Non-nil means use color when available.")
41 (defvar snake-buffer-name "*Snake*"
42 "Name used for Snake buffer.")
44 (defvar snake-buffer-width 30
45 "Width of used portion of buffer.")
47 (defvar snake-buffer-height 22
48 "Height of used portion of buffer.")
50 (defvar snake-width 30
51 "Width of playing area.")
53 (defvar snake-height 20
54 "Height of playing area.")
56 (defvar snake-initial-length 5
57 "Initial length of snake.")
59 (defvar snake-initial-x 10
60 "Initial X position of snake.")
62 (defvar snake-initial-y 10
63 "Initial Y position of snake.")
65 (defvar snake-initial-velocity-x 1
66 "Initial X velocity of snake.")
68 (defvar snake-initial-velocity-y 0
69 "Initial Y velocity of snake.")
71 (defvar snake-tick-period 0.2
72 "The default time taken for the snake to advance one square.")
74 (defvar snake-mode-hook nil
75 "Hook run upon starting Snake.")
77 (defvar snake-score-x 0
78 "X position of score.")
80 (defvar snake-score-y snake-height
81 "Y position of score.")
83 ;; It is not safe to put this in /tmp.
84 ;; Someone could make a symlink in /tmp
85 ;; pointing to a file you don't want to clobber.
86 (defvar snake-score-file "snake-scores"
87 "File for holding high scores.")
89 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 (defvar snake-blank-options
92 '(((glyph colorize)
93 (t ?\040))
94 ((color-x color-x)
95 (mono-x grid-x)
96 (color-tty color-tty))
97 (((glyph color-x) [0 0 0])
98 (color-tty "black"))))
100 (defvar snake-snake-options
101 '(((glyph colorize)
102 (emacs-tty ?O)
103 (t ?\040))
104 ((color-x color-x)
105 (mono-x mono-x)
106 (color-tty color-tty)
107 (mono-tty mono-tty))
108 (((glyph color-x) [1 1 0])
109 (color-tty "yellow"))))
111 (defvar snake-dot-options
112 '(((glyph colorize)
113 (t ?\*))
114 ((color-x color-x)
115 (mono-x grid-x)
116 (color-tty color-tty))
117 (((glyph color-x) [1 0 0])
118 (color-tty "red"))))
120 (defvar snake-border-options
121 '(((glyph colorize)
122 (t ?\+))
123 ((color-x color-x)
124 (mono-x grid-x)
125 (color-tty color-tty))
126 (((glyph color-x) [0.5 0.5 0.5])
127 (color-tty "white"))))
129 (defvar snake-space-options
130 '(((t ?\040))
132 nil))
134 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136 (defconst snake-blank 0)
137 (defconst snake-snake 1)
138 (defconst snake-dot 2)
139 (defconst snake-border 3)
140 (defconst snake-space 4)
142 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 (defvar snake-length 0)
145 (defvar snake-velocity-x 1)
146 (defvar snake-velocity-y 0)
147 (defvar snake-positions nil)
148 (defvar snake-cycle 0)
149 (defvar snake-score 0)
150 (defvar snake-paused nil)
151 (defvar snake-moved-p nil)
152 (defvar snake-velocity-queue nil
153 "This queue stores the velocities requested too quickly by user.
154 They will take effect one at a time at each clock-interval.
155 This is necessary for proper behavior.
157 For instance, if you are moving right, you press up and then left, you
158 want the snake to move up just once before starting to move left. If
159 we implemented all your keystrokes immediately, the snake would
160 effectively never move up. Thus, we need to move it up for one turn
161 and then start moving it leftwards.")
164 (make-variable-buffer-local 'snake-length)
165 (make-variable-buffer-local 'snake-velocity-x)
166 (make-variable-buffer-local 'snake-velocity-y)
167 (make-variable-buffer-local 'snake-positions)
168 (make-variable-buffer-local 'snake-cycle)
169 (make-variable-buffer-local 'snake-score)
170 (make-variable-buffer-local 'snake-paused)
171 (make-variable-buffer-local 'snake-moved-p)
172 (make-variable-buffer-local 'snake-velocity-queue)
174 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 (defvar snake-mode-map
177 (let ((map (make-sparse-keymap 'snake-mode-map)))
179 (define-key map "n" 'snake-start-game)
180 (define-key map "q" 'snake-end-game)
181 (define-key map "p" 'snake-pause-game)
183 (define-key map [left] 'snake-move-left)
184 (define-key map [right] 'snake-move-right)
185 (define-key map [up] 'snake-move-up)
186 (define-key map [down] 'snake-move-down)
187 map))
189 (defvar snake-null-map
190 (let ((map (make-sparse-keymap 'snake-null-map)))
191 (define-key map "n" 'snake-start-game)
192 map))
194 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 (defun snake-display-options ()
197 (let ((options (make-vector 256 nil)))
198 (loop for c from 0 to 255 do
199 (aset options c
200 (cond ((= c snake-blank)
201 snake-blank-options)
202 ((= c snake-snake)
203 snake-snake-options)
204 ((= c snake-dot)
205 snake-dot-options)
206 ((= c snake-border)
207 snake-border-options)
208 ((= c snake-space)
209 snake-space-options)
211 '(nil nil nil)))))
212 options))
214 (defun snake-update-score ()
215 (let* ((string (format "Score: %05d" snake-score))
216 (len (length string)))
217 (loop for x from 0 to (1- len) do
218 (gamegrid-set-cell (+ snake-score-x x)
219 snake-score-y
220 (aref string x)))))
222 (defun snake-init-buffer ()
223 (gamegrid-init-buffer snake-buffer-width
224 snake-buffer-height
225 snake-space)
226 (let ((buffer-read-only nil))
227 (loop for y from 0 to (1- snake-height) do
228 (loop for x from 0 to (1- snake-width) do
229 (gamegrid-set-cell x y snake-border)))
230 (loop for y from 1 to (- snake-height 2) do
231 (loop for x from 1 to (- snake-width 2) do
232 (gamegrid-set-cell x y snake-blank)))))
234 (defun snake-reset-game ()
235 (gamegrid-kill-timer)
236 (snake-init-buffer)
237 (setq snake-length snake-initial-length
238 snake-velocity-x snake-initial-velocity-x
239 snake-velocity-y snake-initial-velocity-y
240 snake-positions nil
241 snake-cycle 1
242 snake-score 0
243 snake-paused nil
244 snake-moved-p nil
245 snake-velocity-queue nil)
246 (let ((x snake-initial-x)
247 (y snake-initial-y))
248 (dotimes (i snake-length)
249 (gamegrid-set-cell x y snake-snake)
250 (setq snake-positions (cons (vector x y) snake-positions))
251 (incf x snake-velocity-x)
252 (incf y snake-velocity-y)))
253 (snake-update-score))
255 (defun snake-update-game (snake-buffer)
256 "Called on each clock tick.
257 Advances the snake one square, testing for collision.
258 Argument SNAKE-BUFFER is the name of the buffer."
259 (when (and (not snake-paused)
260 (eq (current-buffer) snake-buffer))
261 (snake-update-velocity)
262 (let* ((pos (car snake-positions))
263 (x (+ (aref pos 0) snake-velocity-x))
264 (y (+ (aref pos 1) snake-velocity-y))
265 (c (gamegrid-get-cell x y)))
266 (if (or (= c snake-border)
267 (= c snake-snake))
268 (snake-end-game)
269 (cond ((= c snake-dot)
270 (incf snake-length)
271 (incf snake-score)
272 (snake-update-score))
274 (let* ((last-cons (nthcdr (- snake-length 2)
275 snake-positions))
276 (tail-pos (cadr last-cons))
277 (x0 (aref tail-pos 0))
278 (y0 (aref tail-pos 1)))
279 (gamegrid-set-cell x0 y0
280 (if (= (% snake-cycle 5) 0)
281 snake-dot
282 snake-blank))
283 (incf snake-cycle)
284 (setcdr last-cons nil))))
285 (gamegrid-set-cell x y snake-snake)
286 (setq snake-positions
287 (cons (vector x y) snake-positions))
288 (setq snake-moved-p nil)))))
290 (defun snake-update-velocity ()
291 (unless snake-moved-p
292 (if snake-velocity-queue
293 (let ((new-vel (car (last snake-velocity-queue))))
294 (setq snake-velocity-x (car new-vel)
295 snake-velocity-y (cadr new-vel))
296 (setq snake-velocity-queue
297 (nreverse (cdr (nreverse snake-velocity-queue))))))
298 (setq snake-moved-p t)))
300 (defun snake-final-x-velocity ()
301 (or (caar snake-velocity-queue)
302 snake-velocity-x))
304 (defun snake-final-y-velocity ()
305 (or (cadr (car snake-velocity-queue))
306 snake-velocity-y))
308 (defun snake-move-left ()
309 "Make the snake move left."
310 (interactive)
311 (when (zerop (snake-final-x-velocity))
312 (push '(-1 0) snake-velocity-queue)))
314 (defun snake-move-right ()
315 "Make the snake move right."
316 (interactive)
317 (when (zerop (snake-final-x-velocity))
318 (push '(1 0) snake-velocity-queue)))
320 (defun snake-move-up ()
321 "Make the snake move up."
322 (interactive)
323 (when (zerop (snake-final-y-velocity))
324 (push '(0 -1) snake-velocity-queue)))
326 (defun snake-move-down ()
327 "Make the snake move down."
328 (interactive)
329 (when (zerop (snake-final-y-velocity))
330 (push '(0 1) snake-velocity-queue)))
332 (defun snake-end-game ()
333 "Terminate the current game."
334 (interactive)
335 (gamegrid-kill-timer)
336 (use-local-map snake-null-map)
337 (gamegrid-add-score snake-score-file snake-score))
339 (defun snake-start-game ()
340 "Start a new game of Snake."
341 (interactive)
342 (snake-reset-game)
343 (use-local-map snake-mode-map)
344 (gamegrid-start-timer snake-tick-period 'snake-update-game))
346 (defun snake-pause-game ()
347 "Pause (or resume) the current game."
348 (interactive)
349 (setq snake-paused (not snake-paused))
350 (message (and snake-paused "Game paused (press p to resume)")))
352 (defun snake-active-p ()
353 (eq (current-local-map) snake-mode-map))
355 (put 'snake-mode 'mode-class 'special)
357 (defun snake-mode ()
358 "A mode for playing Snake.
360 Snake mode keybindings:
361 \\{snake-mode-map}
363 (kill-all-local-variables)
365 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
367 (use-local-map snake-null-map)
369 (setq major-mode 'snake-mode)
370 (setq mode-name "Snake")
372 (unless (featurep 'emacs)
373 (setq mode-popup-menu
374 '("Snake Commands"
375 ["Start new game" snake-start-game]
376 ["End game" snake-end-game
377 (snake-active-p)]
378 ["Pause" snake-pause-game
379 (and (snake-active-p) (not snake-paused))]
380 ["Resume" snake-pause-game
381 (and (snake-active-p) snake-paused)])))
383 (setq gamegrid-use-glyphs snake-use-glyphs-flag)
384 (setq gamegrid-use-color snake-use-color-flag)
386 (gamegrid-init (snake-display-options))
388 (run-mode-hooks 'snake-mode-hook))
390 ;;;###autoload
391 (defun snake ()
392 "Play the Snake game.
393 Move the snake around without colliding with its tail or with the border.
395 Eating dots causes the snake to get longer.
397 Snake mode keybindings:
398 \\<snake-mode-map>
399 \\[snake-start-game] Starts a new game of Snake
400 \\[snake-end-game] Terminates the current game
401 \\[snake-pause-game] Pauses (or resumes) the current game
402 \\[snake-move-left] Makes the snake move left
403 \\[snake-move-right] Makes the snake move right
404 \\[snake-move-up] Makes the snake move up
405 \\[snake-move-down] Makes the snake move down"
406 (interactive)
408 (switch-to-buffer snake-buffer-name)
409 (gamegrid-kill-timer)
410 (snake-mode)
411 (snake-start-game))
413 (provide 'snake)
415 ;;; snake.el ends here