aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuanma Barranquero2010-04-21 05:53:42 +0200
committerJuanma Barranquero2010-04-21 05:53:42 +0200
commitfcc9374695394ec4733c3cb922cd841ac1fdbff1 (patch)
tree7ae221c8a2d6e40ae4ae65e844a4e089fb7bb61e
parent3dad0889f589a14a6bd74ed18d35829169fbd0ca (diff)
downloademacs-fcc9374695394ec4733c3cb922cd841ac1fdbff1.tar.gz
emacs-fcc9374695394ec4733c3cb922cd841ac1fdbff1.zip
tetris.el: Use `define-derived-mode'; fix window selection; doc fixes.
* play/tetris.el (tetris, tetris-update-speed-function) (tetris-tty-colors, tetris-x-colors, tetris-move-bottom) (tetris-move-left, tetris-move-right, tetris-rotate-prev) (tetris-rotate-next, tetris-end-game, tetris-start-game) (tetris-pause-game): Fix typos in docstrings. (tetris-mode-map, tetris-null-map): Move initialization into declaration. (tetris-mode): Define with `define-derived-mode'; set show-trailing-whitespace to nil. (tetris): Prefer window already displaying the "*Tetris*" buffer.
-rw-r--r--lisp/ChangeLog14
-rw-r--r--lisp/play/tetris.el76
2 files changed, 49 insertions, 41 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index ca6849ce600..0aca3a2697d 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,17 @@
12010-04-21 Juanma Barranquero <lekktu@gmail.com>
2
3 Use `define-derived-mode'; fix window selection; doc fixes.
4 * play/tetris.el (tetris, tetris-update-speed-function)
5 (tetris-tty-colors, tetris-x-colors, tetris-move-bottom)
6 (tetris-move-left, tetris-move-right, tetris-rotate-prev)
7 (tetris-rotate-next, tetris-end-game, tetris-start-game)
8 (tetris-pause-game): Fix typos in docstrings.
9 (tetris-mode-map, tetris-null-map):
10 Move initialization into declaration.
11 (tetris-mode): Define with `define-derived-mode';
12 set show-trailing-whitespace to nil.
13 (tetris): Prefer window already displaying the "*Tetris*" buffer.
14
12010-04-21 Karel Klíč <kklic@redhat.com> 152010-04-21 Karel Klíč <kklic@redhat.com>
2 16
3 * files.el (backup-buffer): Handle SELinux context, and return it 17 * files.el (backup-buffer): Handle SELinux context, and return it
diff --git a/lisp/play/tetris.el b/lisp/play/tetris.el
index d38c5e44cc8..00ebbae2814 100644
--- a/lisp/play/tetris.el
+++ b/lisp/play/tetris.el
@@ -35,7 +35,7 @@
35;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 35;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 36
37(defgroup tetris nil 37(defgroup tetris nil
38 "Play a game of tetris." 38 "Play a game of Tetris."
39 :prefix "tetris-" 39 :prefix "tetris-"
40 :group 'games) 40 :group 'games)
41 41
@@ -61,10 +61,10 @@
61 61
62(defcustom tetris-update-speed-function 62(defcustom tetris-update-speed-function
63 'tetris-default-update-speed-function 63 'tetris-default-update-speed-function
64 "Function run whenever the Tetris score changes 64 "Function run whenever the Tetris score changes.
65Called with two arguments: (SHAPES ROWS) 65Called with two arguments: (SHAPES ROWS)
66SHAPES is the number of shapes which have been dropped 66SHAPES is the number of shapes which have been dropped.
67ROWS is the number of rows which have been completed 67ROWS is the number of rows which have been completed.
68 68
69If the return value is a number, it is used as the timer period." 69If the return value is a number, it is used as the timer period."
70 :group 'tetris 70 :group 'tetris
@@ -77,7 +77,7 @@ If the return value is a number, it is used as the timer period."
77 77
78(defcustom tetris-tty-colors 78(defcustom tetris-tty-colors
79 [nil "blue" "white" "yellow" "magenta" "cyan" "green" "red"] 79 [nil "blue" "white" "yellow" "magenta" "cyan" "green" "red"]
80 "Vector of colors of the various shapes in text mode 80 "Vector of colors of the various shapes in text mode.
81Element 0 is ignored." 81Element 0 is ignored."
82 :group 'tetris 82 :group 'tetris
83 :type (let ((names `("Shape 1" "Shape 2" "Shape 3" 83 :type (let ((names `("Shape 1" "Shape 2" "Shape 3"
@@ -97,7 +97,7 @@ Element 0 is ignored."
97 97
98(defcustom tetris-x-colors 98(defcustom tetris-x-colors
99 [nil [0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]] 99 [nil [0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]]
100 "Vector of colors of the various shapes 100 "Vector of colors of the various shapes.
101Element 0 is ignored." 101Element 0 is ignored."
102 :group 'tetris 102 :group 'tetris
103 :type 'sexp) 103 :type 'sexp)
@@ -274,22 +274,22 @@ Element 0 is ignored."
274;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 274;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275 275
276(defvar tetris-mode-map 276(defvar tetris-mode-map
277 (make-sparse-keymap 'tetris-mode-map)) 277 (let ((map (make-sparse-keymap 'tetris-mode-map)))
278 278 (define-key map "n" 'tetris-start-game)
279(define-key tetris-mode-map "n" 'tetris-start-game) 279 (define-key map "q" 'tetris-end-game)
280(define-key tetris-mode-map "q" 'tetris-end-game) 280 (define-key map "p" 'tetris-pause-game)
281(define-key tetris-mode-map "p" 'tetris-pause-game) 281
282 282 (define-key map " " 'tetris-move-bottom)
283(define-key tetris-mode-map " " 'tetris-move-bottom) 283 (define-key map [left] 'tetris-move-left)
284(define-key tetris-mode-map [left] 'tetris-move-left) 284 (define-key map [right] 'tetris-move-right)
285(define-key tetris-mode-map [right] 'tetris-move-right) 285 (define-key map [up] 'tetris-rotate-prev)
286(define-key tetris-mode-map [up] 'tetris-rotate-prev) 286 (define-key map [down] 'tetris-rotate-next)
287(define-key tetris-mode-map [down] 'tetris-rotate-next) 287 map))
288 288
289(defvar tetris-null-map 289(defvar tetris-null-map
290 (make-sparse-keymap 'tetris-null-map)) 290 (let ((map (make-sparse-keymap 'tetris-null-map)))
291 291 (define-key map "n" 'tetris-start-game)
292(define-key tetris-null-map "n" 'tetris-start-game) 292 map))
293 293
294;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 294;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
295 295
@@ -508,7 +508,7 @@ Drops the shape one square, testing for collision."
508 (tetris-shape-done))))) 508 (tetris-shape-done)))))
509 509
510(defun tetris-move-bottom () 510(defun tetris-move-bottom ()
511 "Drops the shape to the bottom of the playing area" 511 "Drop the shape to the bottom of the playing area."
512 (interactive) 512 (interactive)
513 (if (not tetris-paused) 513 (if (not tetris-paused)
514 (let ((hit nil)) 514 (let ((hit nil))
@@ -521,7 +521,7 @@ Drops the shape one square, testing for collision."
521 (tetris-shape-done)))) 521 (tetris-shape-done))))
522 522
523(defun tetris-move-left () 523(defun tetris-move-left ()
524 "Moves the shape one square to the left" 524 "Move the shape one square to the left."
525 (interactive) 525 (interactive)
526 (unless (or (= tetris-pos-x 0) 526 (unless (or (= tetris-pos-x 0)
527 tetris-paused) 527 tetris-paused)
@@ -532,7 +532,7 @@ Drops the shape one square, testing for collision."
532 (tetris-draw-shape))) 532 (tetris-draw-shape)))
533 533
534(defun tetris-move-right () 534(defun tetris-move-right ()
535 "Moves the shape one square to the right" 535 "Move the shape one square to the right."
536 (interactive) 536 (interactive)
537 (unless (or (= (+ tetris-pos-x (tetris-shape-width)) 537 (unless (or (= (+ tetris-pos-x (tetris-shape-width))
538 tetris-width) 538 tetris-width)
@@ -544,7 +544,7 @@ Drops the shape one square, testing for collision."
544 (tetris-draw-shape))) 544 (tetris-draw-shape)))
545 545
546(defun tetris-rotate-prev () 546(defun tetris-rotate-prev ()
547 "Rotates the shape clockwise" 547 "Rotate the shape clockwise."
548 (interactive) 548 (interactive)
549 (if (not tetris-paused) 549 (if (not tetris-paused)
550 (progn (tetris-erase-shape) 550 (progn (tetris-erase-shape)
@@ -554,7 +554,7 @@ Drops the shape one square, testing for collision."
554 (tetris-draw-shape)))) 554 (tetris-draw-shape))))
555 555
556(defun tetris-rotate-next () 556(defun tetris-rotate-next ()
557 "Rotates the shape anticlockwise" 557 "Rotate the shape anticlockwise."
558 (interactive) 558 (interactive)
559 (if (not tetris-paused) 559 (if (not tetris-paused)
560 (progn 560 (progn
@@ -565,14 +565,14 @@ Drops the shape one square, testing for collision."
565 (tetris-draw-shape)))) 565 (tetris-draw-shape))))
566 566
567(defun tetris-end-game () 567(defun tetris-end-game ()
568 "Terminates the current game" 568 "Terminate the current game."
569 (interactive) 569 (interactive)
570 (gamegrid-kill-timer) 570 (gamegrid-kill-timer)
571 (use-local-map tetris-null-map) 571 (use-local-map tetris-null-map)
572 (gamegrid-add-score tetris-score-file tetris-score)) 572 (gamegrid-add-score tetris-score-file tetris-score))
573 573
574(defun tetris-start-game () 574(defun tetris-start-game ()
575 "Starts a new game of Tetris" 575 "Start a new game of Tetris."
576 (interactive) 576 (interactive)
577 (tetris-reset-game) 577 (tetris-reset-game)
578 (use-local-map tetris-mode-map) 578 (use-local-map tetris-mode-map)
@@ -581,7 +581,7 @@ Drops the shape one square, testing for collision."
581 (gamegrid-start-timer period 'tetris-update-game))) 581 (gamegrid-start-timer period 'tetris-update-game)))
582 582
583(defun tetris-pause-game () 583(defun tetris-pause-game ()
584 "Pauses (or resumes) the current game" 584 "Pause (or resume) the current game."
585 (interactive) 585 (interactive)
586 (setq tetris-paused (not tetris-paused)) 586 (setq tetris-paused (not tetris-paused))
587 (message (and tetris-paused "Game paused (press p to resume)"))) 587 (message (and tetris-paused "Game paused (press p to resume)")))
@@ -591,21 +591,13 @@ Drops the shape one square, testing for collision."
591 591
592(put 'tetris-mode 'mode-class 'special) 592(put 'tetris-mode 'mode-class 'special)
593 593
594(defun tetris-mode () 594(define-derived-mode tetris-mode nil "Tetris"
595 "A mode for playing Tetris. 595 "A mode for playing Tetris."
596
597tetris-mode keybindings:
598 \\{tetris-mode-map}
599"
600 (kill-all-local-variables)
601 596
602 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t) 597 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
603 598
604 (use-local-map tetris-null-map) 599 (use-local-map tetris-null-map)
605 600
606 (setq major-mode 'tetris-mode)
607 (setq mode-name "Tetris")
608
609 (unless (featurep 'emacs) 601 (unless (featurep 'emacs)
610 (setq mode-popup-menu 602 (setq mode-popup-menu
611 '("Tetris Commands" 603 '("Tetris Commands"
@@ -617,12 +609,12 @@ tetris-mode keybindings:
617 ["Resume" tetris-pause-game 609 ["Resume" tetris-pause-game
618 (and (tetris-active-p) tetris-paused)]))) 610 (and (tetris-active-p) tetris-paused)])))
619 611
612 (setq show-trailing-whitespace nil)
613
620 (setq gamegrid-use-glyphs tetris-use-glyphs) 614 (setq gamegrid-use-glyphs tetris-use-glyphs)
621 (setq gamegrid-use-color tetris-use-color) 615 (setq gamegrid-use-color tetris-use-color)
622 616
623 (gamegrid-init (tetris-display-options)) 617 (gamegrid-init (tetris-display-options)))
624
625 (run-mode-hooks 'tetris-mode-hook))
626 618
627;;;###autoload 619;;;###autoload
628(defun tetris () 620(defun tetris ()
@@ -645,6 +637,8 @@ tetris-mode keybindings:
645" 637"
646 (interactive) 638 (interactive)
647 639
640 (select-window (or (get-buffer-window tetris-buffer-name)
641 (selected-window)))
648 (switch-to-buffer tetris-buffer-name) 642 (switch-to-buffer tetris-buffer-name)
649 (gamegrid-kill-timer) 643 (gamegrid-kill-timer)
650 (tetris-mode) 644 (tetris-mode)