Mercurial > emacs
annotate lisp/play/pong.el @ 62773:b55470973370
*** empty log message ***
author | Lute Kamstra <lute@gnu.org> |
---|---|
date | Thu, 26 May 2005 16:43:37 +0000 |
parents | eaa9acd9122c |
children | 18a818a2ee7c 62afea0771d8 |
rev | line source |
---|---|
38294
2ca5a3c17019
Fix first line. From Pavel Jan,Bm(Bk
Gerd Moellmann <gerd@gnu.org>
parents:
35976
diff
changeset
|
1 ;;; pong.el --- classical implementation of pong |
27233 | 2 |
62249
eaa9acd9122c
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
3 ;; Copyright 1999, 2000, 2001, 2005 by Free Software Foundation, Inc. |
27233 | 4 |
38455 | 5 ;; Author: Benjamin Drieu <bdrieu@april.org> |
27233 | 6 ;; Keywords: games |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
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 2, or (at your option) | |
13 ;; any later version. | |
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 | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; This is an implementation of the classical game pong. | |
28 | |
29 ;;; Code: | |
30 | |
31 (eval-when-compile (require 'cl)) | |
32 | |
33 (require 'gamegrid) | |
34 | |
35 ;;; Customization | |
36 | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
37 (defgroup pong nil |
27233 | 38 "Emacs-Lisp implementation of the classical game pong." |
39 :tag "Pong" | |
40 :group 'games) | |
41 | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
42 (defcustom pong-buffer-name "*Pong*" |
27233 | 43 "*Name of the buffer used to play." |
44 :group 'pong | |
45 :type '(string)) | |
46 | |
47 (defcustom pong-width 50 | |
48 "*Width of the playfield." | |
49 :group 'pong | |
50 :type '(integer)) | |
51 | |
43118
e1b601e414dd
(pong-height): Don't use height that exceeds the frame height.
Eli Zaretskii <eliz@gnu.org>
parents:
39887
diff
changeset
|
52 (defcustom pong-height (min 30 (- (frame-height) 6)) |
27233 | 53 "*Height of the playfield." |
54 :group 'pong | |
55 :type '(integer)) | |
56 | |
57 (defcustom pong-bat-width 3 | |
58 "*Width of the bats for pong." | |
59 :group 'pong | |
60 :type '(integer)) | |
61 | |
62 (defcustom pong-blank-color "black" | |
63 "*Color used for background." | |
64 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
65 :type 'color) |
27233 | 66 |
67 (defcustom pong-bat-color "yellow" | |
68 "*Color used for bats." | |
69 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
70 :type 'color) |
27233 | 71 |
72 (defcustom pong-ball-color "red" | |
73 "*Color used for the ball." | |
74 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
75 :type 'color) |
27233 | 76 |
77 (defcustom pong-border-color "white" | |
47459
c48cad4dc557
(pong-border-options): Use color on tty if
Francesco Potortì <pot@gnu.org>
parents:
43120
diff
changeset
|
78 "*Color used for pong borders." |
27233 | 79 :group 'pong |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
80 :type 'color) |
27233 | 81 |
82 (defcustom pong-left-key "4" | |
83 "*Alternate key to press for bat 1 to go up (primary one is [left])." | |
84 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
85 :type '(restricted-sexp :match-alternatives (stringp vectorp))) |
27233 | 86 |
87 (defcustom pong-right-key "6" | |
88 "*Alternate key to press for bat 1 to go down (primary one is [right])." | |
89 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
90 :type '(restricted-sexp :match-alternatives (stringp vectorp))) |
27233 | 91 |
92 (defcustom pong-up-key "8" | |
93 "*Alternate key to press for bat 2 to go up (primary one is [up])." | |
94 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
95 :type '(restricted-sexp :match-alternatives (stringp vectorp))) |
27233 | 96 |
97 (defcustom pong-down-key "2" | |
98 "*Alternate key to press for bat 2 to go down (primary one is [down])." | |
99 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
100 :type '(restricted-sexp :match-alternatives (stringp vectorp))) |
27233 | 101 |
102 (defcustom pong-quit-key "q" | |
103 "*Key to press to quit pong." | |
104 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
105 :type '(restricted-sexp :match-alternatives (stringp vectorp))) |
27233 | 106 |
107 (defcustom pong-pause-key "p" | |
108 "Key to press to pause pong." | |
109 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
110 :type '(restricted-sexp :match-alternatives (stringp vectorp))) |
27233 | 111 |
112 (defcustom pong-resume-key "p" | |
113 "*Key to press to resume pong." | |
114 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
115 :type '(restricted-sexp :match-alternatives (stringp vectorp))) |
27233 | 116 |
117 (defcustom pong-timer-delay 0.1 | |
118 "*Time to wait between every cycle." | |
119 :group 'pong | |
35976
6ee038ed072b
(pong-blank-color, pong-bat-color)
Dave Love <fx@gnu.org>
parents:
30859
diff
changeset
|
120 :type 'number) |
27233 | 121 |
122 | |
123 ;;; This is black magic. Define colors used | |
124 | |
125 (defvar pong-blank-options | |
126 '(((glyph colorize) | |
127 (t ?\040)) | |
128 ((color-x color-x) | |
129 (mono-x grid-x) | |
130 (color-tty color-tty)) | |
131 (((glyph color-x) [0 0 0]) | |
132 (color-tty pong-blank-color)))) | |
133 | |
134 (defvar pong-bat-options | |
135 '(((glyph colorize) | |
136 (emacs-tty ?O) | |
137 (t ?\040)) | |
138 ((color-x color-x) | |
139 (mono-x mono-x) | |
140 (color-tty color-tty) | |
141 (mono-tty mono-tty)) | |
142 (((glyph color-x) [1 1 0]) | |
143 (color-tty pong-bat-color)))) | |
144 | |
145 (defvar pong-ball-options | |
146 '(((glyph colorize) | |
147 (t ?\*)) | |
148 ((color-x color-x) | |
149 (mono-x grid-x) | |
150 (color-tty color-tty)) | |
151 (((glyph color-x) [1 0 0]) | |
152 (color-tty pong-ball-color)))) | |
153 | |
154 (defvar pong-border-options | |
155 '(((glyph colorize) | |
156 (t ?\+)) | |
157 ((color-x color-x) | |
47459
c48cad4dc557
(pong-border-options): Use color on tty if
Francesco Potortì <pot@gnu.org>
parents:
43120
diff
changeset
|
158 (mono-x grid-x) |
c48cad4dc557
(pong-border-options): Use color on tty if
Francesco Potortì <pot@gnu.org>
parents:
43120
diff
changeset
|
159 (color-tty color-tty)) |
c48cad4dc557
(pong-border-options): Use color on tty if
Francesco Potortì <pot@gnu.org>
parents:
43120
diff
changeset
|
160 (((glyph color-x) [0.5 0.5 0.5]) |
27233 | 161 (color-tty pong-border-color)))) |
162 | |
163 (defconst pong-blank 0) | |
164 (defconst pong-bat 1) | |
165 (defconst pong-ball 2) | |
166 (defconst pong-border 3) | |
167 | |
168 | |
169 ;;; Determine initial positions for bats and ball | |
170 | |
171 (defvar pong-xx nil | |
172 "Horizontal speed of the ball.") | |
173 | |
174 (defvar pong-yy nil | |
175 "Vertical speed of the ball.") | |
176 | |
177 (defvar pong-x nil | |
178 "Horizontal position of the ball.") | |
179 | |
180 (defvar pong-y nil | |
181 "Vertical position of the ball.") | |
182 | |
183 (defvar pong-bat-player1 nil | |
184 "Vertical position of bat 1.") | |
185 | |
186 (defvar pong-bat-player2 nil | |
187 "Vertical position of bat 2.") | |
188 | |
189 (defvar pong-score-player1 nil) | |
190 (defvar pong-score-player2 nil) | |
191 | |
192 ;;; Initialize maps | |
193 | |
194 (defvar pong-mode-map | |
195 (make-sparse-keymap 'pong-mode-map) "Modemap for pong-mode.") | |
196 | |
197 (defvar pong-null-map | |
198 (make-sparse-keymap 'pong-null-map) "Null map for pong-mode.") | |
199 | |
200 (define-key pong-mode-map [left] 'pong-move-left) | |
201 (define-key pong-mode-map [right] 'pong-move-right) | |
202 (define-key pong-mode-map [up] 'pong-move-up) | |
203 (define-key pong-mode-map [down] 'pong-move-down) | |
204 (define-key pong-mode-map pong-left-key 'pong-move-left) | |
205 (define-key pong-mode-map pong-right-key 'pong-move-right) | |
206 (define-key pong-mode-map pong-up-key 'pong-move-up) | |
207 (define-key pong-mode-map pong-down-key 'pong-move-down) | |
208 (define-key pong-mode-map pong-quit-key 'pong-quit) | |
209 (define-key pong-mode-map pong-pause-key 'pong-pause) | |
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))) | |
217 (loop for c from 0 to 255 do | |
218 (aset options c | |
219 (cond ((= c pong-blank) | |
220 pong-blank-options) | |
221 ((= c pong-bat) | |
222 pong-bat-options) | |
223 ((= c pong-ball) | |
224 pong-ball-options) | |
225 ((= c pong-border) | |
226 pong-border-options) | |
227 (t | |
228 '(nil nil nil))))) | |
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) | |
246 1) | |
247 | |
248 (let ((buffer-read-only nil)) | |
249 (loop for y from 0 to (1- pong-height) do | |
250 (loop for x from 0 to (1- pong-width) do | |
251 (gamegrid-set-cell x y pong-border))) | |
252 (loop for y from 1 to (- pong-height 2) do | |
253 (loop for x from 1 to (- pong-width 2) do | |
254 (gamegrid-set-cell x y pong-blank)))) | |
255 | |
256 (loop for y from pong-bat-player1 to (1- (+ pong-bat-player1 pong-bat-width)) do | |
257 (gamegrid-set-cell 2 y pong-bat)) | |
258 (loop for y from pong-bat-player2 to (1- (+ pong-bat-player2 pong-bat-width)) do | |
259 (gamegrid-set-cell (- pong-width 3) y pong-bat))) | |
260 | |
261 | |
262 | |
263 (defun pong-move-left () | |
264 "Move bat 2 up. | |
265 This is called left for historical reasons, since in some pong | |
266 implementations you move with left/right paddle." | |
267 (interactive) | |
268 (if (> pong-bat-player1 1) | |
269 (and | |
270 (setq pong-bat-player1 (1- pong-bat-player1)) | |
271 (pong-update-bat 2 pong-bat-player1)))) | |
272 | |
273 | |
274 | |
275 (defun pong-move-right () | |
276 "Move bat 2 up." | |
277 (interactive) | |
278 (if (< (+ pong-bat-player1 pong-bat-width) (1- pong-height)) | |
279 (and | |
280 (setq pong-bat-player1 (1+ pong-bat-player1)) | |
281 (pong-update-bat 2 pong-bat-player1)))) | |
282 | |
283 | |
284 | |
285 (defun pong-move-up () | |
286 "Move bat 2 up." | |
287 (interactive) | |
288 (if (> pong-bat-player2 1) | |
289 (and | |
290 (setq pong-bat-player2 (1- pong-bat-player2)) | |
291 (pong-update-bat (- pong-width 3) pong-bat-player2)))) | |
292 | |
293 | |
294 | |
295 (defun pong-move-down () | |
296 "Move bat 2 down." | |
297 (interactive) | |
298 (if (< (+ pong-bat-player2 pong-bat-width) (1- pong-height)) | |
299 (and | |
300 (setq pong-bat-player2 (1+ pong-bat-player2)) | |
301 (pong-update-bat (- pong-width 3) pong-bat-player2)))) | |
302 | |
303 | |
304 | |
305 (defun pong-update-bat (x y) | |
306 "Move a bat (suppress a cell and draw another one on the other side)." | |
307 | |
308 (cond | |
309 ((string-equal (buffer-name (current-buffer)) pong-buffer-name) | |
310 (gamegrid-set-cell x y pong-bat) | |
311 (gamegrid-set-cell x (1- (+ y pong-bat-width)) pong-bat) | |
312 (if (> y 1) | |
313 (gamegrid-set-cell x (1- y) pong-blank)) | |
314 (if (< (+ y pong-bat-width) (1- pong-height)) | |
315 (gamegrid-set-cell x (+ y pong-bat-width) pong-blank))))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
316 |
27233 | 317 |
318 | |
319 (defun pong-init () | |
320 "Initialize a game." | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
321 |
27233 | 322 (define-key pong-mode-map pong-pause-key 'pong-pause) |
323 | |
324 (add-hook 'kill-buffer-hook 'pong-quit nil t) | |
325 | |
326 ;; Initialization of some variables | |
327 (setq pong-bat-player1 (1+ (/ (- pong-height pong-bat-width) 2))) | |
328 (setq pong-bat-player2 pong-bat-player1) | |
329 (setq pong-xx -1) | |
330 (setq pong-yy 0) | |
331 (setq pong-x (/ pong-width 2)) | |
332 (setq pong-y (/ pong-height 2)) | |
333 | |
334 (pong-init-buffer) | |
335 (gamegrid-kill-timer) | |
336 (gamegrid-start-timer pong-timer-delay 'pong-update-game) | |
337 (pong-update-score)) | |
338 | |
339 | |
340 | |
341 (defun pong-update-game (pong-buffer) | |
342 "\"Main\" function for pong. | |
343 It is called every pong-cycle-delay seconds and | |
344 updates ball and bats positions. It is responsible of collision | |
345 detection and checks if a player scores." | |
346 (if (not (eq (current-buffer) pong-buffer)) | |
347 (pong-pause) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
348 |
27233 | 349 (let ((old-x pong-x) |
350 (old-y pong-y)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
351 |
27233 | 352 (setq pong-x (+ pong-x pong-xx)) |
353 (setq pong-y (+ pong-y pong-yy)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
354 |
27233 | 355 (if (and (> old-y 0) |
356 (< old-y (- pong-height 1))) | |
357 (gamegrid-set-cell old-x old-y pong-blank)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
358 |
27233 | 359 (if (and (> pong-y 0) |
360 (< pong-y (- pong-height 1))) | |
361 (gamegrid-set-cell pong-x pong-y pong-ball)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
362 |
27233 | 363 (cond |
364 ((or (= pong-x 3) (= pong-x 2)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
365 (if (and (>= pong-y pong-bat-player1) |
27233 | 366 (< pong-y (+ pong-bat-player1 pong-bat-width))) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
367 (and |
27233 | 368 (setq pong-yy (+ pong-yy |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
369 (cond |
27233 | 370 ((= pong-y pong-bat-player1) -1) |
371 ((= pong-y (1+ pong-bat-player1)) 0) | |
372 (t 1)))) | |
373 (setq pong-xx (- pong-xx))))) | |
374 | |
375 ((or (= pong-x (- pong-width 4)) (= pong-x (- pong-width 3))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
376 (if (and (>= pong-y pong-bat-player2) |
27233 | 377 (< pong-y (+ pong-bat-player2 pong-bat-width))) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
378 (and |
27233 | 379 (setq pong-yy (+ pong-yy |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
380 (cond |
27233 | 381 ((= pong-y pong-bat-player2) -1) |
382 ((= pong-y (1+ pong-bat-player2)) 0) | |
383 (t 1)))) | |
384 (setq pong-xx (- pong-xx))))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47459
diff
changeset
|
385 |
27233 | 386 ((<= pong-y 1) |
387 (setq pong-yy (- pong-yy))) | |
388 | |
389 ((>= pong-y (- pong-height 2)) | |
390 (setq pong-yy (- pong-yy))) | |
391 | |
392 ((< pong-x 1) | |
393 (setq pong-score-player2 (1+ pong-score-player2)) | |
394 (pong-init)) | |
395 | |
396 ((>= pong-x (- pong-width 1)) | |
397 (setq pong-score-player1 (1+ pong-score-player1)) | |
398 (pong-init)))))) | |
399 | |
400 | |
401 | |
402 (defun pong-update-score () | |
403 "Update score and print it on bottom of the game grid." | |
404 (let* ((string (format "Score: %d / %d" pong-score-player1 pong-score-player2)) | |
405 (len (length string))) | |
406 (loop for x from 0 to (1- len) do | |
407 (if (string-equal (buffer-name (current-buffer)) pong-buffer-name) | |
408 (gamegrid-set-cell x | |
409 pong-height | |
410 (aref string x)))))) | |
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. | |
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. | |
449 | |
30859 | 450 pong-mode keybindings:\\<pong-mode-map> |
27233 | 451 |
30859 | 452 \\{pong-mode-map}" |
27233 | 453 (interactive) |
454 (setq pong-score-player1 0) | |
455 (setq pong-score-player2 0) | |
456 (pong-init)) | |
457 | |
458 | |
459 | |
460 (provide 'pong) | |
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
38294
diff
changeset
|
461 |
52401 | 462 ;;; arch-tag: 1fdf0fc5-13e2-4de4-aae4-09bdd5af99f3 |
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
38294
diff
changeset
|
463 ;;; pong.el ends here |