├── pstris.gif ├── midi.py ├── LICENSE ├── README.md └── tetris.ps /pstris.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nst/PSTris/main/pstris.gif -------------------------------------------------------------------------------- /midi.py: -------------------------------------------------------------------------------- 1 | # mkfifo midi.bin; python3 midi.py < midi.bin 2 | 3 | import sys 4 | import mido 5 | 6 | port_name = 'IAC Driver Bus 1' 7 | 8 | try: 9 | with mido.open_output(port_name) as port: 10 | print(f"Bridge active. Sending to '{port_name}'...", file=sys.stderr) 11 | while True: 12 | # Wait for exactly 3 bytes (Status, Note, Velocity) 13 | data = sys.stdin.buffer.read(3) 14 | if len(data) < 3: break 15 | 16 | try: port.send(mido.Message.from_bytes(data)) 17 | except ValueError as e: print(e) 18 | except (OSError, KeyboardInterrupt) as e: 19 | print(e) 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Nicolas Seriot 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PSTris 2 | A Tetris implementation in PostScript 3 | 4 | 5 | 6 | #### Motivation 7 | 8 | - implement a realtime game in PostScript 9 | - play it in Ghostscript on macOS 10 | 11 | See also: 12 | 13 | - [PSChess](https://github.com/nst/PSChess) 14 | - [PSSokoban](https://github.com/nst/PSSokoban) 15 | - [Programming in PostScript](https://seriot.ch/projects/programming_in_postscript.html) 16 | 17 | ### Features 18 | 19 | - 600 lines, 10 KB of PostScript 20 | - 69 unique operators 21 | - realtime input 22 | - direct drop 23 | - levels with increasing speed 24 | - 7-tetriminos random bags 25 | - high scores 26 | - Nintendo scoring scheme 27 | - sound through external MIDI synthetizer 28 | 29 | ### Play Tetris in Ghostscript 30 | 31 | Terminal window 1: 32 | 33 | $ gs -sNOSAFER tetris.ps 34 | 35 | Terminal window 2: 36 | 37 | $ stty raw -echo; cat >> t.txt 38 | 39 | Play with arrows and space bar in window 2. 40 | 41 | ### Sound (Optional) 42 | 43 | The program write MIDI commands in `midi.bin`. 44 | 45 | You can read `midi.bin` from a helper script to send the commands to a synthetizer. 46 | 47 | On macOS, you can use GarageBand and the brigde `midi.py`: 48 | 49 | 1. in "Audio MIDI Setup" application, open "IAC Driver" and check "Device in online" 50 | 2. in "GarageBand" application, create a new empty project with a MIDI track. "8-Bit Grit" instrument works fine. 51 | 3. run `tail -f midi.bin | python3 midi.py` 52 | 53 | ### Notes and Limitations 54 | 55 | Some implementation details inspired from [MeatFighter](https://meatfighter.com/nintendotetrisai/). 56 | -------------------------------------------------------------------------------- /tetris.ps: -------------------------------------------------------------------------------- 1 | %!PS 2 | 3 | %%Title: tetris.ps 4 | %%Creator: Nicolas Seriot 5 | %%Creation Date: February 2025 6 | 7 | % https://github.com/nst/PSTris 8 | 9 | % gs -sNOSAFER tetris.ps 10 | % stty raw -echo; cat >> t.txt 11 | % tail -f midi.bin | python3 midi.py 12 | 13 | /COLS 10 def 14 | /ROWS 22 def 15 | /S 20 def 16 | /EMPTY 9 def 17 | 18 | /= { =only } bind def 19 | 20 | /Sleep { 1000000 mul {} repeat } bind def 21 | 22 | /INPUT_FILE (t.txt) def 23 | /SCORE_FILE (highscore.txt) def 24 | /MIDI_FILE (midi.bin) def 25 | 26 | /_s_CreateOrTruncateFile { 27 | /s exch def 28 | { 29 | s (w) file closefile 30 | } stopped { 31 | (-- error creating file ) = s = flush 32 | } if 33 | } bind def 34 | 35 | /ReadInput_s_ { 36 | { 37 | INPUT_FILE (r) file 8 string readstring { } { } ifelse 38 | dup length 0 ne { INPUT_FILE _s_CreateOrTruncateFile } if 39 | } stopped { 40 | INPUT_FILE _s_CreateOrTruncateFile 41 | } if 42 | } bind def 43 | 44 | /DrawCell { 45 | 0 0 S S 46 | 4 copy 47 | rectfill 48 | 0 setgray 49 | rectstroke 50 | } bind def 51 | 52 | /_c_r_DrawNextTetrimino { 53 | /r exch def 54 | /c exch def 55 | 56 | /t Tetriminos c get r get def 57 | 58 | gsave 59 | 60 | 240 300 translate 61 | 62 | % clear zone 63 | 1 setgray 64 | 0 0 5 S mul 5 S mul 65 | 4 copy 66 | rectfill 67 | 0 setgray 68 | rectstroke 69 | 70 | Palette c get exec setrgbcolor 71 | 72 | 0 1 4 { /x exch def 73 | 0 1 4 { /y exch def 74 | t y 5 mul x add get 1 eq { 75 | gsave 76 | x S mul 5 y sub 1 sub S mul translate DrawCell 77 | grestore 78 | } if 79 | } for 80 | } for 81 | 82 | grestore 83 | } bind def 84 | 85 | /Palette [ 86 | { 0.5 0 1 } % T - purple 87 | { 0 0 1 } % J - blue 88 | { 1 0 0 } % Z - red 89 | { 1 1 0 } % O - yellow 90 | { 0.5 1 0 } % S - green 91 | { 1 0.5 0 } % L - orange 92 | { 0.5 1 1 } % I - cyan 93 | ] bind def 94 | 95 | /DrawLevelAndScore { 96 | gsave 97 | 98 | 240 150 translate 99 | 100 | % clear zone 101 | 1 setgray 102 | 0 -10 180 150 103 | rectfill 104 | 105 | /Helvetica findfont 18 scalefont setfont 106 | 107 | 0 setgray 108 | 109 | 0 120 moveto (Level:) show 60 120 moveto level ( ) cvs show 110 | 0 90 moveto (Score:) show 60 90 moveto score ( ) cvs show 111 | 0 60 moveto (High:) show 60 60 moveto highscore ( ) cvs show 112 | 113 | gameIsOver { 114 | 0 30 moveto (GAME OVER) show 115 | 0 0 moveto ([N]ew game) show 116 | } if 117 | 118 | grestore 119 | } bind def 120 | 121 | /DrawWorld { 122 | 123 | gsave 124 | 125 | % clear zone 126 | 0.8 setgray 127 | 0 0 COLS S mul ROWS 2 sub S mul rectfill 128 | 129 | 0 1 WORLD length 1 sub { /i exch def 130 | WORLD i get EMPTY ne { 131 | 132 | Palette WORLD i get 10 mod get exec setrgbcolor 133 | 134 | gsave 135 | i COLS mod S mul 136 | ROWS i COLS idiv sub 1 sub S mul 137 | translate 138 | DrawCell 139 | grestore 140 | } if 141 | } for 142 | 143 | 0 setgray 144 | 0 0 COLS S mul ROWS 2 sub S mul rectstroke 145 | 146 | grestore 147 | 148 | } bind def 149 | 150 | /_t_x_y_RemoveActiveT { 151 | 152 | /y exch def 153 | /x exch def 154 | /t exch def 155 | 156 | 0 1 t length 1 sub { /ti exch def 157 | 158 | /wi ti x y _ti_x_y_TIndexToWIndex_wi_ def 159 | 160 | wi null ne { 161 | WORLD wi get 10 ge { 162 | WORLD wi EMPTY put 163 | } if 164 | } if 165 | } for 166 | 167 | } bind def 168 | 169 | /_t_x_y_AddActiveT { 170 | 171 | /y exch def 172 | /x exch def 173 | /t exch def 174 | 175 | 0 1 t length 1 sub { /ti exch def 176 | t ti get 1 eq { 177 | /wi ti x y _ti_x_y_TIndexToWIndex_wi_ def 178 | WORLD wi 10 c add put 179 | } if 180 | } for 181 | 182 | } bind def 183 | 184 | % we may prefer [ ... ] aload length packedarray def for efficiency 185 | /Tu [0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0] bind def 186 | /Tr [0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0] bind def 187 | /Td [0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0] bind def 188 | /Tl [0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0] bind def 189 | /Jl [0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0] bind def 190 | /Ju [0 0 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0] bind def 191 | /Jr [0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0] bind def 192 | /Jd [0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0] bind def 193 | /Zh [0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0] bind def 194 | /Zv [0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0] bind def 195 | /O [0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0] bind def 196 | /Sh [0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0] bind def 197 | /Sv [0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0] bind def 198 | /Lr [0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0] bind def 199 | /Ld [0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 0 0 0] bind def 200 | /Ll [0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0] bind def 201 | /Lu [0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0] bind def 202 | /Iv [0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0] bind def 203 | /Ih [0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0] bind def 204 | 205 | /Tetriminos [ 206 | [Td Tl Tu Tr] 207 | [Jd Jl Ju Jr] 208 | [Zh Zv] 209 | [O] 210 | [Sh Sv] 211 | [Ld Ll Lu Lr] 212 | [Ih Iv] 213 | ] bind def 214 | 215 | /_t_x_y_CanMove_b_ { 216 | 217 | 10 dict begin 218 | 219 | /y exch def 220 | /x exch def 221 | /t exch def 222 | 223 | /canMove true def 224 | 225 | 0 1 t length 1 sub { /ti exch def 226 | t ti get 1 eq { 227 | % world index of non-zero Tetrimino cell 228 | /wi ti x y _ti_x_y_TIndexToWIndex_wi_ def 229 | 230 | wi null eq { 231 | /canMove false def exit 232 | } { 233 | 234 | % walls detection 235 | wi COLS mod x sub abs 2 gt { 236 | /canMove false def exit 237 | } if 238 | 239 | % collision detection 240 | /w WORLD wi get def 241 | w EMPTY ne { 242 | w 10 lt { 243 | /canMove false def exit 244 | } if 245 | } if 246 | 247 | } ifelse 248 | } if 249 | } for 250 | 251 | canMove 252 | 253 | end 254 | } bind def 255 | 256 | % convert Tetrimino index (0-24) into world index (0-(COLS*ROWS-1)) 257 | % leaves null if out of bounds 258 | /_ti_x_y_TIndexToWIndex_wi_ { 259 | /y exch def 260 | /x exch def 261 | /ti exch def 262 | 263 | /wx ti 5 mod 2 sub x add def 264 | /wy ti 5 idiv 2 sub y add def 265 | 266 | /wi wy COLS mul wx add def 267 | 268 | wi 0 lt wi WORLD length ge or { 269 | null 270 | } { 271 | wi 272 | } ifelse 273 | } bind def 274 | 275 | %/MOVE_IN_PLACE 0 def 276 | /MOVE_DOWN 1 def 277 | /MOVE_LEFT 2 def 278 | /MOVE_RIGHT 3 def 279 | 280 | /DrawWorldClippingTArea { 281 | 282 | gsave 283 | 284 | 0 0 COLS S mul % 1 add 285 | ROWS 2 sub S mul % 1 add 286 | rectclip 287 | 288 | Tx 3 sub S mul 289 | ROWS Ty sub 3 sub S mul 290 | 7 S mul 2 add 291 | 7 S mul 2 add 292 | rectclip 293 | 294 | DrawWorld 295 | 296 | grestore 297 | } bind def 298 | 299 | /_c_r_dir_Move_b_ { 300 | 301 | /dir exch def 302 | /r exch def 303 | /c exch def 304 | 305 | /toY Ty def 306 | /toX Tx def 307 | 308 | dir MOVE_DOWN eq { /toY Ty 1 add def } if 309 | dir MOVE_LEFT eq { /toX Tx 1 sub def } if 310 | dir MOVE_RIGHT eq { /toX Tx 1 add def } if 311 | 312 | /t Tetriminos c get r get def 313 | 314 | /canMove t toX toY _t_x_y_CanMove_b_ def 315 | 316 | canMove { 317 | t Tx Ty _t_x_y_RemoveActiveT 318 | t toX toY _t_x_y_AddActiveT 319 | 320 | /Tx toX def 321 | /Ty toY def 322 | } if 323 | 324 | canMove 325 | 326 | } bind def 327 | 328 | /_c_r_Spawn_b_ { 329 | 330 | /r exch def 331 | /c exch def 332 | 333 | /Tx 5 def 334 | /Ty 2 def 335 | 336 | c r null _c_r_dir_Move_b_ 337 | } bind def 338 | 339 | /MoveDown_b_ { 340 | t1c t1r MOVE_DOWN _c_r_dir_Move_b_ 341 | } bind def 342 | 343 | /_dir_Move { 344 | t1c t1r 3 -1 roll _c_r_dir_Move_b_ pop 345 | } bind def 346 | 347 | /Rotate { 348 | /r2 t1r 1 add Tetriminos t1c get length mod def 349 | t1c r2 null _c_r_dir_Move_b_ { 350 | /t1r r2 def 351 | <90 5A 7F> _bytes_SendMIDI 1 Sleep 352 | <80 5A 00> _bytes_SendMIDI 353 | } if 354 | } bind def 355 | 356 | /DisableActiveT { 357 | 0 1 WORLD length 1 sub { 358 | /i exch def 359 | /x WORLD i get def 360 | x 10 ge { WORLD i x 10 mod put } if 361 | } for 362 | } bind def 363 | 364 | /DirectDrop { 365 | <90 3C 7F> _bytes_SendMIDI 2 Sleep 366 | <80 3C 00> _bytes_SendMIDI 367 | 368 | { MoveDown_b_ not { exit } if } loop 369 | } bind def 370 | 371 | /_r_ClearRow { 372 | 373 | -1 2 { 374 | /row exch def 375 | /rowFromAbove WORLD row 1 sub COLS mul COLS getinterval def 376 | WORLD row COLS mul rowFromAbove putinterval 377 | WORLD row 1 sub COLS mul [COLS {EMPTY} repeat] putinterval 378 | } for 379 | 380 | % fill row 2 with EMPTY 381 | WORLD 2 COLS mul [COLS {EMPTY} repeat] putinterval 382 | } bind def 383 | 384 | /_r_WhiteBlink { 385 | /r exch def 386 | 387 | <90 78 7F> _bytes_SendMIDI 8 Sleep 388 | <80 78 00> _bytes_SendMIDI 389 | 390 | gsave 391 | 1 setgray 392 | 393 | 0 1 COLS 1 sub { 394 | /c exch def 395 | 396 | gsave 397 | c S mul ROWS r sub 1 sub S mul translate 398 | DrawCell 399 | grestore 400 | 401 | } for 402 | 403 | grestore 404 | 405 | } bind def 406 | 407 | /CheckFullLine_b_rowsFull_ { 408 | 409 | /b false def 410 | 411 | /rowsFull [ROWS {false} repeat] def 412 | 413 | ROWS 1 sub -1 2 { 414 | 415 | /row exch def 416 | 417 | /lineIsFull true def 418 | 419 | 0 1 COLS 1 sub { /col exch def 420 | WORLD row COLS mul col add get EMPTY eq { 421 | /lineIsFull false def exit 422 | } if 423 | } for 424 | 425 | rowsFull row lineIsFull put 426 | /b b lineIsFull or def 427 | } for 428 | 429 | b rowsFull 430 | } bind def 431 | 432 | %0 srand 433 | 434 | /BAG_SIZE 7 def 435 | /randomBag BAG_SIZE array def 436 | /NewTetrimino_i_ { 437 | 438 | { 439 | /i rand BAG_SIZE mod def 440 | randomBag i get null eq { randomBag i true put exit } if 441 | } loop 442 | 443 | i 444 | 445 | /isFull true def 446 | randomBag { null eq { /isFull false def exit } if } forall 447 | isFull { /randomBag BAG_SIZE array def } if 448 | } bind def 449 | 450 | /StartNewGame { 451 | /MIDI_FILE_WRITE MIDI_FILE (w) file def 452 | 453 | /WORLD [COLS ROWS mul {EMPTY} repeat] def 454 | 455 | /gameIsOver false def 456 | /level 1 def 457 | /delay 300 def 458 | 459 | /linesCleared 0 def 460 | /score 0 def 461 | 462 | /t1c NewTetrimino_i_ def % code 463 | /t1r 0 def % rotation 464 | 465 | /t2c NewTetrimino_i_ def % code 466 | /t2r 0 def % rotation 467 | 468 | /lastDrop realtime def 469 | 470 | t1c t1r _c_r_Spawn_b_ pop 471 | 472 | DrawWorld 473 | 474 | t2c t2r _c_r_DrawNextTetrimino 475 | 476 | DrawLevelAndScore 477 | } def 478 | 479 | /SpawnNextT { 480 | DisableActiveT 481 | 482 | % next T gets current T 483 | /t1c t2c def 484 | /t1r t2r def 485 | 486 | t1c t1r _c_r_Spawn_b_ { 487 | DrawWorldClippingTArea 488 | } { 489 | /gameIsOver true def 490 | DrawLevelAndScore 491 | } ifelse 492 | 493 | /t2c NewTetrimino_i_ def % code 494 | /t2r 0 def % rotation 495 | 496 | t2c t2r _c_r_DrawNextTetrimino 497 | } bind def 498 | 499 | /ReadHighScore_i_ { 500 | SCORE_FILE status { 501 | pop pop pop pop 502 | SCORE_FILE (r) file ( ) readstring pop cvi 503 | } { 504 | 0 505 | } ifelse 506 | } bind def 507 | 508 | /_i_WriteHighScore { 509 | { 510 | ( ) cvs SCORE_FILE (w) file exch writestring 511 | } stopped { 512 | (-- error writing file ) = SCORE_FILE = flush 513 | } if 514 | } bind def 515 | 516 | /_bytes_SendMIDI { 517 | MIDI_FILE_WRITE exch writestring 518 | MIDI_FILE_WRITE flushfile 519 | } def 520 | 521 | 160 300 translate 522 | 0.5 setlinewidth 523 | 524 | /delay 300 def 525 | /highscore ReadHighScore_i_ def 526 | 527 | StartNewGame 528 | flushpage 529 | 200 Sleep 530 | 531 | { 532 | ReadInput_s_ 533 | 534 | /skipDelay false def 535 | 536 | { 537 | /key exch def 538 | 539 | %key 0 ne { (-------- KEY:) =only key == flush } if 540 | 541 | gameIsOver { 542 | key 110 eq { StartNewGame DrawWorld } if 543 | } { 544 | key 67 eq { MOVE_RIGHT _dir_Move DrawWorldClippingTArea } if 545 | key 68 eq { MOVE_LEFT _dir_Move DrawWorldClippingTArea } if 546 | key 66 eq { MoveDown_b_ pop DrawWorldClippingTArea } if 547 | key 65 eq { Rotate DrawWorldClippingTArea } if 548 | 549 | key 32 eq { DirectDrop DrawWorld /skipDelay true def } if 550 | } ifelse 551 | 552 | flushpage 553 | } forall 554 | 555 | gameIsOver not 556 | lastDrop delay add realtime lt and 557 | skipDelay or { 558 | 559 | CheckFullLine_b_rowsFull_ 560 | /rowsFullArray exch def 561 | /hasFullRows exch def 562 | 563 | hasFullRows { 564 | 565 | DisableActiveT 566 | 567 | 0 1 ROWS 1 sub { 568 | /r exch def 569 | rowsFullArray r get { r _r_WhiteBlink } if 570 | } for 571 | 572 | flushpage 573 | 30 Sleep 574 | 575 | /n 0 def 576 | 0 1 ROWS 1 sub { /r exch def 577 | rowsFullArray r get { 578 | /n n 1 add def 579 | r _r_ClearRow 580 | } if 581 | } for 582 | 583 | /linesCleared linesCleared n add def 584 | /level linesCleared 10 idiv 1 add def 585 | /delay 300 level 1 sub 30 mul sub def 586 | /score [40 100 300 1200] n 1 sub get level mul score add def 587 | 588 | score highscore gt { 589 | /highscore score def 590 | score _i_WriteHighScore 591 | } if 592 | 593 | DrawWorld 594 | DrawLevelAndScore 595 | } if 596 | 597 | MoveDown_b_ { 598 | DrawWorldClippingTArea 599 | } { 600 | SpawnNextT 601 | } ifelse 602 | 603 | flushpage 604 | /lastDrop realtime def 605 | } if 606 | 607 | count 0 ne { 608 | (== Stack leak ==) == 609 | count == pstack stop 610 | } if 611 | 612 | } loop 613 | --------------------------------------------------------------------------------