-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
executable file
·554 lines (458 loc) · 17.4 KB
/
Copy pathgame.py
File metadata and controls
executable file
·554 lines (458 loc) · 17.4 KB
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
#!/usr/bin/env python3
"""Pokemon battle, played from the terminal.
Run: python3 game.py
Not part of the subject's file tree - this is the fun one.
"""
import sys
import termios
import time
import tty
import typing
from ex0 import PokedexFactory
from ex0.creature import Creature
from ex0.moves import STRUGGLE, Move
from ex0.pokedex import POKEDEX
from ex0.pokemon import Pokemon, known_moves
from ex0.roll import between, seed
from ex1.creature import VersatileCreature
from ex2 import (AggressiveStrategy, BattleStrategy, DefensiveStrategy,
EasyStrategy, NormalStrategy)
from ex2.strategy import Defender, Healer, Transformer
LEVEL = 100
HALF_LEVEL = 50
BAR_WIDTH = 20
FULL = "█"
EMPTY = "░"
GREEN = "\033[32m"
YELLOW = "\033[33m"
RED = "\033[31m"
CYAN = "\033[36m"
TINT = "\033[38;5;{0}m"
TYPE_COLOURS = {
"Normal": 145, "Fire": 208, "Water": 33, "Electric": 220,
"Grass": 40, "Ice": 51, "Fighting": 124, "Poison": 129,
"Ground": 179, "Flying": 111, "Psychic": 198, "Bug": 106,
"Rock": 137, "Ghost": 97, "Dragon": 63, "Dark": 240,
"Steel": 152, "Fairy": 218,
}
BOLD = "\033[1m"
DIM = "\033[2m"
CLEAR_LINE = "\033[K"
UP_LINES = "\033[{0}A"
HOME = "\033[H"
CLEAR_DOWN = "\033[J"
LOG_LINES = 8
SCREEN_WIDTH = 46
BEAT = 0.55
FRAME = 0.035
DRAIN_STEPS = 12
RESET = "\033[0m"
CURSOR = ">"
HEALTHY = 0.5
HURT = 0.2
UP = "\x1b[A"
DOWN = "\x1b[B"
ENTER = ("\r", "\n")
QUIT = ("q", "Q", "\x03", "\x04")
FIRST_NUMBER = 1
LAST_NUMBER = 1025
NAME_WIDTH = 13
ELEMENT_WIDTH = 16
TYPES = ("Fire", "Water", "Grass")
FAMILIES = {"Fire": "Flame", "Water": "Aqua", "Grass": "Flora"}
GENERATIONS = 9
class Sides(typing.NamedTuple):
"""Who sits behind each half of the screen, and how a win reads."""
player: str
rival: str
player_wins: str
rival_wins: str
SOLO = Sides("", "", "You win!", "You lose.")
DUO = Sides("Player 1", "Player 2",
"Player 1 wins!", "Player 2 wins!")
def read_key() -> str:
if not sys.stdin.isatty():
line = sys.stdin.readline()
if not line:
raise KeyboardInterrupt
return line.rstrip("\n")
handle = sys.stdin.fileno()
saved = termios.tcgetattr(handle)
try:
tty.setcbreak(handle)
key = sys.stdin.read(1)
if key == "\x1b":
key += sys.stdin.read(2)
finally:
termios.tcsetattr(handle, termios.TCSADRAIN, saved)
return key
def pick(title: str, options: list[str]) -> int:
chosen = 0
height = len(options) + 2
redraw = sys.stdin.isatty()
drawn = False
print()
while True:
if drawn and redraw:
print(UP_LINES.format(height), end="")
drawn = True
print(f"{BOLD}{title}{RESET}{CLEAR_LINE}")
for index, option in enumerate(options):
mark = f"{BOLD}{CURSOR}{RESET}" if index == chosen else " "
print(f" {mark} {option}{CLEAR_LINE}")
hint = (f"up/down + enter, press 1-{len(options)},"
f" or q to quit")
print(f"{DIM} {hint}{RESET}{CLEAR_LINE}")
key = read_key()
if key in QUIT:
raise KeyboardInterrupt
if key == UP:
chosen = (chosen - 1) % len(options)
elif key == DOWN:
chosen = (chosen + 1) % len(options)
elif key in ENTER:
return chosen
elif key.isdigit() and 1 <= int(key) <= len(options):
return int(key) - 1
elif key.strip() == "":
return chosen
def tag(element: str, width: int = 0) -> str:
code = TYPE_COLOURS.get(element, TYPE_COLOURS["Normal"])
return f"{TINT.format(code)}{element.upper():<{width}}{RESET}"
def tags(creature: Creature) -> str:
return " ".join(tag(element) for element in creature.elements)
def tags_text(creature: Creature) -> str:
return " ".join(element.upper() for element in creature.elements)
def beat(seconds: float = BEAT) -> None:
if sys.stdin.isatty():
time.sleep(seconds)
def counter(creature: Creature, value: int) -> str:
"""Fixed width, so the bar beside it never shifts column."""
width = len(str(creature.max_hp))
return f"{value:>{width}}/{creature.max_hp}"
def bar_text(creature: Creature) -> str:
return f"{FULL * BAR_WIDTH} {counter(creature, creature.max_hp)}"
def right(plain: str, coloured: str) -> str:
pad = max(0, SCREEN_WIDTH - len(plain))
return " " * pad + coloured
def bar(creature: Creature, shown: int | None = None) -> str:
value = creature.current_hp if shown is None else shown
ratio = value / creature.max_hp
filled = int(ratio * BAR_WIDTH)
if ratio > HEALTHY:
colour = GREEN
elif ratio > HURT:
colour = YELLOW
else:
colour = RED
drawn = FULL * filled + EMPTY * (BAR_WIDTH - filled)
return f"{colour}{drawn}{RESET} {counter(creature, value)}"
def side_label(side: str) -> str:
return f"{side} " if side else ""
def frame(player: Creature, rival: Creature, log: list[str],
show_player: int | None, show_rival: int | None,
sides: Sides = SOLO) -> list[str]:
top = side_label(sides.rival)
bottom = side_label(sides.player)
head = f"{top}{rival.name} Lv.{rival.level} {tags_text(rival)}"
lines = [
f"{BOLD}=== POKEMON BATTLE ==={RESET}",
"",
right(head, f"{DIM}{top}{RESET}{BOLD}{rival.name}{RESET}"
f" Lv.{rival.level} {tags(rival)}"),
right(bar_text(rival), bar(rival, show_rival)),
"",
f"{DIM}{bottom}{RESET}{BOLD}{player.name}{RESET}"
f" Lv.{player.level} {tags(player)}",
f"{bar(player, show_player)}",
f"{DIM}{'-' * SCREEN_WIDTH}{RESET}",
]
recent = log[-LOG_LINES:]
for line in recent:
lines.append(f" {line}")
for _ in range(LOG_LINES - len(recent)):
lines.append("")
return lines
def render(player: Creature, rival: Creature, log: list[str],
show_player: int | None = None,
show_rival: int | None = None,
sides: Sides = SOLO) -> None:
lines = frame(player, rival, log, show_player, show_rival, sides)
if not sys.stdin.isatty():
print("\n".join(lines))
return
body = "\n".join(line + CLEAR_LINE for line in lines)
sys.stdout.write(HOME + body + "\n" + CLEAR_DOWN)
sys.stdout.flush()
def ask_number() -> int:
while True:
print(f"\n{BOLD}Name or number{RESET}"
f" {DIM}({FIRST_NUMBER}-{LAST_NUMBER}){RESET}: ", end="")
sys.stdout.flush()
answer = sys.stdin.readline().strip()
if answer in QUIT:
raise KeyboardInterrupt
if answer.isdigit():
number = int(answer)
if FIRST_NUMBER <= number <= LAST_NUMBER:
return number
for entry in POKEDEX.values():
if entry.name.lower() == answer.lower():
return entry.number
print(f"{RED}No such pokemon.{RESET}")
def previous_form(pokemon: Pokemon) -> Pokemon | None:
for entry in POKEDEX.values():
if entry.evolves_into is pokemon:
return entry
return None
def chain(pokemon: Pokemon) -> list[Pokemon]:
line = [pokemon]
while line[-1].evolves and line[-1].evolves_into is not None:
line.append(line[-1].evolves_into)
return line
def stage_note(before: Pokemon) -> str:
if before.evolves_at:
return f"from Lv.{before.evolves_at}"
return "evolved form"
def stage_labels(line: list[Pokemon]) -> list[str]:
labels = []
for index, form in enumerate(line):
plain = " ".join(form.elements).upper()
pad = " " * max(0, ELEMENT_WIDTH - len(plain))
coloured = " ".join(tag(part) for part in form.elements)
note = stage_note(line[index - 1]) if index else "first form"
labels.append(f"{form.name:<{NAME_WIDTH}}{coloured}{pad}"
f"{DIM}{note}{RESET}")
return labels
def starter_base(element: str, generation: int) -> Pokemon:
family = FAMILIES[element]
for entry in POKEDEX.values():
if entry.family == family and entry.generation == generation:
if entry.starter and entry.evolves:
if previous_form(entry) is None:
return entry
return POKEDEX[FIRST_NUMBER]
def ask_starter() -> Pokemon:
element = TYPES[pick("Which type?", list(TYPES))]
labels = [f"Generation {n}" for n in range(1, GENERATIONS + 1)]
generation = pick("Which generation?", labels) + 1
line = chain(starter_base(element, generation))
return line[pick("Which form?", stage_labels(line))]
def choose_level() -> int | None:
index = pick("Battle level",
[f"Lv.{LEVEL}", f"Lv.{HALF_LEVEL}",
"Random (each side rolls its own)"])
if index == 0:
return LEVEL
if index == 1:
return HALF_LEVEL
return None
def final_form(pokemon: Pokemon) -> Pokemon:
return chain(pokemon)[-1]
def stage_creature(pokemon: Pokemon, level: int | None) -> Creature:
"""Build this exact form, on the level band its stage lives in."""
before = previous_form(pokemon)
if before is None:
return PokedexFactory(pokemon.number, level).create_base()
return PokedexFactory(before.number, level).create_evolved()
def build_creature(pokemon: Pokemon, level: int | None) -> Creature:
rolled = stage_creature(pokemon, level)
return VersatileCreature(pokemon, rolled.level, rolled.moves)
def candidates(effect: str, level: int | None) -> list[Pokemon]:
found = []
for entry in POKEDEX.values():
grown = final_form(entry)
moves = known_moves(grown, level or LEVEL)
has_effect = any(m.effect == effect for m in moves)
can_hit = any(m.effect == "DAMAGE" for m in moves)
if has_effect and can_hit:
found.append(grown)
return found
def choose_pokemon(title: str = "Choose your pokemon") -> Pokemon:
index = pick(title, ["From the whole pokedex",
"From the starters",
"Surprise me"])
if index == 0:
return POKEDEX[ask_number()]
if index == 1:
return ask_starter()
return final_form(POKEDEX[between(FIRST_NUMBER, LAST_NUMBER)])
def choose_rival(level: int | None
) -> tuple[Creature, BattleStrategy]:
index = pick("What strategy should your opponent use?",
["Easy", "Normal", "Aggressive", "Defensive"])
if index == 0:
number = between(FIRST_NUMBER, LAST_NUMBER)
return PokedexFactory(number, level).create_final(), EasyStrategy()
if index == 2:
pool = candidates("BOOST", level)
return (build_creature(pool[between(0, len(pool) - 1)], level),
AggressiveStrategy())
if index == 3:
pool = candidates("HEAL", level)
return (build_creature(pool[between(0, len(pool) - 1)], level),
DefensiveStrategy())
number = between(FIRST_NUMBER, LAST_NUMBER)
plain = PokedexFactory(number, level)
return plain.create_final(), NormalStrategy()
KINDS = {
"DAMAGE": ("ATTACK", RESET),
"HEAL": ("HEAL", GREEN),
"BOOST": ("BOOST", YELLOW),
"GUARD": ("GUARD", CYAN),
"PROTECT": ("PROTECT", CYAN),
"NONE": ("-", DIM),
}
def move_labels(creature: Creature) -> list[str]:
labels = []
for move in creature.moves:
left = creature.pp[move.name]
kind, tint = KINDS[move.effect]
shade = RESET if left else RED
power = f"{move.power:>3}" if move.power else " -"
labels.append(f"{move.name:<16}"
f"{tag(move.element, 9)}"
f"{tint}{kind:<8}{RESET}"
f"{DIM}PWR{RESET} {power} "
f"{shade}PP {left:>2}/{move.pp:<2}{RESET}")
return labels
def choose_move(creature: Creature, side: str = "") -> Move | None:
names = move_labels(creature)
if not names:
return None
who = f"{side} - {creature.name}" if side else creature.name
choice = pick(f"{who}, choose a move", names)
return creature.moves[choice]
def human_turn(attacker: Creature, defender: Creature,
move: Move | None) -> list[str]:
if move is None:
return [attacker.strike(defender, STRUGGLE)]
if not attacker.has_pp(move):
if any(attacker.has_pp(other) for other in attacker.moves):
return [f"{move.name} has no PP left!"]
return [attacker.strike(defender, STRUGGLE)]
if move.effect == "HEAL" and hasattr(attacker, "heal"):
return [typing.cast(Healer, attacker).heal()]
if move.effect in ("GUARD", "PROTECT") and hasattr(attacker, "guard"):
return [typing.cast(Defender, attacker).guard()]
if move.effect == "BOOST" and hasattr(attacker, "transform"):
return [typing.cast(Transformer, attacker).transform()]
if not move.power:
attacker.spend(move)
return [f"{attacker.name} uses {move.name}!"
f" But nothing happened!"]
return [attacker.strike(defender, move)]
def rival_turn(strategy: BattleStrategy, rival: Creature,
player: Creature) -> list[str]:
return strategy.act(rival, player)
def drain(player: Creature, rival: Creature, log: list[str],
was_player: int, was_rival: int,
sides: Sides = SOLO) -> None:
if not sys.stdin.isatty():
return
for step in range(1, DRAIN_STEPS + 1):
share = step / DRAIN_STEPS
now_p = was_player + int((player.current_hp - was_player) * share)
now_r = was_rival + int((rival.current_hp - was_rival) * share)
render(player, rival, log, now_p, now_r, sides)
time.sleep(FRAME)
def resolve(player: Creature, rival: Creature, log: list[str],
lines: list[str], was_player: int, was_rival: int,
sides: Sides = SOLO) -> None:
for line in unfold(lines):
log.append(line)
render(player, rival, log, was_player, was_rival, sides)
beat()
drain(player, rival, log, was_player, was_rival, sides)
def unfold(lines: list[str]) -> list[str]:
out = []
for line in lines:
out.extend(line.splitlines())
return out
def turn_lines(player: Creature, rival: Creature, is_player: bool,
move: Move | None, counter: Move | None,
strategy: BattleStrategy | None) -> list[str]:
if is_player:
return human_turn(player, rival, move)
if strategy is not None:
return rival_turn(strategy, rival, player)
return human_turn(rival, player, counter)
def battle(player: Creature, rival: Creature,
strategy: BattleStrategy | None,
sides: Sides = SOLO) -> None:
log: list[str] = []
faster = player.speed_stat >= rival.speed_stat
while True:
render(player, rival, log, sides=sides)
move = choose_move(player, sides.player)
counter = None
if strategy is None:
counter = choose_move(rival, sides.rival)
if faster:
order = (True, False)
else:
order = (False, True)
for is_player in order:
was_p, was_r = player.current_hp, rival.current_hp
lines = turn_lines(player, rival, is_player,
move, counter, strategy)
resolve(player, rival, log, lines, was_p, was_r, sides)
if rival.fainted():
log.append(f"{GREEN}{rival.name} fainted."
f" {sides.player_wins}{RESET}")
render(player, rival, log, sides=sides)
return
if player.fainted():
log.append(f"{RED}{player.name} fainted."
f" {sides.rival_wins}{RESET}")
render(player, rival, log, sides=sides)
return
def rematch(player: Creature, rival: Creature,
strategy: BattleStrategy | None, sides: Sides) -> None:
"""The same two creatures, restored, for as long as they want."""
while True:
battle(player, rival, strategy, sides)
if pick("Want to replay this battle?", ["Yes", "No"]) == 1:
print(f"\n{DIM}Got away safely!{RESET}")
return
player.restore()
rival.restore()
def ready() -> bool:
if pick("Are you ready to battle?", ["Yes", "No"]) == 1:
print("Maybe next time.")
return False
return True
def single_player() -> None:
pokemon = choose_pokemon()
level = choose_level()
player = build_creature(pokemon, level)
rival, strategy = choose_rival(level)
print(f"\nYou chose {BOLD}{player.name}{RESET}.")
print(f"Your opponent is {BOLD}{rival.name}{RESET}.")
if ready():
rematch(player, rival, strategy, SOLO)
def two_players() -> None:
first = choose_pokemon(f"{DUO.player}, choose your pokemon")
second = choose_pokemon(f"{DUO.rival}, choose your pokemon")
level = choose_level()
player = build_creature(first, level)
rival = build_creature(second, level)
print(f"\n{DUO.player} sends out {BOLD}{player.name}{RESET}.")
print(f"{DUO.rival} sends out {BOLD}{rival.name}{RESET}.")
if ready():
rematch(player, rival, None, DUO)
def main() -> None:
seed(time.time_ns())
print(f"{BOLD}=== POKEMON BATTLE ==={RESET}")
mode = pick("How many players?",
["Single player (you against a strategy)",
"Two players (same terminal)"])
if mode == 0:
single_player()
else:
two_players()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print(f"\n{DIM}Got away safely!{RESET}")