diff options
| -rw-r--r-- | lisp/play/snake.el | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/lisp/play/snake.el b/lisp/play/snake.el index 243f0190ee0..371c7133c74 100644 --- a/lisp/play/snake.el +++ b/lisp/play/snake.el | |||
| @@ -144,7 +144,6 @@ | |||
| 144 | (defvar snake-velocity-x 1) | 144 | (defvar snake-velocity-x 1) |
| 145 | (defvar snake-velocity-y 0) | 145 | (defvar snake-velocity-y 0) |
| 146 | (defvar snake-positions nil) | 146 | (defvar snake-positions nil) |
| 147 | (defvar snake-cycle 0) | ||
| 148 | (defvar snake-score 0) | 147 | (defvar snake-score 0) |
| 149 | (defvar snake-paused nil) | 148 | (defvar snake-paused nil) |
| 150 | (defvar snake-moved-p nil) | 149 | (defvar snake-moved-p nil) |
| @@ -164,7 +163,6 @@ and then start moving it leftwards.") | |||
| 164 | (make-variable-buffer-local 'snake-velocity-x) | 163 | (make-variable-buffer-local 'snake-velocity-x) |
| 165 | (make-variable-buffer-local 'snake-velocity-y) | 164 | (make-variable-buffer-local 'snake-velocity-y) |
| 166 | (make-variable-buffer-local 'snake-positions) | 165 | (make-variable-buffer-local 'snake-positions) |
| 167 | (make-variable-buffer-local 'snake-cycle) | ||
| 168 | (make-variable-buffer-local 'snake-score) | 166 | (make-variable-buffer-local 'snake-score) |
| 169 | (make-variable-buffer-local 'snake-paused) | 167 | (make-variable-buffer-local 'snake-paused) |
| 170 | (make-variable-buffer-local 'snake-moved-p) | 168 | (make-variable-buffer-local 'snake-moved-p) |
| @@ -237,7 +235,6 @@ and then start moving it leftwards.") | |||
| 237 | snake-velocity-x snake-initial-velocity-x | 235 | snake-velocity-x snake-initial-velocity-x |
| 238 | snake-velocity-y snake-initial-velocity-y | 236 | snake-velocity-y snake-initial-velocity-y |
| 239 | snake-positions nil | 237 | snake-positions nil |
| 240 | snake-cycle 1 | ||
| 241 | snake-score 0 | 238 | snake-score 0 |
| 242 | snake-paused nil | 239 | snake-paused nil |
| 243 | snake-moved-p nil | 240 | snake-moved-p nil |
| @@ -251,6 +248,14 @@ and then start moving it leftwards.") | |||
| 251 | (cl-incf y snake-velocity-y))) | 248 | (cl-incf y snake-velocity-y))) |
| 252 | (snake-update-score)) | 249 | (snake-update-score)) |
| 253 | 250 | ||
| 251 | (defun snake-set-dot () | ||
| 252 | (let ((x (random snake-width)) | ||
| 253 | (y (random snake-height))) | ||
| 254 | (while (not (= (gamegrid-get-cell x y) snake-blank)) | ||
| 255 | (setq x (random snake-width)) | ||
| 256 | (setq y (random snake-height))) | ||
| 257 | (gamegrid-set-cell x y snake-dot))) | ||
| 258 | |||
| 254 | (defun snake-update-game (snake-buffer) | 259 | (defun snake-update-game (snake-buffer) |
| 255 | "Called on each clock tick. | 260 | "Called on each clock tick. |
| 256 | Advances the snake one square, testing for collision. | 261 | Advances the snake one square, testing for collision. |
| @@ -268,23 +273,20 @@ Argument SNAKE-BUFFER is the name of the buffer." | |||
| 268 | (cond ((= c snake-dot) | 273 | (cond ((= c snake-dot) |
| 269 | (cl-incf snake-length) | 274 | (cl-incf snake-length) |
| 270 | (cl-incf snake-score) | 275 | (cl-incf snake-score) |
| 271 | (snake-update-score)) | 276 | (snake-update-score) |
| 277 | (snake-set-dot)) | ||
| 272 | (t | 278 | (t |
| 273 | (let* ((last-cons (nthcdr (- snake-length 2) | 279 | (let* ((last-cons (nthcdr (- snake-length 2) |
| 274 | snake-positions)) | 280 | snake-positions)) |
| 275 | (tail-pos (cadr last-cons)) | 281 | (tail-pos (cadr last-cons)) |
| 276 | (x0 (aref tail-pos 0)) | 282 | (x0 (aref tail-pos 0)) |
| 277 | (y0 (aref tail-pos 1))) | 283 | (y0 (aref tail-pos 1))) |
| 278 | (gamegrid-set-cell x0 y0 | 284 | (gamegrid-set-cell x0 y0 snake-blank) |
| 279 | (if (= (% snake-cycle 5) 0) | ||
| 280 | snake-dot | ||
| 281 | snake-blank)) | ||
| 282 | (cl-incf snake-cycle) | ||
| 283 | (setcdr last-cons nil)))) | 285 | (setcdr last-cons nil)))) |
| 284 | (gamegrid-set-cell x y snake-snake) | 286 | (gamegrid-set-cell x y snake-snake) |
| 285 | (setq snake-positions | 287 | (setq snake-positions |
| 286 | (cons (vector x y) snake-positions)) | 288 | (cons (vector x y) snake-positions)) |
| 287 | (setq snake-moved-p nil))))) | 289 | (setq snake-moved-p nil))))) |
| 288 | 290 | ||
| 289 | (defun snake-update-velocity () | 291 | (defun snake-update-velocity () |
| 290 | (unless snake-moved-p | 292 | (unless snake-moved-p |
| @@ -339,6 +341,7 @@ Argument SNAKE-BUFFER is the name of the buffer." | |||
| 339 | "Start a new game of Snake." | 341 | "Start a new game of Snake." |
| 340 | (interactive) | 342 | (interactive) |
| 341 | (snake-reset-game) | 343 | (snake-reset-game) |
| 344 | (snake-set-dot) | ||
| 342 | (use-local-map snake-mode-map) | 345 | (use-local-map snake-mode-map) |
| 343 | (gamegrid-start-timer snake-tick-period 'snake-update-game)) | 346 | (gamegrid-start-timer snake-tick-period 'snake-update-game)) |
| 344 | 347 | ||