Update copyright notices for 2013.
[bpt/emacs.git] / lisp / play / pong.el
CommitLineData
5f72adc0 1;;; pong.el --- classical implementation of pong
c1475eae 2
ab422c4d 3;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
c1475eae 4
9781053a 5;; Author: Benjamin Drieu <bdrieu@april.org>
c1475eae
GM
6;; Keywords: games
7
8;; This file is part of GNU Emacs.
9
b1fc2b50 10;; GNU Emacs is free software: you can redistribute it and/or modify
c1475eae 11;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
c1475eae
GM
14
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.
19
20;; You should have received a copy of the GNU General Public License
b1fc2b50 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c1475eae
GM
22
23;;; Commentary:
24
25;; This is an implementation of the classical game pong.
26
27;;; Code:
28
a464a6c7 29(eval-when-compile (require 'cl-lib))
c1475eae
GM
30
31(require 'gamegrid)
32
33;;; Customization
34
a1506d29 35(defgroup pong nil
c1475eae
GM
36 "Emacs-Lisp implementation of the classical game pong."
37 :tag "Pong"
38 :group 'games)
39
a1506d29 40(defcustom pong-buffer-name "*Pong*"
67d110f1 41 "Name of the buffer used to play."
c1475eae
GM
42 :group 'pong
43 :type '(string))
44
45(defcustom pong-width 50
67d110f1 46 "Width of the playfield."
c1475eae
GM
47 :group 'pong
48 :type '(integer))
49
e029dcaf 50(defcustom pong-height (min 30 (- (frame-height) 6))
67d110f1 51 "Height of the playfield."
c1475eae
GM
52 :group 'pong
53 :type '(integer))
54
55(defcustom pong-bat-width 3
67d110f1 56 "Width of the bats for pong."
c1475eae
GM
57 :group 'pong
58 :type '(integer))
59
60(defcustom pong-blank-color "black"
67d110f1 61 "Color used for background."
c1475eae 62 :group 'pong
a430ca88 63 :type 'color)
c1475eae
GM
64
65(defcustom pong-bat-color "yellow"
67d110f1 66 "Color used for bats."
c1475eae 67 :group 'pong
a430ca88 68 :type 'color)
c1475eae
GM
69
70(defcustom pong-ball-color "red"
67d110f1 71 "Color used for the ball."
c1475eae 72 :group 'pong
a430ca88 73 :type 'color)
c1475eae
GM
74
75(defcustom pong-border-color "white"
67d110f1 76 "Color used for pong borders."
c1475eae 77 :group 'pong
a430ca88 78 :type 'color)
c1475eae
GM
79
80(defcustom pong-left-key "4"
67d110f1 81 "Alternate key to press for bat 1 to go up (primary one is [left])."
c1475eae 82 :group 'pong
a430ca88 83 :type '(restricted-sexp :match-alternatives (stringp vectorp)))
c1475eae
GM
84
85(defcustom pong-right-key "6"
67d110f1 86 "Alternate key to press for bat 1 to go down (primary one is [right])."
c1475eae 87 :group 'pong
a430ca88 88 :type '(restricted-sexp :match-alternatives (stringp vectorp)))
c1475eae
GM
89
90(defcustom pong-up-key "8"
67d110f1 91 "Alternate key to press for bat 2 to go up (primary one is [up])."
c1475eae 92 :group 'pong
a430ca88 93 :type '(restricted-sexp :match-alternatives (stringp vectorp)))
c1475eae
GM
94
95(defcustom pong-down-key "2"
67d110f1 96 "Alternate key to press for bat 2 to go down (primary one is [down])."
c1475eae 97 :group 'pong
a430ca88 98 :type '(restricted-sexp :match-alternatives (stringp vectorp)))
c1475eae
GM
99
100(defcustom pong-quit-key "q"
67d110f1 101 "Key to press to quit pong."
c1475eae 102 :group 'pong
a430ca88 103 :type '(restricted-sexp :match-alternatives (stringp vectorp)))
c1475eae
GM
104
105(defcustom pong-pause-key "p"
106 "Key to press to pause pong."
107 :group 'pong
a430ca88 108 :type '(restricted-sexp :match-alternatives (stringp vectorp)))
c1475eae
GM
109
110(defcustom pong-resume-key "p"
67d110f1 111 "Key to press to resume pong."
c1475eae 112 :group 'pong
a430ca88 113 :type '(restricted-sexp :match-alternatives (stringp vectorp)))
c1475eae
GM
114
115(defcustom pong-timer-delay 0.1
67d110f1 116 "Time to wait between every cycle."
c1475eae 117 :group 'pong
a430ca88 118 :type 'number)
c1475eae
GM
119
120
121;;; This is black magic. Define colors used
122
123(defvar pong-blank-options
124 '(((glyph colorize)
125 (t ?\040))
126 ((color-x color-x)
127 (mono-x grid-x)
128 (color-tty color-tty))
129 (((glyph color-x) [0 0 0])
130 (color-tty pong-blank-color))))
131
132(defvar pong-bat-options
133 '(((glyph colorize)
134 (emacs-tty ?O)
135 (t ?\040))
136 ((color-x color-x)
137 (mono-x mono-x)
138 (color-tty color-tty)
139 (mono-tty mono-tty))
140 (((glyph color-x) [1 1 0])
141 (color-tty pong-bat-color))))
142
143(defvar pong-ball-options
144 '(((glyph colorize)
145 (t ?\*))
146 ((color-x color-x)
147 (mono-x grid-x)
148 (color-tty color-tty))
149 (((glyph color-x) [1 0 0])
150 (color-tty pong-ball-color))))
151
152(defvar pong-border-options
153 '(((glyph colorize)
154 (t ?\+))
155 ((color-x color-x)
6add7517
FP
156 (mono-x grid-x)
157 (color-tty color-tty))
158 (((glyph color-x) [0.5 0.5 0.5])
c1475eae
GM
159 (color-tty pong-border-color))))
160
161(defconst pong-blank 0)
162(defconst pong-bat 1)
163(defconst pong-ball 2)
164(defconst pong-border 3)
165
166
167;;; Determine initial positions for bats and ball
168
169(defvar pong-xx nil
170 "Horizontal speed of the ball.")
171
172(defvar pong-yy nil
173 "Vertical speed of the ball.")
174
175(defvar pong-x nil
176 "Horizontal position of the ball.")
177
178(defvar pong-y nil
179 "Vertical position of the ball.")
180
181(defvar pong-bat-player1 nil
182 "Vertical position of bat 1.")
183
184(defvar pong-bat-player2 nil
185 "Vertical position of bat 2.")
186
187(defvar pong-score-player1 nil)
188(defvar pong-score-player2 nil)
189
190;;; Initialize maps
191
192(defvar pong-mode-map
b016851c
SM
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)
204 map)
205 "Modemap for pong-mode.")
c1475eae
GM
206
207(defvar pong-null-map
208 (make-sparse-keymap 'pong-null-map) "Null map for pong-mode.")
209
c1475eae
GM
210
211
212;;; Fun stuff -- The code
213
214(defun pong-display-options ()
215 "Computes display options (required by gamegrid for colors)."
216 (let ((options (make-vector 256 nil)))
a464a6c7 217 (dotimes (c 256)
c1475eae 218 (aset options c
a464a6c7
SM
219 (cond ((= c pong-blank)
220 pong-blank-options)
c1475eae 221 ((= c pong-bat)
a464a6c7 222 pong-bat-options)
c1475eae 223 ((= c pong-ball)
a464a6c7 224 pong-ball-options)
c1475eae 225 ((= c pong-border)
a464a6c7 226 pong-border-options)
c1475eae 227 (t
a464a6c7 228 '(nil nil nil)))))
c1475eae
GM
229 options))
230
231
232
233(defun pong-init-buffer ()
234 "Initialize pong buffer and draw stuff thanks to gamegrid library."
235 (interactive)
236 (get-buffer-create pong-buffer-name)
237 (switch-to-buffer pong-buffer-name)
238 (use-local-map pong-mode-map)
239
240 (setq gamegrid-use-glyphs t)
241 (setq gamegrid-use-color t)
242 (gamegrid-init (pong-display-options))
243
244 (gamegrid-init-buffer pong-width
245 (+ 2 pong-height)
f47adf18 246 ?\s)
c1475eae
GM
247
248 (let ((buffer-read-only nil))
a464a6c7
SM
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))))
c1475eae 255
a464a6c7
SM
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)))
c1475eae
GM
262
263
264(defun pong-move-left ()
265 "Move bat 2 up.
266This is called left for historical reasons, since in some pong
267implementations you move with left/right paddle."
268 (interactive)
269 (if (> pong-bat-player1 1)
270 (and
271 (setq pong-bat-player1 (1- pong-bat-player1))
272 (pong-update-bat 2 pong-bat-player1))))
273
274
275
276(defun pong-move-right ()
277 "Move bat 2 up."
278 (interactive)
279 (if (< (+ pong-bat-player1 pong-bat-width) (1- pong-height))
280 (and
281 (setq pong-bat-player1 (1+ pong-bat-player1))
282 (pong-update-bat 2 pong-bat-player1))))
283
284
285
286(defun pong-move-up ()
287 "Move bat 2 up."
288 (interactive)
289 (if (> pong-bat-player2 1)
290 (and
291 (setq pong-bat-player2 (1- pong-bat-player2))
292 (pong-update-bat (- pong-width 3) pong-bat-player2))))
293
294
295
296(defun pong-move-down ()
297 "Move bat 2 down."
298 (interactive)
299 (if (< (+ pong-bat-player2 pong-bat-width) (1- pong-height))
300 (and
301 (setq pong-bat-player2 (1+ pong-bat-player2))
302 (pong-update-bat (- pong-width 3) pong-bat-player2))))
303
304
305
306(defun pong-update-bat (x y)
307 "Move a bat (suppress a cell and draw another one on the other side)."
308
309 (cond
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)
313 (if (> y 1)
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)))))
a1506d29 317
c1475eae
GM
318
319
320(defun pong-init ()
321 "Initialize a game."
a1506d29 322
c1475eae
GM
323 (define-key pong-mode-map pong-pause-key 'pong-pause)
324
c1475eae
GM
325 (add-hook 'kill-buffer-hook 'pong-quit nil t)
326
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)
330 (setq pong-xx -1)
331 (setq pong-yy 0)
332 (setq pong-x (/ pong-width 2))
333 (setq pong-y (/ pong-height 2))
334
335 (pong-init-buffer)
336 (gamegrid-kill-timer)
337 (gamegrid-start-timer pong-timer-delay 'pong-update-game)
338 (pong-update-score))
339
340
341
342(defun pong-update-game (pong-buffer)
343 "\"Main\" function for pong.
344It is called every pong-cycle-delay seconds and
345updates ball and bats positions. It is responsible of collision
346detection and checks if a player scores."
347 (if (not (eq (current-buffer) pong-buffer))
348 (pong-pause)
a1506d29 349
c1475eae
GM
350 (let ((old-x pong-x)
351 (old-y pong-y))
a1506d29 352
c1475eae
GM
353 (setq pong-x (+ pong-x pong-xx))
354 (setq pong-y (+ pong-y pong-yy))
a1506d29 355
c1475eae
GM
356 (if (and (> old-y 0)
357 (< old-y (- pong-height 1)))
358 (gamegrid-set-cell old-x old-y pong-blank))
a1506d29 359
c1475eae
GM
360 (if (and (> pong-y 0)
361 (< pong-y (- pong-height 1)))
362 (gamegrid-set-cell pong-x pong-y pong-ball))
a1506d29 363
c1475eae
GM
364 (cond
365 ((or (= pong-x 3) (= pong-x 2))
a1506d29 366 (if (and (>= pong-y pong-bat-player1)
c1475eae 367 (< pong-y (+ pong-bat-player1 pong-bat-width)))
a1506d29 368 (and
c1475eae 369 (setq pong-yy (+ pong-yy
a1506d29 370 (cond
c1475eae
GM
371 ((= pong-y pong-bat-player1) -1)
372 ((= pong-y (1+ pong-bat-player1)) 0)
373 (t 1))))
374 (setq pong-xx (- pong-xx)))))
375
376 ((or (= pong-x (- pong-width 4)) (= pong-x (- pong-width 3)))
a1506d29 377 (if (and (>= pong-y pong-bat-player2)
c1475eae 378 (< pong-y (+ pong-bat-player2 pong-bat-width)))
a1506d29 379 (and
c1475eae 380 (setq pong-yy (+ pong-yy
a1506d29 381 (cond
c1475eae
GM
382 ((= pong-y pong-bat-player2) -1)
383 ((= pong-y (1+ pong-bat-player2)) 0)
384 (t 1))))
385 (setq pong-xx (- pong-xx)))))
a1506d29 386
c1475eae
GM
387 ((<= pong-y 1)
388 (setq pong-yy (- pong-yy)))
389
390 ((>= pong-y (- pong-height 2))
391 (setq pong-yy (- pong-yy)))
392
393 ((< pong-x 1)
394 (setq pong-score-player2 (1+ pong-score-player2))
395 (pong-init))
396
397 ((>= pong-x (- pong-width 1))
398 (setq pong-score-player1 (1+ pong-score-player1))
399 (pong-init))))))
400
401
402
403(defun pong-update-score ()
404 "Update score and print it on bottom of the game grid."
a464a6c7
SM
405 (let* ((string (format "Score: %d / %d"
406 pong-score-player1 pong-score-player2))
c1475eae 407 (len (length string)))
a464a6c7
SM
408 (dotimes (x len)
409 (if (string-equal (buffer-name (current-buffer)) pong-buffer-name)
410 (gamegrid-set-cell x pong-height (aref string x))))))
c1475eae
GM
411
412
413
414(defun pong-pause ()
415 "Pause the game."
416 (interactive)
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
420 ;; line is harmless.
421 (cancel-function-timers 'pong-update-game)
422 (define-key pong-mode-map pong-resume-key 'pong-resume))
423
424
425
426(defun pong-resume ()
427 "Resume a paused game."
428 (interactive)
429 (define-key pong-mode-map pong-pause-key 'pong-pause)
430 (gamegrid-start-timer pong-timer-delay 'pong-update-game))
431
432
433
434(defun pong-quit ()
435 "Quit the game and kill the pong buffer."
436 (interactive)
437 (gamegrid-kill-timer)
438 ;; Be sure not to draw things in another buffer and wait for some
439 ;; time.
440 (run-with-timer pong-timer-delay nil 'kill-buffer pong-buffer-name))
441
442
443
444;;;###autoload
445(defun pong ()
446 "Play pong and waste time.
447This is an implementation of the classical game pong.
448Move left and right bats and try to bounce the ball to your opponent.
449
e8951be0 450pong-mode keybindings:\\<pong-mode-map>
c1475eae 451
e8951be0 452\\{pong-mode-map}"
c1475eae
GM
453 (interactive)
454 (setq pong-score-player1 0)
455 (setq pong-score-player2 0)
456 (pong-init))
457
458
459
460(provide 'pong)
6e44da43
PJ
461
462;;; pong.el ends here