bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

snake.el


From: Deepak Goel
Subject: snake.el
Date: 17 Sep 2001 13:04:00 -0400
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.3

Hello

i  mailed  this  to  the  author.maintainer  and  also  posted  it  to
gnu.emacs.sources.  Upon  Stefan's suggestion,  am also posting  it to
gnu.emacs.bug:


Greetings
        
 Have you ever  wished snake didn't kill you for  'no fault of yours'?
 Snake had  a bug.  Imagine  you are moving  to the right. Now  if you
 either press "up" it is fine,  if you press "left" snake just ignores
 it.  If you press "up" followed by a "left", that is fine too as long
 as it is not too quick. But  if you press "up" followed by a "left" a
 bit too  quickly, snake  disregards your 'up'.   Okay, that  is still
 acceptable. The  problem is that  snake then does not  disregard your
 left and slams into itself and kills you.  This was definitely a bug.
 Snake should not accept the left..

 But there is an even better solution --- the snake should accept both
 your up  and a  left..  Should move  up just  one step and  them move
 left..   None of your  commands should  ever be  ignored.. yes,  i am
 talking of a que..

 To see what i am saying, you  just gotta play the old version and the
 new version..  Posted here -------
        
-----------------------------------------------------
----------------CUT HERE -------------------------------

;;; Snake.el -- Implementation of Snake for Emacs

;; Copyright (C) 1997 Free Software Foundation, Inc.

;; Author: Glynn Clements <address@hidden>
;; Created: 1997-09-10
;; Keywords: games

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Introduction:
;; Stuff that gets posted to gnu.emacs.sources
;; as introduction

(Defvar snake-introduction
  "Greetings
        
 Have you ever  wished snake didn't kill you for  'no fault of yours'?
 Snake had  a bug.  Imagine  you are moving  to the right. Now  if you
 either press \"up\" it is fine,  if you press \"left\" snake just ignores
 it.  If you press \"up\" followed by a \"left\", that is fine too as long
 as it is not too quick. But  if you press \"up\" followed by a \"left\" a
 bit too  quickly, snake  disregards your 'up'.   Okay, that  is still
 acceptable. The  problem is that  snake then does not  disregard your
 left and slams into itself and kills you.  This was definitely a bug.
 Snake should not accept the left..

 But there is an even better solution --- the snake should accept both
 your up  and a  left..  Should move  up just  one step and  them move
 left..   None of your  commands should  ever be  ignored.. yes,  i am
 talking of a que..

 To see what i am saying, you  just gotta play the old version and the
 new version..  Btw,  what's the max anyone got  with default settings
 ---- mine is, like, 350's..
        ")


(Defun snake-introduction ()
  "Provides electric help for function `snake-introduction'."
  (Interactive)
  (with-electric-help
   '(lambda () (insert snake-introduction) nil) "*doc*"))

;;



;;; Commentary:

(Eval-when-compile
  (require 'cl))

(Require 'gamegrid)

(Defvar snake-moved-p nil)

;; ;;;;;;;;;;;;; Customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(Defvar snake-use-glyphs t
  "Non-nil means use glyphs when available.")

(Defvar snake-use-color t
  "Non-nil means use color when available.")

(Defvar snake-buffer-name "*Snake*"
  "Name used for Snake buffer.")

(Defvar snake-buffer-width 30
  "Width of used portion of buffer.")

(Defvar snake-buffer-height 22
  "Height of used portion of buffer.")

(Defvar snake-width 30
  "Width of playing area.")

(Defvar snake-height 20
  "Height of playing area.")

(Defvar snake-initial-length 5
  "Initial length of snake.")

(Defvar snake-initial-x 10
  "Initial X position of snake.")

(Defvar snake-initial-y 10
  "Initial Y position of snake.")

(Defvar snake-initial-velocity-x 1
  "Initial X velocity of snake.")

(Defvar snake-initial-velocity-y 0
  "Initial Y velocity of snake.")

(Defvar snake-tick-period 0.2
  "The default time taken for the snake to advance one square.")

(Defvar snake-mode-hook nil
  "Hook run upon starting Snake.")

(Defvar snake-score-x 0
  "X position of score.")

(Defvar snake-score-y snake-height
  "Y position of score.")

(Defvar snake-score-file "/tmp/snake-scores"
  "File for holding high scores.")

;; ;;;;;;;;;;;;; Display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(Defvar snake-blank-options
  '(((glyph colorize)
     (t ?\040))
    ((color-x color-x)
     (mono-x grid-x)
     (color-tty color-tty))
    (((glyph color-x) [0 0 0])
     (color-tty "black"))))

(Defvar snake-snake-options
  '(((glyph colorize)
     (emacs-tty ?O)
     (t ?\040))
    ((color-x color-x)
     (mono-x mono-x)
     (color-tty color-tty)
     (mono-tty mono-tty))
    (((glyph color-x) [1 1 0])
     (color-tty "yellow"))))

(Defvar snake-dot-options
  '(((glyph colorize)
     (t ?\*))
    ((color-x color-x)
     (mono-x grid-x)
     (color-tty color-tty))
    (((glyph color-x) [1 0 0])
     (color-tty "red"))))

(Defvar snake-border-options
  '(((glyph colorize)
     (t ?\+))
    ((color-x color-x)
     (mono-x grid-x))
    (((glyph color-x) [0.5 0.5 0.5])
     (color-tty "white"))))

(Defvar snake-space-options
  '(((t ?\040))
    nil
    nil))

;; ;;;;;;;;;;;;; Constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(Defconst snake-blank   0)
(defconst snake-snake   1)
(defconst snake-dot     2)
(defconst snake-border  3)
(defconst snake-space   4)

;; ;;;;;;;;;;;;; Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(Defvar snake-length 0)
(defvar snake-velocity-x 1)
(defvar snake-velocity-y 0)
(defvar snake-positions nil)
(defvar snake-cycle 0)
(defvar snake-score 0)
(defvar snake-paused nil)

(Make-variable-buffer-local 'snake-length)
(make-variable-buffer-local 'snake-velocity-x)
(make-variable-buffer-local 'snake-velocity-y)
(make-variable-buffer-local 'snake-positions)
(make-variable-buffer-local 'snake-cycle)
(make-variable-buffer-local 'snake-score)
(make-variable-buffer-local 'snake-paused)

;; ;;;;;;;;;;;;; Keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(Defvar snake-mode-map
  (make-sparse-keymap 'snake-mode-map))

(Define-key snake-mode-map "n"          'snake-start-game)
(define-key snake-mode-map "q"          'snake-end-game)
(define-key snake-mode-map "p"          'snake-pause-game)

(Define-key snake-mode-map [left]       'snake-move-left)
(define-key snake-mode-map [right]      'snake-move-right)
(define-key snake-mode-map [up]         'snake-move-up)
(define-key snake-mode-map [down]       'snake-move-down)

(Defvar snake-null-map
  (make-sparse-keymap 'snake-null-map))

(Define-key snake-null-map "n"          'snake-start-game)

;; ;;;;;;;;;;;;;;;; Game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(Defun snake-display-options ()
  (let ((options (make-vector 256 nil)))
    (loop for c from 0 to 255 do
      (aset options c
            (cond ((= c snake-blank)
                   snake-blank-options)
                  ((= c snake-snake)
                   snake-snake-options)
                  ((= c snake-dot)
                   snake-dot-options)
                  ((= c snake-border)
                   snake-border-options)
                  ((= c snake-space)
                   snake-space-options)
                  (t
                   '(nil nil nil)))))
    options))

(Defun snake-update-score ()
  (let* ((string (format "Score:  %05d" snake-score))
         (len (length string)))
    (loop for x from 0 to (1- len) do
      (gamegrid-set-cell (+ snake-score-x x)
                         snake-score-y
                         (aref string x)))))

(Defun snake-init-buffer ()
  (gamegrid-init-buffer snake-buffer-width
                        snake-buffer-height
                        snake-space)
  (let ((buffer-read-only nil))
    (loop for y from 0 to (1- snake-height) do
          (loop for x from 0 to (1- snake-width) do
                (gamegrid-set-cell x y snake-border)))
    (loop for y from 1 to (- snake-height 2) do
          (loop for x from 1 to (- snake-width 2) do
                (gamegrid-set-cell x y snake-blank)))))

(Defun snake-reset-game ()
  (gamegrid-kill-timer)
  (snake-init-buffer)
  (setq snake-length            snake-initial-length
        snake-velocity-x        snake-initial-velocity-x
        snake-velocity-y        snake-initial-velocity-y
        snake-positions         nil
        snake-cycle             1
        snake-score             0
        snake-paused            nil)
  (let ((x snake-initial-x)
        (y snake-initial-y))
    (dotimes (i snake-length)
      (gamegrid-set-cell x y snake-snake)
      (setq snake-positions (cons (vector x y) snake-positions))
      (incf x snake-velocity-x)
      (incf y snake-velocity-y)))
  (snake-update-score))

(Defun snake-update-game (snake-buffer)
  "Called on each clock tick.
Advances the snake one square, testing for collision."
  (If (and (not snake-paused)
           (eq (current-buffer) snake-buffer))
      (progn
        (snake-update-velocity)
        (let* ((pos (car snake-positions))
               (x (+ (aref pos 0) snake-velocity-x))
               (y (+ (aref pos 1) snake-velocity-y))
               (c (gamegrid-get-cell x y)))
          (if (or (= c snake-border)
                  (= c snake-snake))
              (snake-end-game)
            (cond ((= c snake-dot)
                   (incf snake-length)
                   (incf snake-score)
                   (snake-update-score))
                  (t
                   (let* ((last-cons (nthcdr (- snake-length 2)
                                             snake-positions))
                          (tail-pos (cadr last-cons))
                          (x0 (aref tail-pos 0))
                          (y0 (aref tail-pos 1)))
                     (gamegrid-set-cell x0 y0
                                        (if (= (% snake-cycle 5) 0)
                                            snake-dot
                                          snake-blank))
                     (incf snake-cycle)
                     (setcdr last-cons nil))))
            (gamegrid-set-cell x y snake-snake)
            (setq snake-positions
                  (cons (vector x y) snake-positions)))
          (setq snake-moved-p nil)))))

(Defvar snake-velocity-que nil
  "Is a que..")

(Defun snake-update-velocity ()
  (unless snake-moved-p
    (if snake-velocity-que
        (let ((new-vel (reverse snake-velocity-que)))
          (setq snake-velocity-x (first (first new-vel))
                snake-velocity-y (second (first new-vel)))
          (setq snake-velocity-que (reverse (cdr new-vel)))))
    (setq snake-moved-p t)))

(Defun snake-final-x-velocity ()
  (if (null snake-velocity-que)
      snake-velocity-x
    (caar snake-velocity-que)))

(Defun snake-final-y-velocity ()
  (if (null snake-velocity-que)
      snake-velocity-y
    (cadar snake-velocity-que)))

(Defun snake-move-left ()
  "Makes the snake move left"
  (interactive)
  (when
      (zerop (snake-final-x-velocity))
    (setq snake-velocity-que
          (cons '(-1 0)
                snake-velocity-que))))

(Defun snake-move-right ()
  "Makes the snake move right"
  (interactive)
  (when
      (zerop (snake-final-x-velocity))
    (setq snake-velocity-que
          (cons '(1 0)
                snake-velocity-que))))

(Defun snake-move-up ()
  "Makes the snake move up"
  (interactive)
  (when
      (zerop (snake-final-y-velocity))
    (setq snake-velocity-que
          (cons '(0 -1)
                snake-velocity-que))))

(Defun snake-move-down ()
  "Makes the snake move down"
  (interactive)
  (when
      (zerop (snake-final-y-velocity))
    (setq snake-velocity-que
          (cons '(0 1)
                snake-velocity-que))))

(Defun snake-end-game ()
  "Terminates the current game"
  (interactive)
  (gamegrid-kill-timer)
  (use-local-map snake-null-map)
  (gamegrid-add-score snake-score-file snake-score))

(Defun snake-start-game ()
  "Starts a new game of Snake"
  (interactive)
  (snake-reset-game)
  (setq snake-velocity-que nil)
  (use-local-map snake-mode-map)
  (gamegrid-start-timer snake-tick-period 'snake-update-game))

(Defun snake-pause-game ()
  "Pauses (or resumes) the current game"
  (interactive)
  (setq snake-paused (not snake-paused))
  (message (and snake-paused "Game paused (press p to resume)")))

(Defun snake-active-p ()
  (eq (current-local-map) snake-mode-map))

(Put 'snake-mode 'mode-class 'special)

(Defun snake-mode ()
  "A mode for playing Snake.

Snake-mode keybindings:
   \\{snake-mode-map}
"
  (kill-all-local-variables)

  (Make-local-hook 'kill-buffer-hook)
  (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)

  (Use-local-map snake-null-map)

  (Setq major-mode 'snake-mode)
  (setq mode-name "Snake")

  (Setq mode-popup-menu
        '("Snake Commands"
          ["Start new game"     snake-start-game]
          ["End game"           snake-end-game
           (snake-active-p)]
          ["Pause"              snake-pause-game
           (and (snake-active-p) (not snake-paused))]
          ["Resume"             snake-pause-game
           (and (snake-active-p) snake-paused)]))

  (Setq gamegrid-use-glyphs snake-use-glyphs)
  (setq gamegrid-use-color snake-use-color)

  (Gamegrid-init (snake-display-options))

  (Run-hooks 'snake-mode-hook))

;;;###Autoload
(defun snake ()
  "Play the Snake game.
Move the snake around without colliding with its tail or with the border.

Eating dots causes the snake to get longer.

Snake-mode keybindings:
   \\<snake-mode-map>
\\[snake-start-game]    Starts a new game of Snake
\\[snake-end-game]      Terminates the current game
\\[snake-pause-game]    Pauses (or resumes) the current game
\\[snake-move-left]     Makes the snake move left
\\[snake-move-right]    Makes the snake move right
\\[snake-move-up]       Makes the snake move up
\\[snake-move-down]     Makes the snake move down

"
  (Interactive)

  (Switch-to-buffer snake-buffer-name)
  (gamegrid-kill-timer)
  (snake-mode)
  (setq snake-moved-p nil)
  (snake-start-game))

(Provide 'snake)

;;; Snake.el ends here

-------------------- End of forwarded message --------------------


Have a good day,

Deepak                             <URL:http://www.glue.umd.edu/~deego>
--
Get a REAL religion. Adopt a penguin. Worship the LAMBDA.  Shun the borg. 
Down with terrorism.    Use parentheses, use prefix.         Try gnu.org. 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]