Compare commits
14 Commits
220f5759e9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
3046af13e5
|
|||
| ae8b0c77a5 | |||
| afd1fcd6be | |||
| 3cd2ddbd46 | |||
| f36bce66e2 | |||
| f625c80fd3 | |||
| db7faca3d9 | |||
| 11b1dad78b | |||
| 3471e41272 | |||
| ae6c23b6b7 | |||
| 6e70f74874 | |||
| 172ffc2abf | |||
| 31b863b49b | |||
| e549107fe1 |
@ -5,11 +5,10 @@
|
|||||||
{"file": "Vampire Wars.ttf", "alphabet": "abcdefghijklmnopqrstuvwxyz0123456789", "difficulty": 0.1},
|
{"file": "Vampire Wars.ttf", "alphabet": "abcdefghijklmnopqrstuvwxyz0123456789", "difficulty": 0.1},
|
||||||
{"file": "CloisterBlack.ttf", "alphabet": "0123456789", "difficulty": 0.1},
|
{"file": "CloisterBlack.ttf", "alphabet": "0123456789", "difficulty": 0.1},
|
||||||
{"file": "Noxlock-Free.otf", "alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "difficulty": 0.2},
|
{"file": "Noxlock-Free.otf", "alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "difficulty": 0.2},
|
||||||
{"file": "CloisterBlack.ttf", "alphabet": "abcdefghijklmnopqrstuvwxyz", "difficulty": 0.3},
|
{"file": "CloisterBlack.ttf", "alphabet": "abcdefghijklmnopqrstuvwxyz", "difficulty": 0.5},
|
||||||
{"file": "LEDLIGHT.otf", "alphabet": "abcdefghijklmnopqrstuvwxyz", "difficulty": 0.4},
|
{"file": "LEDLIGHT.otf", "alphabet": "abcdefghijklmnopqrstuvwxyz", "difficulty": 0.4},
|
||||||
{"file": "Flame on Black.ttf", "alphabet": "abcdefghijklmnopqrstuvwxyz0123456789!?/#<>", "difficulty": 0.5},
|
{"file": "Flame on Black.ttf", "alphabet": "0123456789!?/#<>", "difficulty": 0.5},
|
||||||
{"file": "CloisterBlack.ttf", "alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "difficulty": 0.6},
|
{"file": "CloisterBlack.ttf", "alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "difficulty": 0.6},
|
||||||
{"file": "mevno2.ttf", "alphabet": "abcdefghijklmnopqrstuvwxyz", "difficulty": 0.8},
|
|
||||||
{"file": "CloisterBlack.ttf", "alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "difficulty": 0.8},
|
{"file": "CloisterBlack.ttf", "alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "difficulty": 0.8},
|
||||||
{"file": "RoyalInitialen.ttf", "alphabet": "abcdefghijklmnopqrstuvwxyz", "difficulty": 0.8},
|
{"file": "RoyalInitialen.ttf", "alphabet": "abcdefghijklmnopqrstuvwxyz", "difficulty": 0.8},
|
||||||
{"file": "DeathMohawk_PERSONAL_USE_ONLY.otf", "alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "difficulty": 1.0}
|
{"file": "DeathMohawk_PERSONAL_USE_ONLY.otf", "alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "difficulty": 1.0}
|
||||||
|
|||||||
BIN
fonts/mevno2.ttf
BIN
fonts/mevno2.ttf
Binary file not shown.
101
game.py
101
game.py
@ -1,14 +1,22 @@
|
|||||||
from PIL import Image, ImageDraw, ImageFont
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
import math as maths
|
import math as maths
|
||||||
|
import numpy as np
|
||||||
import logging
|
import logging
|
||||||
import pygame
|
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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
def normalDist(difficulty):
|
def normalDist(difficulty):
|
||||||
"""
|
"""
|
||||||
This function generates a random number between 0-1
|
This function generates a random number between 0-1
|
||||||
@ -33,10 +41,38 @@ 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']
|
||||||
|
|
||||||
|
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)
|
||||||
|
self.fonts.append(font)
|
||||||
|
|
||||||
|
def match(self, char):
|
||||||
|
if self.charIdx == len(self.chars):
|
||||||
|
return True, True
|
||||||
|
|
||||||
|
matches = char.upper() == self.chars[self.charIdx].upper()
|
||||||
|
print(f'{char.upper()} == {self.chars[self.charIdx]}')
|
||||||
|
if matches:
|
||||||
|
self.charIdx += 1
|
||||||
|
return True, self.charIdx == len(self.chars)
|
||||||
|
else:
|
||||||
|
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):
|
||||||
"""
|
"""
|
||||||
@ -54,9 +90,17 @@ 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))
|
||||||
|
# Holds when the timer started
|
||||||
|
self.timerStart = 0
|
||||||
|
# Holds how much time the user has in total (not updated)
|
||||||
|
self.timerLength = 0
|
||||||
|
# The font used for the timer
|
||||||
|
self.timerFont = pygame.font.SysFont('', 100)
|
||||||
|
# Used for animating the red flash on the current character if the users enters it wrong.
|
||||||
|
self.lastWrong = 0
|
||||||
|
|
||||||
self.fonts = []
|
self.fonts = []
|
||||||
self.Drawsurface = Image.new("RGB",(800,220),(255,255,255))
|
self.Drawsurface = Image.new("RGB",(800,220),(255,255,255))
|
||||||
@ -93,13 +137,13 @@ 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):
|
||||||
# Calculate the base X position of this character
|
# Calculate the base X position of this character
|
||||||
# TODO: Make this better, I'm not too happy with my implementation here.
|
# TODO: Make this better, I'm not too happy with my implementation here.
|
||||||
x = self.size[0] / (capLength + 3)
|
x = drawSurface.size[0] / capLength
|
||||||
x *= i
|
x *= i
|
||||||
# Get the selected difficulty for this character
|
# Get the selected difficulty for this character
|
||||||
charDiff = normalDist(self.gameDifficulty)
|
charDiff = normalDist(self.gameDifficulty)
|
||||||
@ -110,15 +154,20 @@ 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)
|
||||||
|
|
||||||
print(captcha)
|
for char, font in zip(captcha.chars, captcha.fonts):
|
||||||
|
print(f'{char}: {font.path}')
|
||||||
|
|
||||||
|
# Start the timer
|
||||||
|
self.timerStart = time.perf_counter()
|
||||||
|
self.timerLength = random.randint(7, 15)
|
||||||
|
|
||||||
def onEvent(self, event):
|
def onEvent(self, event):
|
||||||
"""
|
"""
|
||||||
@ -128,13 +177,22 @@ 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:
|
|
||||||
self.gameDifficulty += 0.1
|
|
||||||
elif event.key == pygame.K_3:
|
|
||||||
self.createCaptcha()
|
|
||||||
print(self.gameDifficulty)
|
print(self.gameDifficulty)
|
||||||
|
elif event.key == pygame.K_F2:
|
||||||
|
self.gameDifficulty += 0.1
|
||||||
|
print(self.gameDifficulty)
|
||||||
|
elif event.key == pygame.K_F3:
|
||||||
|
self.createCaptcha()
|
||||||
|
|
||||||
|
elif event.unicode != '' and event.unicode in alphabet:
|
||||||
|
correct, finished = self.currentCaptcha.match(event.unicode)
|
||||||
|
if not correct:
|
||||||
|
self.lastWrong = time.perf_counter()
|
||||||
|
print(correct)
|
||||||
|
if finished:
|
||||||
|
self.createCaptcha()
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""
|
"""
|
||||||
@ -142,10 +200,29 @@ class Game(gameUtils.Game):
|
|||||||
game logic that should be ran once per frame in here.
|
game logic that should be ran once per frame in here.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.surf.fill((0, 0, 0)) #sets the background colour
|
r = 1 - min(time.perf_counter() - self.lastWrong, 1)
|
||||||
|
|
||||||
|
self.surf.fill((r * 255, 0, 0)) #sets the background colour
|
||||||
self.surf.blit(gameUtils.centre(self.currentCaptchaImg, self.size)) #draws the text to center of the screen
|
self.surf.blit(gameUtils.centre(self.currentCaptchaImg, self.size)) #draws the text to center of the screen
|
||||||
#TODO: actually scale the text, however, more work needs to be done on the actual function of the game first
|
#TODO: actually scale the text, however, more work needs to be done on the actual function of the game first
|
||||||
|
|
||||||
|
# Draw the captcha progerss if there is a captcha
|
||||||
|
if self.currentCaptcha is not None:
|
||||||
|
x1 = self.size[0] / 2 - self.currentCaptchaImg.size[0] / 2
|
||||||
|
y = self.size[1] / 2 + self.currentCaptchaImg.size[1] / 2
|
||||||
|
x2 = x1 + (self.currentCaptcha.charIdx / len(self.currentCaptcha.chars)) * self.currentCaptchaImg.size[0]
|
||||||
|
pygame.draw.line(self.surf, (0, 255, 0), (x1, y), (x2, y), 5)
|
||||||
|
|
||||||
|
# Draw the timer if there's any time left
|
||||||
|
timeLeft = self.timerLength - (time.perf_counter() - self.timerStart)
|
||||||
|
if timeLeft > 0:
|
||||||
|
normalisedTimeLeft = max(timeLeft / self.timerLength, 0)
|
||||||
|
x = round((1-normalisedTimeLeft) * (self.size[0] // 2))
|
||||||
|
w = normalisedTimeLeft * self.size[0]
|
||||||
|
c = np.round((255, 255*normalisedTimeLeft, 255*normalisedTimeLeft))
|
||||||
|
pygame.draw.rect(self.surf, c, (x, 0, w, 10))
|
||||||
|
self.surf.blit(self.timerFont.render(str(round(timeLeft, 1)), True, c), (0, 10))
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""
|
"""
|
||||||
Ran when the game closes.
|
Ran when the game closes.
|
||||||
|
|||||||
Reference in New Issue
Block a user