1 ;;; pong.el --- classical implementation of pong
3 ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
5 ;; Author: Benjamin Drieu <bdrieu@april.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This is an implementation of the classical game pong.
29 (eval-when-compile (require 'cl-lib
))
36 "Emacs-Lisp implementation of the classical game pong."
40 (defcustom pong-buffer-name
"*Pong*"
41 "Name of the buffer used to play."
45 (defcustom pong-width
50
46 "Width of the playfield."
50 (defcustom pong-height
(min 30 (- (frame-height) 6))
51 "Height of the playfield."
55 (defcustom pong-bat-width
3
56 "Width of the bats for pong."
60 (defcustom pong-blank-color
"black"
61 "Color used for background."
65 (defcustom pong-bat-color
"yellow"
66 "Color used for bats."
70 (defcustom pong-ball-color
"red"
71 "Color used for the ball."
75 (defcustom pong-border-color
"white"
76 "Color used for pong borders."
80 (defcustom pong-left-key
"4"
81 "Alternate key to press for bat 1 to go up (primary one is [left])."
83 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
85 (defcustom pong-right-key
"6"
86 "Alternate key to press for bat 1 to go down (primary one is [right])."
88 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
90 (defcustom pong-up-key
"8"
91 "Alternate key to press for bat 2 to go up (primary one is [up])."
93 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
95 (defcustom pong-down-key
"2"
96 "Alternate key to press for bat 2 to go down (primary one is [down])."
98 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
100 (defcustom pong-quit-key
"q"
101 "Key to press to quit pong."
103 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
105 (defcustom pong-pause-key
"p"
106 "Key to press to pause pong."
108 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
110 (defcustom pong-resume-key
"p"
111 "Key to press to resume pong."
113 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
)))
115 (defcustom pong-timer-delay
0.1
116 "Time to wait between every cycle."
121 ;;; This is black magic. Define colors used
123 (defvar pong-blank-options
128 (color-tty color-tty
))
129 (((glyph color-x
) [0 0 0])
130 (color-tty pong-blank-color
))))
132 (defvar pong-bat-options
138 (color-tty color-tty
)
140 (((glyph color-x
) [1 1 0])
141 (color-tty pong-bat-color
))))
143 (defvar pong-ball-options
148 (color-tty color-tty
))
149 (((glyph color-x
) [1 0 0])
150 (color-tty pong-ball-color
))))
152 (defvar pong-border-options
157 (color-tty color-tty
))
158 (((glyph color-x
) [0.5 0.5 0.5])
159 (color-tty pong-border-color
))))
161 (defconst pong-blank
0)
162 (defconst pong-bat
1)
163 (defconst pong-ball
2)
164 (defconst pong-border
3)
167 ;;; Determine initial positions for bats and ball
170 "Horizontal speed of the ball.")
173 "Vertical speed of the ball.")
176 "Horizontal position of the ball.")
179 "Vertical position of the ball.")
181 (defvar pong-bat-player1 nil
182 "Vertical position of bat 1.")
184 (defvar pong-bat-player2 nil
185 "Vertical position of bat 2.")
187 (defvar pong-score-player1 nil
)
188 (defvar pong-score-player2 nil
)
192 (defvar pong-mode-map
193 (let ((map (make-sparse-keymap 'pong-mode-map
)))
194 (define-key map
[left] 'pong-move-left)
195 (define-key map [right] 'pong-move-right)
196 (define-key map [up] 'pong-move-up)
197 (define-key map [down] 'pong-move-down)
198 (define-key map pong-left-key 'pong-move-left)
199 (define-key map pong-right-key 'pong-move-right)
200 (define-key map pong-up-key 'pong-move-up)
201 (define-key map pong-down-key 'pong-move-down)
202 (define-key map pong-quit-key 'pong-quit)
203 (define-key map pong-pause-key 'pong-pause)
205 "Modemap for pong-mode.")
207 (defvar pong-null-map
208 (make-sparse-keymap 'pong-null-map) "Null map for pong-mode.")
212 ;;; Fun stuff -- The code
214 (defun pong-display-options ()
215 "Computes display options (required by gamegrid for colors)."
216 (let ((options (make-vector 256 nil)))
219 (cond ((= c pong-blank)
233 (defun pong-init-buffer ()
234 "Initialize pong buffer and draw stuff thanks to gamegrid library."
236 (get-buffer-create pong-buffer-name)
237 (switch-to-buffer pong-buffer-name)
238 (use-local-map pong-mode-map)
240 (setq gamegrid-use-glyphs t)
241 (setq gamegrid-use-color t)
242 (gamegrid-init (pong-display-options))
244 (gamegrid-init-buffer pong-width
248 (let ((buffer-read-only nil))
249 (dotimes (y pong-height)
250 (dotimes (x pong-width)
251 (gamegrid-set-cell x y pong-border)))
252 (cl-loop for y from 1 to (- pong-height 2) do
253 (cl-loop for x from 1 to (- pong-width 2) do
254 (gamegrid-set-cell x y pong-blank))))
256 (cl-loop for y from pong-bat-player1
257 to (1- (+ pong-bat-player1 pong-bat-width))
258 do (gamegrid-set-cell 2 y pong-bat))
259 (cl-loop for y from pong-bat-player2
260 to (1- (+ pong-bat-player2 pong-bat-width))
261 do (gamegrid-set-cell (- pong-width 3) y pong-bat)))
264 (defun pong-move-left ()
266 This is called left for historical reasons, since in some pong
267 implementations you move with left/right paddle."
269 (if (> pong-bat-player1 1)
271 (setq pong-bat-player1 (1- pong-bat-player1))
272 (pong-update-bat 2 pong-bat-player1))))
276 (defun pong-move-right ()
279 (if (< (+ pong-bat-player1 pong-bat-width) (1- pong-height))
281 (setq pong-bat-player1 (1+ pong-bat-player1))
282 (pong-update-bat 2 pong-bat-player1))))
286 (defun pong-move-up ()
289 (if (> pong-bat-player2 1)
291 (setq pong-bat-player2 (1- pong-bat-player2))
292 (pong-update-bat (- pong-width 3) pong-bat-player2))))
296 (defun pong-move-down ()
299 (if (< (+ pong-bat-player2 pong-bat-width) (1- pong-height))
301 (setq pong-bat-player2 (1+ pong-bat-player2))
302 (pong-update-bat (- pong-width 3) pong-bat-player2))))
306 (defun pong-update-bat (x y)
307 "Move a bat (suppress a cell and draw another one on the other side)."
310 ((string-equal (buffer-name (current-buffer)) pong-buffer-name)
311 (gamegrid-set-cell x y pong-bat)
312 (gamegrid-set-cell x (1- (+ y pong-bat-width)) pong-bat)
314 (gamegrid-set-cell x (1- y) pong-blank))
315 (if (< (+ y pong-bat-width) (1- pong-height))
316 (gamegrid-set-cell x (+ y pong-bat-width) pong-blank)))))
323 (define-key pong-mode-map pong-pause-key 'pong-pause)
325 (add-hook 'kill-buffer-hook 'pong-quit nil t)
327 ;; Initialization of some variables
328 (setq pong-bat-player1 (1+ (/ (- pong-height pong-bat-width) 2)))
329 (setq pong-bat-player2 pong-bat-player1)
332 (setq pong-x (/ pong-width 2))
333 (setq pong-y (/ pong-height 2))
336 (gamegrid-kill-timer)
337 (gamegrid-start-timer pong-timer-delay 'pong-update-game)
342 (defun pong-update-game (pong-buffer)
343 "\"Main\" function for pong.
344 It is called every pong-cycle-delay seconds and
345 updates ball and bats positions. It is responsible of collision
346 detection and checks if a player scores."
347 (if (not (eq (current-buffer) pong-buffer))
353 (setq pong-x (+ pong-x pong-xx))
354 (setq pong-y (+ pong-y pong-yy))
357 (< old-y (- pong-height 1)))
358 (gamegrid-set-cell old-x old-y pong-blank))
360 (if (and (> pong-y 0)
361 (< pong-y (- pong-height 1)))
362 (gamegrid-set-cell pong-x pong-y pong-ball))
365 ((or (= pong-x 3) (= pong-x 2))
366 (if (and (>= pong-y pong-bat-player1)
367 (< pong-y (+ pong-bat-player1 pong-bat-width)))
369 (setq pong-yy (+ pong-yy
371 ((= pong-y pong-bat-player1) -1)
372 ((= pong-y (1+ pong-bat-player1)) 0)
374 (setq pong-xx (- pong-xx)))))
376 ((or (= pong-x (- pong-width 4)) (= pong-x (- pong-width 3)))
377 (if (and (>= pong-y pong-bat-player2)
378 (< pong-y (+ pong-bat-player2 pong-bat-width)))
380 (setq pong-yy (+ pong-yy
382 ((= pong-y pong-bat-player2) -1)
383 ((= pong-y (1+ pong-bat-player2)) 0)
385 (setq pong-xx (- pong-xx)))))
388 (setq pong-yy (- pong-yy)))
390 ((>= pong-y (- pong-height 2))
391 (setq pong-yy (- pong-yy)))
394 (setq pong-score-player2 (1+ pong-score-player2))
397 ((>= pong-x (- pong-width 1))
398 (setq pong-score-player1 (1+ pong-score-player1))
403 (defun pong-update-score ()
404 "Update score and print it on bottom of the game grid."
405 (let* ((string (format "Score: %d / %d"
406 pong-score-player1 pong-score-player2))
407 (len (length string)))
409 (if (string-equal (buffer-name (current-buffer)) pong-buffer-name)
410 (gamegrid-set-cell x pong-height (aref string x))))))
417 (gamegrid-kill-timer)
418 ;; Oooohhh ugly. I don't know why, gamegrid-kill-timer don't do the
419 ;; jobs it is made for. So I have to do it "by hand". Anyway, next
421 (cancel-function-timers 'pong-update-game)
422 (define-key pong-mode-map pong-resume-key 'pong-resume))
426 (defun pong-resume ()
427 "Resume a paused game."
429 (define-key pong-mode-map pong-pause-key 'pong-pause)
430 (gamegrid-start-timer pong-timer-delay 'pong-update-game))
435 "Quit the game and kill the pong buffer."
437 (gamegrid-kill-timer)
438 ;; Be sure not to draw things in another buffer and wait for some
440 (run-with-timer pong-timer-delay nil 'kill-buffer pong-buffer-name))
446 "Play pong and waste time.
447 This is an implementation of the classical game pong.
448 Move left and right bats and try to bounce the ball to your opponent.
450 pong-mode keybindings:\\<pong-mode-map>
454 (setq pong-score-player1 0)
455 (setq pong-score-player2 0)
462 ;;; pong.el ends here