pycade-game 1.0.0__tar.gz
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.
- pycade_game-1.0.0/LICENSE +21 -0
- pycade_game-1.0.0/PKG-INFO +26 -0
- pycade_game-1.0.0/README.md +8 -0
- pycade_game-1.0.0/pyproject.toml +29 -0
- pycade_game-1.0.0/setup.cfg +4 -0
- pycade_game-1.0.0/src/pycade/__init__.py +2 -0
- pycade_game-1.0.0/src/pycade/__main__.py +3 -0
- pycade_game-1.0.0/src/pycade/game.py +541 -0
- pycade_game-1.0.0/src/pycade_game.egg-info/PKG-INFO +26 -0
- pycade_game-1.0.0/src/pycade_game.egg-info/SOURCES.txt +11 -0
- pycade_game-1.0.0/src/pycade_game.egg-info/dependency_links.txt +1 -0
- pycade_game-1.0.0/src/pycade_game.egg-info/entry_points.txt +2 -0
- pycade_game-1.0.0/src/pycade_game.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cole
|
|
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.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pycade-game
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Terminal arcade: Snake, Pong, Tic-Tac-Toe and Hangman
|
|
5
|
+
Author: Cole
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://sourceforge.net/projects/pycade/
|
|
8
|
+
Keywords: games,terminal,arcade,curses,snake,pong
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Environment :: Console :: Curses
|
|
11
|
+
Classifier: Topic :: Games/Entertainment :: Arcade
|
|
12
|
+
Classifier: Operating System :: MacOS
|
|
13
|
+
Classifier: Operating System :: POSIX
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Pycade
|
|
20
|
+
|
|
21
|
+
Terminal arcade games: Snake, Pong, Tic-Tac-Toe and Hangman. Uses only Python's built-in curses library.
|
|
22
|
+
|
|
23
|
+
pip install pycade-game
|
|
24
|
+
pycade
|
|
25
|
+
|
|
26
|
+
Arrow keys or WASD move, number keys play Tic-Tac-Toe, q goes back.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pycade-game"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Terminal arcade: Snake, Pong, Tic-Tac-Toe and Hangman"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Cole" }]
|
|
13
|
+
keywords = ["games", "terminal", "arcade", "curses", "snake", "pong"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Environment :: Console :: Curses",
|
|
17
|
+
"Topic :: Games/Entertainment :: Arcade",
|
|
18
|
+
"Operating System :: MacOS",
|
|
19
|
+
"Operating System :: POSIX",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
pycade = "pycade.game:run"
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://sourceforge.net/projects/pycade/"
|
|
27
|
+
|
|
28
|
+
[tool.setuptools.packages.find]
|
|
29
|
+
where = ["src"]
|
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
PYCADE - a tiny game collection for the macOS terminal.
|
|
4
|
+
|
|
5
|
+
Run: python3 pycade.py
|
|
6
|
+
Needs: Python 3 (curses is built in). Terminal size 80x24 or bigger works best.
|
|
7
|
+
|
|
8
|
+
Games: Snake, Pong (vs CPU), Tic-Tac-Toe (vs computer), Hangman
|
|
9
|
+
"""
|
|
10
|
+
import curses
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import random
|
|
14
|
+
import time
|
|
15
|
+
|
|
16
|
+
SCORE_FILE = os.path.expanduser("~/.pycade_scores.json")
|
|
17
|
+
|
|
18
|
+
GREEN, RED, YELLOW, CYAN, MAGENTA, BLUE = 1, 2, 3, 4, 5, 6
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# ---------------------------------------------------------------- helpers
|
|
22
|
+
def load_scores():
|
|
23
|
+
try:
|
|
24
|
+
with open(SCORE_FILE) as f:
|
|
25
|
+
return json.load(f)
|
|
26
|
+
except (OSError, ValueError):
|
|
27
|
+
return {}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def record(key, value, mode="max"):
|
|
31
|
+
scores = load_scores()
|
|
32
|
+
if mode == "max":
|
|
33
|
+
scores[key] = max(scores.get(key, 0), value)
|
|
34
|
+
else:
|
|
35
|
+
scores[key] = scores.get(key, 0) + value
|
|
36
|
+
try:
|
|
37
|
+
with open(SCORE_FILE, "w") as f:
|
|
38
|
+
json.dump(scores, f)
|
|
39
|
+
except OSError:
|
|
40
|
+
pass
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def init_colors():
|
|
44
|
+
curses.start_color()
|
|
45
|
+
try:
|
|
46
|
+
curses.use_default_colors()
|
|
47
|
+
bg = -1
|
|
48
|
+
except curses.error:
|
|
49
|
+
bg = curses.COLOR_BLACK
|
|
50
|
+
for i, c in enumerate(
|
|
51
|
+
[curses.COLOR_GREEN, curses.COLOR_RED, curses.COLOR_YELLOW,
|
|
52
|
+
curses.COLOR_CYAN, curses.COLOR_MAGENTA, curses.COLOR_BLUE], 1):
|
|
53
|
+
curses.init_pair(i, c, bg)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def col(n):
|
|
57
|
+
return curses.color_pair(n)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def put(s, y, x, text, attr=0):
|
|
61
|
+
h, w = s.getmaxyx()
|
|
62
|
+
if y < 0 or y >= h or x >= w:
|
|
63
|
+
return
|
|
64
|
+
if x < 0:
|
|
65
|
+
text = text[-x:]
|
|
66
|
+
x = 0
|
|
67
|
+
text = text[: w - x]
|
|
68
|
+
if not text:
|
|
69
|
+
return
|
|
70
|
+
try:
|
|
71
|
+
s.addstr(y, x, text, attr)
|
|
72
|
+
except curses.error:
|
|
73
|
+
pass
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def center(s, y, text, attr=0):
|
|
77
|
+
_, w = s.getmaxyx()
|
|
78
|
+
put(s, y, max(0, (w - len(text)) // 2), text, attr)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def wait_key(s):
|
|
82
|
+
s.timeout(-1)
|
|
83
|
+
curses.flushinp()
|
|
84
|
+
return s.getch()
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def is_quit(k):
|
|
88
|
+
return k in (ord("q"), ord("Q"), 27)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def message(s, title, lines, attr=0):
|
|
92
|
+
s.erase()
|
|
93
|
+
h, _ = s.getmaxyx()
|
|
94
|
+
top = max(0, h // 2 - len(lines) // 2 - 2)
|
|
95
|
+
center(s, top, title, attr | curses.A_BOLD)
|
|
96
|
+
for i, line in enumerate(lines):
|
|
97
|
+
center(s, top + 2 + i, line)
|
|
98
|
+
center(s, top + 3 + len(lines), "Press any key...", curses.A_DIM)
|
|
99
|
+
s.refresh()
|
|
100
|
+
return wait_key(s)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def too_small(s):
|
|
104
|
+
message(s, "Terminal too small", ["Make the window bigger and try again."], col(RED))
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def box(s, y, x, h, w, attr=0):
|
|
108
|
+
"""Draw a box with its top-left corner at (y, x), outer size h x w."""
|
|
109
|
+
put(s, y, x, "┌" + "─" * (w - 2) + "┐", attr)
|
|
110
|
+
for r in range(1, h - 1):
|
|
111
|
+
put(s, y + r, x, "│", attr)
|
|
112
|
+
put(s, y + r, x + w - 1, "│", attr)
|
|
113
|
+
put(s, y + h - 1, x, "└" + "─" * (w - 2) + "┘", attr)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# ------------------------------------------------------------------ snake
|
|
117
|
+
SNAKE_DIRS = {
|
|
118
|
+
curses.KEY_UP: (-1, 0), ord("w"): (-1, 0), ord("W"): (-1, 0),
|
|
119
|
+
curses.KEY_DOWN: (1, 0), ord("s"): (1, 0), ord("S"): (1, 0),
|
|
120
|
+
curses.KEY_LEFT: (0, -1), ord("a"): (0, -1), ord("A"): (0, -1),
|
|
121
|
+
curses.KEY_RIGHT: (0, 1), ord("d"): (0, 1), ord("D"): (0, 1),
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def play_snake(s):
|
|
126
|
+
while True:
|
|
127
|
+
h, w = s.getmaxyx()
|
|
128
|
+
cols = max(12, min((w - 2) // 2, 30))
|
|
129
|
+
rows = max(8, min(h - 5, 20))
|
|
130
|
+
if w < cols * 2 + 2 or h < rows + 4:
|
|
131
|
+
return too_small(s)
|
|
132
|
+
y0 = 2
|
|
133
|
+
x0 = (w - (cols * 2 + 2)) // 2
|
|
134
|
+
|
|
135
|
+
snake = [(rows // 2, cols // 2 - i) for i in range(3)]
|
|
136
|
+
d = (0, 1)
|
|
137
|
+
score = 0
|
|
138
|
+
|
|
139
|
+
def new_food():
|
|
140
|
+
taken = set(snake)
|
|
141
|
+
free = [(r, c) for r in range(rows) for c in range(cols) if (r, c) not in taken]
|
|
142
|
+
return random.choice(free) if free else None
|
|
143
|
+
|
|
144
|
+
food = new_food()
|
|
145
|
+
best = load_scores().get("snake", 0)
|
|
146
|
+
dead = False
|
|
147
|
+
|
|
148
|
+
while not dead:
|
|
149
|
+
s.erase()
|
|
150
|
+
center(s, 0, f"SNAKE score {score} best {max(best, score)} (arrows/WASD, q quits)", col(GREEN))
|
|
151
|
+
box(s, y0 - 1, x0, rows + 2, cols * 2 + 2, col(BLUE))
|
|
152
|
+
if food:
|
|
153
|
+
put(s, y0 + food[0], x0 + 1 + food[1] * 2, "● ", col(RED) | curses.A_BOLD)
|
|
154
|
+
for i, (r, c) in enumerate(snake):
|
|
155
|
+
put(s, y0 + r, x0 + 1 + c * 2, "██" if i else "▓▓", col(GREEN))
|
|
156
|
+
s.refresh()
|
|
157
|
+
|
|
158
|
+
tick = max(0.055, 0.13 - score * 0.002)
|
|
159
|
+
end = time.time() + tick
|
|
160
|
+
pending = None
|
|
161
|
+
while True:
|
|
162
|
+
rem = end - time.time()
|
|
163
|
+
if rem <= 0:
|
|
164
|
+
break
|
|
165
|
+
s.timeout(max(1, int(rem * 1000)))
|
|
166
|
+
k = s.getch()
|
|
167
|
+
if is_quit(k):
|
|
168
|
+
return
|
|
169
|
+
nd = SNAKE_DIRS.get(k)
|
|
170
|
+
if nd and (nd[0] != -d[0] or nd[1] != -d[1]):
|
|
171
|
+
pending = nd
|
|
172
|
+
if pending:
|
|
173
|
+
d = pending
|
|
174
|
+
|
|
175
|
+
head = (snake[0][0] + d[0], snake[0][1] + d[1])
|
|
176
|
+
grow = head == food
|
|
177
|
+
if not grow:
|
|
178
|
+
snake.pop()
|
|
179
|
+
if not (0 <= head[0] < rows and 0 <= head[1] < cols) or head in snake:
|
|
180
|
+
dead = True
|
|
181
|
+
break
|
|
182
|
+
snake.insert(0, head)
|
|
183
|
+
if grow:
|
|
184
|
+
score += 1
|
|
185
|
+
food = new_food()
|
|
186
|
+
if food is None:
|
|
187
|
+
break
|
|
188
|
+
|
|
189
|
+
record("snake", score, "max")
|
|
190
|
+
k = message(s, "GAME OVER" if dead else "YOU FILLED THE BOARD!",
|
|
191
|
+
[f"Score: {score}", "Any key = play again, q = menu"], col(RED))
|
|
192
|
+
if is_quit(k):
|
|
193
|
+
return
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
# three-line instruction helper not needed; pong below
|
|
197
|
+
# ------------------------------------------------------------------- pong
|
|
198
|
+
def play_pong(s):
|
|
199
|
+
h, w = s.getmaxyx()
|
|
200
|
+
fw = max(30, min(w - 2, 70))
|
|
201
|
+
fh = max(10, min(h - 5, 20))
|
|
202
|
+
if w < fw + 2 or h < fh + 4:
|
|
203
|
+
return too_small(s)
|
|
204
|
+
y0 = 2
|
|
205
|
+
x0 = (w - fw - 2) // 2
|
|
206
|
+
ph = max(3, fh // 4)
|
|
207
|
+
target = 7
|
|
208
|
+
|
|
209
|
+
py = cpy = (fh - ph) / 2
|
|
210
|
+
pscore = cscore = 0
|
|
211
|
+
|
|
212
|
+
def serve(direction):
|
|
213
|
+
return fw / 2, fh / 2, 0.7 * direction, random.choice([-0.3, 0.3])
|
|
214
|
+
|
|
215
|
+
bx, by, vx, vy = serve(random.choice([-1, 1]))
|
|
216
|
+
frame = 1 / 30
|
|
217
|
+
|
|
218
|
+
while True:
|
|
219
|
+
t0 = time.time()
|
|
220
|
+
s.timeout(0)
|
|
221
|
+
while True:
|
|
222
|
+
k = s.getch()
|
|
223
|
+
if k == -1:
|
|
224
|
+
break
|
|
225
|
+
if is_quit(k):
|
|
226
|
+
return
|
|
227
|
+
if k in (curses.KEY_UP, ord("w"), ord("W")):
|
|
228
|
+
py -= 2
|
|
229
|
+
elif k in (curses.KEY_DOWN, ord("s"), ord("S")):
|
|
230
|
+
py += 2
|
|
231
|
+
py = max(0, min(fh - ph, py))
|
|
232
|
+
|
|
233
|
+
# ball
|
|
234
|
+
bx += vx
|
|
235
|
+
by += vy
|
|
236
|
+
if by < 0:
|
|
237
|
+
by, vy = -by, -vy
|
|
238
|
+
if by > fh - 1:
|
|
239
|
+
by, vy = 2 * (fh - 1) - by, -vy
|
|
240
|
+
|
|
241
|
+
# cpu paddle
|
|
242
|
+
if vx > 0 or bx > fw / 2:
|
|
243
|
+
diff = (by - ph / 2) - cpy
|
|
244
|
+
else:
|
|
245
|
+
diff = ((fh - ph) / 2) - cpy
|
|
246
|
+
cpy += max(-0.42, min(0.42, diff))
|
|
247
|
+
cpy = max(0, min(fh - ph, cpy))
|
|
248
|
+
|
|
249
|
+
# paddle hits (player at column 1, cpu at column fw-2)
|
|
250
|
+
if vx < 0 and bx <= 1.5:
|
|
251
|
+
if int(py) <= int(by) <= int(py) + ph - 1:
|
|
252
|
+
bx = 2.0
|
|
253
|
+
vx = min(1.3, -vx * 1.06)
|
|
254
|
+
vy = ((by - (py + ph / 2)) / (ph / 2)) * 0.6
|
|
255
|
+
elif bx < 0:
|
|
256
|
+
cscore += 1
|
|
257
|
+
bx, by, vx, vy = serve(-1)
|
|
258
|
+
time.sleep(0.5)
|
|
259
|
+
elif vx > 0 and bx >= fw - 2.5:
|
|
260
|
+
if int(cpy) <= int(by) <= int(cpy) + ph - 1:
|
|
261
|
+
bx = fw - 3.0
|
|
262
|
+
vx = -min(1.3, vx * 1.06)
|
|
263
|
+
vy = ((by - (cpy + ph / 2)) / (ph / 2)) * 0.6
|
|
264
|
+
elif bx > fw - 1:
|
|
265
|
+
pscore += 1
|
|
266
|
+
bx, by, vx, vy = serve(1)
|
|
267
|
+
time.sleep(0.5)
|
|
268
|
+
|
|
269
|
+
# draw
|
|
270
|
+
s.erase()
|
|
271
|
+
center(s, 0, f"PONG YOU {pscore} - {cscore} CPU (first to {target}) up/down or W/S, q quits", col(YELLOW))
|
|
272
|
+
box(s, y0 - 1, x0, fh + 2, fw + 2, col(BLUE))
|
|
273
|
+
for r in range(0, fh, 2):
|
|
274
|
+
put(s, y0 + r, x0 + 1 + fw // 2, "┊", curses.A_DIM)
|
|
275
|
+
for r in range(ph):
|
|
276
|
+
put(s, y0 + int(py) + r, x0 + 2, "█", col(CYAN))
|
|
277
|
+
put(s, y0 + int(cpy) + r, x0 + fw - 1, "█", col(RED))
|
|
278
|
+
put(s, y0 + max(0, min(fh - 1, int(by))), x0 + 1 + max(0, min(fw - 1, int(bx))), "●", curses.A_BOLD)
|
|
279
|
+
s.refresh()
|
|
280
|
+
|
|
281
|
+
if pscore >= target or cscore >= target:
|
|
282
|
+
won = pscore >= target
|
|
283
|
+
if won:
|
|
284
|
+
record("pong_wins", 1, "add")
|
|
285
|
+
k = message(s, "YOU WIN!" if won else "CPU WINS",
|
|
286
|
+
[f"Final score: {pscore} - {cscore}", "Any key = rematch, q = menu"],
|
|
287
|
+
col(GREEN if won else RED))
|
|
288
|
+
if is_quit(k):
|
|
289
|
+
return
|
|
290
|
+
py = cpy = (fh - ph) / 2
|
|
291
|
+
pscore = cscore = 0
|
|
292
|
+
bx, by, vx, vy = serve(random.choice([-1, 1]))
|
|
293
|
+
|
|
294
|
+
spare = frame - (time.time() - t0)
|
|
295
|
+
if spare > 0:
|
|
296
|
+
time.sleep(spare)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
# ------------------------------------------------------------ tic-tac-toe
|
|
300
|
+
LINES = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)]
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def winner(b):
|
|
304
|
+
for a, c, d in LINES:
|
|
305
|
+
if b[a] != " " and b[a] == b[c] == b[d]:
|
|
306
|
+
return b[a]
|
|
307
|
+
return None
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def minimax(b, turn):
|
|
311
|
+
w = winner(b)
|
|
312
|
+
if w == "O":
|
|
313
|
+
return 1
|
|
314
|
+
if w == "X":
|
|
315
|
+
return -1
|
|
316
|
+
if " " not in b:
|
|
317
|
+
return 0
|
|
318
|
+
scores = []
|
|
319
|
+
for i in range(9):
|
|
320
|
+
if b[i] == " ":
|
|
321
|
+
b[i] = turn
|
|
322
|
+
scores.append(minimax(b, "X" if turn == "O" else "O"))
|
|
323
|
+
b[i] = " "
|
|
324
|
+
return max(scores) if turn == "O" else min(scores)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def ai_move(b):
|
|
328
|
+
empty = [i for i in range(9) if b[i] == " "]
|
|
329
|
+
if len(empty) == 9:
|
|
330
|
+
return random.choice([0, 2, 4, 6, 8])
|
|
331
|
+
if random.random() < 0.2: # sometimes slips up so you can win
|
|
332
|
+
return random.choice(empty)
|
|
333
|
+
best, best_moves = -2, []
|
|
334
|
+
for i in empty:
|
|
335
|
+
b[i] = "O"
|
|
336
|
+
sc = minimax(b, "X")
|
|
337
|
+
b[i] = " "
|
|
338
|
+
if sc > best:
|
|
339
|
+
best, best_moves = sc, [i]
|
|
340
|
+
elif sc == best:
|
|
341
|
+
best_moves.append(i)
|
|
342
|
+
return random.choice(best_moves)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def draw_ttt(s, b, stats, note=""):
|
|
346
|
+
s.erase()
|
|
347
|
+
h, w = s.getmaxyx()
|
|
348
|
+
top = max(1, h // 2 - 6)
|
|
349
|
+
center(s, top, "TIC-TAC-TOE", col(MAGENTA) | curses.A_BOLD)
|
|
350
|
+
center(s, top + 1, f"You (X) {stats[0]} Computer (O) {stats[1]} Draws {stats[2]}")
|
|
351
|
+
gx = max(0, (w - 11) // 2)
|
|
352
|
+
gy = top + 3
|
|
353
|
+
for r in range(3):
|
|
354
|
+
put(s, gy + r * 2, gx, " │ │ ")
|
|
355
|
+
if r < 2:
|
|
356
|
+
put(s, gy + r * 2 + 1, gx, "───┼───┼───")
|
|
357
|
+
for c in range(3):
|
|
358
|
+
i = r * 3 + c
|
|
359
|
+
x = gx + c * 4 + 1
|
|
360
|
+
if b[i] == "X":
|
|
361
|
+
put(s, gy + r * 2, x, "X", col(RED) | curses.A_BOLD)
|
|
362
|
+
elif b[i] == "O":
|
|
363
|
+
put(s, gy + r * 2, x, "O", col(CYAN) | curses.A_BOLD)
|
|
364
|
+
else:
|
|
365
|
+
put(s, gy + r * 2, x, str(i + 1), curses.A_DIM)
|
|
366
|
+
center(s, gy + 7, note or "Press 1-9 to place your X (q = menu)")
|
|
367
|
+
s.refresh()
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def play_ttt(s):
|
|
371
|
+
stats = [0, 0, 0]
|
|
372
|
+
human_first = True
|
|
373
|
+
while True:
|
|
374
|
+
b = [" "] * 9
|
|
375
|
+
turn = "X" if human_first else "O"
|
|
376
|
+
while winner(b) is None and " " in b:
|
|
377
|
+
draw_ttt(s, b, stats)
|
|
378
|
+
if turn == "X":
|
|
379
|
+
k = wait_key(s)
|
|
380
|
+
if is_quit(k):
|
|
381
|
+
return
|
|
382
|
+
if ord("1") <= k <= ord("9") and b[k - ord("1")] == " ":
|
|
383
|
+
b[k - ord("1")] = "X"
|
|
384
|
+
turn = "O"
|
|
385
|
+
else:
|
|
386
|
+
draw_ttt(s, b, stats, "Computer is thinking...")
|
|
387
|
+
time.sleep(0.4)
|
|
388
|
+
b[ai_move(b)] = "O"
|
|
389
|
+
turn = "X"
|
|
390
|
+
w = winner(b)
|
|
391
|
+
if w == "X":
|
|
392
|
+
stats[0] += 1
|
|
393
|
+
record("ttt_wins", 1, "add")
|
|
394
|
+
note = "You win! Any key = next round, q = menu"
|
|
395
|
+
elif w == "O":
|
|
396
|
+
stats[1] += 1
|
|
397
|
+
note = "Computer wins. Any key = next round, q = menu"
|
|
398
|
+
else:
|
|
399
|
+
stats[2] += 1
|
|
400
|
+
note = "Draw. Any key = next round, q = menu"
|
|
401
|
+
draw_ttt(s, b, stats, note)
|
|
402
|
+
if is_quit(wait_key(s)):
|
|
403
|
+
return
|
|
404
|
+
human_first = not human_first
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
# ---------------------------------------------------------------- hangman
|
|
408
|
+
WORDS = (
|
|
409
|
+
"python terminal keyboard monitor network program computer algorithm pixel "
|
|
410
|
+
"arcade cursor window battery browser coffee guitar rocket planet jungle "
|
|
411
|
+
"castle dragon wizard puzzle mystery harbor lantern compass voyage thunder "
|
|
412
|
+
"blanket crystal meadow orange pepper whistle balloon journey kitchen "
|
|
413
|
+
"library factory bridge island garden"
|
|
414
|
+
).split()
|
|
415
|
+
|
|
416
|
+
GALLOWS = [
|
|
417
|
+
" +---+\n | |\n |\n |\n |\n |\n=========",
|
|
418
|
+
" +---+\n | |\n O |\n |\n |\n |\n=========",
|
|
419
|
+
" +---+\n | |\n O |\n | |\n |\n |\n=========",
|
|
420
|
+
" +---+\n | |\n O |\n /| |\n |\n |\n=========",
|
|
421
|
+
" +---+\n | |\n O |\n /|\\ |\n |\n |\n=========",
|
|
422
|
+
" +---+\n | |\n O |\n /|\\ |\n / |\n |\n=========",
|
|
423
|
+
" +---+\n | |\n O |\n /|\\ |\n / \\ |\n |\n=========",
|
|
424
|
+
]
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def play_hangman(s):
|
|
428
|
+
streak = 0
|
|
429
|
+
while True:
|
|
430
|
+
word = random.choice(WORDS)
|
|
431
|
+
guessed = set()
|
|
432
|
+
misses = 0
|
|
433
|
+
while True:
|
|
434
|
+
s.erase()
|
|
435
|
+
h, w = s.getmaxyx()
|
|
436
|
+
top = max(1, h // 2 - 8)
|
|
437
|
+
center(s, top, f"HANGMAN streak {streak} (type letters, q = menu)", col(YELLOW) | curses.A_BOLD)
|
|
438
|
+
art = GALLOWS[misses].split("\n")
|
|
439
|
+
for i, line in enumerate(art):
|
|
440
|
+
center(s, top + 2 + i, line.ljust(10), col(RED))
|
|
441
|
+
shown = " ".join(ch if ch in guessed else "_" for ch in word)
|
|
442
|
+
center(s, top + 11, shown, curses.A_BOLD)
|
|
443
|
+
wrong = " ".join(sorted(g for g in guessed if g not in word))
|
|
444
|
+
center(s, top + 13, f"Wrong: {wrong} ({6 - misses} left)")
|
|
445
|
+
s.refresh()
|
|
446
|
+
if all(ch in guessed for ch in word):
|
|
447
|
+
streak += 1
|
|
448
|
+
record("hangman_wins", 1, "add")
|
|
449
|
+
result = "You got it!"
|
|
450
|
+
break
|
|
451
|
+
if misses >= 6:
|
|
452
|
+
streak = 0
|
|
453
|
+
result = f"Out of guesses. The word was '{word}'."
|
|
454
|
+
break
|
|
455
|
+
k = wait_key(s)
|
|
456
|
+
if k == 27:
|
|
457
|
+
return
|
|
458
|
+
if 65 <= k <= 90 or 97 <= k <= 122:
|
|
459
|
+
ch = chr(k).lower()
|
|
460
|
+
if ch == "q" and not guessed and misses == 0:
|
|
461
|
+
pass
|
|
462
|
+
if ch not in guessed:
|
|
463
|
+
guessed.add(ch)
|
|
464
|
+
if ch not in word:
|
|
465
|
+
misses += 1
|
|
466
|
+
k = message(s, result, [f"Word: {word}", "Any key = next word, Esc = menu"],
|
|
467
|
+
col(GREEN) if result.startswith("You") else col(RED))
|
|
468
|
+
if k == 27:
|
|
469
|
+
return
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
# ------------------------------------------------------------------- menu
|
|
473
|
+
GAMES = [
|
|
474
|
+
("Snake", play_snake, "Eat, grow, don't crash"),
|
|
475
|
+
("Pong", play_pong, "Beat the CPU to 7"),
|
|
476
|
+
("Tic-Tac-Toe", play_ttt, "You vs a mostly unbeatable computer"),
|
|
477
|
+
("Hangman", play_hangman, "Guess the word in 6 misses"),
|
|
478
|
+
]
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
def menu(s):
|
|
482
|
+
sel = 0
|
|
483
|
+
while True:
|
|
484
|
+
s.erase()
|
|
485
|
+
h, w = s.getmaxyx()
|
|
486
|
+
top = max(1, h // 2 - 8)
|
|
487
|
+
art = [
|
|
488
|
+
"╔════════════════════════════════╗",
|
|
489
|
+
"║ PYCADE ║",
|
|
490
|
+
"╚════════════════════════════════╝",
|
|
491
|
+
]
|
|
492
|
+
for i, line in enumerate(art):
|
|
493
|
+
center(s, top + i, line, col(CYAN) | curses.A_BOLD)
|
|
494
|
+
for i, (name, _, desc) in enumerate(GAMES):
|
|
495
|
+
y = top + 5 + i * 2
|
|
496
|
+
if i == sel:
|
|
497
|
+
center(s, y, f"▶ {i + 1}. {name:<12} {desc}", col(GREEN) | curses.A_BOLD)
|
|
498
|
+
else:
|
|
499
|
+
center(s, y, f" {i + 1}. {name:<12} {desc}")
|
|
500
|
+
sc = load_scores()
|
|
501
|
+
center(s, top + 6 + len(GAMES) * 2,
|
|
502
|
+
f"Snake best: {sc.get('snake', 0)} Wins - Pong: {sc.get('pong_wins', 0)} "
|
|
503
|
+
f"Tic-Tac-Toe: {sc.get('ttt_wins', 0)} Hangman: {sc.get('hangman_wins', 0)}",
|
|
504
|
+
curses.A_DIM)
|
|
505
|
+
center(s, top + 8 + len(GAMES) * 2, "↑/↓ + Enter, or press 1-4. q = quit", curses.A_DIM)
|
|
506
|
+
s.refresh()
|
|
507
|
+
|
|
508
|
+
k = wait_key(s)
|
|
509
|
+
if is_quit(k):
|
|
510
|
+
return
|
|
511
|
+
if k in (curses.KEY_UP, ord("k")):
|
|
512
|
+
sel = (sel - 1) % len(GAMES)
|
|
513
|
+
elif k in (curses.KEY_DOWN, ord("j")):
|
|
514
|
+
sel = (sel + 1) % len(GAMES)
|
|
515
|
+
elif ord("1") <= k <= ord("0") + len(GAMES):
|
|
516
|
+
sel = k - ord("1")
|
|
517
|
+
GAMES[sel][1](s)
|
|
518
|
+
elif k in (10, 13, curses.KEY_ENTER):
|
|
519
|
+
GAMES[sel][1](s)
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def main(s):
|
|
523
|
+
try:
|
|
524
|
+
curses.curs_set(0)
|
|
525
|
+
except curses.error:
|
|
526
|
+
pass
|
|
527
|
+
s.keypad(True)
|
|
528
|
+
init_colors()
|
|
529
|
+
menu(s)
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
def run():
|
|
533
|
+
try:
|
|
534
|
+
curses.wrapper(main)
|
|
535
|
+
except KeyboardInterrupt:
|
|
536
|
+
pass
|
|
537
|
+
print("Thanks for playing!")
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
if __name__ == "__main__":
|
|
541
|
+
run()
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pycade-game
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Terminal arcade: Snake, Pong, Tic-Tac-Toe and Hangman
|
|
5
|
+
Author: Cole
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://sourceforge.net/projects/pycade/
|
|
8
|
+
Keywords: games,terminal,arcade,curses,snake,pong
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Environment :: Console :: Curses
|
|
11
|
+
Classifier: Topic :: Games/Entertainment :: Arcade
|
|
12
|
+
Classifier: Operating System :: MacOS
|
|
13
|
+
Classifier: Operating System :: POSIX
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Pycade
|
|
20
|
+
|
|
21
|
+
Terminal arcade games: Snake, Pong, Tic-Tac-Toe and Hangman. Uses only Python's built-in curses library.
|
|
22
|
+
|
|
23
|
+
pip install pycade-game
|
|
24
|
+
pycade
|
|
25
|
+
|
|
26
|
+
Arrow keys or WASD move, number keys play Tic-Tac-Toe, q goes back.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/pycade/__init__.py
|
|
5
|
+
src/pycade/__main__.py
|
|
6
|
+
src/pycade/game.py
|
|
7
|
+
src/pycade_game.egg-info/PKG-INFO
|
|
8
|
+
src/pycade_game.egg-info/SOURCES.txt
|
|
9
|
+
src/pycade_game.egg-info/dependency_links.txt
|
|
10
|
+
src/pycade_game.egg-info/entry_points.txt
|
|
11
|
+
src/pycade_game.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pycade
|