Added basic implementation of createCaptcha(), Needs to be cleaned up

This commit is contained in:
2025-06-27 07:18:11 +01:00
parent ce771d26a3
commit 904601c697

48
game.py
View File

@ -1,10 +1,13 @@
from PIL import Image, ImageDraw, ImageFont
import math as maths
import logging
import pygame
import gameUtils
import NoPELib
import random
import json
import time
import os
def normalDist(difficulty):
"""
@ -22,6 +25,12 @@ def normalDist(difficulty):
# Cap it to be 0-1 again and return
return min(max(v, 0), 1)
class Font:
def __init__(self, data, fontSize):
self.font = ImageFont.truetype(os.path.join('./fonts/', data['file']), fontSize)
self.alphabet = data['alphabet']
self.difficulty = data['difficulty']
class Game(gameUtils.Game):
def __init__(self, *args, **kwargs):
"""
@ -43,24 +52,51 @@ class Game(gameUtils.Game):
# The image of the captcha
self.currentCaptchaImg = ''
self.fonts = {}
self.fonts = []
self.Drawsurface = Image.new("RGB",(800,220),(255,255,255))
# I haven't finished this yet, you might want to create
# an object or something, I don't know. Keep in mind you
# have to load all of the font files for use later.
with open(f'./fonts/fonts.json', 'r') as f:
self.fontList = json.loads(f.read())#loads the fonts from the json (Why the fuck was the variable called 'j' before????)
for fontData in json.loads(f.read()):
self.fonts.append(Font(fontData, 100))
#self.fontList = json.loads(f.read())#loads the fonts from the json (Why the fuck was the variable called 'j' before????)
#for fontData in j:
# self.fonts.update()
def fontsByDifficulty(self, difficulty, width=0.3):
"""
Gets a list of fonts that fall within the specified difficulty.
"""
return [
font for font in self.fonts
if font.difficulty >= difficulty-width and font.difficulty <= difficulty+width
]
def createCaptcha(self):
"""
Use PIL (https://pillow.readthedocs.io/en/stable/handbook/tutorial.html)
to draw the fonts, add noise, random shapes, transforms etc. based on
self.gameDifficulty
"""
pass
self.Drawsurface = Image.new("RGB",(800,220),(255,255,255)) # creates a surface to draw the fonts on
draw = ImageDraw.Draw(self.Drawsurface) # creates an object to let us draw to the surface
captcha = ''
capLength = random.randint(3, 6)
for i in range(capLength):
x = self.size[0] / (capLength + 3)
x *= i
charDiff = normalDist(self.gameDifficulty)
font = random.choice(self.fontsByDifficulty(charDiff))
char = random.choice(font.alphabet)
draw.ellipse((x, 10, x+5, 15), fill = 'blue', outline ='blue')
draw.text((x, 10), char, font=font.font, fill=(0,0,0)) #actually draws the text
captcha += char
print(captcha)
def onEvent(self, event):
"""
@ -87,11 +123,7 @@ class Game(gameUtils.Game):
# Don't remove this. It does important things. :3
super().update()
self.Drawsurface = Image.new("RGB",(800,220),(255,255,255)) #creates a surface to draw the fonts on
self.textTest = ImageFont.truetype("./fonts/"+self.fontList[2]["file"],200) #loads the font
self.draw = ImageDraw.Draw(self.Drawsurface) #creates an object to let us draw to the surface
self.draw.text((10,10),"Hello", font=self.textTest, fill=(0,0,0)) #actually draws the text
#self.textTest = ImageFont.truetype("./fonts/"+self.fonts[2]["file"],200) #loads the font
self.pygameConvert = pygame.image.fromstring(self.Drawsurface.tobytes(), self.Drawsurface.size, self.Drawsurface.mode) #converts the pillow image to a pygame image
#no, there wasn't an easier way I could find to achieve this, stab me