diff --git a/game.py b/game.py index 020f27c..a2ae21b 100644 --- a/game.py +++ b/game.py @@ -5,10 +5,13 @@ import pygame import gameUtils import NoPELib import random +import string import json import time import os +alphabet = string.ascii_letters + string.digits + string.punctuation + def normalDist(difficulty): """ This function generates a random number between 0-1 @@ -37,6 +40,30 @@ class Font: self.alphabet = data['alphabet'] 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): 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 self.gameDifficulty = 0 # The actual text in the captcha, used to check what the user typed - self.currentCaptchaText = '' + self.currentCaptcha = None # The image of the captcha self.currentCaptchaImg = pygame.Surface((0, 0)) @@ -93,7 +120,7 @@ class Game(gameUtils.Game): draw = ImageDraw.Draw(drawSurface) # Temporarily stores the current captcha text - captcha = '' + captcha = Captcha() # Get a random cpatcha length capLength = random.randint(3, 6) for i in range(capLength): @@ -110,10 +137,10 @@ class Game(gameUtils.Game): # Draw the selected character, with the selected font draw.text((x, 10), char, font=font.font, fill=(0,0,0)) # Append the character to the captcha - captcha += char + captcha.addChar(char, font) # Store the correct captcha text - self.currentCaptchaText = captcha + self.currentCaptcha = captcha # Store the created image self.currentCaptchaImg = PIL2PG(drawSurface) @@ -128,13 +155,16 @@ class Game(gameUtils.Game): event (pygame.Event): The event that was fired. """ if event.type == pygame.KEYDOWN: - if event.key == pygame.K_1: + if event.key == pygame.K_F1: self.gameDifficulty -= 0.1 - elif event.key == pygame.K_2: + print(self.gameDifficulty) + elif event.key == pygame.K_F2: self.gameDifficulty += 0.1 - elif event.key == pygame.K_3: + print(self.gameDifficulty) + elif event.key == pygame.K_F3: self.createCaptcha() - print(self.gameDifficulty) + elif event.unicode != '' and event.unicode in alphabet: + print(self.currentCaptcha.match(event.unicode)) def update(self): """