-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheightball_print.py
More file actions
72 lines (58 loc) · 2.65 KB
/
Copy patheightball_print.py
File metadata and controls
72 lines (58 loc) · 2.65 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
#!/usr/bin/env python3
"""Print a Magic 8-Ball answer on the S01 thermal printer.
Ask a yes/no question and the 8-ball gives one of its classic 20 answers.
Examples:
python eightball_print.py --question "Will it rain tomorrow?"
python eightball_print.py
python eightball_print.py --no-print
"""
from __future__ import annotations
import argparse
from pathlib import Path
import random
from PIL import Image, ImageDraw
from print_common import ROOT, Card, font
ANSWERS = [
"It is certain.", "It is decidedly so.", "Without a doubt.", "Yes definitely.",
"You may rely on it.", "As I see it, yes.", "Most likely.", "Outlook good.",
"Yes.", "Signs point to yes.",
"Reply hazy, try again.", "Ask again later.", "Better not tell you now.",
"Cannot predict now.", "Concentrate and ask again.",
"Don't count on it.", "My reply is no.", "My sources say no.",
"Outlook not so good.", "Very doubtful.",
]
def draw_ball(size: int = 150) -> Image.Image:
img = Image.new("L", (size, size), 255)
d = ImageDraw.Draw(img)
cx = cy = size / 2
r = size / 2 - 4
d.ellipse((cx - r, cy - r, cx + r, cy + r), fill=0) # the ball
wr = r * 0.30
wy = cy - r * 0.38
d.ellipse((cx - wr, wy - wr, cx + wr, wy + wr), fill=255) # white "8" window
d.text((cx, wy), "8", fill=0, font=font(True, int(wr * 1.5)), anchor="mm")
return img
def main() -> int:
parser = argparse.ArgumentParser(description="Print a Magic 8-Ball answer on the S01 thermal printer.")
parser.add_argument("--question", default=None, help="Your yes/no question.")
parser.add_argument("--seed", type=int, default=None)
parser.add_argument("--out", type=Path, default=ROOT / "eightball.png")
parser.add_argument("--darkness", type=int, choices=range(1, 6), default=3)
parser.add_argument("--bottom-feed", type=int, default=24)
parser.add_argument("--no-print", action="store_true")
args = parser.parse_args()
answer = random.Random(args.seed).choice(ANSWERS)
print(f"8-ball: {answer}")
card = Card()
card.title("MAGIC 8-BALL")
if args.question:
q = args.question if args.question.endswith("?") else args.question + "?"
card.gap(2).para(f"\"{q}\"", size=14, bold=False, center=True)
card.gap(4).image(draw_ball(), border=False)
card.gap(4).line("THE BALL SAYS", size=12, bold=False, center=True)
card.gap(2).divider()
card.para(answer, size=20, bold=True, center=True)
card.divider().footer("shake to ask again", "magic 8-ball")
return card.finish(args.out, args.bottom_feed, args.darkness, do_print=not args.no_print)
if __name__ == "__main__":
raise SystemExit(main())