emacs-devel
[Top][All Lists]
Advanced

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

Snake new patch..


From: Deepak Goel
Subject: Snake new patch..
Date: 30 Jan 2002 11:26:23 -0500
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1

Folks 

 At the end of this mail, please find a new suggested patch.. which
takes into account comments by Pavel and Stefan.  This time, i am also
cc-ing this mail to Glynn (the author of snake also).

PS: As suggested by Pavel, i did check the patch by applying the patch
to snake-cvs.el, compiling it, loading the compiled version, and
playing it.

PPS: upon stefan's suggestion, made some other minor changes to the
older file's formatting so that checkdoc-current-buffer now
completely likes the file.  


Glynn, i don't remeber why i didn't send the patch to you in the first
place, sorry for that.  I guess i must have assumed snake is too old
to be actively maintained. Or maybe i did send it to you and didn't
hear back..  Don't remember..  In any case, for some intro to the
changes i am suggesting, see the doc for `snake-velocity-que'..
here's some more intro on what the new snake does that is different:

" 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..   "


Have a good day,


Deepak                             <http://www.glue.umd.edu/~deego>
-- 
Greenspun's Tenth Rule of Programming: any sufficiently complicated C
or Fortran program contains an ad hoc informally-specified bug-ridden
slow implementation of half of Common Lisp."


> > > ====================================================
Results of diff -cw snake-cvs.el snake-new.el:

*** ./snake-cvs.el      Mon Jan 28 15:16:39 2002
--- ./snake.el  Wed Jan 30 11:03:42 2002
***************
*** 25,43 ****
  
  ;;; Commentary:
  
! ;;; Code:
  
  (eval-when-compile
    (require 'cl))
  
  (require 'gamegrid)
  
  ;; ;;;;;;;;;;;;; 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*"
--- 25,48 ----
  
  ;;; Commentary:
  
! ;;; History:
! ;; none..
! 
  
+ ;;; Code:
  (eval-when-compile
    (require 'cl))
  
  (require 'gamegrid)
  
+ (defvar snake-moved-p nil)
+ 
  ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
! (defvar snake-use-glyphs-flag t
    "Non-nil means use glyphs when available.")
  
! (defvar snake-use-color-flag t
    "Non-nil means use color when available.")
  
  (defvar snake-buffer-name "*Snake*"
***************
*** 177,182 ****
--- 182,188 ----
  ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  (defun snake-display-options ()
+   "No doc here."
    (let ((options (make-vector 256 nil)))
      (loop for c from 0 to 255 do
        (aset options c
***************
*** 195,200 ****
--- 201,207 ----
      options))
  
  (defun snake-update-score ()
+   "No doc here."
    (let* ((string (format "Score:  %05d" snake-score))
         (len (length string)))
      (loop for x from 0 to (1- len) do
***************
*** 203,208 ****
--- 210,216 ----
                         (aref string x)))))
  
  (defun snake-init-buffer ()
+   "No doc here."
    (gamegrid-init-buffer snake-buffer-width
                        snake-buffer-height
                        snake-space)
***************
*** 215,220 ****
--- 223,229 ----
                (gamegrid-set-cell x y snake-blank)))))
  
  (defun snake-reset-game ()
+   "No doc here."
    (gamegrid-kill-timer)
    (snake-init-buffer)
    (setq snake-length          snake-initial-length
***************
*** 235,243 ****
  
  (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))
        (let* ((pos (car snake-positions))
             (x (+ (aref pos 0) snake-velocity-x))
             (y (+ (aref pos 1) snake-velocity-y))
--- 244,255 ----
  
  (defun snake-update-game (snake-buffer)
    "Called on each clock tick.
! Advances the snake one square, testing for collision.
! Argument SNAKE-BUFFER is the name of the buffer."
    (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))
***************
*** 263,319 ****
                   (setcdr last-cons nil))))
          (gamegrid-set-cell x y snake-snake)
          (setq snake-positions
!               (cons (vector x y) snake-positions))))))
  
  (defun snake-move-left ()
!   "Makes the snake move left"
    (interactive)
!   (unless (= snake-velocity-x 1)
!     (setq snake-velocity-x -1
!         snake-velocity-y 0)))
  
  (defun snake-move-right ()
!   "Makes the snake move right"
    (interactive)
!   (unless (= snake-velocity-x -1)
!     (setq snake-velocity-x 1
!         snake-velocity-y 0)))
  
  (defun snake-move-up ()
!   "Makes the snake move up"
    (interactive)
!   (unless (= snake-velocity-y 1)
!     (setq snake-velocity-x 0
!         snake-velocity-y -1)))
  
  (defun snake-move-down ()
!   "Makes the snake move down"
    (interactive)
!   (unless (= snake-velocity-y -1)
!     (setq snake-velocity-x 0
!         snake-velocity-y 1)))
  
  (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)
    (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)
--- 275,375 ----
                     (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.
! Stores the velocities resulting from the keystrokes
! pressed (too quickly) by the user.  They will take effect one at a
! time at each clock-interval.  This is necessary for a proper game.
! For instance, if you are moving right, you press up and then left, you
! want the snake to move up just once before startign to move left.  If
! we implemented all your keystrokes immediately, the snake would
! effectively never move up.  Thus, we need to move it up for one turn
! and then start moving it leftwards.")
! 
! (defun snake-update-velocity ()
!   "No doc here."
!   (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 ()
!   "No doc here."
!   (if (null snake-velocity-que)
!       snake-velocity-x
!     (caar snake-velocity-que)))
! 
! (defun snake-final-y-velocity ()
!   "No doc here."
!   (if (null snake-velocity-que)
!       snake-velocity-y
!     (cadar snake-velocity-que)))
  
  (defun snake-move-left ()
!   "Make 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 ()
!   "Make 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 ()
!   "Make 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 ()
!   "Make 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 ()
!   "Start 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 ()
+   "No doc here."
    (eq (current-local-map) snake-mode-map))
  
  (put 'snake-mode 'mode-class 'special)
***************
*** 321,331 ****
  (defun snake-mode ()
    "A mode for playing Snake.
  
! snake-mode keybindings:
!    \\{snake-mode-map}
! "
    (kill-all-local-variables)
  
    (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
  
    (use-local-map snake-null-map)
--- 377,389 ----
  (defun snake-mode ()
    "A mode for playing Snake.
  
! `snake-mode' keybindings:
!    \\{snake-mode-map}"
    (kill-all-local-variables)
  
+   ;; 2002-01-28 T15:25:26-0500 (Monday)    Deepak Goel
+   ;; now removed because is also removed in the latest cvs..
+   ;; (make-local-hook 'kill-buffer-hook)
    (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
  
    (use-local-map snake-null-map)
***************
*** 343,350 ****
          ["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))
  
--- 401,408 ----
          ["Resume"             snake-pause-game
           (and (snake-active-p) snake-paused)]))
  
!   (setq gamegrid-use-glyphs snake-use-glyphs-flag)
!   (setq gamegrid-use-color snake-use-color-flag)
  
    (gamegrid-init (snake-display-options))
  
***************
*** 357,363 ****
  
  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
--- 415,421 ----
  
  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
***************
*** 365,380 ****
  \\[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)
    (snake-start-game))
  
  (provide 'snake)
  
  ;;; snake.el ends here
--- 423,438 ----
  \\[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
+ 




reply via email to

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