Added basic implementation of Captcha class

This commit is contained in:
2025-06-30 12:24:09 +01:00
parent 220f5759e9
commit e549107fe1

46
game.py
View File

@ -5,10 +5,13 @@ import pygame
import gameUtils import gameUtils
import NoPELib import NoPELib
import random import random
import string
import json import json
import time import time
import os import os
alphabet = string.ascii_letters + string.digits + string.punctuation
def normalDist(difficulty): def normalDist(difficulty):
""" """
This function generates a random number between 0-1 This function generates a random number between 0-1
@ -37,6 +40,30 @@ class Font:
self.alphabet = data['alphabet'] self.alphabet = data['alphabet']
self.difficulty = data['difficulty'] self.difficulty = data['difficulty']
class Captcha:
"""
Holds and manages the current captcha.
"""
def __init__(self):
self.chars = []
self.fonts = []
self.charIdx = 0 # Holds which character the player is on,
# anthing below this index should appear green.
def addChar(self, char, font):
self.chars.append(char.upper())
self.fonts.append(font)
def match(self, char):
matches = char.upper() == self.chars[self.charIdx]
print(f'{char.upper()} == {self.chars[self.charIdx]}')
if matches:
self.charIdx += 1
return True
else:
return False
class Game(gameUtils.Game): class Game(gameUtils.Game):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
""" """
@ -54,7 +81,7 @@ class Game(gameUtils.Game):
# A value between 0-1 that defines which fonts the game will use # A value between 0-1 that defines which fonts the game will use
self.gameDifficulty = 0 self.gameDifficulty = 0
# The actual text in the captcha, used to check what the user typed # The actual text in the captcha, used to check what the user typed
self.currentCaptchaText = '' self.currentCaptcha = None
# The image of the captcha # The image of the captcha
self.currentCaptchaImg = pygame.Surface((0, 0)) self.currentCaptchaImg = pygame.Surface((0, 0))
@ -93,7 +120,7 @@ class Game(gameUtils.Game):
draw = ImageDraw.Draw(drawSurface) draw = ImageDraw.Draw(drawSurface)
# Temporarily stores the current captcha text # Temporarily stores the current captcha text
captcha = '' captcha = Captcha()
# Get a random cpatcha length # Get a random cpatcha length
capLength = random.randint(3, 6) capLength = random.randint(3, 6)
for i in range(capLength): for i in range(capLength):
@ -110,10 +137,10 @@ class Game(gameUtils.Game):
# Draw the selected character, with the selected font # Draw the selected character, with the selected font
draw.text((x, 10), char, font=font.font, fill=(0,0,0)) draw.text((x, 10), char, font=font.font, fill=(0,0,0))
# Append the character to the captcha # Append the character to the captcha
captcha += char captcha.addChar(char, font)
# Store the correct captcha text # Store the correct captcha text
self.currentCaptchaText = captcha self.currentCaptcha = captcha
# Store the created image # Store the created image
self.currentCaptchaImg = PIL2PG(drawSurface) self.currentCaptchaImg = PIL2PG(drawSurface)
@ -128,13 +155,16 @@ class Game(gameUtils.Game):
event (pygame.Event): The event that was fired. event (pygame.Event): The event that was fired.
""" """
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1: if event.key == pygame.K_F1:
self.gameDifficulty -= 0.1 self.gameDifficulty -= 0.1
elif event.key == pygame.K_2: print(self.gameDifficulty)
elif event.key == pygame.K_F2:
self.gameDifficulty += 0.1 self.gameDifficulty += 0.1
elif event.key == pygame.K_3: print(self.gameDifficulty)
elif event.key == pygame.K_F3:
self.createCaptcha() self.createCaptcha()
print(self.gameDifficulty) elif event.unicode != '' and event.unicode in alphabet:
print(self.currentCaptcha.match(event.unicode))
def update(self): def update(self):
""" """