1 ;;; pong.el --- classical implementation of pong
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Benjamin Drieu <bdrieu@april.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This is an implementation of the classical game pong.
30 (eval-when-compile (require 'cl
))
37 "Emacs-Lisp implementation of the classical game pong."
41 (defcustom pong-buffer-name
"*Pong*"
42 "Name of the buffer used to play."
46 (defcustom pong-width
50
47 "Width of the playfield."
51 (defcustom pong-height
(min 30 (- (frame-height) 6))
52 "Height of the playfield."
56 (defcustom pong-bat-width
3
57 "Width of the bats for pong."
61 (defcustom pong-blank-color
"black"
62 "Color used for background."
66 (defcustom pong-bat-color
"yellow"
67 "Color used for bats."
71 (defcustom pong-ball-color
"red"
72 "Color used for the ball."
76 (defcustom pong-border-color
"white"
77 "Color used for pong borders."
81 (defcustom pong-left-key
"4"
82 "Alternate key to press for bat 1 to go up (primary one is [left])."
84 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
86 (defcustom pong-right-key
"6"
87 "Alternate key to press for bat 1 to go down (primary one is [right])."
89 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
91 (defcustom pong-up-key
"8"
92 "Alternate key to press for bat 2 to go up (primary one is [up])."
94 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
96 (defcustom pong-down-key
"2"
97 "Alternate key to press for bat 2 to go down (primary one is [down])."
99 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
101 (defcustom pong-quit-key
"q"
102 "Key to press to quit pong."
104 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
106 (defcustom pong-pause-key
"p"
107 "Key to press to pause pong."
109 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
111 (defcustom pong-resume-key
"p"
112 "Key to press to resume pong."
114 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
116 (defcustom pong-timer-delay
0.1
117 "Time to wait between every cycle."
122 ;;; This is black magic. Define colors used
124 (defvar pong-blank-options
129 (color-tty color-tty
))
130 (((glyph color-x
) [0 0 0])
131 (color-tty pong-blank-color
))))
133 (defvar pong-bat-options
139 (color-tty color-tty
)
141 (((glyph color-x
) [1 1 0])
142 (color-tty pong-bat-color
))))
144 (defvar pong-ball-options
149 (color-tty color-tty
))
150 (((glyph color-x
) [1 0 0])
151 (color-tty pong-ball-color
))))
153 (defvar pong-border-options
158 (color-tty color-tty
))
159 (((glyph color-x
) [0.5 0.5 0.5])
160 (color-tty pong-border-color
))))
162 (defconst pong-blank
0)
163 (defconst pong-bat
1)
164 (defconst pong-ball
2)
165 (defconst pong-border
3)
168 ;;; Determine initial positions for bats and ball
171 "Horizontal speed of the ball.")
174 "Vertical speed of the ball.")
177 "Horizontal position of the ball.")
180 "Vertical position of the ball.")
182 (defvar pong-bat-player1 nil
183 "Vertical position of bat 1.")
185 (defvar pong-bat-player2 nil
186 "Vertical position of bat 2.")
188 (defvar pong-score-player1 nil
)
189 (defvar pong-score-player2 nil
)
193 (defvar pong-mode-map
194 (make-sparse-keymap 'pong-mode-map
) "Modemap for pong-mode.")
196 (defvar pong-null-map
197 (make-sparse-keymap 'pong-null-map
) "Null map for pong-mode.")
199 (define-key pong-mode-map
[left] 'pong-move-left)
200 (define-key pong-mode-map [right] 'pong-move-right)
201 (define-key pong-mode-map [up] 'pong-move-up)
202 (define-key pong-mode-map [down] 'pong-move-down)
203 (define-key pong-mode-map pong-left-key 'pong-move-left)
204 (define-key pong-mode-map pong-right-key 'pong-move-right)
205 (define-key pong-mode-map pong-up-key 'pong-move-up)
206 (define-key pong-mode-map pong-down-key 'pong-move-down)
207 (define-key pong-mode-map pong-quit-key 'pong-quit)
208 (define-key pong-mode-map pong-pause-key 'pong-pause)
211 ;;; Fun stuff -- The code
213 (defun pong-display-options ()
214 "Computes display options (required by gamegrid for colors)."
215 (let ((options (make-vector 256 nil)))
216 (loop for c from 0 to 255 do
218 (cond ((= c pong-blank)
232 (defun pong-init-buffer ()
233 "Initialize pong buffer and draw stuff thanks to gamegrid library."
235 (get-buffer-create pong-buffer-name)
236 (switch-to-buffer pong-buffer-name)
237 (use-local-map pong-mode-map)
239 (setq gamegrid-use-glyphs t)
240 (setq gamegrid-use-color t)
241 (gamegrid-init (pong-display-options))
243 (gamegrid-init-buffer pong-width
247 (let ((buffer-read-only nil))
248 (loop for y from 0 to (1- pong-height) do
249 (loop for x from 0 to (1- pong-width) do
250 (gamegrid-set-cell x y pong-border)))
251 (loop for y from 1 to (- pong-height 2) do
252 (loop for x from 1 to (- pong-width 2) do
253 (gamegrid-set-cell x y pong-blank))))
255 (loop for y from pong-bat-player1 to (1- (+ pong-bat-player1 pong-bat-width)) do
256 (gamegrid-set-cell 2 y pong-bat))
257 (loop for y from pong-bat-player2 to (1- (+ pong-bat-player2 pong-bat-width)) do
258 (gamegrid-set-cell (- pong-width 3) y pong-bat)))
262 (defun pong-move-left ()
264 This is called left for historical reasons, since in some pong
265 implementations you move with left/right paddle."
267 (if (> pong-bat-player1 1)
269 (setq pong-bat-player1 (1- pong-bat-player1))
270 (pong-update-bat 2 pong-bat-player1))))
274 (defun pong-move-right ()
277 (if (< (+ pong-bat-player1 pong-bat-width) (1- pong-height))
279 (setq pong-bat-player1 (1+ pong-bat-player1))
280 (pong-update-bat 2 pong-bat-player1))))
284 (defun pong-move-up ()
287 (if (> pong-bat-player2 1)
289 (setq pong-bat-player2 (1- pong-bat-player2))
290 (pong-update-bat (- pong-width 3) pong-bat-player2))))
294 (defun pong-move-down ()
297 (if (< (+ pong-bat-player2 pong-bat-width) (1- pong-height))
299 (setq pong-bat-player2 (1+ pong-bat-player2))
300 (pong-update-bat (- pong-width 3) pong-bat-player2))))
304 (defun pong-update-bat (x y)
305 "Move a bat (suppress a cell and draw another one on the other side)."
308 ((string-equal (buffer-name (current-buffer)) pong-buffer-name)
309 (gamegrid-set-cell x y pong-bat)
310 (gamegrid-set-cell x (1- (+ y pong-bat-width)) pong-bat)
312 (gamegrid-set-cell x (1- y) pong-blank))
313 (if (< (+ y pong-bat-width) (1- pong-height))
314 (gamegrid-set-cell x (+ y pong-bat-width) pong-blank)))))
321 (define-key pong-mode-map pong-pause-key 'pong-pause)
323 (add-hook 'kill-buffer-hook 'pong-quit nil t)
325 ;; Initialization of some variables
326 (setq pong-bat-player1 (1+ (/ (- pong-height pong-bat-width) 2)))
327 (setq pong-bat-player2 pong-bat-player1)
330 (setq pong-x (/ pong-width 2))
331 (setq pong-y (/ pong-height 2))
334 (gamegrid-kill-timer)
335 (gamegrid-start-timer pong-timer-delay 'pong-update-game)
340 (defun pong-update-game (pong-buffer)
341 "\"Main\" function for pong.
342 It is called every pong-cycle-delay seconds and
343 updates ball and bats positions. It is responsible of collision
344 detection and checks if a player scores."
345 (if (not (eq (current-buffer) pong-buffer))
351 (setq pong-x (+ pong-x pong-xx))
352 (setq pong-y (+ pong-y pong-yy))
355 (< old-y (- pong-height 1)))
356 (gamegrid-set-cell old-x old-y pong-blank))
358 (if (and (> pong-y 0)
359 (< pong-y (- pong-height 1)))
360 (gamegrid-set-cell pong-x pong-y pong-ball))
363 ((or (= pong-x 3) (= pong-x 2))
364 (if (and (>= pong-y pong-bat-player1)
365 (< pong-y (+ pong-bat-player1 pong-bat-width)))
367 (setq pong-yy (+ pong-yy
369 ((= pong-y pong-bat-player1) -1)
370 ((= pong-y (1+ pong-bat-player1)) 0)
372 (setq pong-xx (- pong-xx)))))
374 ((or (= pong-x (- pong-width 4)) (= pong-x (- pong-width 3)))
375 (if (and (>= pong-y pong-bat-player2)
376 (< pong-y (+ pong-bat-player2 pong-bat-width)))
378 (setq pong-yy (+ pong-yy
380 ((= pong-y pong-bat-player2) -1)
381 ((= pong-y (1+ pong-bat-player2)) 0)
383 (setq pong-xx (- pong-xx)))))
386 (setq pong-yy (- pong-yy)))
388 ((>= pong-y (- pong-height 2))
389 (setq pong-yy (- pong-yy)))
392 (setq pong-score-player2 (1+ pong-score-player2))
395 ((>= pong-x (- pong-width 1))
396 (setq pong-score-player1 (1+ pong-score-player1))
401 (defun pong-update-score ()
402 "Update score and print it on bottom of the game grid."
403 (let* ((string (format "Score: %d / %d" pong-score-player1 pong-score-player2))
404 (len (length string)))
405 (loop for x from 0 to (1- len) do
406 (if (string-equal (buffer-name (current-buffer)) pong-buffer-name)
416 (gamegrid-kill-timer)
417 ;; Oooohhh ugly. I don't know why, gamegrid-kill-timer don't do the
418 ;; jobs it is made for. So I have to do it "by hand". Anyway, next
420 (cancel-function-timers 'pong-update-game)
421 (define-key pong-mode-map pong-resume-key 'pong-resume))
425 (defun pong-resume ()
426 "Resume a paused game."
428 (define-key pong-mode-map pong-pause-key 'pong-pause)
429 (gamegrid-start-timer pong-timer-delay 'pong-update-game))
434 "Quit the game and kill the pong buffer."
436 (gamegrid-kill-timer)
437 ;; Be sure not to draw things in another buffer and wait for some
439 (run-with-timer pong-timer-delay nil 'kill-buffer pong-buffer-name))
445 "Play pong and waste time.
446 This is an implementation of the classical game pong.
447 Move left and right bats and try to bounce the ball to your opponent.
449 pong-mode keybindings:\\<pong-mode-map>
453 (setq pong-score-player1 0)
454 (setq pong-score-player2 0)
461 ;; arch-tag: 1fdf0fc5-13e2-4de4-aae4-09bdd5af99f3
462 ;;; pong.el ends here