annotate lisp/play/snake.el @ 32348:abc299ad3386

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