pong.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. ;;; pong.el --- classical implementation of pong
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Benjamin Drieu <bdrieu@april.org>
  4. ;; Keywords: games
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This is an implementation of the classical game pong.
  18. ;;; Code:
  19. (eval-when-compile (require 'cl))
  20. (require 'gamegrid)
  21. ;;; Customization
  22. (defgroup pong nil
  23. "Emacs-Lisp implementation of the classical game pong."
  24. :tag "Pong"
  25. :group 'games)
  26. (defcustom pong-buffer-name "*Pong*"
  27. "Name of the buffer used to play."
  28. :group 'pong
  29. :type '(string))
  30. (defcustom pong-width 50
  31. "Width of the playfield."
  32. :group 'pong
  33. :type '(integer))
  34. (defcustom pong-height (min 30 (- (frame-height) 6))
  35. "Height of the playfield."
  36. :group 'pong
  37. :type '(integer))
  38. (defcustom pong-bat-width 3
  39. "Width of the bats for pong."
  40. :group 'pong
  41. :type '(integer))
  42. (defcustom pong-blank-color "black"
  43. "Color used for background."
  44. :group 'pong
  45. :type 'color)
  46. (defcustom pong-bat-color "yellow"
  47. "Color used for bats."
  48. :group 'pong
  49. :type 'color)
  50. (defcustom pong-ball-color "red"
  51. "Color used for the ball."
  52. :group 'pong
  53. :type 'color)
  54. (defcustom pong-border-color "white"
  55. "Color used for pong borders."
  56. :group 'pong
  57. :type 'color)
  58. (defcustom pong-left-key "4"
  59. "Alternate key to press for bat 1 to go up (primary one is [left])."
  60. :group 'pong
  61. :type '(restricted-sexp :match-alternatives (stringp vectorp)))
  62. (defcustom pong-right-key "6"
  63. "Alternate key to press for bat 1 to go down (primary one is [right])."
  64. :group 'pong
  65. :type '(restricted-sexp :match-alternatives (stringp vectorp)))
  66. (defcustom pong-up-key "8"
  67. "Alternate key to press for bat 2 to go up (primary one is [up])."
  68. :group 'pong
  69. :type '(restricted-sexp :match-alternatives (stringp vectorp)))
  70. (defcustom pong-down-key "2"
  71. "Alternate key to press for bat 2 to go down (primary one is [down])."
  72. :group 'pong
  73. :type '(restricted-sexp :match-alternatives (stringp vectorp)))
  74. (defcustom pong-quit-key "q"
  75. "Key to press to quit pong."
  76. :group 'pong
  77. :type '(restricted-sexp :match-alternatives (stringp vectorp)))
  78. (defcustom pong-pause-key "p"
  79. "Key to press to pause pong."
  80. :group 'pong
  81. :type '(restricted-sexp :match-alternatives (stringp vectorp)))
  82. (defcustom pong-resume-key "p"
  83. "Key to press to resume pong."
  84. :group 'pong
  85. :type '(restricted-sexp :match-alternatives (stringp vectorp)))
  86. (defcustom pong-timer-delay 0.1
  87. "Time to wait between every cycle."
  88. :group 'pong
  89. :type 'number)
  90. ;;; This is black magic. Define colors used
  91. (defvar pong-blank-options
  92. '(((glyph colorize)
  93. (t ?\040))
  94. ((color-x color-x)
  95. (mono-x grid-x)
  96. (color-tty color-tty))
  97. (((glyph color-x) [0 0 0])
  98. (color-tty pong-blank-color))))
  99. (defvar pong-bat-options
  100. '(((glyph colorize)
  101. (emacs-tty ?O)
  102. (t ?\040))
  103. ((color-x color-x)
  104. (mono-x mono-x)
  105. (color-tty color-tty)
  106. (mono-tty mono-tty))
  107. (((glyph color-x) [1 1 0])
  108. (color-tty pong-bat-color))))
  109. (defvar pong-ball-options
  110. '(((glyph colorize)
  111. (t ?\*))
  112. ((color-x color-x)
  113. (mono-x grid-x)
  114. (color-tty color-tty))
  115. (((glyph color-x) [1 0 0])
  116. (color-tty pong-ball-color))))
  117. (defvar pong-border-options
  118. '(((glyph colorize)
  119. (t ?\+))
  120. ((color-x color-x)
  121. (mono-x grid-x)
  122. (color-tty color-tty))
  123. (((glyph color-x) [0.5 0.5 0.5])
  124. (color-tty pong-border-color))))
  125. (defconst pong-blank 0)
  126. (defconst pong-bat 1)
  127. (defconst pong-ball 2)
  128. (defconst pong-border 3)
  129. ;;; Determine initial positions for bats and ball
  130. (defvar pong-xx nil
  131. "Horizontal speed of the ball.")
  132. (defvar pong-yy nil
  133. "Vertical speed of the ball.")
  134. (defvar pong-x nil
  135. "Horizontal position of the ball.")
  136. (defvar pong-y nil
  137. "Vertical position of the ball.")
  138. (defvar pong-bat-player1 nil
  139. "Vertical position of bat 1.")
  140. (defvar pong-bat-player2 nil
  141. "Vertical position of bat 2.")
  142. (defvar pong-score-player1 nil)
  143. (defvar pong-score-player2 nil)
  144. ;;; Initialize maps
  145. (defvar pong-mode-map
  146. (let ((map (make-sparse-keymap 'pong-mode-map)))
  147. (define-key map [left] 'pong-move-left)
  148. (define-key map [right] 'pong-move-right)
  149. (define-key map [up] 'pong-move-up)
  150. (define-key map [down] 'pong-move-down)
  151. (define-key map pong-left-key 'pong-move-left)
  152. (define-key map pong-right-key 'pong-move-right)
  153. (define-key map pong-up-key 'pong-move-up)
  154. (define-key map pong-down-key 'pong-move-down)
  155. (define-key map pong-quit-key 'pong-quit)
  156. (define-key map pong-pause-key 'pong-pause)
  157. map)
  158. "Modemap for pong-mode.")
  159. (defvar pong-null-map
  160. (make-sparse-keymap 'pong-null-map) "Null map for pong-mode.")
  161. ;;; Fun stuff -- The code
  162. (defun pong-display-options ()
  163. "Computes display options (required by gamegrid for colors)."
  164. (let ((options (make-vector 256 nil)))
  165. (loop for c from 0 to 255 do
  166. (aset options c
  167. (cond ((= c pong-blank)
  168. pong-blank-options)
  169. ((= c pong-bat)
  170. pong-bat-options)
  171. ((= c pong-ball)
  172. pong-ball-options)
  173. ((= c pong-border)
  174. pong-border-options)
  175. (t
  176. '(nil nil nil)))))
  177. options))
  178. (defun pong-init-buffer ()
  179. "Initialize pong buffer and draw stuff thanks to gamegrid library."
  180. (interactive)
  181. (get-buffer-create pong-buffer-name)
  182. (switch-to-buffer pong-buffer-name)
  183. (use-local-map pong-mode-map)
  184. (setq gamegrid-use-glyphs t)
  185. (setq gamegrid-use-color t)
  186. (gamegrid-init (pong-display-options))
  187. (gamegrid-init-buffer pong-width
  188. (+ 2 pong-height)
  189. ?\s)
  190. (let ((buffer-read-only nil))
  191. (loop for y from 0 to (1- pong-height) do
  192. (loop for x from 0 to (1- pong-width) do
  193. (gamegrid-set-cell x y pong-border)))
  194. (loop for y from 1 to (- pong-height 2) do
  195. (loop for x from 1 to (- pong-width 2) do
  196. (gamegrid-set-cell x y pong-blank))))
  197. (loop for y from pong-bat-player1 to (1- (+ pong-bat-player1 pong-bat-width)) do
  198. (gamegrid-set-cell 2 y pong-bat))
  199. (loop for y from pong-bat-player2 to (1- (+ pong-bat-player2 pong-bat-width)) do
  200. (gamegrid-set-cell (- pong-width 3) y pong-bat)))
  201. (defun pong-move-left ()
  202. "Move bat 2 up.
  203. This is called left for historical reasons, since in some pong
  204. implementations you move with left/right paddle."
  205. (interactive)
  206. (if (> pong-bat-player1 1)
  207. (and
  208. (setq pong-bat-player1 (1- pong-bat-player1))
  209. (pong-update-bat 2 pong-bat-player1))))
  210. (defun pong-move-right ()
  211. "Move bat 2 up."
  212. (interactive)
  213. (if (< (+ pong-bat-player1 pong-bat-width) (1- pong-height))
  214. (and
  215. (setq pong-bat-player1 (1+ pong-bat-player1))
  216. (pong-update-bat 2 pong-bat-player1))))
  217. (defun pong-move-up ()
  218. "Move bat 2 up."
  219. (interactive)
  220. (if (> pong-bat-player2 1)
  221. (and
  222. (setq pong-bat-player2 (1- pong-bat-player2))
  223. (pong-update-bat (- pong-width 3) pong-bat-player2))))
  224. (defun pong-move-down ()
  225. "Move bat 2 down."
  226. (interactive)
  227. (if (< (+ pong-bat-player2 pong-bat-width) (1- pong-height))
  228. (and
  229. (setq pong-bat-player2 (1+ pong-bat-player2))
  230. (pong-update-bat (- pong-width 3) pong-bat-player2))))
  231. (defun pong-update-bat (x y)
  232. "Move a bat (suppress a cell and draw another one on the other side)."
  233. (cond
  234. ((string-equal (buffer-name (current-buffer)) pong-buffer-name)
  235. (gamegrid-set-cell x y pong-bat)
  236. (gamegrid-set-cell x (1- (+ y pong-bat-width)) pong-bat)
  237. (if (> y 1)
  238. (gamegrid-set-cell x (1- y) pong-blank))
  239. (if (< (+ y pong-bat-width) (1- pong-height))
  240. (gamegrid-set-cell x (+ y pong-bat-width) pong-blank)))))
  241. (defun pong-init ()
  242. "Initialize a game."
  243. (define-key pong-mode-map pong-pause-key 'pong-pause)
  244. (add-hook 'kill-buffer-hook 'pong-quit nil t)
  245. ;; Initialization of some variables
  246. (setq pong-bat-player1 (1+ (/ (- pong-height pong-bat-width) 2)))
  247. (setq pong-bat-player2 pong-bat-player1)
  248. (setq pong-xx -1)
  249. (setq pong-yy 0)
  250. (setq pong-x (/ pong-width 2))
  251. (setq pong-y (/ pong-height 2))
  252. (pong-init-buffer)
  253. (gamegrid-kill-timer)
  254. (gamegrid-start-timer pong-timer-delay 'pong-update-game)
  255. (pong-update-score))
  256. (defun pong-update-game (pong-buffer)
  257. "\"Main\" function for pong.
  258. It is called every pong-cycle-delay seconds and
  259. updates ball and bats positions. It is responsible of collision
  260. detection and checks if a player scores."
  261. (if (not (eq (current-buffer) pong-buffer))
  262. (pong-pause)
  263. (let ((old-x pong-x)
  264. (old-y pong-y))
  265. (setq pong-x (+ pong-x pong-xx))
  266. (setq pong-y (+ pong-y pong-yy))
  267. (if (and (> old-y 0)
  268. (< old-y (- pong-height 1)))
  269. (gamegrid-set-cell old-x old-y pong-blank))
  270. (if (and (> pong-y 0)
  271. (< pong-y (- pong-height 1)))
  272. (gamegrid-set-cell pong-x pong-y pong-ball))
  273. (cond
  274. ((or (= pong-x 3) (= pong-x 2))
  275. (if (and (>= pong-y pong-bat-player1)
  276. (< pong-y (+ pong-bat-player1 pong-bat-width)))
  277. (and
  278. (setq pong-yy (+ pong-yy
  279. (cond
  280. ((= pong-y pong-bat-player1) -1)
  281. ((= pong-y (1+ pong-bat-player1)) 0)
  282. (t 1))))
  283. (setq pong-xx (- pong-xx)))))
  284. ((or (= pong-x (- pong-width 4)) (= pong-x (- pong-width 3)))
  285. (if (and (>= pong-y pong-bat-player2)
  286. (< pong-y (+ pong-bat-player2 pong-bat-width)))
  287. (and
  288. (setq pong-yy (+ pong-yy
  289. (cond
  290. ((= pong-y pong-bat-player2) -1)
  291. ((= pong-y (1+ pong-bat-player2)) 0)
  292. (t 1))))
  293. (setq pong-xx (- pong-xx)))))
  294. ((<= pong-y 1)
  295. (setq pong-yy (- pong-yy)))
  296. ((>= pong-y (- pong-height 2))
  297. (setq pong-yy (- pong-yy)))
  298. ((< pong-x 1)
  299. (setq pong-score-player2 (1+ pong-score-player2))
  300. (pong-init))
  301. ((>= pong-x (- pong-width 1))
  302. (setq pong-score-player1 (1+ pong-score-player1))
  303. (pong-init))))))
  304. (defun pong-update-score ()
  305. "Update score and print it on bottom of the game grid."
  306. (let* ((string (format "Score: %d / %d" pong-score-player1 pong-score-player2))
  307. (len (length string)))
  308. (loop for x from 0 to (1- len) do
  309. (if (string-equal (buffer-name (current-buffer)) pong-buffer-name)
  310. (gamegrid-set-cell x
  311. pong-height
  312. (aref string x))))))
  313. (defun pong-pause ()
  314. "Pause the game."
  315. (interactive)
  316. (gamegrid-kill-timer)
  317. ;; Oooohhh ugly. I don't know why, gamegrid-kill-timer don't do the
  318. ;; jobs it is made for. So I have to do it "by hand". Anyway, next
  319. ;; line is harmless.
  320. (cancel-function-timers 'pong-update-game)
  321. (define-key pong-mode-map pong-resume-key 'pong-resume))
  322. (defun pong-resume ()
  323. "Resume a paused game."
  324. (interactive)
  325. (define-key pong-mode-map pong-pause-key 'pong-pause)
  326. (gamegrid-start-timer pong-timer-delay 'pong-update-game))
  327. (defun pong-quit ()
  328. "Quit the game and kill the pong buffer."
  329. (interactive)
  330. (gamegrid-kill-timer)
  331. ;; Be sure not to draw things in another buffer and wait for some
  332. ;; time.
  333. (run-with-timer pong-timer-delay nil 'kill-buffer pong-buffer-name))
  334. ;;;###autoload
  335. (defun pong ()
  336. "Play pong and waste time.
  337. This is an implementation of the classical game pong.
  338. Move left and right bats and try to bounce the ball to your opponent.
  339. pong-mode keybindings:\\<pong-mode-map>
  340. \\{pong-mode-map}"
  341. (interactive)
  342. (setq pong-score-player1 0)
  343. (setq pong-score-player2 0)
  344. (pong-init))
  345. (provide 'pong)
  346. ;;; pong.el ends here