This commit is contained in:
2024-05-24 10:09:50 +02:00
parent cfe3d3f329
commit c390a3eb9a
8 changed files with 68 additions and 23 deletions

View File

@ -8,7 +8,7 @@ Franklin Gothic
Frutiger Frutiger
Helvetica Helvetica
Gill Sans Gill Sans
#Gotham Gotham
#Myriad Pro #Myriad Pro
#News Gothic #News Gothic
#Roboto #Roboto

View File

@ -20,7 +20,7 @@ def pick_fonts(game_length: int):
return [(i, FONTS[i]) for i in font_list] return [(i, FONTS[i]) for i in font_list]
def start_game(game_length: int = 10) -> str: def start_game(game_length: int = 10, hard_mode: bool = False) -> str:
picked_fonts = pick_fonts(game_length) picked_fonts = pick_fonts(game_length)
images = [] images = []
for font in picked_fonts: for font in picked_fonts:
@ -33,7 +33,8 @@ def start_game(game_length: int = 10) -> str:
"i": "0", "i": "0",
"game_length": str(game_length), "game_length": str(game_length),
"images": ','.join(images), "images": ','.join(images),
"fonts": ','.join(i[0] for i in picked_fonts) "fonts": ','.join(i[0] for i in picked_fonts),
"hard_mode": str(hard_mode)
} }
game = "\n".join([f"{key}:{value}" for key, value in game_dict.items()]) game = "\n".join([f"{key}:{value}" for key, value in game_dict.items()])
@ -48,7 +49,8 @@ def test_game(game_id: str):
def guess_font(game_id: str, font_guess: str): def guess_font(game_id: str, font_guess: str):
game_dict = read_file(f"current_games/{game_id}") game_dict = read_file(f"current_games/{game_id}")
i = int(game_dict['i']) i = int(game_dict['i'])
font = game_dict['fonts'].split(',')[i] font_guess = font_guess.lower().replace(" ","-")
font = game_dict['fonts'].split(',')[i].lower().replace(" ","-")
if font_guess == font: if font_guess == font:
game_dict['points'] = str(int(game_dict['points']) + 1) game_dict['points'] = str(int(game_dict['points']) + 1)

View File

@ -15,7 +15,7 @@ def gen_image(font: ImageFont.ImageFont) -> str:
text_list = [] text_list = []
line = "" line = ""
for word in text.split(" "): for word in text.split(" "):
if drawer.textsize(line + word, font)[0] > text_width: if drawer.textlength(line + word, font) > text_width:
text_list.append(line) text_list.append(line)
line = "" line = ""
else: else:
@ -36,7 +36,7 @@ def gen_image(font: ImageFont.ImageFont) -> str:
path = f"static/images/{filename}.png" path = f"static/images/{filename}.png"
new_size = (IMAGE_SIZE[0] // 2, IMAGE_SIZE[1] // 2) new_size = (IMAGE_SIZE[0] // 2, IMAGE_SIZE[1] // 2)
img = img.resize(new_size, resample=Image.ANTIALIAS) img = img.resize(new_size)
img.save(path) img.save(path)
return filename return filename

View File

@ -57,7 +57,7 @@ def make_test_pages(fonts: dict[str, ImageFont.FreeTypeFont]):
final_text = [] final_text = []
current_text = "" current_text = ""
for char in TEST_TEXT: for char in TEST_TEXT:
if font.getsize(current_text + char)[0] > (writable_area): if font.getbbox(current_text + char)[2] > (writable_area):
final_text.append(current_text) final_text.append(current_text)
current_text = char current_text = char
else: else:
@ -94,7 +94,7 @@ def make_fonts():
font_path = f"./fonts/{font_name}.{file_type}" font_path = f"./fonts/{font_name}.{file_type}"
tmp_font = ImageFont.truetype(font_path, FONT_SIZE) tmp_font = ImageFont.truetype(font_path, FONT_SIZE)
letter_height = int(average([ letter_height = int(average([
tmp_font.getsize(i)[1] for i in TEST_TEXT tmp_font.getbbox(i)[3] for i in TEST_TEXT
])) ]))
new_size = (FONT_SIZE**2)//letter_height new_size = (FONT_SIZE**2)//letter_height
new_font = ImageFont.truetype(font_path, new_size) new_font = ImageFont.truetype(font_path, new_size)

View File

@ -32,7 +32,17 @@ def font_game():
game = read_file(f"current_games/{game_id}") game = read_file(f"current_games/{game_id}")
if int(game['i']) == int(game['game_length']): end_game = (
(
int(game['game_length']) != 0 and
int(game['i']) == int(game['game_length'])
) or
(
int(game['game_length']) == 0 and
int(game['i']) > int(game['points'])
)
)
if end_game:
os.remove(f"current_games/{game_id}") os.remove(f"current_games/{game_id}")
return flask.render_template("final.html", points=game['points'], return flask.render_template("final.html", points=game['points'],
game_length=game['game_length']) game_length=game['game_length'])
@ -45,7 +55,8 @@ def font_game():
"url": url, "url": url,
"i": str(int(game['i']) + 1), "i": str(int(game['i']) + 1),
"round_n": game['i'], "round_n": game['i'],
"game_length": game['game_length'], "game_length": int(game['game_length']),
"hard_mode": game['hard_mode'] == 'True',
"points":game['points'], "points":game['points'],
"fonts":FONTS, "fonts":FONTS,
"id":args['id'] "id":args['id']
@ -63,7 +74,9 @@ def start():
else: else:
game_length = 10 game_length = 10
game_id = start_game(game_length) hard_mode = 'hard_mode' in flask.request.form
game_id = start_game(game_length, hard_mode)
return flask.redirect(f"/fontgame?id={game_id}") return flask.redirect(f"/fontgame?id={game_id}")
@app.route("/all") @app.route("/all")

View File

@ -28,6 +28,23 @@
font-size: 20pt; font-size: 20pt;
} }
.menupage form {
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
align-items: center;
justify-items: center;
}
.menupage form p {
margin-top: -10pt;
margin-bottom: 10pt;
}
.menupage form div {
margin-bottom: 10pt;
}
.menupage form .number { .menupage form .number {
width: 50pt; width: 50pt;
text-align: right; text-align: right;

View File

@ -7,19 +7,25 @@
<img src="{{ url }}" > <img src="{{ url }}" >
<div class="right"> <div class="right">
<div class="stats"> <div class="stats">
{% if game_length > 0 %}
<div class="rounds"> <div class="rounds">
Round {{ i }} of {{ game_length }} Round {{ i }} of {{ game_length }}
</div> </div>
{% endif %}
<div class="points"> <div class="points">
{{ points }}/{{ round_n }} points. {{ points }}/{{ round_n }} points.
</div> </div>
</div> </div>
<form method="post" action="/fontgame?id={{ id }}"> <form method="post" action="/fontgame?id={{ id }}" autocomplete="off">
<select name="font"> {% if not hard_mode %}
<select name="font" autofocus>
{% for font in fonts %} {% for font in fonts %}
<option>{{ font }}</option> <option>{{ font }}</option>
{% endfor %} {% endfor %}
</select> </select>
{% else %}
<input type="text" name="font" autofocus>
{% endif %}
<input type="hidden" name="id" value="{{ id }}"> <input type="hidden" name="id" value="{{ id }}">
<input type="submit" value="Submit"> <input type="submit" value="Submit">
</form> </form>

View File

@ -6,8 +6,15 @@
<body class="menupage"> <body class="menupage">
<h1>Font Game</h1> <h1>Font Game</h1>
<form method="post" action="/startgame"> <form method="post" action="/startgame">
<div>
<label for="game_length">Game rounds:</label> <label for="game_length">Game rounds:</label>
<input type="number" name="game_length" min=1 class="number" value=10> <input type="number" name="game_length" min=0 class="number" value=10>
</div>
<p>(0 for endless mode)</p>
<div>
<label for="hard_mode">Hard mode:</label>
<input type="checkbox" name="hard_mode" class="checkbox">
</div>
<input type="submit" value="Start"> <input type="submit" value="Start">
</form> </form>
</body> </body>