games / tetris.el

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
;;; tetris.el -- Implementation of Tetris for Emacs.

;; Copyright (C) 1998 Free Software Foundation, Inc.

;; Author: Glynn Clements <glynn.clements@virgin.net>
;; Version: 2.03
;; Created: 1997-08-13
;; Keywords: games

;; This file is part of XEmacs.

;; XEmacs is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.

;; XEmacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with XEmacs; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
;; 02111-1307, USA.

;;; Synched up with: Not synched.

;;; Commentary:

;; Modified: 1997-08-17, added tetris-move-bottom
;; Modified: 1997-08-22, changed setting of display table for compatibility
;;	with XEmacs 19.15
;; Modified: 1997-08-23, changed setting of display table for TTY compatibility
;; Modified: 1997-08-24, various changes for FSF Emacs compatibility
;; Modified: 1997-08-25
;;	modified existing docstrings, added new docstrings
;;	L now rotates the same way as T and mirror-L
;;	now adds tetris-end-game to buffer-local value of kill-buffer-hook
;; Modified: 1997-08-26, miscellaneous bugfixes
;; Modified: 1997-08-27
;;	added color support for non-glyph mode
;;	added tetris-mode-hook
;;	added tetris-update-speed-function
;; Modified: 1997-09-09, changed layout to work in a 22 line window
;; Modified: 1997-09-10, exported display handling to gamegrid.el
;; Modified: 1997-09-12
;;	fixed tetris-shift-down to deal with multiple rows correctly
;; Modified: 1998-01-05  (cgw) added pause, score, high score file
;; Modified: 1998-05-28
;;	Make new shapes appear at the centre of the top edge
;;	Test whether the next shape can be placed before replacing it
;;	Make tetris-shift-down clear the top row
;;	Added popup menu
;; Modified: 1998-06-23, copyright assigned to FSF
;; Modified: 2003-06-14
;;	update email address, remove URL
;;	various changes from David Costanzo <david_costanzo@yahoo.com>

;; Tested with XEmacs 20.3/4/5, 21.4.13 and Emacs 19.34

(eval-when-compile
  (require 'cl))

(require 'gamegrid)

;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar tetris-use-glyphs t
  "Non-nil means use glyphs when available")

(defvar tetris-use-color t
  "Non-nil means use color when available")

(defvar tetris-draw-border-with-glyphs t
  "Non-nil means draw a border even when using glyphs")

(defvar tetris-default-tick-period 0.3
  "The default time taken for a shape to drop one row")

(defvar tetris-clear-wait-tick-period 0.5
  "The default time taken to clear a full row.")

(defvar tetris-update-speed-function
  'tetris-default-update-speed-function
  "Function run whenever the Tetris score changes
Called with two arguments: (SHAPES ROWS)
SHAPES is the number of shapes which have been dropped
ROWS is the number of rows which have been completed

If the return value is a number, it is used as the timer period")

(defvar tetris-mode-hook nil
  "Hook run upon starting Tetris")

(defvar tetris-tty-colors
  [nil "blue" "white" "yellow" "magenta" "cyan" "green" "red"]
  "Vector of colors of the various shapes in text mode
Element 0 is ignored")

(defvar tetris-x-colors
  [nil [0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]]
  "Vector of colors of the various shapes
Element 0 is ignored")

(defvar tetris-buffer-name "*Tetris*"
  "Name used for Tetris buffer")

(defvar tetris-buffer-width 30
  "Width of used portion of buffer")

(defvar tetris-buffer-height 22
  "Height of used portion of buffer")

(defvar tetris-width 10
  "Width of playing area")

(defvar tetris-height 20
  "Height of playing area")

(defvar tetris-top-left-x 3
  "X position of top left of playing area")

(defvar tetris-top-left-y 1
  "Y position of top left of playing area")

(defvar tetris-next-x (+ (* 2 tetris-top-left-x) tetris-width)
  "X position of next shape")

(defvar tetris-next-y tetris-top-left-y
  "Y position of next shape")

(defvar tetris-score-x tetris-next-x
  "X position of score")

(defvar tetris-score-y (+ tetris-next-y 6)
  "Y position of score")

(defvar tetris-score-file "/tmp/tetris-scores"
;; anybody with a well-connected server want to host this?
;(defvar tetris-score-file "/anonymous@ftp.pgt.com:/pub/cgw/tetris-scores"
  "File for holding high scores")

;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar tetris-border-options
  '(((glyph colorize)
     (t ?\+))
    ((color-x color-x)
     (mono-x grid-x)
     (t nil))
    (((glyph color-x) [0.5 0.5 0.5])
     (t nil))))

(defvar tetris-blank-options
  '(((glyph colorize)
     (t ?\040))
    ((color-x color-x)
     (mono-x grid-x)
     (color-tty color-tty)
     (t nil))
    (((glyph color-x) [0 0 0])
     (color-tty "black")
     (t nil))))

(defvar tetris-clearing-options
  '(((glyph colorize)
     (t ?\*))
    ((color-x color-x)
     (mono-x grid-x)
     (t nil))
    (((glyph color-x) [1.0 1.0 1.0])
     (color-tty "white")
     (t nil))))

(defvar tetris-cell-options
  '(((glyph colorize)
     (emacs-tty ?O)
     (t ?\040))
    ((color-x color-x)
     (mono-x mono-x)
     (color-tty color-tty)
     (mono-tty mono-tty)
     (t nil))
    ;; color information is taken from tetris-x-colors and tetris-tty-colors
    ))

(defvar tetris-space-options
  '(((t ?\040))
    nil
    nil))

;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defconst tetris-shapes
  [[[[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]]
    [[1 1 0 0] [1 1 0 0] [1 1 0 0] [1 1 0 0]]
    [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]
    [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]

   [[[2 2 2 0] [0 2 0 0] [2 0 0 0] [2 2 0 0]]
    [[0 0 2 0] [0 2 0 0] [2 2 2 0] [2 0 0 0]]
    [[0 0 0 0] [2 2 0 0] [0 0 0 0] [2 0 0 0]]
    [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]

   [[[3 3 3 0] [3 3 0 0] [0 0 3 0] [3 0 0 0]]
    [[3 0 0 0] [0 3 0 0] [3 3 3 0] [3 0 0 0]]
    [[0 0 0 0] [0 3 0 0] [0 0 0 0] [3 3 0 0]]
    [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]

   [[[4 4 0 0] [0 4 0 0] [4 4 0 0] [0 4 0 0]]
    [[0 4 4 0] [4 4 0 0] [0 4 4 0] [4 4 0 0]]
    [[0 0 0 0] [4 0 0 0] [0 0 0 0] [4 0 0 0]]
    [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]

   [[[0 5 5 0] [5 0 0 0] [0 5 5 0] [5 0 0 0]]
    [[5 5 0 0] [5 5 0 0] [5 5 0 0] [5 5 0 0]]
    [[0 0 0 0] [0 5 0 0] [0 0 0 0] [0 5 0 0]]
    [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]

   [[[0 6 0 0] [0 6 0 0] [0 0 0 0] [0 6 0 0]]
    [[6 6 6 0] [0 6 6 0] [6 6 6 0] [6 6 0 0]]
    [[0 0 0 0] [0 6 0 0] [0 6 0 0] [0 6 0 0]]
    [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]

   [[[7 7 7 7] [0 7 0 0] [7 7 7 7] [0 7 0 0]]
    [[0 0 0 0] [0 7 0 0] [0 0 0 0] [0 7 0 0]]
    [[0 0 0 0] [0 7 0 0] [0 0 0 0] [0 7 0 0]]
    [[0 0 0 0] [0 7 0 0] [0 0 0 0] [0 7 0 0]]]])

;;the scoring rules were taken from "xtetris".  Blocks score differently 
;;depending on their rotation

(defconst tetris-shape-scores 
  [ [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]] )

(defconst tetris-shape-dimensions
  [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]])

(defconst tetris-blank 0)

(defconst tetris-border 8)

(defconst tetris-space 9)

(defconst tetris-clearing 11)

;;; Game states
(defconst tetris-state-playing       0)
(defconst tetris-state-clearing      1)
(defconst tetris-state-clearing-wait 2)

(defun tetris-default-update-speed-function (shapes rows)
  (/ 20.0 (+ 50.0 rows)))

;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar tetris-shape 0)
(defvar tetris-rot 0)
(defvar tetris-next-shape 0)
(defvar tetris-n-shapes 0)
(defvar tetris-n-rows 0)
(defvar tetris-score 0)
(defvar tetris-pos-x 0)
(defvar tetris-pos-y 0)
(defvar tetris-paused nil)
(defvar tetris-state  tetris-state-playing)
(defvar tetris-shapes-bounding-boxes nil)

(make-variable-buffer-local 'tetris-shape)
(make-variable-buffer-local 'tetris-rot)
(make-variable-buffer-local 'tetris-next-shape)
(make-variable-buffer-local 'tetris-n-shapes)
(make-variable-buffer-local 'tetris-n-rows)
(make-variable-buffer-local 'tetris-score)
(make-variable-buffer-local 'tetris-pos-x)
(make-variable-buffer-local 'tetris-pos-y)
(make-variable-buffer-local 'tetris-paused)

;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar tetris-mode-map
  (make-sparse-keymap 'tetris-mode-map))

(define-key tetris-mode-map "n"		'tetris-start-game)
(define-key tetris-mode-map "q"		'tetris-end-game)
(define-key tetris-mode-map "p"		'tetris-pause-game)

(define-key tetris-mode-map " "		'tetris-move-bottom)
(define-key tetris-mode-map [left]	'tetris-move-left)
(define-key tetris-mode-map [right]	'tetris-move-right)
(define-key tetris-mode-map [up]	'tetris-rotate-prev)
(define-key tetris-mode-map [down]	'tetris-rotate-next)

(defvar tetris-null-map
  (make-sparse-keymap 'tetris-null-map))

(define-key tetris-null-map "n"		'tetris-start-game)

;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun tetris-display-options ()
  (let ((options (make-vector 256 nil)))
    (loop for c from 0 to 255 do
      (aset options c
	    (cond ((= c tetris-blank)
		    tetris-blank-options)
                  ((and (>= c 1) (<= c 7))
		   (append
		    tetris-cell-options
		    `((((glyph color-x) ,(aref tetris-x-colors c))
		       (color-tty ,(aref tetris-tty-colors c))
		       (t nil)))))
		   ((= c tetris-border)
		    tetris-border-options)
		   ((= c tetris-space)
		    tetris-space-options)
		   ((= c tetris-clearing)
		    tetris-clearing-options)
                  (t
                   '(nil nil nil)))))
    options))

(defun tetris-get-tick-period ()
  (if (boundp 'tetris-update-speed-function)
      (let ((period (apply tetris-update-speed-function
			   tetris-n-shapes
			   tetris-n-rows nil)))
	(and (numberp period) period))))

(defun tetris-shape-rotation-get-cell (shape rotation x y)
  (aref (aref (aref (aref tetris-shapes
			  shape)
		    y)
	      rotation)
	x))

(defun tetris-get-shape-cell (x y)
  (tetris-shape-rotation-get-cell tetris-shape tetris-rot x y))

(defun tetris-shape-width (shape rotation)
  (aref (aref tetris-shape-dimensions shape)
	(% rotation 2)))

(defun tetris-shape-height (shape rotation)
  (aref (aref tetris-shape-dimensions shape)
	(- 1 (% rotation 2))))

(defun tetris-draw-score ()
  (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes)
			 (format "Rows:   %05d" tetris-n-rows)
			 (format "Score:  %05d" tetris-score))))
    (loop for y from 0 to 2 do
	  (let* ((string (aref strings y))
		 (len (length string)))
	    (loop for x from 0 to (1- len) do
		  (gamegrid-set-cell (+ tetris-score-x x)
				     (+ tetris-score-y y)
				     (aref string x)))))))


(defun tetris-shape-min-x (shape rotation)
  (tetris-bounding-box-min-x (aref (aref tetris-shapes-bounding-boxes 
					    shape) 
				      rotation)))

(defun tetris-shape-max-x (shape rotation)
  (tetris-bounding-box-max-x (aref (aref tetris-shapes-bounding-boxes 
					    shape) 
				      rotation)))

(defun tetris-shape-min-y (shape rotation)
  (tetris-bounding-box-min-y (aref (aref tetris-shapes-bounding-boxes 
					    shape) 
				      rotation)))

(defun tetris-shape-max-y (shape rotation)
  (tetris-bounding-box-max-y (aref (aref tetris-shapes-bounding-boxes 
					    shape) 
				      rotation)))

;; Structure for a 2D bounding box.
(defstruct (tetris-bounding-box)
  (min-x 0)
  (min-y 0)
  (max-x 3)
  (max-y 3))


;; Adds a new point to the bounding box, adjusting the min and max values 
;; for x and y necessary
(defun tetris-bounding-box-add-point (box x y)
  (if (< x (tetris-bounding-box-min-x box)) ; new min-x
      (setf (tetris-bounding-box-min-x box) x))
  (if (< y (tetris-bounding-box-min-y box)) ; new min-y
      (setf (tetris-bounding-box-min-y box) y))
  (if (> x (tetris-bounding-box-max-x box)) ; new max-x
      (setf (tetris-bounding-box-max-x box) x))
  (if (> y (tetris-bounding-box-max-y box)) ; new min-y
      (setf (tetris-bounding-box-max-y box) y)))

;; Computes the bounding boxes for all shape/rotation pairs.
;; Returns the result as a 2D array of tetris-bounding-box structures, 
;; where the first dimensions is keyed by the shape index and the second
;; dimension is keyed by the rotation.
(defun tetris-bounding-box-compute-all () 
  
  ; create the structure
  (let ((bounding-boxes (make-vector 7 nil)))
    (loop for current-shape from 0 to 6 do
      (setf (aref bounding-boxes current-shape) (make-vector 4 nil))
      (loop for current-rotation from 0 to 3 do
	(let ((bounding-box (make-tetris-bounding-box :min-x 3 
							 :min-y 3 
							 :max-x 0 
							 :max-y 0)))

	  (setf (aref (aref bounding-boxes 
			    current-shape) 
		      current-rotation)
		bounding-box))))

  ; fill in the values
  (loop for current-shape from 0 to 6 do
      (loop for current-rotation from 0 to 3 do
	(loop for x from 0 to 3 do
	    (loop for y from 0 to 3 do
	      (if (/= tetris-space
		      (tetris-shape-rotation-get-cell current-shape current-rotation x y))
		  (tetris-bounding-box-add-point (aref (aref bounding-boxes 
								current-shape) 
							  current-rotation)
						    x 
						    y))))))
  bounding-boxes))


(defun tetris-update-score ()
  (tetris-draw-score)
  (let ((period (tetris-get-tick-period)))
    (if period (gamegrid-set-timer period))))

(defun tetris-new-shape ()
  (setq tetris-shape tetris-next-shape)
  (setq tetris-rot 0)
  (setq tetris-next-shape (random 7))
  (setq tetris-pos-x (/ (- tetris-width (tetris-shape-width tetris-shape 
                                                            tetris-rot)) 
                        2))
  (setq tetris-pos-y 0)
  (if (tetris-test-shape)
      (tetris-end-game)
    (tetris-draw-shape))
  (tetris-draw-next-shape)
  (tetris-update-score))

(defun tetris-draw-next-shape ()
  (loop for y from 0 to 3 do
	(loop for x from 0 to 3 do
	      (gamegrid-set-cell (+ tetris-next-x x)
				 (+ tetris-next-y y)
				 (let ((tetris-shape tetris-next-shape)
				       (tetris-rot 0))
				   (tetris-get-shape-cell x y))))))

(defun tetris-draw-shape ()
  (let ((min-x (tetris-shape-min-x tetris-shape tetris-rot))
	(min-y (tetris-shape-min-y tetris-shape tetris-rot))
	(max-x (tetris-shape-max-x tetris-shape tetris-rot))
	(max-y (tetris-shape-max-y tetris-shape tetris-rot)))
    (loop for y from min-y to max-y do
	(loop for x from min-x to max-x do
	      (let ((c (tetris-get-shape-cell x y)))
		(if (/= c tetris-blank)
		    (gamegrid-set-cell (+ tetris-top-left-x
					  tetris-pos-x
					  x)
				       (+ tetris-top-left-y
					  tetris-pos-y
					  y)
				       c)))))))

(defun tetris-erase-shape ()
  (let ((min-x (tetris-shape-min-x tetris-shape tetris-rot))
	(min-y (tetris-shape-min-y tetris-shape tetris-rot))
	(max-x (tetris-shape-max-x tetris-shape tetris-rot))
	(max-y (tetris-shape-max-y tetris-shape tetris-rot)))
    (loop for y from min-y to max-y do
	(loop for x from min-x to max-x do
	      (let ((c (tetris-get-shape-cell x y))
		    (px (+ tetris-top-left-x tetris-pos-x x))
		    (py (+ tetris-top-left-y tetris-pos-y y)))
		(if (/= c tetris-blank)
		    (gamegrid-set-cell px py tetris-blank)))))))

(defun tetris-test-shape ()
  (let ((min-x (tetris-shape-min-x tetris-shape tetris-rot))
	(min-y (tetris-shape-min-y tetris-shape tetris-rot))
	(max-x (tetris-shape-max-x tetris-shape tetris-rot))
	(max-y (tetris-shape-max-y tetris-shape tetris-rot)))
    (let ((hit nil))
       (loop for y from min-y to max-y do
	    (loop for x from min-x to max-x do
		(unless hit
		  (setq hit
			(let* ((c (tetris-get-shape-cell x y))
			      (xx (+ tetris-pos-x x))
			      (yy (+ tetris-pos-y y))
			      (px (+ tetris-top-left-x xx))
			      (py (+ tetris-top-left-y yy)))
			  (and (/= c tetris-blank)
			       (or (>= xx tetris-width)
				   (>= yy tetris-height)
				   (/= (gamegrid-get-cell px py)
				       tetris-blank))))))))
    hit)))

(defun tetris-full-rows () 
  (let (full-rows)
    (loop for y from 0 to (1- tetris-height) do
      (if (tetris-full-row y)
	  (push y full-rows)))
    full-rows))

(defun tetris-mark-rows-for-clearing (rows) 
  (dolist (row-y rows)
    (loop for x from 0 to (1- tetris-width) do
      (gamegrid-set-cell (+ tetris-top-left-x x)
			 (+ tetris-top-left-y row-y)
			 tetris-clearing)
      )))

(defun tetris-full-row (y)
  (let ((full t))
    (loop for x from 0 to (1- tetris-width) do
	  (if (= (gamegrid-get-cell (+ tetris-top-left-x x)
				    (+ tetris-top-left-y y))
		 tetris-blank)
	      (setq full nil)))
    full))

(defun tetris-shift-row (y)
  (if (= y 0)
      (loop for x from 0 to (1- tetris-width) do
	(gamegrid-set-cell (+ tetris-top-left-x x)
			   (+ tetris-top-left-y y)
			   tetris-blank))
  (loop for x from 0 to (1- tetris-width) do
	(let ((c (gamegrid-get-cell (+ tetris-top-left-x x)
				    (+ tetris-top-left-y y -1))))
	  (gamegrid-set-cell (+ tetris-top-left-x x)
			     (+ tetris-top-left-y y)
			   c)))))

(defun tetris-shift-down ()
  (loop for y0 from 0 to (1- tetris-height) do
	(if (tetris-full-row y0)
	    (progn (setq tetris-n-rows (1+ tetris-n-rows))
		   (loop for y from y0 downto 0 do
			 (tetris-shift-row y))))))

(defun tetris-draw-border-p ()
  (or (not (eq gamegrid-display-mode 'glyph))
      tetris-draw-border-with-glyphs))

(defun tetris-init-buffer ()
  (gamegrid-init-buffer tetris-buffer-width
			tetris-buffer-height
			tetris-space)
  (let ((buffer-read-only nil))
    (if (tetris-draw-border-p)
	(loop for y from -1 to tetris-height do
	      (loop for x from -1 to tetris-width do
		    (gamegrid-set-cell (+ tetris-top-left-x x)
				       (+ tetris-top-left-y y)
				       tetris-border))))
    (loop for y from 0 to (1- tetris-height) do
	  (loop for x from 0 to (1- tetris-width) do
		(gamegrid-set-cell (+ tetris-top-left-x x)
				   (+ tetris-top-left-y y)
				   tetris-blank)))
    (if (tetris-draw-border-p)
	(loop for y from -1 to 4 do
	      (loop for x from -1 to 4 do
		    (gamegrid-set-cell (+ tetris-next-x x)
				       (+ tetris-next-y y)
				       tetris-border))))))

(defun tetris-reset-game ()
  (gamegrid-kill-timer)
  (tetris-init-buffer)
  (setq tetris-next-shape (random 7))
  (setq tetris-shape	0
	tetris-rot	0
	tetris-pos-x	0
	tetris-pos-y	0
	tetris-n-shapes	0
	tetris-n-rows	0
	tetris-score	0
	tetris-paused	nil
        tetris-state    tetris-state-playing)
  (tetris-new-shape))

(defun tetris-shape-done ()
  (let ((full-rows (tetris-full-rows)))

    ;; Update the score while the shape still exists
    (setq tetris-n-shapes (1+ tetris-n-shapes))
    (setq tetris-score
	  (+ tetris-score 
	     (aref (aref tetris-shape-scores tetris-shape) tetris-rot)))
    (tetris-update-score)

    (if full-rows
	;; state transition to clear the full rows
	(progn
	  (setq tetris-state tetris-state-clearing-wait)
	  (setq tetris-shape nil))
      ;; drop the next shape
      (tetris-new-shape))))

(defun tetris-update-game (tetris-buffer)
  "Called on each clock tick.
Drops the shape one square, testing for collision."
  (if (eq (current-buffer) tetris-buffer)
      (cond 
	((eq tetris-state tetris-state-playing) ; The game is playing
	 (if (not tetris-paused)
	     (let (hit)
	       (tetris-erase-shape)
	       (setq tetris-pos-y (1+ tetris-pos-y))
	       (setq hit (tetris-test-shape))
	       (if hit
		   (setq tetris-pos-y (1- tetris-pos-y)))
	       (tetris-draw-shape)
	       (if hit
		   (tetris-shape-done)))))
	

	 ((eq tetris-state tetris-state-clearing-wait) ; The game is marking a full row
	  (tetris-mark-rows-for-clearing (tetris-full-rows))
	  (setq tetris-state tetris-state-clearing)

	  ; Show the marked rows for a peroid of time that is
          ; independent of current game speed.
	  (gamegrid-set-timer tetris-clear-wait-tick-period))
	

	 ((eq tetris-state tetris-state-clearing) ; The game is clearing a full row

	  (tetris-shift-down) 
	  (tetris-new-shape)

	  ; Restore the tick period back to game-speed
	  (let ((period (tetris-get-tick-period)))
	    (if period 
		(gamegrid-set-timer period)
		(gamegrid-set-timer tetris-default-tick-period)))
	      
	  (setq tetris-state tetris-state-playing)))))


(defun tetris-move-bottom ()
  "Drops the shape to the bottom of the playing area"
  (interactive)
  (if (eq tetris-state tetris-state-playing)
      (let ((hit nil))
        (tetris-erase-shape)
        (while (not hit)
          (setq tetris-pos-y (1+ tetris-pos-y))
          (setq hit (tetris-test-shape)))
        (setq tetris-pos-y (1- tetris-pos-y))
        (tetris-draw-shape)
        (tetris-shape-done))))

(defun tetris-move-left ()
  "Moves the shape one square to the left"
  (interactive)
  (if (eq tetris-state tetris-state-playing)
      (progn
        (tetris-erase-shape)
        (setq tetris-pos-x (1- tetris-pos-x))
        (if (tetris-test-shape)
            (setq tetris-pos-x (1+ tetris-pos-x)))
        (tetris-draw-shape))))

(defun tetris-move-right ()
  "Moves the shape one square to the right"
  (interactive)
  (if (eq tetris-state tetris-state-playing)
      (progn
        (tetris-erase-shape)
        (setq tetris-pos-x (1+ tetris-pos-x))
        (if (tetris-test-shape)
            (setq tetris-pos-x (1- tetris-pos-x)))
        (tetris-draw-shape))))

(defun tetris-rotate-prev ()
  "Rotates the shape clockwise"
  (interactive)
  (if (eq tetris-state tetris-state-playing)
      (progn
        (tetris-erase-shape)
        (setq tetris-rot (% (+ 1 tetris-rot) 4))
        (if (tetris-test-shape)
            (setq tetris-rot (% (+ 3 tetris-rot) 4)))
        (tetris-draw-shape))))

(defun tetris-rotate-next ()
  "Rotates the shape anticlockwise"
  (interactive)
  (if (eq tetris-state tetris-state-playing)
      (progn
        (tetris-erase-shape)
        (setq tetris-rot (% (+ 3 tetris-rot) 4))
        (if (tetris-test-shape)
            (setq tetris-rot (% (+ 1 tetris-rot) 4)))
        (tetris-draw-shape))))

(defun tetris-end-game ()
  "Terminates the current game"
  (interactive)
  (gamegrid-kill-timer)
  (use-local-map tetris-null-map)
  (gamegrid-add-score tetris-score-file tetris-score))

(defun tetris-start-game ()
  "Starts a new game of Tetris"
  (interactive)
  (tetris-reset-game)
  (use-local-map tetris-mode-map)
  (let ((period (or (tetris-get-tick-period)
		    tetris-default-tick-period)))
    (gamegrid-start-timer period 'tetris-update-game)))

(defun tetris-pause-game ()
  "Pauses (or resumes) the current game"
  (interactive)
  (setq tetris-paused (not tetris-paused))
  (message (and tetris-paused "Game paused (press p to resume)")))

(defun tetris-active-p ()
  (eq (current-local-map) tetris-mode-map))

(put 'tetris-mode 'mode-class 'special)

(defun tetris-mode ()
  "A mode for playing Tetris.

tetris-mode keybindings:
   \\{tetris-mode-map}
"
  (kill-all-local-variables)

  (make-local-hook 'kill-buffer-hook)
  (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)

  (use-local-map tetris-null-map)

  (setq major-mode 'tetris-mode)
  (setq mode-name "Tetris")

  (setq mode-popup-menu
	'("Tetris Commands"
	  ["Start new game"	tetris-start-game]
	  ["End game"		tetris-end-game
	   (tetris-active-p)]
	  ["Pause"		tetris-pause-game
	   (and (tetris-active-p) (not tetris-paused))]
	  ["Resume"		tetris-pause-game
	   (and (tetris-active-p) tetris-paused)]))

  (setq gamegrid-use-glyphs tetris-use-glyphs)
  (setq gamegrid-use-color tetris-use-color)

  ;; Precompute the bounding boxes for known each shape/rotation pair
  (setq tetris-shapes-bounding-boxes (tetris-bounding-box-compute-all))

  (gamegrid-init (tetris-display-options))

  (run-hooks 'tetris-mode-hook))

;;;###autoload
(defun tetris ()
  "Tetris

Shapes drop from the top of the screen, and the user has to move and
rotate the shape to fit in with those at the bottom of the screen so
as to form complete rows.

tetris-mode keybindings:
   \\<tetris-mode-map>
\\[tetris-start-game]	Starts a new game of Tetris
\\[tetris-end-game]	Terminates the current game
\\[tetris-pause-game]	Pauses (or resumes) the current game
\\[tetris-move-left]	Moves the shape one square to the left
\\[tetris-move-right]	Moves the shape one square to the right
\\[tetris-rotate-prev]	Rotates the shape clockwise
\\[tetris-rotate-next]	Rotates the shape anticlockwise
\\[tetris-move-bottom]	Drops the shape to the bottom of the playing area

"
  (interactive)

  (switch-to-buffer tetris-buffer-name)
  (gamegrid-kill-timer)
  (tetris-mode)
  (tetris-start-game))

(provide 'tetris)

;;; tetris.el ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.