Added basic timer

This commit is contained in:
2025-07-06 19:34:07 +01:00
parent 6e70f74874
commit ae6c23b6b7

13
game.py
View File

@ -1,5 +1,6 @@
from PIL import Image, ImageDraw, ImageFont
import math as maths
import numpy as np
import logging
import pygame
import gameUtils
@ -160,6 +161,10 @@ class Game(gameUtils.Game):
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(5, 10)
def onEvent(self, event):
"""
Ran when an event is fired.
@ -193,6 +198,14 @@ class Game(gameUtils.Game):
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
if self.timerLength != 0:
timeLeft = self.timerLength - (time.perf_counter() - self.timerStart)
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))
def close(self):
"""
Ran when the game closes.