tetris.gm (TETRIS in GAME language)
tetris.py(supertext 版)を GAME 言語に移植し、miep(C 版)で動かします。
ファイル構成
| ファイル | 説明 |
|---|---|
tetris.gm |
ゲーム本体(GAME 言語) |
miep.c |
GAME 言語インタプリタ。キー入力の & 項と待ち時間の *WT 命令を追加したもの |
起動方法
make
sudo ./miep tetris.gm
80桁×25行以上の端末で起動してください。
キーは /dev/input/eventN から直接読みます。このデバイスは通常 root しか読めないので、sudo で起動します。sudo を使わずに遊ぶ場合は、FreeBSD なら /etc/devfs.rules で読み取りを許可します。
[localrules=10]
add path 'input/event*' mode 0660 group operator
/etc/rc.conf に devfs_system_ruleset="localrules" を書き、sudo service devfs restart すると、operator グループのユーザーが読めるようになります。
キーボードは自動で探します。特定のデバイスを使う場合は環境変数 MIEP_KBD で指定します。
sudo MIEP_KBD=/dev/input/event7 ./miep tetris.gm
ゲームの遊び方
起動すると「HIT ENTER」と表示されるので、ENTER を押すと始まります。
落ちてくるテトリスを動かしたり回したりして、横一列をそろえると消えます。積み上がって新しいテトリスが置けなくなったらゲームオーバーです。ゲームオーバーになったら q で終了します。
画面の左上に得点(SCORE)、出したテトリスの数(TETRIS)、消した行数(LINES)、その下に次のテトリス(NEXT)を表示します。
キー操作
| 操作 | キー |
|---|---|
| 左に移動 |
4 / ←
|
| 右に移動 |
6 / →
|
| 回転 |
SPACE / ↑
|
| 速く落とす |
2 / ↓
|
| 終了 | q |
数字キーはテンキーでも、上段の数字キーでも操作できます。回転はキーを押し直すたびに1回だけ回ります。
得点
- 1コマごとに 1 点
- テトリスが固定されるたびに、消した行数 l に応じて 2^l×100 点(0行で 100、1行で 200、2行で 400、3行で 800、4行で 1600)
速さ
出したテトリスの数が増えると速くなります。テトリスは 2 コマに 1 マス落ちます。
| 出したテトリス数 | 1コマの長さ |
|---|---|
| 0〜39 | 70 ミリ秒 |
| 40〜70 | 58 ミリ秒 |
| 71〜100 | 46 ミリ秒 |
| 101〜130 | 35 ミリ秒 |
| 131〜160 | 23 ミリ秒 |
| 161〜 | 12 ミリ秒 |
2 を押している間は 1コマ 20 ミリ秒になります(1マス約 40 ミリ秒で落ちます)。
回転と固定
- 回転して重なるときは、位置を少しずらして(キック)回せる場所を探します。I テトリスはさらに 2〜3 マス離れた位置も探します。
- 着地してから約 0.5 秒たつと固定されます。着地中に移動や回転をすると、15 回まで固定を先に延ばせます。
しくみ
-
&nはキーコード n のキーが押されていれば 1 を返します。キーコードは/usr/include/dev/evdev/input-event-codes.hの値です(q=16、SPACE=57、テンキーの4=75 など)。前回調べてから一瞬だけ押されたキーも 1 になるので、短く叩いたキーも取りこぼしません。 - 1コマごとに
*WT Wで W ミリ秒待ちます。 - ゲーム中は押したキーが端末に表示されません。終了時には端末の設定を戻し、ゲーム中に打った文字も捨てます。
- 画面は 40×25 マスで、1マスを2桁で描きます。フィールドは横 10 マス × 縦 24 マスです。ブロックは背景色で塗った2桁の空白で描き、枠は灰色です。L 字のテトリスは 256 色のオレンジ(
ESC[48;5;208m)で描くので、256 色に対応した端末で遊んでください。 - 画面の内容(文字と色)はメモリ上の画面バッファに持ち、当たり判定やライン消去に使います。
- テトリスの形は、向きごとに4つのブロックの (dx, dy) をバイト配列に持っています。
- miep には時計がないので、着地してからの時間は 1コマ = W+3 ミリ秒と見積もって数えます(3 ミリ秒はインタプリタと画面出力にかかる時間の見積もり)。
調整
| 行 | 内容 | 既定値 |
|---|---|---|
| 2510〜2560 | 出したテトリス数ごとの 1コマの長さ W(ミリ秒) | 70〜12 |
| 2250 |
2 を押しているときの 1コマの長さ |
W=20 |
| 2130 | 固定までの時間(ミリ秒) | 500 |
| 2120 | 固定を延ばせる回数 | 15 |
| 2270 | 1コマあたりの処理時間の見積もり(ミリ秒) | 3 |
原作との違い
-
&と*WTは C 版のmiepとbeam.pyだけの機能です。miep.pyでは動きません。 - 矢印キーでも操作できるようにしました。
- 1コマの長さは、supertext の
sleep()が実際に待つ時間を測ってミリ秒に置き換えました。 - 得点は 16 ビットの変数ではあふれるので、H×10000+L の2つに分けて持っています。
- 原作で白だった L 字のテトリスはオレンジにしました。得点と行数の数字は白ではなく端末の標準の文字色で表示します。白い背景の端末でも見えるようにするためです。
- 終了すると画面を消して端末に戻ります。
English
tetris.gm (TETRIS in GAME language)
This is a port of tetris.py (the supertext version) to the GAME language. It runs on miep (the C version).
Files
| File | Description |
|---|---|
tetris.gm |
The game itself (GAME language) |
miep.c |
GAME language interpreter, extended with the & key-input term and the *WT wait command |
How to run
make
sudo ./miep tetris.gm
Use a terminal of at least 80 columns × 25 lines.
Keys are read directly from /dev/input/eventN. These devices are normally readable only by root, so start the game with sudo. To play without sudo on FreeBSD, allow read access in /etc/devfs.rules:
[localrules=10]
add path 'input/event*' mode 0660 group operator
Then add devfs_system_ruleset="localrules" to /etc/rc.conf and run sudo service devfs restart; users in the operator group can then read the devices.
The keyboard is detected automatically. To use a specific device, set the MIEP_KBD environment variable:
sudo MIEP_KBD=/dev/input/event7 ./miep tetris.gm
How to play
When the game starts, "HIT ENTER" is shown. Press ENTER to begin.
Move and rotate the falling pieces to complete horizontal lines, which then disappear. When a new piece can no longer be placed, the game is over. Press q on the game-over screen to quit.
The top left of the screen shows the score (SCORE), the number of pieces dealt (TETRIS), and the number of lines cleared (LINES). The next piece (NEXT) is shown below them.
Controls
| Action | Key |
|---|---|
| Move left |
4 / ←
|
| Move right |
6 / →
|
| Rotate |
SPACE / ↑
|
| Drop faster |
2 / ↓
|
| Quit | q |
The number keys work on both the numeric keypad and the top row. Rotation turns the piece once per key press.
Scoring
- 1 point per frame
- Each time a piece locks, 2^l×100 points for l cleared lines (100 for 0 lines, 200 for 1, 400 for 2, 800 for 3, 1600 for 4)
Speed
The game speeds up as more pieces are dealt. A piece falls one cell every 2 frames.
| Pieces dealt | Frame length |
|---|---|
| 0–39 | 70 ms |
| 40–70 | 58 ms |
| 71–100 | 46 ms |
| 101–130 | 35 ms |
| 131–160 | 23 ms |
| 161– | 12 ms |
While 2 is held, a frame is 20 ms (a piece falls one cell about every 40 ms).
Rotation and locking
- If a rotated piece would overlap something, the game tries slightly shifted positions (kicks) to find room. The I piece also tries positions 2–3 cells away.
- A piece locks about 0.5 seconds after it lands. Moving or rotating it while landed postpones the lock, up to 15 times.
How it works
-
&nreturns 1 if the key with key code n is pressed. Key codes are the values in/usr/include/dev/evdev/input-event-codes.h(q=16,SPACE=57, keypad4=75, and so on). A key that was pressed briefly since the last check also returns 1, so short taps are not missed. - Each frame waits W milliseconds with
*WT W. - Keys pressed during the game are not echoed to the terminal. On exit, the terminal settings are restored and the characters typed during the game are discarded.
- The screen is 40×25 cells, each drawn 2 columns wide. The playfield is 10 cells wide and 24 cells tall. Blocks are drawn as two spaces painted with a background color; the frame is gray. The L piece is drawn in 256-color orange (
ESC[48;5;208m), so use a terminal that supports 256 colors. - The screen contents (characters and colors) are kept in a screen buffer in memory, which is used for collision checks and line clearing.
- Each piece shape is stored as the (dx, dy) of its four blocks per rotation, in a byte array.
- miep has no clock, so the time since landing is counted by assuming one frame takes W+3 ms (3 ms is an estimate of the interpreter and screen output time).
Tuning
| Line | Description | Default |
|---|---|---|
| 2510–2560 | Frame length W (ms) for each number of pieces dealt | 70–12 |
| 2250 | Frame length while 2 is held |
W=20 |
| 2130 | Time until a landed piece locks (ms) | 500 |
| 2120 | How many times the lock can be postponed | 15 |
| 2270 | Estimated processing time per frame (ms) | 3 |
Differences from the original
-
&and*WTare available only in the C version ofmiepand inbeam.py. They do not work inmiep.py. - Arrow keys were added as alternative controls.
- Frame lengths were converted to milliseconds by measuring how long supertext's
sleep()actually waits. - The score would overflow a 16-bit variable, so it is kept in two parts, H×10000+L.
- The L piece, white in the original, is orange. The score and line numbers use the terminal's default text color instead of white. Both changes keep them visible on terminals with a white background.
- On exit, the screen is cleared and you return to the terminal.
Code
#!/usr/bin/env miep
10#==== TETRIS (tetris.py の GAME 言語版) ====
11# キーは /dev/input/eventN から &キーコード で読む (押されていれば 1)
12# 4/6 (テンキー, ←→ も可) 左右移動 SPACE (↑ も可) 回転 2 (テンキー, ↓ も可) 落下 q 終了
13# 画面は 40x25 マス (1マス=2桁)。フィールドは x=16〜25, y=0〜23、枠は x=15,26 と y=24
14# S 画面バッファ S:y*40+x) 文字 ($90 枠, $97 ブロック, 0 空き) S:y*40+x+1000) 色 (ESC[n m の n)
15# P 形データ (バイト) P:(t*4+m)*8+i*2) = i 番目のブロックの dx, +1 で dy
16# Q (0-6) 回転数 (7-13) 色 (208 は 256 色のオレンジ) (20-57) キック表 dx,dy (60-64) 消した行数ごとの得点
17# X,Y,M,T 落下中の x,y,向き,種類 N 次の種類 (-1 はまだ無し) G 出したテトリス数
18# H,L 得点 (H*10000+L) O 消した行数 U コマ数 V 回転キーが押されたまま W 1コマの長さ(ミリ秒)
19# E 着地してからの時間(ミリ秒, 1コマ=W+3 と見積もる, -1 は着地していない) R ロック延長の回数 F このコマで動かした
20# canputp (5000) は A,B,C を壊さず D,I,J,Z を使う。put/erase (5100/5120) は A,B,C,D,I,J を使う
100 S=$4000 P=$5000 Q=$5200
110 !=1000
120 $=27 "]0;TETRIS" $=7 $=27 "[?25l" $=27 "[0m" $=27 "[2J"
130 !=1500
140 N=-1 G=0 H=0 L=0 O=0 U=0 V=0 !=4000
150 $=27 "[13;35H" $=27 "[92mHIT ENTER" $=27 "[0m"
160 @
170 *WT 20
180 ;=&16 #=9900
190 @=(&28+&96)
200 $=27 "[13;35H" " "
210 E=-1 R=0 !=2500
220 #=2000
1000#==== 形・色・キック表 ====
1010 I=0,1999 S:I)=0 @=I+1
1100 P:0)=0 P:1)=0 P:2)=0 P:3)=1 P:4)=0 P:5)=2 P:6)=0 P:7)=3
1110 P:8)=0 P:9)=0 P:10)=1 P:11)=0 P:12)=2 P:13)=0 P:14)=3 P:15)=0
1120 P:32)=0 P:33)=0 P:34)=0 P:35)=1 P:36)=1 P:37)=1 P:38)=1 P:39)=2
1130 P:40)=1 P:41)=0 P:42)=2 P:43)=0 P:44)=0 P:45)=1 P:46)=1 P:47)=1
1140 P:64)=1 P:65)=0 P:66)=0 P:67)=1 P:68)=1 P:69)=1 P:70)=0 P:71)=2
1150 P:72)=0 P:73)=0 P:74)=1 P:75)=0 P:76)=1 P:77)=1 P:78)=2 P:79)=1
1160 P:96)=1 P:97)=0 P:98)=1 P:99)=1 P:100)=2 P:101)=1 P:102)=1 P:103)=2
1170 P:104)=1 P:105)=0 P:106)=0 P:107)=1 P:108)=1 P:109)=1 P:110)=2 P:111)=1
1180 P:112)=1 P:113)=0 P:114)=0 P:115)=1 P:116)=1 P:117)=1 P:118)=1 P:119)=2
1190 P:120)=0 P:121)=0 P:122)=1 P:123)=0 P:124)=2 P:125)=0 P:126)=1 P:127)=1
1200 P:128)=0 P:129)=0 P:130)=1 P:131)=0 P:132)=0 P:133)=1 P:134)=1 P:135)=1
1210 P:160)=1 P:161)=0 P:162)=2 P:163)=0 P:164)=2 P:165)=1 P:166)=2 P:167)=2
1220 P:168)=0 P:169)=0 P:170)=1 P:171)=0 P:172)=2 P:173)=0 P:174)=0 P:175)=1
1230 P:176)=0 P:177)=0 P:178)=0 P:179)=1 P:180)=0 P:181)=2 P:182)=1 P:183)=2
1240 P:184)=2 P:185)=0 P:186)=0 P:187)=1 P:188)=1 P:189)=1 P:190)=2 P:191)=1
1250 P:192)=0 P:193)=0 P:194)=1 P:195)=0 P:196)=0 P:197)=1 P:198)=0 P:199)=2
1260 P:200)=0 P:201)=0 P:202)=0 P:203)=1 P:204)=1 P:205)=1 P:206)=2 P:207)=1
1270 P:208)=2 P:209)=0 P:210)=2 P:211)=1 P:212)=1 P:213)=2 P:214)=2 P:215)=2
1280 P:216)=0 P:217)=0 P:218)=1 P:219)=0 P:220)=2 P:221)=0 P:222)=2 P:223)=1
1290 Q(0)=2 Q(1)=2 Q(2)=2 Q(3)=4 Q(4)=1 Q(5)=4 Q(6)=4
1300 Q(20)=0 Q(21)=0 Q(22)=-1 Q(23)=0 Q(24)=1 Q(25)=0 Q(26)=0 Q(27)=-1 Q(28)=-1 Q(29)=-1 Q(30)=1 Q(31)=-1 Q(32)=-2 Q(33)=0
1310 Q(34)=2 Q(35)=0 Q(36)=-2 Q(37)=-1 Q(38)=2 Q(39)=-1 Q(40)=0 Q(41)=1 Q(42)=-1 Q(43)=1 Q(44)=1 Q(45)=1 Q(46)=0 Q(47)=-2
1320 Q(48)=0 Q(49)=-3 Q(50)=-3 Q(51)=0 Q(52)=3 Q(53)=0 Q(54)=0 Q(55)=2 Q(56)=0 Q(57)=3
1330 Q(7)=101 Q(8)=102 Q(9)=105 Q(10)=106 Q(11)=103 Q(12)=104 Q(13)=208
1340 Q(60)=100 Q(61)=200 Q(62)=400 Q(63)=800 Q(64)=1600 ]
1500#==== draw_frame ====
1510 A=$90 B=47
1520 D=0,24 C=15 !=8100 C=26 !=8100 @=D+1
1530 D=24 C=16,25 !=8100 @=C+1 ]
2000#==== メインループ ====
2010 U=U+1 F=0 !=5120
2020 ;=&16 #=9900
2030 A=&57+&103 ;=A=0 V=0
2040 ;=A ;=V=0 !=3000 V=1
2050 D=0 ;=&5+&75+&105 D=-1
2060 ;=&7+&77+&106 D=1
2070 ;=D A=X+D B=Y C=M !=5000 ;=Z X=A F=1
2080 A=U/2 ;=%0 A=X B=Y+1 C=M !=5000 ;=Z Y=B
2090 A=X B=Y+1 C=M !=5000 !=5100
2100 ;=Z E=-1 R=0 #=2200
2110 ;=E<0 E=0 #=2200
2120 ;=F ;=R<15 E=0 R=R+1
2130 ;=E<500 #=2200
2140 !=6000 O=O+Z L=L+Q(Z+60) !=4000 E=-1 R=0
2150 ;=X=-1 #=7000
2200 L=L+1 ;=L>=10000 L=L-10000 H=H+1
2210 $=27 "[1;1H" $=27 "[92mSCORE:" $=27 "[39m" ;=H=0 ?=L #=2230
2220 ?=H $=L/1000+48 $=%0/100+48 $=%0/10+48 $=%0+48
2230 $=27 "[2;1H" $=27 "[93mTETRIS:" ?=G
2240 $=27 "[3;1H" $=27 "[96mLINES: " $=27 "[39m" ?=O $=27 "[0m"
2250 !=2500 A=&3+&80+&108 ;=A W=20
2260 *WT W
2270 ;=E>=0 E=E+W+3
2280 #=2010
2500#==== difficulty: G から W (ミリ秒) ====
2510 W=70
2520 ;=G>=40 W=58
2530 ;=G>=71 W=46
2540 ;=G>=101 W=35
2550 ;=G>=131 W=23
2560 ;=G>=161 W=12
2570 ]
3000#==== rotate: 回せたら X,Y,M を変え、変わったら F=1 ====
3010 C=M+1/Q(T) C=%0 Z=0 K=0
3020 @
3030 A=Q(K*2+20)+X B=Q(K*2+21)+Y !=5000 K=K+1
3040 @=(Z+(K>=((T=0)*6+13)))
3050 ;=Z ;=(A<>X)+(B<>Y)+(C<>M) F=1 X=A Y=B M=C
3060 ]
4000#==== place_tetris: 置けなければ X=-1 ====
4010 ;=N>=0 X=-10 Y=7 M=0 T=N !=5120
4020 T=N ;=N<0 T='7
4030 N='7 !=4100
4040 X=5 Y=0 M=0 A=X B=Y C=M !=5000
4050 ;=Z !=5100 G=G+1 ]
4060 X=-1 ]
4100#==== disp_next ====
4110 $=27 "[6;1H" $=27 "[92mNEXT" $=27 "[0m"
4120 Z=T X=-10 Y=7 M=0 T=N !=5100 T=Z ]
5000#==== canputp: 位置 (A,B) 向き C で T が置けるなら Z=1 ====
5010 J=C/Q(T) J=%0+(T*4)*8 Z=1
5020 I=0,3
5030 D=P:I*2+J)+A ;=(D<1)+(D>10) Z=0
5040 D=P:I*2+J+1)+B ;=(D<0)+(D>23) Z=0
5050 ;=Z ;=S:D*40+15+P:I*2+J)+A)<>0 Z=0
5060 @=I+1 ]
5100#==== put: X,Y,M,T を描く ====
5110 A=$97 B=Q(T+7) #=5130
5120#==== erase: X,Y,M,T を消す ====
5125 A=0 B=0
5130 J=M/Q(T) J=%0+(T*4)*8
5140 I=0,3
5150 C=P:I*2+J)+X+15 D=P:I*2+J+1)+Y !=8100
5160 @=I+1 ]
6000#==== linecheck: 揃った行を消し、消した行数を Z に ====
6010 Z=0 D=0,23
6020 A=1 J=16,25
6030 ;=S:D*40+J)=0 A=0
6040 @=J+1
6050 ;=A !=6100 Z=Z+1
6060 @=D+1 ]
6100#==== scrolldown: フィールドの 0〜D 行を1行下へずらして描き直す ====
6110 ;=D=0 #=6150
6120 I=D @
6130 J=16,25 S:I*40+J)=S:I-1*40+J) S:I*40+J+1000)=S:I-1*40+J+1000) @=J+1
6140 I=I-1 @=(I<1)
6150 J=16,25 S:J)=0 S:J+1000)=0 @=J+1
6160 I=0,D
6170 $=27 "[" ?=I+1 ";33H"
6180 J=16,25 B=S:I*40+J+1000) !=8200 @=J+1
6200 @=I+1 ]
7000#==== GAMEOVER ====
7010 $=27 "[13;35H" $=27 "[91mGAMEOVER"
7020 $=27 "[15;29H" $=27 "[94;102m'q'key to quit" $=27 "[0m"
7030 @
7040 *WT 100
7050 @=(&16)
7060 #=9900
8000#==== putchar: マス (C,D) に文字 A 色 B ====
8100 S:D*40+C)=A S:D*40+C+1000)=B $=27 "[" ?=D+1 ";" ?=C*2+1 "H"
8200#==== 色 B で1マス (2桁) 描く。208 は 256 色のオレンジ ====
8210 $=27 "["
8220 ;=B=208 "48;5;"
8230 ?=B "m " $=27 "[0m" ]
9900#==== 終了 ====
9910 $=27 "[0m" $=27 "[2J" $=27 "[H" $=27 "[?25h"
9920 #=-1