Created
August 4, 2019 00:02
-
-
Save yjzhang/f75a70f46144f715368f59d2d50c17d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# makes an svg bingo board | |
import svgwrite | |
def text_to_font_size(text): | |
if isinstance(text, str): | |
total_text = text | |
else: | |
total_text = ''.join(text) | |
if len(total_text) < 10: | |
return 18 | |
elif len(total_text) < 20: | |
return 14 | |
else: | |
return 12 | |
def make_bingo_board(text_boxes, filename='board.svg'): | |
board = svgwrite.Drawing(filename=filename, size=('600px', '600px'), profile="full") | |
# draw vertical lines | |
for i in range(6): | |
board.add(board.line((50 + 100*i, 60), (50 + 100*i, 560), stroke='black')) | |
# draw horizontal lines | |
for i in range(6): | |
board.add(board.line((50, 60 + 100*i), (550, 60 + 100*i), stroke='black')) | |
# add free space | |
board.add(board.text(text='Free space', fill='black', insert=(str(50+100*2 + 50), str(60+100*2 + 50)), | |
style="font-size:18;font-family:Comic Sans MS", text_anchor='middle', dominant_baseline='middle')) | |
# insert text | |
import random | |
random.shuffle(text_boxes) | |
for i in range(5): | |
for j in range(5): | |
if i==2 and j==2: | |
continue | |
index = i*5 + j | |
if (i>=2 and j >= 2) or i > 2: | |
index -= 1 | |
if len(text_boxes) > index: | |
text = text_boxes[index] | |
font_size = text_to_font_size(text) | |
if isinstance(text, str): | |
board.add(board.text(text=text, fill='black', insert=(str(50+100*i + 50), str(60+100*j + 50)), | |
style="font-size:{0};font-family:Comic Sans MS".format(font_size), text_anchor='middle', dominant_baseline='middle')) | |
elif isinstance(text, list): | |
for text_index, t in enumerate(text): | |
board.add(board.text(text=t, fill='black', insert=(str(50+100*i + 50), str(60+100*j + 25 + (font_size+7)*text_index)), | |
style="font-size:{0};font-family:Comic Sans MS".format(font_size), text_anchor='middle', dominant_baseline='middle')) | |
board.save() | |
return board |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment