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
Helvetica
Gill Sans
#Gotham
Gotham
#Myriad Pro
#News Gothic
#Roboto

View File

@ -20,7 +20,7 @@ def pick_fonts(game_length: int):
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)
images = []
for font in picked_fonts:
@ -33,7 +33,8 @@ def start_game(game_length: int = 10) -> str:
"i": "0",
"game_length": str(game_length),
"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()])
@ -48,7 +49,8 @@ def test_game(game_id: str):
def guess_font(game_id: str, font_guess: str):
game_dict = read_file(f"current_games/{game_id}")
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:
game_dict['points'] = str(int(game_dict['points']) + 1)

View File

@ -15,7 +15,7 @@ def gen_image(font: ImageFont.ImageFont) -> str:
text_list = []
line = ""
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)
line = ""
else:
@ -36,7 +36,7 @@ def gen_image(font: ImageFont.ImageFont) -> str:
path = f"static/images/{filename}.png"
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)
return filename

View File

@ -57,7 +57,7 @@ def make_test_pages(fonts: dict[str, ImageFont.FreeTypeFont]):
final_text = []
current_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)
current_text = char
else:
@ -94,7 +94,7 @@ def make_fonts():
font_path = f"./fonts/{font_name}.{file_type}"
tmp_font = ImageFont.truetype(font_path, FONT_SIZE)
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_font = ImageFont.truetype(font_path, new_size)

View File

@ -32,7 +32,17 @@ def font_game():
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}")
return flask.render_template("final.html", points=game['points'],
game_length=game['game_length'])
@ -45,7 +55,8 @@ def font_game():
"url": url,
"i": str(int(game['i']) + 1),
"round_n": game['i'],
"game_length": game['game_length'],
"game_length": int(game['game_length']),
"hard_mode": game['hard_mode'] == 'True',
"points":game['points'],
"fonts":FONTS,
"id":args['id']
@ -63,7 +74,9 @@ def start():
else:
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}")
@app.route("/all")

View File

@ -28,6 +28,23 @@
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 {
width: 50pt;
text-align: right;

View File

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

View File

@ -1,13 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<head>
<link rel="stylesheet" href="/static/css/stylesheet.css">
</head>
<body class="menupage">
<h1>Font Game</h1>
<form method="post" action="/startgame">
<label for="game_length">Game rounds:</label>
<input type="number" name="game_length" min=1 class="number" value=10>
<div>
<label for="game_length">Game rounds:</label>
<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">
</form>
</body>