aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Huonker2010-07-24 01:26:42 +0200
committerStefan Monnier2010-07-24 01:26:42 +0200
commit195e19e4f90710c5ad4be9a3e47fcfa3b02e1604 (patch)
tree3a8d6b76783eb220fba4085de88b5b9140d999ef
parent9cf2db99c671636d9a37eec7027bdf6d2d9a5814 (diff)
downloademacs-195e19e4f90710c5ad4be9a3e47fcfa3b02e1604.tar.gz
emacs-195e19e4f90710c5ad4be9a3e47fcfa3b02e1604.zip
* lisp/play/tetris.el: Cleanup image representation and rotation.
(tetris-tty-colors, tetris-x-colors, tetris-blank): Remove leading nil element, adjust values. (tetris-shapes, tetris-shape-scores): Change representation of shapes and remove some redundancy. (tetris-get-shape-cell, tetris-shape-width, tetris-draw-next-shape) (tetris-draw-shape, tetris-erase-shape, tetris-test-shape): Adjust for working with new representation of shapes. (tetris-shape-rotations): New function. (tetris-move-bottom, tetris-move-left, tetris-move-right) (tetris-rotate-prev, tetris-rotate-next): Adjust for working with the new version of tetris-test-shape.
-rw-r--r--lisp/ChangeLog14
-rw-r--r--lisp/play/tetris.el233
2 files changed, 127 insertions, 120 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index dcd6d4fd28c..a51004a74a1 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,17 @@
12010-07-23 Lukas Huonker <l.huonker@gmail.com>
2
3 * play/tetris.el (tetris-tty-colors, tetris-x-colors, tetris-blank):
4 Remove leading nil element, adjust values.
5 (tetris-shapes, tetris-shape-scores):
6 Change representation of shapes and remove some redundancy.
7 (tetris-get-shape-cell, tetris-shape-width, tetris-draw-next-shape)
8 (tetris-draw-shape, tetris-erase-shape, tetris-test-shape):
9 Adjust for working with new representation of shapes.
10 (tetris-shape-rotations): New function.
11 (tetris-move-bottom, tetris-move-left, tetris-move-right)
12 (tetris-rotate-prev, tetris-rotate-next):
13 Adjust for working with the new version of tetris-test-shape.
14
12010-07-23 Markus Triska <markus.triska@gmx.at> 152010-07-23 Markus Triska <markus.triska@gmx.at>
2 16
3 * progmodes/ps-mode.el: Use comint (bug#5954). 17 * progmodes/ps-mode.el: Use comint (bug#5954).
diff --git a/lisp/play/tetris.el b/lisp/play/tetris.el
index 00ebbae2814..68d1590e571 100644
--- a/lisp/play/tetris.el
+++ b/lisp/play/tetris.el
@@ -76,13 +76,12 @@ If the return value is a number, it is used as the timer period."
76 :type 'hook) 76 :type 'hook)
77 77
78(defcustom tetris-tty-colors 78(defcustom tetris-tty-colors
79 [nil "blue" "white" "yellow" "magenta" "cyan" "green" "red"] 79 ["blue" "white" "yellow" "magenta" "cyan" "green" "red"]
80 "Vector of colors of the various shapes in text mode. 80 "Vector of colors of the various shapes in text mode."
81Element 0 is ignored."
82 :group 'tetris 81 :group 'tetris
83 :type (let ((names `("Shape 1" "Shape 2" "Shape 3" 82 :type (let ((names `("Shape 1" "Shape 2" "Shape 3"
84 "Shape 4" "Shape 5" "Shape 6" "Shape 7")) 83 "Shape 4" "Shape 5" "Shape 6" "Shape 7"))
85 (result `(vector (const nil)))) 84 (result nil))
86 (while names 85 (while names
87 (add-to-list 'result 86 (add-to-list 'result
88 (cons 'choice 87 (cons 'choice
@@ -96,9 +95,8 @@ Element 0 is ignored."
96 result)) 95 result))
97 96
98(defcustom tetris-x-colors 97(defcustom tetris-x-colors
99 [nil [0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]] 98 [[0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]]
100 "Vector of colors of the various shapes. 99 "Vector of colors of the various shapes."
101Element 0 is ignored."
102 :group 'tetris 100 :group 'tetris
103 :type 'sexp) 101 :type 'sexp)
104 102
@@ -196,51 +194,44 @@ Element 0 is ignored."
196;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 194;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197 195
198(defconst tetris-shapes 196(defconst tetris-shapes
199 [[[[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]] 197 [[[[0 0] [1 0] [0 1] [1 1]]]
200 [[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]] 198
201 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] 199 [[[0 0] [1 0] [2 0] [2 1]]
202 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] 200 [[1 -1] [1 0] [1 1] [0 1]]
203 201 [[0 -1] [0 0] [1 0] [2 0]]
204 [[[2 2 2 0] [0 2 0 0] [2 0 0 0] [2 2 0 0]] 202 [[1 -1] [2 -1] [1 0] [1 1]]]
205 [[0 0 2 0] [0 2 0 0] [2 2 2 0] [2 0 0 0]] 203
206 [[0 0 0 0] [2 2 0 0] [0 0 0 0] [2 0 0 0]] 204 [[[0 0] [1 0] [2 0] [0 1]]
207 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] 205 [[0 -1] [1 -1] [1 0] [1 1]]
208 206 [[2 -1] [0 0] [1 0] [2 0]]
209 [[[3 3 3 0] [3 3 0 0] [0 0 3 0] [3 0 0 0]] 207 [[1 -1] [1 0] [1 1] [2 1]]]
210 [[3 0 0 0] [0 3 0 0] [3 3 3 0] [3 0 0 0]] 208
211 [[0 0 0 0] [0 3 0 0] [0 0 0 0] [3 3 0 0]] 209 [[[0 0] [1 0] [1 1] [2 1]]
212 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] 210 [[1 0] [0 1] [1 1] [0 2]]]
213 211
214 [[[4 4 0 0] [0 4 0 0] [4 4 0 0] [0 4 0 0]] 212 [[[1 0] [2 0] [0 1] [1 1]]
215 [[0 4 4 0] [4 4 0 0] [0 4 4 0] [4 4 0 0]] 213 [[0 0] [0 1] [1 1] [1 2]]]
216 [[0 0 0 0] [4 0 0 0] [0 0 0 0] [4 0 0 0]] 214
217 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] 215 [[[1 0] [0 1] [1 1] [2 1]]
218 216 [[1 0] [1 1] [2 1] [1 2]]
219 [[[0 5 5 0] [5 0 0 0] [0 5 5 0] [5 0 0 0]] 217 [[0 1] [1 1] [2 1] [1 2]]
220 [[5 5 0 0] [5 5 0 0] [5 5 0 0] [5 5 0 0]] 218 [[1 0] [0 1] [1 1] [1 2]]]
221 [[0 0 0 0] [0 5 0 0] [0 0 0 0] [0 5 0 0]] 219
222 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]] 220 [[[0 0] [1 0] [2 0] [3 0]]
223 221 [[1 -1] [1 0] [1 1] [1 2]]]]
224 [[[0 6 0 0] [6 0 0 0] [6 6 6 0] [0 6 0 0]] 222 "Each shape is described by a vector that contains the coordinates of
225 [[6 6 6 0] [6 6 0 0] [0 6 0 0] [6 6 0 0]] 223each one of its four blocks.")
226 [[0 0 0 0] [6 0 0 0] [0 0 0 0] [0 6 0 0]]
227 [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
228
229 [[[7 7 7 7] [7 0 0 0] [7 7 7 7] [7 0 0 0]]
230 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]
231 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]
232 [[0 0 0 0] [7 0 0 0] [0 0 0 0] [7 0 0 0]]]])
233 224
234;;the scoring rules were taken from "xtetris". Blocks score differently 225;;the scoring rules were taken from "xtetris". Blocks score differently
235;;depending on their rotation 226;;depending on their rotation
236 227
237(defconst tetris-shape-scores 228(defconst tetris-shape-scores
238 [ [6 6 6 6] [6 7 6 7] [6 7 6 7] [6 7 6 7] [6 7 6 7] [5 5 6 5] [5 8 5 8]] ) 229 [[6] [6 7 6 7] [6 7 6 7] [6 7] [6 7] [5 5 6 5] [5 8]] )
239 230
240(defconst tetris-shape-dimensions 231(defconst tetris-shape-dimensions
241 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]]) 232 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]])
242 233
243(defconst tetris-blank 0) 234(defconst tetris-blank 7)
244 235
245(defconst tetris-border 8) 236(defconst tetris-border 8)
246 237
@@ -299,7 +290,7 @@ Element 0 is ignored."
299 (aset options c 290 (aset options c
300 (cond ((= c tetris-blank) 291 (cond ((= c tetris-blank)
301 tetris-blank-options) 292 tetris-blank-options)
302 ((and (>= c 1) (<= c 7)) 293 ((and (>= c 0) (<= c 6))
303 (append 294 (append
304 tetris-cell-options 295 tetris-cell-options
305 `((((glyph color-x) ,(aref tetris-x-colors c)) 296 `((((glyph color-x) ,(aref tetris-x-colors c))
@@ -320,20 +311,16 @@ Element 0 is ignored."
320 tetris-n-rows nil))) 311 tetris-n-rows nil)))
321 (and (numberp period) period)))) 312 (and (numberp period) period))))
322 313
323(defun tetris-get-shape-cell (x y) 314(defun tetris-get-shape-cell (block)
324 (aref (aref (aref (aref tetris-shapes 315 (aref (aref (aref tetris-shapes
325 tetris-shape) 316 tetris-shape) tetris-rot)
326 y) 317 block))
327 tetris-rot)
328 x))
329 318
330(defun tetris-shape-width () 319(defun tetris-shape-width ()
331 (aref (aref tetris-shape-dimensions tetris-shape) 320 (aref (aref tetris-shape-dimensions tetris-shape) 0))
332 (% tetris-rot 2)))
333 321
334(defun tetris-shape-height () 322(defun tetris-shape-rotations ()
335 (aref (aref tetris-shape-dimensions tetris-shape) 323 (length (aref tetris-shapes tetris-shape)))
336 (- 1 (% tetris-rot 2))))
337 324
338(defun tetris-draw-score () 325(defun tetris-draw-score ()
339 (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes) 326 (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes)
@@ -365,52 +352,58 @@ Element 0 is ignored."
365 (tetris-update-score))) 352 (tetris-update-score)))
366 353
367(defun tetris-draw-next-shape () 354(defun tetris-draw-next-shape ()
368 (loop for y from 0 to 3 do 355 (loop for x from 0 to 3 do
369 (loop for x from 0 to 3 do 356 (loop for y from 0 to 3 do
370 (gamegrid-set-cell (+ tetris-next-x x) 357 (gamegrid-set-cell (+ tetris-next-x x)
371 (+ tetris-next-y y) 358 (+ tetris-next-y y)
372 (let ((tetris-shape tetris-next-shape) 359 tetris-blank)))
373 (tetris-rot 0)) 360 (loop for i from 0 to 3 do
374 (tetris-get-shape-cell x y)))))) 361 (let ((tetris-shape tetris-next-shape)
362 (tetris-rot 0))
363 (gamegrid-set-cell (+ tetris-next-x
364 (aref (tetris-get-shape-cell i) 0))
365 (+ tetris-next-y
366 (aref (tetris-get-shape-cell i) 1))
367 tetris-shape))))
375 368
376(defun tetris-draw-shape () 369(defun tetris-draw-shape ()
377 (loop for y from 0 to (1- (tetris-shape-height)) do 370 (loop for i from 0 to 3 do
378 (loop for x from 0 to (1- (tetris-shape-width)) do 371 (let ((c (tetris-get-shape-cell i)))
379 (let ((c (tetris-get-shape-cell x y))) 372 (gamegrid-set-cell (+ tetris-top-left-x
380 (if (/= c tetris-blank) 373 tetris-pos-x
381 (gamegrid-set-cell (+ tetris-top-left-x 374 (aref c 0))
382 tetris-pos-x 375 (+ tetris-top-left-y
383 x) 376 tetris-pos-y
384 (+ tetris-top-left-y 377 (aref c 1))
385 tetris-pos-y 378 tetris-shape))))
386 y)
387 c))))))
388 379
389(defun tetris-erase-shape () 380(defun tetris-erase-shape ()
390 (loop for y from 0 to (1- (tetris-shape-height)) do 381 (loop for i from 0 to 3 do
391 (loop for x from 0 to (1- (tetris-shape-width)) do 382 (let ((c (tetris-get-shape-cell i)))
392 (let ((c (tetris-get-shape-cell x y)) 383 (gamegrid-set-cell (+ tetris-top-left-x
393 (px (+ tetris-top-left-x tetris-pos-x x)) 384 tetris-pos-x
394 (py (+ tetris-top-left-y tetris-pos-y y))) 385 (aref c 0))
395 (if (/= c tetris-blank) 386 (+ tetris-top-left-y
396 (gamegrid-set-cell px py tetris-blank)))))) 387 tetris-pos-y
388 (aref c 1))
389 tetris-blank))))
397 390
398(defun tetris-test-shape () 391(defun tetris-test-shape ()
399 (let ((hit nil)) 392 (let ((hit nil))
400 (loop for y from 0 to (1- (tetris-shape-height)) do 393 (loop for i from 0 to 3 do
401 (loop for x from 0 to (1- (tetris-shape-width)) do 394 (unless hit
402 (unless hit 395 (setq hit
403 (setq hit 396 (let* ((c (tetris-get-shape-cell i))
404 (let* ((c (tetris-get-shape-cell x y)) 397 (xx (+ tetris-pos-x
405 (xx (+ tetris-pos-x x)) 398 (aref c 0)))
406 (yy (+ tetris-pos-y y)) 399 (yy (+ tetris-pos-y
407 (px (+ tetris-top-left-x xx)) 400 (aref c 1))))
408 (py (+ tetris-top-left-y yy))) 401 (or (>= xx tetris-width)
409 (and (/= c tetris-blank) 402 (>= yy tetris-height)
410 (or (>= xx tetris-width) 403 (/= (gamegrid-get-cell
411 (>= yy tetris-height) 404 (+ xx tetris-top-left-x)
412 (/= (gamegrid-get-cell px py) 405 (+ yy tetris-top-left-y))
413 tetris-blank)))))))) 406 tetris-blank))))))
414 hit)) 407 hit))
415 408
416(defun tetris-full-row (y) 409(defun tetris-full-row (y)
@@ -510,33 +503,30 @@ Drops the shape one square, testing for collision."
510(defun tetris-move-bottom () 503(defun tetris-move-bottom ()
511 "Drop the shape to the bottom of the playing area." 504 "Drop the shape to the bottom of the playing area."
512 (interactive) 505 (interactive)
513 (if (not tetris-paused) 506 (unless tetris-paused
514 (let ((hit nil)) 507 (let ((hit nil))
515 (tetris-erase-shape) 508 (tetris-erase-shape)
516 (while (not hit) 509 (while (not hit)
517 (setq tetris-pos-y (1+ tetris-pos-y)) 510 (setq tetris-pos-y (1+ tetris-pos-y))
518 (setq hit (tetris-test-shape))) 511 (setq hit (tetris-test-shape)))
519 (setq tetris-pos-y (1- tetris-pos-y)) 512 (setq tetris-pos-y (1- tetris-pos-y))
520 (tetris-draw-shape) 513 (tetris-draw-shape)
521 (tetris-shape-done)))) 514 (tetris-shape-done))))
522 515
523(defun tetris-move-left () 516(defun tetris-move-left ()
524 "Move the shape one square to the left." 517 "Move the shape one square to the left."
525 (interactive) 518 (interactive)
526 (unless (or (= tetris-pos-x 0) 519 (unless tetris-paused
527 tetris-paused)
528 (tetris-erase-shape) 520 (tetris-erase-shape)
529 (setq tetris-pos-x (1- tetris-pos-x)) 521 (setq tetris-pos-x (1- tetris-pos-x))
530 (if (tetris-test-shape) 522 (if (tetris-test-shape)
531 (setq tetris-pos-x (1+ tetris-pos-x))) 523 (setq tetris-pos-x (1+ tetris-pos-x)))
532 (tetris-draw-shape))) 524 (tetris-draw-shape)))
533 525
534(defun tetris-move-right () 526(defun tetris-move-right ()
535 "Move the shape one square to the right." 527 "Move the shape one square to the right."
536 (interactive) 528 (interactive)
537 (unless (or (= (+ tetris-pos-x (tetris-shape-width)) 529 (unless tetris-paused
538 tetris-width)
539 tetris-paused)
540 (tetris-erase-shape) 530 (tetris-erase-shape)
541 (setq tetris-pos-x (1+ tetris-pos-x)) 531 (setq tetris-pos-x (1+ tetris-pos-x))
542 (if (tetris-test-shape) 532 (if (tetris-test-shape)
@@ -546,23 +536,26 @@ Drops the shape one square, testing for collision."
546(defun tetris-rotate-prev () 536(defun tetris-rotate-prev ()
547 "Rotate the shape clockwise." 537 "Rotate the shape clockwise."
548 (interactive) 538 (interactive)
549 (if (not tetris-paused) 539 (unless tetris-paused
550 (progn (tetris-erase-shape) 540 (tetris-erase-shape)
551 (setq tetris-rot (% (+ 1 tetris-rot) 4)) 541 (setq tetris-rot (% (+ 1 tetris-rot)
552 (if (tetris-test-shape) 542 (tetris-shape-rotations)))
553 (setq tetris-rot (% (+ 3 tetris-rot) 4))) 543 (if (tetris-test-shape)
554 (tetris-draw-shape)))) 544 (setq tetris-rot (% (+ 3 tetris-rot)
545 (tetris-shape-rotations))))
546 (tetris-draw-shape)))
555 547
556(defun tetris-rotate-next () 548(defun tetris-rotate-next ()
557 "Rotate the shape anticlockwise." 549 "Rotate the shape anticlockwise."
558 (interactive) 550 (interactive)
559 (if (not tetris-paused) 551 (unless tetris-paused
560 (progn
561 (tetris-erase-shape) 552 (tetris-erase-shape)
562 (setq tetris-rot (% (+ 3 tetris-rot) 4)) 553 (setq tetris-rot (% (+ 3 tetris-rot)
554 (tetris-shape-rotations)))
563 (if (tetris-test-shape) 555 (if (tetris-test-shape)
564 (setq tetris-rot (% (+ 1 tetris-rot) 4))) 556 (setq tetris-rot (% (+ 1 tetris-rot)
565 (tetris-draw-shape)))) 557 (tetris-shape-rotations))))
558 (tetris-draw-shape)))
566 559
567(defun tetris-end-game () 560(defun tetris-end-game ()
568 "Terminate the current game." 561 "Terminate the current game."