annotate lisp/play/snake.el @ 68648:067115a6e738

Update years in copyright notice; nfc.
author Thien-Thi Nguyen <ttn@gnuvola.org>
date Mon, 06 Feb 2006 12:31:40 +0000
parents 836785857446
children f7702c5f335d c5406394f567
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: 22489
diff changeset
1 ;;; snake.el --- implementation of Snake for Emacs
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
68634
836785857446 Update copyright notices of all files in the lisp/play directory.
Romain Francoise <romain@orebokech.com>
parents: 64701
diff changeset
3 ;; Copyright (C) 1997, 2002, 2003, 2004, 2005,
836785857446 Update copyright notices of all files in the lisp/play directory.
Romain Francoise <romain@orebokech.com>
parents: 64701
diff changeset
4 ;; 2006 Free Software Foundation, Inc.
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 ;; Author: Glynn Clements <glynn@sensei.co.uk>
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 ;; Created: 1997-09-10
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 ;; Keywords: games
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
e4d597ddd2d0 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
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 ;; any later version.
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 ;; GNU General Public License for more details.
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
e4d597ddd2d0 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
64085
18a818a2ee7c Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 62770
diff changeset
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18a818a2ee7c Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 62770
diff changeset
25 ;; Boston, MA 02110-1301, USA.
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 ;;; Commentary:
e4d597ddd2d0 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: 22489
diff changeset
29 ;;; Code:
c6e12c6b1498 Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 22489
diff changeset
30
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 (eval-when-compile
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 (require 'cl))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 (require 'gamegrid)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
38 (defvar snake-use-glyphs-flag t
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 "Non-nil means use glyphs when available.")
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
41 (defvar snake-use-color-flag t
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 "Non-nil means use color when available.")
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-name "*Snake*"
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 "Name used for Snake 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-width 30
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 "Width 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-buffer-height 22
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 "Height of used portion of buffer.")
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-width 30
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 "Width 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-height 20
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 "Height of playing area.")
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-length 5
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 "Initial length 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-x 10
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 "Initial X 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-y 10
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 "Initial Y position 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-x 1
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 "Initial X 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-initial-velocity-y 0
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 "Initial Y velocity of snake.")
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-tick-period 0.2
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 "The default time taken for the snake to advance one square.")
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-mode-hook nil
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 "Hook run upon starting Snake.")
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-x 0
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 "X 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-y snake-height
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 "Y position of score.")
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85
43650
93a9551db080 (snake-score-file): Put in home dir, not in /tmp.
Richard M. Stallman <rms@gnu.org>
parents: 43464
diff changeset
86 ;; It is not safe to put this in /tmp.
43655
1ae60187a39d fixed parens in the last patch
Sam Steingold <sds@gnu.org>
parents: 43650
diff changeset
87 ;; Someone could make a symlink in /tmp
43650
93a9551db080 (snake-score-file): Put in home dir, not in /tmp.
Richard M. Stallman <rms@gnu.org>
parents: 43464
diff changeset
88 ;; pointing to a file you don't want to clobber.
44484
d47ceb68ccc8 (snake-score-file): Default to just "snake-scores".
Colin Walters <walters@gnu.org>
parents: 43655
diff changeset
89 (defvar snake-score-file "snake-scores"
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 "File for holding high scores.")
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 (defvar snake-blank-options
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 '(((glyph colorize)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 (t ?\040))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 ((color-x color-x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 (mono-x grid-x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 (color-tty color-tty))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 (((glyph color-x) [0 0 0])
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 (color-tty "black"))))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 (defvar snake-snake-options
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 '(((glyph colorize)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 (emacs-tty ?O)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 (t ?\040))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 ((color-x color-x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 (mono-x mono-x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 (color-tty color-tty)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 (mono-tty mono-tty))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 (((glyph color-x) [1 1 0])
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 (color-tty "yellow"))))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 (defvar snake-dot-options
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 '(((glyph colorize)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 (t ?\*))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 ((color-x color-x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 (mono-x grid-x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 (color-tty color-tty))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 (((glyph color-x) [1 0 0])
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 (color-tty "red"))))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 (defvar snake-border-options
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 '(((glyph colorize)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 (t ?\+))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 ((color-x color-x)
47460
2b50b378bb16 (snake-border-options): Use color on tty if available.
Francesco Potortì <pot@gnu.org>
parents: 44484
diff changeset
127 (mono-x grid-x)
2b50b378bb16 (snake-border-options): Use color on tty if available.
Francesco Potortì <pot@gnu.org>
parents: 44484
diff changeset
128 (color-tty color-tty))
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 (((glyph color-x) [0.5 0.5 0.5])
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 (color-tty "white"))))
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 (defvar snake-space-options
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 '(((t ?\040))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134 nil
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 nil))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 (defconst snake-blank 0)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 (defconst snake-snake 1)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 (defconst snake-dot 2)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 (defconst snake-border 3)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 (defconst snake-space 4)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 (defvar snake-length 0)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 (defvar snake-velocity-x 1)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 (defvar snake-velocity-y 0)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 (defvar snake-positions nil)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 (defvar snake-cycle 0)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 (defvar snake-score 0)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (defvar snake-paused nil)
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
154 (defvar snake-moved-p nil)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
155 (defvar snake-velocity-queue nil
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
156 "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
157 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
158 This is necessary for proper behavior.
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
159
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
160 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
161 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
162 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
163 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
164 and then start moving it leftwards.")
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
165
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 (make-variable-buffer-local 'snake-length)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (make-variable-buffer-local 'snake-velocity-x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 (make-variable-buffer-local 'snake-velocity-y)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 (make-variable-buffer-local 'snake-positions)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 (make-variable-buffer-local 'snake-cycle)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 (make-variable-buffer-local 'snake-score)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 (make-variable-buffer-local 'snake-paused)
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
174 (make-variable-buffer-local 'snake-moved-p)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
175 (make-variable-buffer-local 'snake-velocity-queue)
22488
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 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 (defvar snake-mode-map
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 (make-sparse-keymap 'snake-mode-map))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 (define-key snake-mode-map "n" 'snake-start-game)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 (define-key snake-mode-map "q" 'snake-end-game)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 (define-key snake-mode-map "p" 'snake-pause-game)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 (define-key snake-mode-map [left] 'snake-move-left)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 (define-key snake-mode-map [right] 'snake-move-right)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 (define-key snake-mode-map [up] 'snake-move-up)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 (define-key snake-mode-map [down] 'snake-move-down)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 (defvar snake-null-map
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 (make-sparse-keymap 'snake-null-map))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (define-key snake-null-map "n" 'snake-start-game)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 (defun snake-display-options ()
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 (let ((options (make-vector 256 nil)))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 (loop for c from 0 to 255 do
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 (aset options c
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 (cond ((= c snake-blank)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 snake-blank-options)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 ((= c snake-snake)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 snake-snake-options)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 ((= c snake-dot)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 snake-dot-options)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 ((= c snake-border)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 snake-border-options)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 ((= c snake-space)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 snake-space-options)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 (t
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 '(nil nil nil)))))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 options))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 (defun snake-update-score ()
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 (let* ((string (format "Score: %05d" snake-score))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 (len (length string)))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 (loop for x from 0 to (1- len) do
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 (gamegrid-set-cell (+ snake-score-x x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 snake-score-y
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 (aref string x)))))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 (defun snake-init-buffer ()
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 (gamegrid-init-buffer snake-buffer-width
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 snake-buffer-height
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 snake-space)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 (let ((buffer-read-only nil))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 (loop for y from 0 to (1- snake-height) do
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 (loop for x from 0 to (1- snake-width) do
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 (gamegrid-set-cell x y snake-border)))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 (loop for y from 1 to (- snake-height 2) do
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 (loop for x from 1 to (- snake-width 2) do
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 (gamegrid-set-cell x y snake-blank)))))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 (defun snake-reset-game ()
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (gamegrid-kill-timer)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 (snake-init-buffer)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 (setq snake-length snake-initial-length
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 snake-velocity-x snake-initial-velocity-x
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 snake-velocity-y snake-initial-velocity-y
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 snake-positions nil
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 snake-cycle 1
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 snake-score 0
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
245 snake-paused nil
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
246 snake-moved-p nil
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
247 snake-velocity-queue nil)
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 (let ((x snake-initial-x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 (y snake-initial-y))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 (dotimes (i snake-length)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (gamegrid-set-cell x y snake-snake)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 (setq snake-positions (cons (vector x y) snake-positions))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 (incf x snake-velocity-x)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 (incf y snake-velocity-y)))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 (snake-update-score))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 (defun snake-update-game (snake-buffer)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 "Called on each clock tick.
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
259 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
260 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
261 (when (and (not snake-paused)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
262 (eq (current-buffer) snake-buffer))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
263 (snake-update-velocity)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
264 (let* ((pos (car snake-positions))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
265 (x (+ (aref pos 0) snake-velocity-x))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
266 (y (+ (aref pos 1) snake-velocity-y))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
267 (c (gamegrid-get-cell x y)))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
268 (if (or (= c snake-border)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
269 (= c snake-snake))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
270 (snake-end-game)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
271 (cond ((= c snake-dot)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
272 (incf snake-length)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
273 (incf snake-score)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
274 (snake-update-score))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
275 (t
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
276 (let* ((last-cons (nthcdr (- snake-length 2)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
277 snake-positions))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
278 (tail-pos (cadr last-cons))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
279 (x0 (aref tail-pos 0))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
280 (y0 (aref tail-pos 1)))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
281 (gamegrid-set-cell x0 y0
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
282 (if (= (% snake-cycle 5) 0)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
283 snake-dot
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
284 snake-blank))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
285 (incf snake-cycle)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
286 (setcdr last-cons nil))))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
287 (gamegrid-set-cell x y snake-snake)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
288 (setq snake-positions
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
289 (cons (vector x y) snake-positions))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
290 (setq snake-moved-p nil)))))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
291
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
292 (defun snake-update-velocity ()
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
293 (unless snake-moved-p
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
294 (if snake-velocity-queue
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
295 (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
296 (setq snake-velocity-x (car new-vel)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
297 snake-velocity-y (cadr new-vel))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
298 (setq snake-velocity-queue
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
299 (nreverse (cdr (nreverse snake-velocity-queue))))))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
300 (setq snake-moved-p t)))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
301
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
302 (defun snake-final-x-velocity ()
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
303 (or (caar snake-velocity-queue)
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
304 snake-velocity-x))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
305
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
306 (defun snake-final-y-velocity ()
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
307 (or (cadr (car snake-velocity-queue))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
308 snake-velocity-y))
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 (defun snake-move-left ()
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
311 "Make the snake move left."
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 (interactive)
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
313 (when (zerop (snake-final-x-velocity))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
314 (push '(-1 0) snake-velocity-queue)))
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 (defun snake-move-right ()
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
317 "Make the snake move right."
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 (interactive)
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
319 (when (zerop (snake-final-x-velocity))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
320 (push '(1 0) snake-velocity-queue)))
22488
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 (defun snake-move-up ()
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
323 "Make the snake move up."
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324 (interactive)
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
325 (when (zerop (snake-final-y-velocity))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
326 (push '(0 -1) snake-velocity-queue)))
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 (defun snake-move-down ()
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
329 "Make the snake move down."
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 (interactive)
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
331 (when (zerop (snake-final-y-velocity))
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
332 (push '(0 1) snake-velocity-queue)))
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 (defun snake-end-game ()
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
335 "Terminate the current game."
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (interactive)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 (gamegrid-kill-timer)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (use-local-map snake-null-map)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 (gamegrid-add-score snake-score-file snake-score))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 (defun snake-start-game ()
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
342 "Start a new game of Snake."
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 (interactive)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (snake-reset-game)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 (use-local-map snake-mode-map)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 (gamegrid-start-timer snake-tick-period 'snake-update-game))
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 (defun snake-pause-game ()
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
349 "Pause (or resume) the current game."
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 (interactive)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 (setq snake-paused (not snake-paused))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 (message (and snake-paused "Game paused (press p to resume)")))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 (defun snake-active-p ()
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 (eq (current-local-map) snake-mode-map))
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 (put 'snake-mode 'mode-class 'special)
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 (defun snake-mode ()
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 "A mode for playing Snake.
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
362 Snake mode keybindings:
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 \\{snake-mode-map}
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 "
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 (kill-all-local-variables)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
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 (use-local-map snake-null-map)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 (setq major-mode 'snake-mode)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 (setq mode-name "Snake")
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373
64391
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
374 (unless (featurep 'emacs)
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
375 (setq mode-popup-menu
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
376 '("Snake Commands"
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
377 ["Start new game" snake-start-game]
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
378 ["End game" snake-end-game
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
379 (snake-active-p)]
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
380 ["Pause" snake-pause-game
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
381 (and (snake-active-p) (not snake-paused))]
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
382 ["Resume" snake-pause-game
1818c2670a66 (snake-mode): Avoid warning.
Richard M. Stallman <rms@gnu.org>
parents: 64085
diff changeset
383 (and (snake-active-p) snake-paused)])))
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
385 (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
386 (setq gamegrid-use-color snake-use-color-flag)
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 (gamegrid-init (snake-display-options))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389
62770
93e71f3dcd14 * play/5x5.el (5x5-mode):
Lute Kamstra <lute@gnu.org>
parents: 62249
diff changeset
390 (run-mode-hooks 'snake-mode-hook))
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 ;;;###autoload
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (defun snake ()
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 "Play the Snake game.
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 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
396
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 Eating dots causes the snake to get longer.
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398
43464
dc17128932c4 (snake-velocity-queue, snake-update-velocity)
Richard M. Stallman <rms@gnu.org>
parents: 41485
diff changeset
399 Snake mode keybindings:
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 \\<snake-mode-map>
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 \\[snake-start-game] Starts a new game of Snake
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 \\[snake-end-game] Terminates the current game
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 \\[snake-pause-game] Pauses (or resumes) the current game
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 \\[snake-move-left] Makes the snake move left
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 \\[snake-move-right] Makes the snake move right
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 \\[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
407 \\[snake-move-down] Makes the snake move down"
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 (interactive)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 (switch-to-buffer snake-buffer-name)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411 (gamegrid-kill-timer)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 (snake-mode)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413 (snake-start-game))
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 (provide 'snake)
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 47460
diff changeset
417 ;;; arch-tag: 512ffc92-cfac-4287-9a4e-92890701a5c8
22488
e4d597ddd2d0 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 ;;; snake.el ends here