tetris-terminal 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tetris/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .tetris import Tetris
2
+
3
+ __all__ = ["Tetris"]
tetris/cli.py ADDED
@@ -0,0 +1,18 @@
1
+ import curses
2
+ import sys
3
+
4
+ from tetris import Tetris
5
+
6
+
7
+ def wrapper(stdscr: curses.window):
8
+ tetris = Tetris(stdscr)
9
+ tetris.main()
10
+
11
+
12
+ def main() -> int:
13
+ curses.wrapper(wrapper)
14
+ return 0
15
+
16
+
17
+ if __name__ == "__main__":
18
+ sys.exit(main())
tetris/tetris.py ADDED
@@ -0,0 +1,203 @@
1
+ """
2
+ tetris class definition and main game logic
3
+ """
4
+
5
+ import curses
6
+ import time
7
+
8
+ from collections import deque
9
+ from copy import copy
10
+ from random import randint, shuffle
11
+
12
+
13
+ class Tetris:
14
+ """
15
+ main game class
16
+ """
17
+
18
+ x = 431424
19
+ y = 598356
20
+ r = 427089
21
+ c = 348480
22
+ p = 615696
23
+
24
+ px = 247872
25
+ py = 799248
26
+ pr = 0
27
+
28
+ tick = 0
29
+
30
+ board = [[0] * 10 for _ in range(20)]
31
+
32
+ piece_chars = ["Z", "S", "O", "J", "T", "I", "L"]
33
+
34
+ block = [
35
+ [x, y, x, y],
36
+ [r, p, r, p],
37
+ [c, c, c, c],
38
+ [599636, 431376, 598336, 432192],
39
+ [411985, 610832, 415808, 595540],
40
+ [px, py, px, py],
41
+ [614928, 399424, 615744, 428369],
42
+ ]
43
+
44
+ def __init__(self, stdscr: curses.window):
45
+ self.stdscr = stdscr
46
+
47
+ queue = deque(maxlen=14)
48
+
49
+ score = 0
50
+ lock_until = 0
51
+
52
+ def now_ms(self) -> int:
53
+ return int(time.time() * 1000)
54
+
55
+ def reset_lock_delay(self) -> None:
56
+ self.lock_until = 0
57
+
58
+ def start_lock_delay(self) -> None:
59
+ if not self.lock_until:
60
+ self.lock_until = self.now_ms() + 500
61
+
62
+ def NUM(self, x: int, y: int) -> int:
63
+ return 3 & self.block[self.p][x] >> y
64
+
65
+ def fill_bag(self) -> None:
66
+ bag = list(range(7))
67
+ shuffle(bag)
68
+ while bag:
69
+ self.queue.append(bag.pop())
70
+
71
+ def init_queue(self) -> None:
72
+ self.fill_bag()
73
+ self.fill_bag()
74
+
75
+ def next_from_queue(self) -> int:
76
+ if len(self.queue) == 7:
77
+ self.fill_bag()
78
+ return self.queue.popleft()
79
+
80
+ def new_piece(self) -> None:
81
+ self.y = self.py = 0
82
+ self.p = self.next_from_queue()
83
+ self.r = self.pr = randint(0, 3)
84
+ self.x = self.px = randint(0, 9 - self.NUM(self.r, 16))
85
+ self.reset_lock_delay()
86
+
87
+ def frame(self, stdscr: curses.window) -> None:
88
+ stdscr.move(0, 3)
89
+ stdscr.addstr("Next: ")
90
+ for i in range(5):
91
+ stdscr.addstr(f"{self.piece_chars[self.queue[i]]} ")
92
+
93
+ for i in range(20):
94
+ stdscr.move(i + 1, 1)
95
+ for j in range(10):
96
+ stdscr.addstr(" ", curses.color_pair(self.board[i][j]))
97
+ stdscr.move(21, 1)
98
+ stdscr.addstr(f"Score: {self.score}")
99
+ stdscr.refresh()
100
+
101
+ def set_piece(self, x: int, y: int, r: int, v: int) -> None:
102
+ for i in range(0, 8, 2):
103
+ self.board[self.NUM(r, i * 2) + y][self.NUM(r, (i * 2) + 2) + x] = v
104
+
105
+ def update_piece(self) -> None:
106
+ self.set_piece(self.px, self.py, self.pr, 0)
107
+ self.px, self.py, self.pr = self.x, self.y, self.r
108
+ self.set_piece(self.x, self.y, self.r, self.p + 1)
109
+
110
+ def remove_line(self) -> None:
111
+ for row in range(self.y, self.y + self.NUM(self.r, 18) + 1):
112
+ self.c = 1
113
+ for i in range(10):
114
+ self.c *= self.board[row][i]
115
+ if not self.c:
116
+ continue
117
+ for i in range(row - 1, 0, -1):
118
+ self.board[i + 1] = copy(self.board[i])
119
+ self.board[0] = [0] * 10
120
+ self.score += 1
121
+
122
+ def check_hit(self, x: int, y: int, r: int) -> int:
123
+ if y + self.NUM(r, 18) > 19:
124
+ return 1
125
+ self.set_piece(self.px, self.py, self.pr, 0)
126
+ self.c = 0
127
+ for i in range(0, 8, 2):
128
+ if self.board[y + self.NUM(r, i * 2)][x + self.NUM(r, (i * 2) + 2)]:
129
+ self.c += 1
130
+ self.set_piece(self.px, self.py, self.pr, self.p + 1)
131
+ return self.c
132
+
133
+ def do_tick(self) -> int:
134
+ self.tick += 1
135
+ if self.tick > 30:
136
+ self.tick = 0
137
+ if not self.check_hit(self.x, self.y + 1, self.r):
138
+ self.y += 1
139
+ self.update_piece()
140
+ self.reset_lock_delay()
141
+ else:
142
+ if not self.y:
143
+ return 0
144
+ self.start_lock_delay()
145
+ if self.now_ms() >= self.lock_until:
146
+ self.remove_line()
147
+ self.new_piece()
148
+ if self.lock_until and self.now_ms() >= self.lock_until:
149
+ if self.check_hit(self.x, self.y + 1, self.r):
150
+ self.remove_line()
151
+ self.new_piece()
152
+ else:
153
+ self.reset_lock_delay()
154
+ return 1
155
+
156
+ def runloop(self) -> None:
157
+ while self.do_tick():
158
+ time.sleep(0.01)
159
+ c = self.stdscr.getch()
160
+ if (
161
+ c == ord("a")
162
+ and self.x > 0
163
+ and not self.check_hit(self.x - 1, self.y, self.r)
164
+ ):
165
+ self.x -= 1
166
+ if (
167
+ c == ord("d")
168
+ and self.x + self.NUM(self.r, 16) < 9
169
+ and not self.check_hit(self.x + 1, self.y, self.r)
170
+ ):
171
+ self.x += 1
172
+ if c == ord("s"):
173
+ while not self.check_hit(self.x, self.y + 1, self.r):
174
+ self.y += 1
175
+ self.update_piece()
176
+ self.reset_lock_delay()
177
+ self.remove_line()
178
+ self.new_piece()
179
+ if c == ord("w"):
180
+ self.r += 1
181
+ self.r %= 4
182
+ while self.x + self.NUM(self.r, 16) > 9:
183
+ self.x -= 1
184
+ if self.check_hit(self.x, self.y, self.r):
185
+ self.x = self.px
186
+ self.r = self.pr
187
+ if c == ord("q"):
188
+ return
189
+ self.update_piece()
190
+ self.frame(self.stdscr)
191
+
192
+ def main(self) -> None:
193
+ self.init_queue()
194
+ for i in range(1, 8):
195
+ curses.init_pair(i, i, i)
196
+ self.new_piece()
197
+
198
+ curses.resize_term(22, 22)
199
+ curses.curs_set(0)
200
+
201
+ self.stdscr.timeout(0)
202
+ self.stdscr.box()
203
+ self.runloop()
@@ -0,0 +1,65 @@
1
+ Metadata-Version: 2.4
2
+ Name: tetris-terminal
3
+ Version: 0.0.1
4
+ Summary: A tetris game runs in the terminal
5
+ Author-email: jayzhu <jay.l.zhu@foxmail.com>
6
+ Project-URL: homepage, https://github.com/zlh124/pytetris
7
+ Classifier: Development Status :: 3 - Alpha
8
+ Classifier: Environment :: Console :: Curses
9
+ Classifier: Intended Audience :: End Users/Desktop
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: POSIX :: Linux
12
+ Classifier: Operating System :: MacOS :: MacOS X
13
+ Classifier: Operating System :: Microsoft :: Windows
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Games/Entertainment :: Puzzle Games
22
+ Classifier: Topic :: Terminals
23
+ Requires-Python: >=3.8
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Requires-Dist: windows-curses; sys_platform == "win32"
27
+ Dynamic: license-file
28
+
29
+ ![gameplay](./gameplay.gif)
30
+ # Tetris Terminal🎮
31
+ A terminal-based Tetris game written in Python using the `curses` library.
32
+
33
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
34
+ [![Python 3.8+](https://img.shields.io/badge/Python-3.8%2B-blue)]()
35
+
36
+ ### Features
37
+ - Classic Tetris gameplay with 7 standard tetrominoes
38
+ - Real-time score
39
+ - Next piece preview
40
+
41
+ ### Platform Support
42
+ Based on Python's [`curses`](https://docs.python.org/3/library/curses.html) module:
43
+ - âś… **Linux/macOS**: Works out of the box
44
+ - ✅️ **Windows**: With [`windows-curses`](https://github.com/zephyrproject-rtos/windows-curses)
45
+
46
+ ### Installation & Usage
47
+ ```bash
48
+ pip install tetris-terminal
49
+ tetris
50
+ ```
51
+
52
+ ### Controls
53
+ | Key | Action |
54
+ |-----------|-----------------|
55
+ | `a` | Move left |
56
+ | `d` | Move right |
57
+ | `w` | Rotate piece |
58
+ | `s` | Hard drop |
59
+ | `q` | Quit game |
60
+
61
+ ### License
62
+ MIT License - see [LICENSE](LICENSE) for details.
63
+
64
+ ### Acknowledgements
65
+ Game logic adapted from [tinytetris](https://github.com/taylorconor/tinytetris) (a C implementation).
@@ -0,0 +1,9 @@
1
+ tetris/__init__.py,sha256=dc-DxhEtor4mRYzQuXqhkmQbtrcWdhV_FIs54KIu_xU,52
2
+ tetris/cli.py,sha256=1BnDQ7qXbUo2Pf_iIHGO2F7YvcTP1yZapnlhsP7n_eM,266
3
+ tetris/tetris.py,sha256=QyuK1GHEOoHyZMewaRy5wI2UewddAjk_gkk_N21v2i0,5994
4
+ tetris_terminal-0.0.1.dist-info/licenses/LICENSE,sha256=obY8tJlAQre7kO4i31BT-57NTH18M2EmBIsDTcmM28o,1075
5
+ tetris_terminal-0.0.1.dist-info/METADATA,sha256=s22Pjg8DehvwFy0z-RwdSm5c-oRQZW9UuY8jYTJv7rU,2346
6
+ tetris_terminal-0.0.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
7
+ tetris_terminal-0.0.1.dist-info/entry_points.txt,sha256=JJSSe-Qvy_oyapzvi5phbPNveo9Cd8p-BoIU-ZKF9xY,43
8
+ tetris_terminal-0.0.1.dist-info/top_level.txt,sha256=T4Nlqe4Ss3LOGe002n2WprpZwOcJCNZfBLfFLKtwIOI,7
9
+ tetris_terminal-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ tetris = tetris.cli:main
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright 2026 Jay Zhu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ tetris