Added auto-regeneration of captcha when complete

This shit starting to look like a game lads
This commit is contained in:
2025-06-30 14:43:15 +01:00
parent e549107fe1
commit 31b863b49b

21
game.py
View File

@ -10,6 +10,10 @@ import json
import time import time
import os import os
# TODO: The current alphabets in the fonts.json file is kinda bullshit
# someone should probably play some test rounds and fix the diffuculty issue.
# TODO: Re-draw the captcha when a character is complete.
alphabet = string.ascii_letters + string.digits + string.punctuation alphabet = string.ascii_letters + string.digits + string.punctuation
def normalDist(difficulty): def normalDist(difficulty):
@ -36,6 +40,7 @@ def PIL2PG(pilImage: Image) -> pygame.Surface:
class Font: class Font:
def __init__(self, data, fontSize): def __init__(self, data, fontSize):
self.path = data['file']
self.font = ImageFont.truetype(os.path.join('./fonts/', data['file']), fontSize) self.font = ImageFont.truetype(os.path.join('./fonts/', data['file']), fontSize)
self.alphabet = data['alphabet'] self.alphabet = data['alphabet']
self.difficulty = data['difficulty'] self.difficulty = data['difficulty']
@ -56,13 +61,16 @@ class Captcha:
self.fonts.append(font) self.fonts.append(font)
def match(self, char): def match(self, char):
if self.charIdx == len(self.chars):
return True, True
matches = char.upper() == self.chars[self.charIdx] matches = char.upper() == self.chars[self.charIdx]
print(f'{char.upper()} == {self.chars[self.charIdx]}') print(f'{char.upper()} == {self.chars[self.charIdx]}')
if matches: if matches:
self.charIdx += 1 self.charIdx += 1
return True return True, self.charIdx == len(self.chars)
else: else:
return False return False, self.charIdx == len(self.chars)
class Game(gameUtils.Game): class Game(gameUtils.Game):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -145,7 +153,8 @@ class Game(gameUtils.Game):
# Store the created image # Store the created image
self.currentCaptchaImg = PIL2PG(drawSurface) self.currentCaptchaImg = PIL2PG(drawSurface)
print(captcha) for char, font in zip(captcha.chars, captcha.fonts):
print(f'{char}: {font.path}')
def onEvent(self, event): def onEvent(self, event):
""" """
@ -163,8 +172,12 @@ class Game(gameUtils.Game):
print(self.gameDifficulty) print(self.gameDifficulty)
elif event.key == pygame.K_F3: elif event.key == pygame.K_F3:
self.createCaptcha() self.createCaptcha()
elif event.unicode != '' and event.unicode in alphabet: elif event.unicode != '' and event.unicode in alphabet:
print(self.currentCaptcha.match(event.unicode)) correct, finished = self.currentCaptcha.match(event.unicode)
print(correct)
if finished:
self.createCaptcha()
def update(self): def update(self):
""" """